 
// starting the script on page load
	var toDelID=0 ;
$(document).ready(function(){

 
                
 
showWS();
activateForms();
tooltip();
	
$("#closebox").live("click", function(event){
 	event.preventDefault();
    $("#box").fadeOut("slow"); 
    $("#processing").fadeOut("slow");
  //  $("#transbox").fadeOut("slow");
  });
  
$("#logout").live("click", function(event){
	event.preventDefault();	
var a = this.href || this.alt;
$.getJSON(a,
        function(msg){
   				if (msg.el){
   					hideshow(msg.el,0);
				//$("#divws").slideUp(2000);
                
                
				$('#'+msg.el).html(msg.txt);  //
				$('#'+msg.el).hide();
				hideshow(msg.el,1);
				$("#divws").html("Your favorite sites here…");
                $("#divws").height(40);
				activateLinks();
				activateForms();
				}else{
			//	error(1,msg.txt);//	window.location=msg.txt;
				}   
        });


  });
  
  
activateLinks();
			   
});



function activateLinks(){
	
	  $(".clsblur").blur(function() { 
      var thisDval = $(this).attr("alt");
       $(this).removeClass("active");
      $(this).addClass("inactive");       
      var thisval = $(this).val();
      if (thisval==""){

      $(this).val(thisDval);
      }
      

});
 
$(".clsblur").focus(function() { 
$(this).removeClass("inactive");
        $(this).addClass("active"); 
        var thisDval = $(this).attr("alt");
        var thisval = $(this).val();
      if (thisval==thisDval){
      	$(this).val("");
      }
      
});

	$(".showpage").live("click", function(event){
event.preventDefault();
var a = this.href || this.alt;
 
$("#thecontents").load(a,function(){
$('#box').center(true);
$("#box").fadeIn("slow");
$("#processing").fadeIn("slow");
tLeft= $(this).width() -9 ;
$('#closebox').css("left",tLeft + "px");
//activatelinks();
activateForms();
});
	this.blur();
return false;
            });
            
}
 function tooltip(){
$("a").easyTooltip();
}
  

function activateForms(){
	$('form').submit(function(e) {
	e.preventDefault();	
    frm=$(this).attr("id");
  	processform(frm);
	});
}

function processform(frm)
{
 sUrl=	$('#'+frm).attr("action");
 hideshow('er_' + frm,0)
 hideshow('loading',1);
error(0);
		   //alert(sUrl);
$.ajax({
		type: "POST",
		url: sUrl,
		data: $('#'+frm).serialize(),
		dataType: "json",
		success: function(msg){
  //  alert(msg.status);
			if(parseInt(msg.status)==1)
			{
			   //alert(msg.status);
				if (msg.el){
			 	//	alert(msg.el);
			$('#'+msg.el).hide();
				$('#'+msg.el).html(msg.txt);  //
			 
				hideshow(msg.el,1);
				
				}else{
				blinkmsg(msg.txt);
				error(1,msg.txt);//	window.location=msg.txt;
				}

				hideshow('er_' + frm,0)

				if (frm=="frmAddWS"){
					if (msg.el){}
					else{
					addWS(msg.myws) ;}
				}
				
				if (frm=="frmlogin"){
					showWS();
				}
				
				
		    $("#box").fadeOut("slow"); 
    		$("#processing").fadeOut("slow");
    
			}
			else if(parseInt(msg.status)==0)
			{
			 	// error(1,msg.txt);
			 
			 	$('#er_' + frm).html(msg.txt);
			 	//hideshow('er_' + frm,1)
			 	blinkEL('er_' + frm)
			 	
			}
			hideshow('loading',0);
		}
	});

}


function hideshow(el,act)
{
if(act){
	$('#'+el).fadeIn("slow");
}else{
	$('#'+el).fadeOut("slow");
}
}

function error(act,txt)
{
	hideshow('error',act);
	if(txt) $('#error').html(txt);
}

function blinkEL(el){
$('#'+el).fadeIn("slow");	
setTimeout(function() { $('#'+el).fadeOut(); }, 3000);
}

function blinkmsg(msg){
msg="<img border=0' src='images/info7.png' align='absmiddle'> " +msg;	
$("#msgbox").html(msg);
$("#showmsg").fadeIn("slow");	
setTimeout(function() { $("#showmsg").fadeOut(); }, 3000);
}

function activateDel() {

    //$.post("someurl.php",someData,doSomething(data, myDiv),"json");

    $(".del").live("click", function (event) {

        toDelID = this.id;
       
        event.preventDefault();
        jConfirm('Are you sure you want ot delete this bookmark?', 'Confirmation Dialog',

        function (r) {

            if (r == true) {

                //var id = this.id ;
                var a = "websites.php?action=del&id=" + toDelID;

                $.getJSON(a, function (msg) {
                    if (msg.status == 1) {
                        //	$("#msgbox").html(msg.txt);  //
                        blinkmsg(msg.txt);
                        $("#" + toDelID).remove();
                        $("#spws_" + toDelID).remove();
                    } else {
                        //$("#msgbox").html(msg.txt);  //
                        blinkmsg(msg.txt);
                    }
                });

                $("#box").fadeOut(300);
            }

        });

    });
}
 function showWS() 
{
		$.getJSON("websites.php?action=getws",function(data)
		{
			$("#divws").html("");
				$.each(data.myws, function(i,data){
					
					if (data.title.length>25){
						cTitle=data.title.substring(0,22) + "...";
					}else
					{
					cTitle=data.title;	
					}
					  div_data ="<div class=mywsp id='spws_" + data.id +"'><a class='myws'  href='"+data.url+"' title='"+data.title+"<br>"+data.url+"'>" + cTitle+" </a> <a class='del' id='"+data.id+"'  title='delete this' href='page.php?action=show&page=tpl_addws.htm&myws_id="+data.id+"' >x</a></div>";
				   
					$(div_data).appendTo("#divws");
				});
 
							$("#divws").slideDown("slow");
			 activateDel();
			 	tooltip();
			 	
			}
			
		);
	
	 
} 


 function addWS(data) 
{
	// count all soan with ID containing spws_... acoording to that alter class
					if (data.title.length>25){
						cTitle=data.title.substring(0,22) + "...";
					}else
					{
					cTitle=data.title;	
					}
					  div_data ="<div class=mywsp id='spws_" + data.id +"'><a class='myws'  href='"+data.url+"' title='"+data.title+"<br>"+data.url+"'>" + cTitle+" </a> <a class='del' id='"+data.id+"'  title='delete this' href='page.php?action=show&page=tpl_addws.htm&myws_id="+data.id+"' >x</a></div>";
					  
$(div_data).appendTo("#divws");
tooltip();
activateDel();
return false;
} 