Cufon.replace('.carrouselLinkList li a .title', {hover: 'true'});
$(document).ready(function() {
	
	initMainmenu();
	
	if ($.browser.mac() == true) { 
		$(".submenu, #menuSubContainer .subMenuItem .subSubMenuItems a, " +
				"#menuSubContainer .subMenuItem a, " +
				".button a span, " +
				".carrouselLinkList li a, " +
				".readmore > a, .bt_back, " +
				"h1, h2, h3, h4, h5, h6, " +
				".paginationControl a, " +
				".searchResultItem strong a, " +
				"#footer #footerSitemap ul li.mainItem a.sitemapMainPage").css("font-weight", "100"); 
	}
	
	// Append link-urls when print-stylesheet is activated:
	$(".newsItem a, .home #left a, .home #right .highlightWrapper a, #highlight .highlightNews a, .button a").each(function(){
		var printUrl = $(this).attr("href");
		if (printUrl.search("mailto:") == -1 && printUrl.search("http:") == -1) { printUrl = "http://www.orde.nl"+printUrl; }
		$(this).append("<span class='printUrl'>"+printUrl+"</span>");
	});
	
	$(".topNav li a").hover(function() {
		$(this).parent("li").next("li").addClass("notSeparated");
		if ($(this).hasClass("active")) { 
			$(this).parent("li").next("li").find("a").removeClass("tabLeft").addClass("tabActiveLeft");
		}
	}, function() { 
		if (!$(this).hasClass("active")) { 
			$(this).parent("li").next("li").removeClass("notSeparated");
		} else {
			$(this).parent("li").next("li").find("a").addClass("tabLeft").removeClass("tabActiveLeft");
		}
	});
	
	$(".topNav li a.active").each(function() {
		$(this).parent("li").next("li").addClass("notSeparated").addClass("tabLeft");
	});
	
	$(".highlightPagination .pageNumbers a").click(function(){
		$(".highlightPagination .pageNumbers a").removeClass('active');
		$(this).addClass('active');
		$(".highlightWrapper").removeClass("display").addClass("hidden");
		var countWrappers = $(".highlightWrapper").size();
		var selectedNumber = $(this).text();
		if (isNaN(selectedNumber) || selectedNumber < 1 || selectedNumber > countWrappers ){
			selectedNumber = 0;
		} else {
			selectedNumber = selectedNumber - 1;
		}
		$('.highlightWrapper::eq('+selectedNumber+')').addClass('display').removeClass('hidden');
		return false;
	});
	
	$(".newsItem").click(function(){
		if (url = $(".textLimit", this).children(".url").attr("href")) {
			window.location.href = url;
		}
	});
	
	$(".paginationControl .pageNumbers a:not(.active)").hover(function(){
		$(this).prev("a:not(.active)").css("border-color", "#e5f1ef");
	}, function(){
		$(this).prev("a:not(.active)").css("border-color", "#b7b7b7");
	});
	
	$('.paginationControl .pageNumbers a.active').prev('a').css('border-color', '#fff');
    if($.browser.msie() && parseInt($.browser.version.number()) === 7) {
        $('.paginationControl .pageNumbers a:last-child').css('border-color', '#fff');
    }
	
	$('.contentYoutube iframe').each(function(){
	    var url = $(this).attr("src");
	    $(this).attr("src",url+"?wmode=transparent");
	});
	
	$("#loginPopup").fancybox({
			'hideOnContentClick' : true,
			'padding': 30, 
			'type': 'iframe', 
			'width': 420, 
			//height: 480, 
			//height: 200,
			'scrolling': 'no', 
			'overlayColor': '#ffffff', 
			'overlayOpacity': '0.8',
			'onComplete': function() { 
				$("#fancybox-close").addClass("customClose"); 
				$('#fancybox-frame').load(function() { // wait for frame to load and then gets it's height
					$('#fancybox-wrap').height($(this).contents().find('body').height()+50);
					$('#fancybox-inner').height($(this).contents().find('body').height()+2);
				});
			}}
	);
	
	$("#socialShare p, #addThis, .bubbleArrow").hover(function(){
		$("#shareBubble").show();
		//$(".bubbleArrow").show();
	}, function() {
		$("#shareBubble").hide();
		//$(".bubbleArrow").hide();
	});
	
});

function initMainmenu()
{
	var activemenu = "";
	var timeout = 500;
	var openTimeout = 200;
	var closeTimer;
	var openTimer;

	//when a li (menuitem) is hovered, open submenu
	$('.topNav a.mainMenu').bind("mouseover touchend", function(){
		var subId = $(this).parent().attr('id').replace('sub_', '');
		clearTimeout(openTimer);
		clearTimeout(closeTimer);
		if (activemenu && activemenu != this) {
			$(activemenu).removeClass("activeMenuItem");
		}
		$(this).addClass("activeMenuItem");
		activemenu = this;
		
		$('.topNav a').removeClass('hovered');
		$(this).addClass("hovered");
		$(this).next("li").addClass("notSeparated");
		
		if($(this).parent().hasClass('hasSubItems')) {
			openTimer = setTimeout(function() {
				$('#menuSubContainer > div').hide();
				$('#menuSubContainer > div#Sub_'+subId).show();
				$('#menuSubContainer').slideDown("fast");
				$(".SubItemContainer").each(function(){ 
					var submenuHeight = parseInt($(this).height())-27 + "px";
					$(this).find(".subMenuItem").css("height", submenuHeight);
				});
			}, openTimeout);
		}
		else {
			$('#menuSubContainer').slideUp("fast");
		}
	}).bind("mouseout", function(){
		if (activemenu == this && !$('#menuSubContainer:visible').length) {
			$(this).removeClass("activeMenuItem");
		}
		$(this).removeClass("hovered");
		if (!$(this).hasClass("active")) { 
			$(this).next("li").removeClass("notSeparated");
		}
	});
	
	//if mouse stops hovering the menu, initiate a timer
	$('.mainmenu_wrapper').bind("mouseleave", function(){
		clearTimeout(openTimer);
		if (activemenu) {
			closeTimer = setTimeout(hideDropdown, timeout);
		}
	}).bind("mouseover", function(){
		clearTimeout(closeTimer);
	});

	//hide submenu when timer has finished
	function hideDropdown(){
		clearTimeout(openTimer);
		$('#menuSubContainer').slideUp("fast", function(){
			$(activemenu).removeClass("activeMenuItem");
			activemenu = null;
		});
	}

	$('.SubItemContainer').mouseover(function(){
		var mainItemId = $(this).attr("id").replace("S", "s");
		$("#"+mainItemId).find(".mainMenu").addClass("hovered");
		$("#"+mainItemId).next("li").addClass("notSeparated");
	});
	$('.SubItemContainer').mouseleave(function(){ 
		var mainItemId = $(this).attr("id").replace("S", "s");
		$("#"+mainItemId).find(".mainMenu").removeClass("hovered");
		if (!$("#"+mainItemId).find(".mainMenu").hasClass("active")) { 
			$("#"+mainItemId).next("li").removeClass("notSeparated");
		}
	});
	
	function showDropdown(){
		$('#menuSubContainer').slideDown("fast");
	}
	
	//show information of a particular submenu item	
	$('.subMenuItem').bind("mouseover", function(){
		var hovered = $(this).attr('id').replace('subMenuItem_', '');
		$(this).find('a').addClass("activeMenuItem"); 
		
		$('.subMenuTekst').hide();
		$('#SubTekst_'+hovered).show();
	});
	
	//remove color class after mouse stops hovering the item
	$('.subMenuItem').bind("mouseleave", function(){
		$(this).find('a').removeClass("activeMenuItem");
	});
	
	// iPad submenu actions
	clickedOnce = '';
	$('.mainmenu_wrapper a.mainMenu').live('touchstart', function(e) {
		var id = $(this).parent("li").attr("id");
		if (clickedOnce == id) { 
			window.location = $(this).attr("href");
		} else { 
			clickedOnce = id;
			return false;
		}
	});
	$('#menuSubContainer').live('click', function(e) {
		hideDropdown();
		clickedOnce = '';
	});
}
function autoIframeHeight(iframe) {              
    document.domain = document.domain.substring(document.domain.substring(0,document.domain.lastIndexOf('.')).lastIndexOf('.')+1);
    var iframeDocument = iframe.contentWindow.document;
    iframe.scrolling = 'no';
    iframe.style.overflow = 'hidden';
    iframe.allowTransparency = true;
    iframe.frameBorder = 0;
    iframe.height = iframeDocument.getElementsByTagName('body')[0].scrollHeight;
}     
