// JavaScript Document

$(document).ready(function()
{

	// Fixes for same height on homepage smallColumn items
	$('body.home div.smallColumn div.contentBlock').eq(0).css('height',210);
	$('body.home div.smallColumn div.newsItems').eq(0).css('height',210);	
	// no margin last partnerblock link
	$('div#Content div.partnerBlock div.contentInfo a:last, div#Content div.partnerBlock div.contentInfo img:last').css('margin-right',0);
	
	if ($('div.controlBorder').length > 0)
	{
		$('body').addClass('backoffice');
		backoffice = true;
	}
	
	
	//if (backoffice = true) {
	//	continue;
//	}
	//else {
	// Platform random images 	
	//set container for images on platform
	var platformImage = $("body.home div.platformOverview div.platformBlock div.image");
	platformImage.find('p img').hide();
	//console.log(platformImage);
	
	platformImage.each(function(index) {
		//zoek alle links met images erin
		var pfImages = $(this).find('p img');
		// sla het aantal images aanwezig op
		var imageCount = pfImages.length;
		//verstop alle images in een platform
		//pfImages.hide();
		//genereer random nr tussen 0 en aantal images
		var imgNumber = Math.floor(Math.random() * imageCount);
		// gebruik imgNumber om random array item uit te schrijven
		pfImages.eq(imgNumber).show();
	});
	
	//}
	
	
	
	
	
	
	if($('div.platformOverview').length > 0)
	{
		$('div.newsletter select option:first').remove();
		$('div.newsletter select option:eq(1)').attr("selected","selected");
	}
	
	if($('body.home').length > 0 && $('div.platformOverview').length == 0)
	{
		$(this).find('div.contentBlock, div.newsItems').css('height','auto');
		
		var cols = $('div#Content div.smallColumn');
		
		var maximum = getColsize(cols);
		
		cols.each(function()
		{
			var blockMax = maximum;
			
			if ($(this).find('div.platformBlock, div.newsItems').length > 0)
			{
				blockMax += 20 + 21;
			}
			
			//$(this).find('div.contentInfo:first, div.platformBlock:first').height(blockMax);
			$(this).find('div.contentInfo:first, div.platformBlock:first, div.newsItems:first').eq(0).height(blockMax);
			
			//$(this).find('div.contentBlock:first div.contentInfo').css('height','152px');
			//$(this).find('div.contentBlock:first div.newsItem').css('height','193px');
			
			$(this).find('div.contentBlock').css('z-index','0');
			$(this).find('div.contentBlock:first').css('z-index','100');
		});

	}
	
	
	// Menu -------------------------------------------------------------------------------------------
	var hoverout;
	var activehover;
	var hoversub;
	var hoversubout;
	
	$('div#Menu ul li ul').css('display','none');
	
	function mouseOut()
	{
		$('div#Menu ul li ul').css('display','none');
		
		if ($(activehover).hasClass('AspNet-TreeView-Selected') || $(activehover).hasClass('AspNet-TreeView-ChildSelected'))
			return
		else
		{
			$('div#Menu ul li').removeClass('hover');
				
			activehover.find('a:first').css('background-position','left top'); //IE6 Fix
			
			Cufon.replace(activehover.find('a:first'), {color: '#ffffff'});
			Cufon.replace('div#Menu ul li ul li a', {color: '#0483b5'});
			Cufon.replace('div#Menu ul li ul li ul li a', {color: '#ffffff'});
		}
		
		activehover = null;
	}
	
	$('div#Menu ul li').not('div#Menu ul li ul li').mouseenter(function()
	{
		/*if ($(this).hasClass('AspNet-TreeView-Selected'))
		{
			mouseOut()
			return;
		}*/
		
		if (hoverout)
			clearTimeout(hoverout);
		
		if (activehover && activehover.html() != $(this).html())
			mouseOut();
		
		activehover = $(this);
		activehover.find('> ul').css('display','block');
		activehover.addClass('hover');
		activehover.find('a:first').css('background-position','left bottom'); //IE6 Fix
	});
	
	$('div#Menu ul li ul li a').mouseenter(function()
	{
		clearTimeout(hoversubout);
		
		$(this).parent().parent().find('ul').css('display','none');
		
		$(this).parent().parent().find('li').removeClass('active');
		$(this).parent().addClass('active');
		
		hoversub = $(this).parent();
		hoversubout = setTimeout(subOut, 200);
	});
	
	$('div#Menu ul li ul li ul').mouseleave(function()
	{
		$(this).css('display','none')
		Cufon.replace('div#Menu ul li ul li a', {color: '#0483b5'});
		Cufon.replace('div#Menu ul li ul li ul li a', {color: '#ffffff'});
	});
	
	function subOut()
	{
		hoversub.find('> ul').css('display','block')
	}
	
	$('div#Menu').mouseleave(function()
	{
		if (activehover.find('ul').html() == null)
			hoverout = setTimeout(mouseOut, 0);
		else
			hoverout = setTimeout(mouseOut, 1000);
	});
	$('div#Menu ul li ul').mouseenter(function()
	{
		clearTimeout(hoverout);
	});
	
	$('div#LoginForm input').focusin(function()
	{
		$(this).val('');
	});
	
	// Selectbox --------------------------------------------------------------------------
	$('div.newsletter select, div.platform select').selectbox();
	
	
	// platformOverview -------------------------------------------------------------------
	$('div.platformOverview div.platformBlock').click(function() 
	{
		var urlDoorway = $(this).find("a.blueTitle").attr('href');	
		//alert($(this).find("a.blueTitle").attr('href'));
		
		$(window.location).attr('href', urlDoorway);
	});	
	
	$('div.platformOverview div.platformBlock').hover(function() 
	{
		$(this).find("div.button").css('display', 'block');
	},
	function() 
	{
		$(this).find("div.button").css('display', 'none');
	});
	
	// Make save as print button: convertToPrintBtn ---------S. Bonardt 04-10-2011----------------------------------------------------
	//Prepare button replacement for print button: convertToPrint();
	var button = $('div.surveyPrintBtn div.SurveyContainer div.Buttons div.NavNext input[type="submit"]');
		//console.log(button);
	convertToPrintBtn(button);	
	
	function convertToPrintBtn(item) {
		var btnParent = $(item).parent();
			//console.log(btnParent);
		$(item).remove();
			//console.log('removing...');
		$('<input type="button" id="JqPrintBtn" value="Print pagina" onClick="window.print(); return false;">').appendTo(btnParent);
	}

	
	// CustomForm Button -------------------------------------------------------------------------------------------
	$('div.customForm_Button input').hover(function()
	{
		$(this).parent().addClass('hover');
	}, function()
	{
		$(this).parent().removeClass('hover');
	});
	
	
	// ImageViewer -------------------------------------------------------------------------------------------
	var imageViewer = $('div.imageViewer table.imageBlock');
	var slideSpeed = 500;
	var sliding = false;
	var autoSlide;
	
	imageViewer.find('a:first').css('display','block');
	imageViewer.find('a:first').addClass('active');
	
	var editbutton = imageViewer.find('div.editbutton').html();
	
	if (editbutton == null)
		autoSlide = setTimeout(moveNext, 5000);
	
	$('div.imageViewer div.navRight').click(function()
	{
		moveNext();
	})
	$('div.imageViewer div.navLeft').click(function()
	{
		movePrev();
	})
	
	function moveNext()
	{
		if (sliding == true) return;
		
		clearTimeout(autoSlide);
		
		var activeImage = imageViewer.find('a.active');
		
		sliding = true;
	
		activeImage.removeClass('active');
		activeImage.fadeOut(slideSpeed);
		
		activeImage.next().addClass('active');
		
		setTimeout(function()
		{
			sliding = false;
			activeImage.next().fadeIn(slideSpeed);
			activeImage.appendTo(imageViewer.find('tr td p'));
			
			autoSlide = setTimeout(moveNext, 5000);
		}, slideSpeed);
	}
	function movePrev()
	{
		if (sliding == true) return;
		clearTimeout(autoSlide);
		
		imageViewer.find('a:last').prependTo(imageViewer.find('tr td p'));
		
		var activeImage = imageViewer.find('a.active');
		sliding = true;
		
		activeImage.removeClass('active');
		activeImage.fadeOut(slideSpeed);
		
		activeImage.prev().addClass('active');
		
		setTimeout(function()
		{
			sliding = false;
			activeImage.prev().fadeIn(slideSpeed);
			
			autoSlide = setTimeout(movePrev, 5000);
		}, slideSpeed);
	}
	
	// introduce -------------------------------------------------------------------------------------------
	$('div.introduce div.contentIntroduce div.iContent').css('display','none');
	var divViewer = $('div.introduce div.contentIntroduce');
	var slideSpeed2 = 500;
	var sliding2 = false;
	var autoSlide2;
	
	divViewer.find('div:first').css('display','block');
	divViewer.find('div:first').addClass('active');
	
	autoSlide2 = setTimeout(moveNextDiv, 8000);
	
	$('div.introduce div.navRight').click(function()
	{
		moveNextDiv();
	})
	$('div.introduce div.navLeft').click(function()
	{
		movePrevDiv();
	})
	
	function moveNextDiv()
	{
		if (sliding2 == true) return;
		
		clearTimeout(autoSlide2);
		
		var activeDiv = divViewer.find('div.active');
		
		sliding2 = true;
	
		activeDiv.removeClass('active');
		activeDiv.fadeOut(slideSpeed2);
		
		activeDiv.next().addClass('active');
		
		setTimeout(function()
		{
			sliding2 = false;
			activeDiv.next().fadeIn(slideSpeed2);
			activeDiv.appendTo(divViewer);
			
			autoSlide2 = setTimeout(moveNextDiv, 8000);
		}, slideSpeed2);
	}
	function movePrevDiv()
	{
		if (sliding2 == true) return;
		clearTimeout(autoSlide2);
		
		
		divViewer.find('div.iContent:last').prependTo(divViewer);
		var activeDiv = divViewer.find('div.active');
		sliding2 = true;
		
		activeDiv.removeClass('active');
		activeDiv.fadeOut(slideSpeed2);
		
		activeDiv.prev().addClass('active');
		
		setTimeout(function()
		{
			sliding2 = false;
			activeDiv.prev().fadeIn(slideSpeed2);
			
			autoSlide2 = setTimeout(movePrevDiv, 8000);
		}, slideSpeed2);
	}
	
	
	$('div.newsItems div.newsOverview a').addClass('arrow');
	$('div.newsItems div.newsLabel, div.newsItem div.newsLabel').append('<div class="topRight">&nbsp;</div>');
	
	//Enquete
	$('div.SurveyContainer div.NavNext input[value="Enquete versturen"]').hide();
	
	//CRM News
	$('div.smallColumn div.contentBlock').each(function()
	{
		/*
		var h2 = $.trim($(this).find('h2:first').text());
		var h3 = $(this).find('h3:first');
		var p = $(this).find('p');
		
		if (h2 == '' && h3.length > 0 && $.trim(h3.text()) == '' && p.length == 0)
		{
			$(this).parent().hide();
		}
		*/
		
		var h2 = $(this).find('h2:first').text();
		var info = $(this).find('div.contentInfo').text();
		
		if ($.trim(info) == '' && $.trim(h2) == '')
		{
			$(this).hide();
		}

	});

	// Cufon -------------------------------------------------------------------------------------------
	Cufon.now();
	
	Cufon.set('fontFamily','Gotham');
	Cufon.replace('div#HeaderInfo');
	
	Cufon.set('fontFamily','Interstate');
	Cufon.replace('div#Menu a', {hover: '#0f364f'});
	Cufon.replace('div#Menu ul ul ul a', {hover: '#91ddff'});
	Cufon.replace('h2.blueTitle');
	Cufon.replace('div.newsLabel div.label h2');

});

function getColsize(cols)
{
	var maximum = 0;
	
	cols.each(function()
	{
		var height = $(this).find('div.contentInfo:first, div.platformBlock:first, div.newsItems:first').height();
		
		if (height > maximum)
		{
			maximum = height;
		}
		
	});
	
	return maximum;
}

