$(document).ready(function(){
	
    var windowHeight = $(window).height();
	var documentHeight = $(document).height();

    $("#homepage #content #library a").hover(
      function () {
        $(this).children("img.balloon").attr("src", wp_style_path+"/images/library_balloon_hover_mask_homepage.png")
      }, 
      function () {
        $(this).children("img.balloon").attr("src", wp_style_path+"/images/library_balloon_mask_homepage.png")
      }
    );
	$("#loggedin_homepage #library a").hover(
      function () {
        $(this).children("img.balloon").attr("src", wp_style_path+"/images/library_balloon_hover_mask.png");
		$(this).children("img.balloonfavorite").attr("src", wp_style_path+"/images/library_balloon_favorite_hover_mask.png");
      }, 
      function () {
        $(this).children("img.balloon").attr("src", wp_style_path+"/images/library_balloon_mask.png");
		$(this).children("img.balloonfavorite").attr("src", wp_style_path+"/images/library_balloon_favorite_mask.png");
      }
    );
	
	$("ul#category_covers li a img").live('mouseover',
		function () {
			var whichLi = $(this).parent().parent();;
			//console.log("whichLi: " + whichLi);
			var hoveredIndex = $("ul#category_covers li").index(whichLi);
			//console.log("hoveredIndex: " + hoveredIndex);
			var hoveredOffset = $(whichLi).position();
			$("ul#category_hovers li:eq("+hoveredIndex+") div.hover_info").css({
				'display' : 'block',
				'left' : (hoveredOffset.left-10)+'px',
				'top' : (hoveredOffset.top-10)+'px'
			});
			$("ul#category_hovers li:eq("+hoveredIndex+") div.hover_info img.favorite_icon").mouseover(
				function () {
					$("ul#category_hovers li div.hover_info .toggle_favorite").css({
						'display' : 'block',
						'zIndex' : '150'
				});
		}
    );
		}
    );
	$("ul#category_hovers li a img").live('mouseout',
		function () {
			$("ul#category_hovers li div.hover_info").css({'display' : 'none'});
		}
    );
	$("img.favorite_icon").live('mouseout', 
		function () {
			$("ul#category_hovers li div.hover_info .toggle_favorite").css({
				'display' : 'none',
				'zIndex' : '-1000'
			});
		}
    );

	$("img.favorite_icon").live('click', function() 
	{		
		var parentListElement = $(this).parent().parent().parent();
		var bookID = parentListElement.attr('id').substring(6);
		var isFavorite = parentListElement.hasClass('favorite');
		var coverListElement = $('#cover_'+bookID);	
		parentListElement.toggleClass('favorite');
		coverListElement.toggleClass('favorite');
		
		if (isFavorite)
		{
			if (wp_category == 128)
			{
				parentListElement.remove();
				coverListElement.remove();
			} else 
			{
				coverListElement.find('.favorite_icon').remove();
			}
		} else 
		{
			coverListElement.find('.cover_only').append("<img src='" + wp_style_path +"/images/favorite_book_mask.png' width='25' style='z-index:100000' class='favorite_icon'/>");
		}
		parentListElement.find('.toggle_favorite').text(isFavorite ? 'Voeg toe aan mijn favorieten' : 'Verwijder uit mijn favorieten');
		
		//$('#test1').jGrowl("Toegevoegd aan favorieten: "+bookID+', isfavorite: '+isFavorite+', id: '+parentListElement.attr('id'));

		url = document.location.href.split('#')[0];
		params = "wpfpaction="+(isFavorite ? "remove" : "add")+"&postid="+bookID+"&ajax=1";
		$.get(url, params, function(data) 
		{
			$('#test1').jGrowl(isFavorite ? "Het boek is verwijderd uit uw favorieten.":"Het boek is toegevoegd aan uw favorieten.");
		});
		return false;
	});

	
	/**
	* getPageScroll() by quirksmode.com
	*
	* @return Array Return an array with x,y page scroll values.
	*/
	function ___getPageScroll() {
	   var xScroll, yScroll;
	   if (self.pageYOffset) {
		   yScroll = self.pageYOffset;
		   xScroll = self.pageXOffset;
	   } else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		   yScroll = document.documentElement.scrollTop;
		   xScroll = document.documentElement.scrollLeft;
	   } else if (document.body) {// all other Explorers
		   yScroll = document.body.scrollTop;
		   xScroll = document.body.scrollLeft;	
	   }
	   arrayPageScroll = new Array(xScroll,yScroll);
	   return arrayPageScroll;
	};

	function setOverlayHeight(){
		$("#alert .overlay").css({
			'height' : documentHeight + 'px'
		});
	}
	
	function showAlert(notify) {
		$("#alert .box h6.notify").html(notify);
		$("#alert").show();
		var boxHeight = $("#alert .box").outerHeight();
		//console.log("boxHeight: " + boxHeight);
		var pageScroll = ___getPageScroll();
		var alertOverlayPos = pageScroll[1] + ( (windowHeight - boxHeight) / 2);
		if (alertOverlayPos < 0){
			alertOverlayPos = 0;
		}
		$("#alert .box").css({
			'top' : alertOverlayPos + 'px'
		});
	}
	function hideAlert() {
		$("#alert").hide();
	}
	$("#nav_login").click(
		function (){
			showAlert("Inloggen");	
			return false;
		}
    );
	$(".cover_no_access").click(
		function (){
			showAlert("Voor volledige toegang tot het Sprekend Boek moet u nu inloggen");	
			return false;
		}
    );
	$("#alert .overlay").click(
		function (){
			hideAlert();
			return false;
		}
    );
	$("#alert .close_button a").click(
		function (){
			hideAlert();
			return false;
		}
    );
});