﻿$(document).ready(function() {
	//My Favorites
	
	$("#bt_favorites1,#bt_favorites2").mouseover(function(){
    $(this).css("cursor","pointer");
    });
	
	$("#bt_favorites1,#bt_favorites2").click(function(){
		$("#content_favorites").slideToggle("fast",function(){
			ShowFavorites();
		});
	
	
	})
	
	$("#a_favorites").click(function(){
		var strFavorites = $.cookie("url");
		var strNewFavorites = "";
		var strOldFavorites = "";
                var strAddUrl=window.location.href.toString();
                var strAddTitle=document.title.toString();

                var strAddFavorites = "<div>"+strAddUrl+"|"+strAddTitle+"</div>";
		if(strFavorites != null){
			var arrFavorites = strFavorites.split("</div>");
			
			if(strFavorites.indexOf(strAddFavorites)<0){
				if(arrFavorites.length>10){
					var t;
					var ClickCount;
					var ClickPreCount;
					for(var j=1;j<arrFavorites.length;j++){
						for(var i=arrFavorites.length-1;i>0;i--){
							
							ClickCount = $.cookie(arrFavorites[i].substr((arrFavorites[i].indexOf("<div>")+5),(arrFavorites[i].indexOf("|")-arrFavorites[i].indexOf("<div>")-5)));
																																													ClickPreCount = $.cookie(arrFavorites[i-1].substr((arrFavorites[i-1].indexOf("('")+2),(arrFavorites[i-1].indexOf("|")-arrFavorites[i-1].indexOf("<div>")-5)));
							if(ClickCount == null){ClickPreCount = 0;}
							if(ClickPreCount == null){ClickNextCount = 0;}
							if(ClickCount>ClickPreCount){
								t = arrFavorites[i];
								arrFavorites[i] = arrFavorites[i-1];
								arrFavorites[i-1] = t;
							}
						}
					}
					///////////////////////////////////////////////////////////////////
					arrFavorites.splice(9,1);
					for(var k=0;k<arrFavorites.length-1;k++){
						strOldFavorites += arrFavorites[k]+"</div>";
					}
					strNewFavorites = strOldFavorites+strAddFavorites;
				}else{
					strNewFavorites = strFavorites+strAddFavorites;
				}
			}
		}else{
			strNewFavorites = strAddFavorites;
		}
		//update cookie
		if(strNewFavorites!=""){
			$.cookie("url",strNewFavorites,{expires:365});
			
		}
		
		ShowFavorites();					 
	})
							
	

})

function ShowFavorites(){
	    var htmlfav="";
	    var temstr= $.cookie("url");
		if(temstr!=null && temstr.length>2 && temstr!='' && temstr!='undefined')
		{
	     totalurl=temstr.replace("<div>",'');
	     var temurls = new Array();  
         var suburls = new Array();
         temurls=totalurl.split("</div>");
         var sum=temurls.length-1;
         var i=0;
         for (i=0;i<sum;i++)
          {
		  suburls=temurls[i].split("|"); 
		  linktitle=suburls[1];
		  linkurl=suburls[0];
                  linkurl=linkurl.replace("<div>",'');
                  linkurl=escape(linkurl);

		  htmlfav+="<table width=\"168px\" border=\"0\" class=\"myfavtab\"  style=\"background:#d9e3ef;\"><tr><td align=\"left\" valign=\"top\" ><div><a class=\"select_item\" href=\"#\" onclick=\"FavoritesClick('"+linkurl+"')\">"+linktitle+"</a></div></td><td align=\"left\" class=\"changebgtd\" width=\"20px\" style=\"padding-top:8px; vertical-align:top;\"><a href=\"#\" onclick=\"delmyfav('"+linkurl+"','"+linktitle+"')\"><img src=\"/resources/Common/images/btn_delete_off.gif\" border=\"0\" class=\"delmyfavimg\" /></td></tr></table>";	  
	  }  
	    $("#item_favorites").empty();
		$("#item_favorites").html(htmlfav);	
		 $('.delmyfavimg').hover(
	function(event){
       //$(event.target).css("background","#668bc0");
       $(event.target).attr("src","/resources/Common/images/btn_delete_over.gif");
        $(event.target).parent().parent().parent().parent().parent().css("background","#668bc0");
		$(event.target).parent().parent().parent().parent().find("a").css("background","#668bc0");
            },
            function (event) {
         //$(event.target).css("background","#d9e3ef");
                $(event.target).attr("src","/resources/Common/images/btn_delete_off.gif");
                $(event.target).parent().parent().parent().parent().parent().css("background","#d9e3ef");
		$(event.target).parent().parent().parent().parent().find("a").css("background","#d9e3ef");
            }
			);
          $('.select_item').hover(
		function(event){
       $(event.target).css("background","#668bc0");
	     $(event.target).css("color","#FFFFFF");
         $(event.target).parent().parent().parent().parent().parent().css("background","#668bc0");
          $(event.target).parent().parent().parent().parent().parent().find("img").attr("src","/resources/Common/images/btn_delete_over.gif");
	   },
            function (event) {
                $(event.target).css("background","#d9e3ef");
				$(event.target).css("color","#666666");
				  $(event.target).parent().parent().parent().parent().parent().css("background","#d9e3ef");
				  $(event.target).parent().parent().parent().parent().parent().find("img").unbind('hover');
				  $(event.target).parent().parent().parent().parent().parent().find("img").attr("src","/resources/Common/images/btn_delete_off.gif");
            }
			);
	  }
		
	}

function delmyfav(favurl,favtitle){
            favurl=unescape(favurl);

	    var objstr= "<div>"+favurl+"|"+favtitle+"</div>";
		$.cookie("url",$.cookie("url").replace(objstr,''),{expires:365});
		if ($.cookie("url") ==null || $.cookie("url").length<1)
		{
		  $("#item_favorites").empty();
		}
		else
		{
			ShowFavorites();
		}
	}

function FavoritesClick(url){
	//update click count
	if($.cookie(url) == null){
		$.cookie(url,1,{expires:365});
	}else{
		$.cookie(url,parseInt($.cookie(url),10)+1,{expires:365});
	}
	
	
	var strFavorites = $.cookie("url");
	if(strFavorites != null){
		var arrFavorites = strFavorites.split("</div>");
		//sort favorites array
		///////////////////////////////////////////////////////////////////
		var t;
		var ClickCount;
		var ClickPreCount;
		for(var j=1;j<arrFavorites.length;j++){
			for(var i=arrFavorites.length-1;i>0;i--){
				ClickCount = $.cookie(arrFavorites[i].substr((arrFavorites[i].indexOf("<div>")+5),(arrFavorites[i].indexOf("|")-arrFavorites[i].indexOf("<div>")-5)));
				ClickPreCount = $.cookie(arrFavorites[i-1].substr((arrFavorites[i-1].indexOf("<div>")+5),(arrFavorites[i-1].indexOf("|")-arrFavorites[i-1].indexOf("<div>")-5)));
				if(ClickCount == null){ClickPreCount = 0;}
				if(ClickPreCount == null){ClickNextCount = 0;}
				if(ClickCount>ClickPreCount){
					t = arrFavorites[i];
					arrFavorites[i] = arrFavorites[i-1];
					arrFavorites[i-1] = t;
				}
			}
		}
		var strNewFavorites;
		for(var k=0;k<arrFavorites.length-1;k++){
			strNewFavorites += arrFavorites[k]+"</div>";
		}
		$.cookie("url",strNewFavorites.replace("undefined",""),{expires:365});
	}
	
	location.href = unescape(url);
}
