$(document).ready(function()
{		
	//hover for the left (white) box on the index page
	$("#indexLeftContent").hover
	(
		function() 
		{
			$(this).children('.download').css('visibility', 'visible');
			$(this).children('#indexJaarverslagLinkArrowContainer').css('visibility', 'visible');
		},
		function()
		{
			$(this).children('.download').css('visibility', 'hidden');
			$(this).children('#indexJaarverslagLinkArrowContainer').css('visibility', 'hidden');
		}
	);
	
	//hover for the right (green) box on the index page
	$("#indexRightContent").hover
	(
		function() 
		{
			$(this).children('.download').css('visibility', 'visible');
			$(this).children('#menu').css('visibility', 'visible');
		},
		function()
		{
			$(this).children('.download').css('visibility', 'hidden');
			$(this).children('#menu').css('visibility', 'hidden');
		}
	);	
	
	//makes sure the data overlay is always equal to the main data section
	$("#toelichtingOverlay").height($("#sectionData").height());
	
	
	$(".jaarverslagContentBlock").hover
	(
		function() 
		{
		
			$(this).addClass('active');

			/*
			var obj = $(this);
			$(this).children('').flip
			(
				{ 
					direction: 'lr', 
					color: '#005984',
					speed: 150,
					onEnd: function()
					{
						var div = jQuery(obj.children().get(0));
						div.addClass('active');
					}
				}
			); 
			*/
		},
		function()
		{
			$(this).removeClass('active');
			/*
			var obj = $(this);
			$(this).children('').flip
			(
				{ 
					direction: 'rl', 
					color: '#cee007',
					speed: 150,
					onBefore: function()
					{
						var div = jQuery(obj.children().get(0));	
						
						div.removeClass('active');
					}
				}
			); 
			*/
		}	
		
	);
	
	
	
	$(".dataOpenToelichting").bind
	(
	  	"click ", showOverlay
	);
	
	//makes sure the data overlay is always equal to the main data section
	$("#toelichtingOverlay").height($("#sectionData").height());	
	
	
	$("#closeJaarverslagOverlay").click
	(
		function() 
		{	
      		$("#contentContainer").hide("slide", {direction: "down", mode: "hide"}, 500);
			//open jaarverslag index pagina
			window.location.href = "jaarverslag.html";
		}
		
	);
	
	//animates the page titles
	$("#selectTitleInactive").click
	(
		function() 
		{ 
			//get the left pos for the active and inactive element
			var leftPos = $("#selectTitleActive").position().left;
			var rightPos = $(this).position().left;
			
			if($(this).attr('class') == 'jaarrekening')
			{
				//change the inactive element  to the active, and vice versa
      			$(this).children("img").attr("src", "images/header-select-jaarrekening-active.png");
				$("#selectTitleActive").children("img").attr("src", "images/header-select-jaarverslag-inactive.png");
			}
			else
			{
				//change the inactive element  to the active, and vice versa
      			$(this).children("img").attr("src", "images/header-select-jaarverslag-active.png");
				$("#selectTitleActive").children("img").attr("src", "images/header-select-jaarrekening-inactive.png");	
			}
			//animate the activating and deactivating elements to their new locations
			$(this).animate({left: leftPos}, 750);
			$("#selectTitleActive").animate({left: rightPos}, 750);
			
			//after all the animations are done, redirect the browser to the next page. 
			//Redirect is added to the queue to make sure it is performed last
			$(this).queue
			(
				function() 
				{
					if($(this).attr('class') == 'jaarrekening')
					{
						window.location.href = "jaarrekening52a3.html?h=Jaarrekening&amp;p=Geconsolideerde jaarrekening";
						//window.location.href = "pdf/WT_Jaarrekening_verkort.pdf";
					}
					else
					{
						window.location.href = "jaarverslag.html";	
					}
		  		}
			);
		}
	);
	
	
}
);

function showOverlay(eventObject)
{
	//create a jquery object from the passed object
	var obj = jQuery(eventObject.target);
	
	obj.parent().children().removeClass('hover');
	obj.parent().children('.dataOpenToelichting').css('visibility', 'hidden');
	
	//get the content from the currently selected dataCategoryItem

	var content = obj.parent().children('.dataCategoryItem').html();
	var content_class="dataCategoryItem";

	//alert(content);

	if (!content){
		content = obj.parent().children('.dataCategorySubHeader').html();
		content_class="dataCategorySubHeader";
	} 
	if (!content){
		content = obj.parent().children('.dataCategoryTotalBold').html();
		content_class="dataCategoryTotalBold";
	}


	
	//the link for the AJAX call
	var link = obj.parent().children('.dataOpenToelichting').children('a').attr('href');
	
	//alert(link);
	//apply the content to the selected item div 
	//and position it over the table row
	//if (!$("#selectedDataItem").html(content)){

	//$("#selectedDataItem").html(content);

	//$("#selectedDataItem").css('top', (obj.position().top));

	
	//verticale positie toelichting overlay afhankelijk van positie geselecteerde tabelrij
	$("#toelichtingOverlay").css('top', (obj.position().top-40));	
	
	//show both the overlay and selected item div
	$("#toelichtingOverlay").css('visibility', 'visible');
	
	//stel de hoogte van de layer in
	//$("#toelichtingOverlay").css('height', '200px');	
	if(content_class == "dataCategoryItem"){
		$("#selectedDataItem").html(content);

		$("#selectedDataItem").css('top', (obj.position().top));
		$("#selectedDataItem").css('visibility', 'visible');
		$("#selectedDataItem").css('font-weight', 'normal');
		$("#selectedDataItem").css('margin-top', '0px');
	}
	if(content_class == "dataCategorySubHeader"){
		$("#selectedDataItem").html(content);

		$("#selectedDataItem").css('top', (obj.position().top));
		$("#selectedDataItem").css('visibility', 'visible');
		$("#selectedDataItem").css('font-weight', 'bold');
		$("#selectedDataItem").css('margin-top', '5px');
	}
	if(content_class == "dataCategoryTotalBold"){
		$("#selectedDataItem").html(content);

		$("#selectedDataItem").css('top', (obj.position().top));
		$("#selectedDataItem").css('visibility', 'visible');
		$("#selectedDataItem").css('font-weight', 'bold');
		$("#selectedDataItem").css('margin-top', '0px');

	}
	
	//juiste toelichting inladen in de layer
	$("#toelichtingOverlay").load(link);
	
	//$("#toelichtingOverlay").css('height', document.getElementById('toelichtingDataContainer').offsetHeight+40);	
	//alert(document.getElementById('toelichtingDataContainer').offsetHeight);
	//unbind all the functions from (mouseover/mouseout) the table row
	$(".dataCategoryEntryRow").unbind();
	
	//when the overlay is open, each dataCategoryItem should be clickable
	//for each dataCategoryItem, add this entire function to the lick
	$(".dataCategoryEntryRow").children(".dataCategoryItem.dataToelichting'").each(function (i) 																				
	{
		dataCategoryItem = jQuery(this);
		dataCategoryItem.css('cursor', 'pointer');
		dataCategoryItem.bind
		(
			"click ", showOverlay
		);
	});
	$(".dataCategoryEntryRow").children(".dataCategorySubHeader.dataToelichting'").each(function (i) 																				
	{
		dataCategoryItem = jQuery(this);
		dataCategoryItem.css('cursor', 'pointer');
		dataCategoryItem.bind
		(
			"click ", showOverlay
		);
	});
	$(".dataCategoryEntryRow").children(".dataCategoryTotalBold.dataToelichting'").each(function (i) 																				
	{
		dataCategoryItem = jQuery(this);
		dataCategoryItem.css('cursor', 'pointer');
		dataCategoryItem.bind
		(
			"click ", showOverlay
		);
	});
	
	
	
}

function closeOverlay()
{
	//close the overlay div
	$("#toelichtingOverlay").css('visibility', 'hidden');	
	
	//close the selected item div
	$("#selectedDataItem").css('visibility', 'hidden');
	
	//unbind the showOverlay function from the dataCategoryItem elements
	$(".dataCategoryEntryRow").children('.dataCategoryItem').each(function (i) 
	{
		dataCategoryItem = jQuery(this);
		dataCategoryItem.css('cursor', 'auto');
		dataCategoryItem.unbind();
	});
	
	//rebind the mouseover/mouseleave functions
	bindJaarRekeningHover();
}

//quick function to call to bind both mouseover and mouseleave binds
//to a jaarrekening data item entry
function bindJaarRekeningHover()
{
	$(".dataCategoryEntryRow").bind
	(
	  	"mouseover", dataItemMouseOver
	);
	
	$(".dataCategoryEntryRow").bind
	(
	  	"mouseout", dataItemMouseOut
	);	
}

//applies the mouseover style to a jaarrekening data item entry
function dataItemMouseOver(eventObject) 
{
	$(this).children().addClass('hover');
	$(this).children('td:last-child').css('background-color', '#FFFFFF');
	$(this).children('.dataOpenToelichting').css('visibility', 'visible');
}

//removes the mouseover style from a jaarrekening data item entry
function dataItemMouseOut(eventObject)
{
	$(this).children().removeClass('hover');
	$(this).children('.dataOpenToelichting').css('visibility', 'hidden');
}

//animates the backgrounds on the index menu
function animateBackgrounds()
{
	$("#flashBackground").crossSlide
	(
		{
		fade: 1
		}, 
		[
			{
				src:  'images/achtergronden/1.jpg',
				from: '100% 100% 1x',
				to:   '100% 20% 1.2x',
				time: 5
		  	}, 
			{
				src:  'images/achtergronden/2.jpg',
				from: '80% 100% 1.2x',
				to:   '20% 100% 1x',
				time: 5
			}, 
			{
				src:  'images/achtergronden/3.jpg',
				from: '100% 80% 1x',
				to:   '80% 0% 1.2x',
				time: 5
		  	}, 
			{
    			src:  'images/achtergronden/4.jpg',
    			from: '100% 50% 1.2x',
    			to:   '30% 50% 1x',
    			time: 5
  			}, 
			{
    			src:  'images/achtergronden/5.jpg',
    			from: '100% 50% 1x',
    			to:   '50% 30% 1.2x',
    			time: 5
  			}
		]
	);	
	
}
