/**/
/*  This javascript file requires the jQuery library 
/*  Author: J.Oliemans
/**/

// plugin definition
$.fn.createPullDown = function (parent) {

	var childrenUl = parent + " ul";
	var childrenLi = parent + " li";
	var delay = 0;

	$(childrenUl).css({ display: "none" });
	$(childrenLi).hover(function () {
		$(this).find('ul:first').css({ visibility: "visible", display: "none" }).show(delay);
		// show the possible columns
		if ($(this).find('ul.floatcols-2').length > -1)
		{ $(this).find('ul.floatcols-2').css({ visibility: "visible", display: "none" }).show(delay); }
		if ($(this).find('ul.floatcols-3').length > -1)
		{ $(this).find('ul.floatcols-3').css({ visibility: "visible", display: "none" }).show(delay); }
		if ($(this).find('ul.floatcols-4').length > -1)
		{ $(this).find('ul.floatcols-4').css({ visibility: "visible", display: "none" }).show(delay); }
		if ($(this).find('ul.floatcols-5').length > -1)
		{ $(this).find('ul.floatcols-5').css({ visibility: "visible", display: "none" }).show(delay); }
	}, function () {
		$(this).find('ul:first').css({ visibility: "hidden" });
		// hide the possible columns
		if ($(this).find('ul.floatcols-2').length > -1)
		{ $(this).find('ul.floatcols-2').css({ visibility: "hidden" }); }
		if ($(this).find('ul.floatcols-3').length > -1)
		{ $(this).find('ul.floatcols-3').css({ visibility: "hidden" }); }
		if ($(this).find('ul.floatcols-4').length > -1)
		{ $(this).find('ul.floatcols-4').css({ visibility: "hidden" }); }
		if ($(this).find('ul.floatcols-5').length > -1)
		{ $(this).find('ul.floatcols-5').css({ visibility: "hidden" }); }
	});
};

// createpulldowns for the homeContainers
function createPulldownContainers() {
	$("a.quickLinksPulldown").each(function () {
		$(this).next().hide();

		$(this).click(function (event) {
			$(this).next().slideToggle(100);
			$(this).toggleClass('selected');
		});
	});
}

// createpulldowns for the testMenu
function createTestMenu() {
	$("a.testLabPulldownMenu").each(function () {
		$(this).next().hide();

		$(this).click(function (event) {
			$(this).next().slideToggle(100);
			$(this).toggleClass('testSelected');
		});
	});
	var node = $("a.active");
	var depth = 0;
	while (!node.hasClass("contextMenuList") && (depth < 5)) {
		node = node.parent();
		depth += 1;
	}
	node = node.prev();
	node.trigger("click");
}


// create tabs
function createTabs() {
	var firstTab = $("#productTabs > ul > li > a > span:first").text();
	if (firstTab.length > 0) {
		$("#productTabs > ul").tabs();
	}
}

function createFilterTabs() {
	// this shows the hidden second tabmenu
	$("div#contextMenuContainer2").css({ left: "0px" });
	$("div#contextMenuContainer2").css({ position: "relative" });

	$("#filterTabs > ul").hide();

	$("#filterTabs > ul").tabs();

	$("#filterTabs > ul").show();

	var tab1 = $("#filterTabs ul li a span").eq(0).text();
	var tab2 = $("#filterTabs ul li a span").eq(1).text();


	if (typeof (strSelectedYear) != "undefined" && tab1 == strSelectedYear)
		$("#filterTabs > ul").tabs("select", 0);

	if (typeof (strSelectedYear) != "undefined" && tab2 == strSelectedYear)
		$("#filterTabs > ul").tabs("select", 1);

}

function SwitchFAQ() {
	var divListing = document.getElementById("faq-listing");
	var divDetail = document.getElementById("faq-detail");

	if (divListing && divDetail) {
		divListing.style.display = "block";
		divDetail.style.display = "none";
	}
}

function calculateLocatienet(url, postcode) {
	if (postcode == "") {
		alert("U moet een postcode invullen.");
		return false;
	}

	var startPostcode = removeSpaces(postcode);
	var completeUrl = url +
	                    "&speedprofile=1&optfactor=90&detailmap=on&language=dutch&country_start=NL" +
	                    "&postcode_start=" + startPostcode;

	window.open(completeUrl, "Route", "width=800,height=600, menubar=no,status=no,directory=no,location=no,resizable=yes,scrollbars=yes,toolbar=no");

	return true;
}
//String functions
function removeSpaces(value) { return value.replace(/ /g, ""); }

// function for faq
function prepareFaq() {

	$('a.faq-detail').click(function () {
		if ($('div#faq-listing:visible')) {
			$('div#faq-listing').hide()
		}

		var tempUrl = $(this).attr('href');

		tempUrl = tempUrl.replace(/ /g, "%20");
		$('div#faq-detail').load(tempUrl);
		$('div#faq-detail').show();

		$('a.testFaqBack').click(function () {
			$('div#faq-detail').hide();
			$('a.testFaqBack').hide();
			$('div#faq-listing').show()
		});
		$('a.testFaqBack').show();

		return false;
	});

}

// show hide the third column for testlab iframe
function showHideColumn(value) {
	if ($('div#column3-sidebar')) {
		if (value == 'hide') {
			$('div#column3-sidebar').hide();
		} else if ((value == 'show')) {
			$('div#column3-sidebar').show();
		}
	}
}

function resizeMainHeader(maxLength) {
    var header = $('div#content h1');
    var headerText = header.text();
    var words = headerText.split(" ");
    var factor = 1;

    for (i = 0; i < words.length; i++) {
        if (words[i].length > maxLength) {
            var tempfactor = maxLength / words[i].length;
            //get scalefactor
            if (tempfactor < factor) {
                factor = tempfactor;
            }
        }
    }
    var currentSize = header.css('font-size');
    if (currentSize == null) {
       
    }
    else {
        var currentSizeNr = currentSize.substring(0, 2);
        var newSizeNr = Math.floor(currentSizeNr * factor);
        var newSize = String(newSizeNr) + "px";

        header.css('font-size', newSize);
    } 
}

// plugin definition
$.fn.createDoubleColumnList = function (parent) {
	//local
	/*var maxItemTwoClmn = 3;
	var maxItemThreeClmn = 6;
	var maxItemFourClmn = 9;
	var maxItemFiveClmn = 12;*/

	var maxItemTwoClmn = 10;
	var maxItemThreeClmn = 30;
	var maxItemFourClmn = 50;
	var maxItemFiveClmn = 70;

	var childrenUl = parent + " ul";
	var childrenLi = parent + " li ul li";

	$(parent).find("ul").each(function (i) {
		var listLength = $(this).find('li').length;
		var divLength = Math.floor(listLength / 2);

		if (listLength > maxItemTwoClmn && listLength < maxItemThreeClmn) {
			$(this).columns({ columns: 2 });
		}
		else if (listLength > maxItemThreeClmn && listLength < maxItemFourClmn) {
			$(this).columns({ columns: 3 });
		}
		else if (listLength > maxItemFourClmn && listLength < maxItemFiveClmn) {
			$(this).columns({ columns: 4 });
		}
		else if (listLength >= maxItemFiveClmn) {
			$(this).columns({ columns: 5 });
		}
	});

};

//$.fn.createStaticDoubleColumnList = function(parent)
//{
//    $(parent).find("ul").each(function (i) 
//    {
//        $(this).columns({ columns: 2 });
//    });
//};


function sitesLinkMenu() {
	//hide de list
	var list = $('a.sitesLink').next();
	list.hide();
	$('a.sitesLink').mouseover(function () {
		list.show();
	});

	$('a.sitesLink').mouseout(function () {
		list.hide();
	});

	list.mouseover(function () {
		list.show();
	});

	list.mouseout(function () {
		list.hide();
	});
}

// this function is called when the DOM is loaded
$(document).ready(function () {


    // initialize banner pop-up
    if ($('#ABMbanner')) {
        if ($('#ABMbanner').length != 0) {
            openABMbannerPopup();
        }
    }
    // initialize pulldown menu's				

    if ($('#nav')) {
        $("#nav ul").createPullDown("#nav");
    }
    if ($('#sitesNavigation')) {
        $("#sitesNavigation ul").createPullDown("#sitesNavigation");
    }
    if ($("#locationNavigation ul")) {
        $("#locationNavigation ul").createPullDown("#locationNavigation");
    }
    if ($("#contextMenuNoTabs ul")) {
        $("#contextMenuNoTabs ul").createPullDown("#contextMenuNoTabs");
    }
    if ($("#contextMenu ul")) {
        $("#contextMenu ul").createPullDown("#contextMenu");
    }
    if ($("#contextMenu2 ul")) {
        $("#contextMenu2 ul").createPullDown("#contextMenu2");
    }
    // tabbed submenu
    //contextMenu();
    //contextMenu2();

    // initialize tabs
    if ($("div#productTabs")) {
        if ($("div#productTabs").length > 0) {
            createTabs();
        }
    }
    // only run the tabs script if needed
    if ($("div#filterTabs")) {
        createFilterTabs();
    }

    if ($("a.quickLinksPulldown")) {
        createPulldownContainers();
    }

    if ($("a.testLabPulldownMenu")) {
        createTestMenu();
    }

    if ($("a.faq-detail")) {
        prepareFaq();
    }

    if ($('div#content h1')) {
        resizeMainHeader(22);
    }
    if ($('div#content h2')) {
        resizeMainHeader(22);
    }


    if ($("#contextMenu ul")) {
        $("#contextMenu ul").createDoubleColumnList("#contextMenu");
    }
    if ($("#contextMenu2 ul")) {
        $("#contextMenu2 ul").createDoubleColumnList("#contextMenu2");
    }
    if ($("#contextMenuNoTabs ul")) {
        $("#contextMenuNoTabs ul").createDoubleColumnList("#contextMenuNoTabs");
    }

    if ($('#tabs')) {
        $('#tabs').tabs();
    }

    if ($('ul.filterList ul')) {
        $('.filterList').columns({ columns: 2, columnClass: 'columns', columnWrapperClass: 'columns-wrapper' });
    }

/*
    if ($('ul.competenceList')) {
        $('.competenceList').columns({ columns: 3, columnClass: 'competence-columns', columnWrapperClass: 'competence-columns-wrapper' });
    }
*/

    if ($('ul.competenceTrainingList')) {
        $('.competenceTrainingList').columns({ columns: 2, columnClass: 'competence-training-columns', columnWrapperClass: 'competence-training-columns-wrapper ' });
    }

    if ($('#detail-tabs')) {
        $('#detail-tabs').tabs();
    }

    if ($('a.sitesLink')) {
        sitesLinkMenu();
    }
});

function openABMbannerPopup() {
	var target = $('a img', '#ABMbanner');
	var currentbanner = target.attr('src');
	var bannerwidth = target.width();
	var bannerheight = target.height();
	var lastDisplayed;

	var x = 0;

	var cookievalue = readCookie('ABMbanner');
	if (cookievalue != 'undefined') {
		if (cookievalue != currentbanner) {
			if (lastDisplayed == currentbanner) {
				return false;
			}
			tb_show("", "#TB_inline?height=" + bannerheight + "&width=" + bannerwidth + "&inlineId=ABMbanner&KeepThis=true");
			createCookie('ABMbanner', (currentbanner));
			lastDisplayed = currentbanner;
		}
		else {
			return false;
		}
	}

	function createCookie(name, value, days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
			var expires = "; expires=" + date.toGMTString();
		}
		else {
			var expires = "";
		}

		document.cookie = name + "=" + value + expires + "; path=/";
	}

	function readCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for (var i = 0; i < ca.length; i++) {
			var c = ca[i];
			while (c.charAt(0) == ' ') c = c.substring(1, c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
		}
		return null;
	}

	function eraseCookie(name) {
		createCookie(name, "", -1);
	}

}
/*
function openFormHelp()
{
console.log("openFormHelp");
$( "<iframe width='400' height='500' ></iframe>" ).append( "TB_overlay" );
tb_show("", "#TB_inline?height=400&amp;width=500&amp;inlineId=vraagstukInfoContent");
}
*/



/* The function pageload is only called on pages containing an .Net UpdatePanel. */
function pageLoad(sender, args) {
    if ($('ul.competenceList').length) {
        $('.competenceList').columns({ columns: 3, columnClass: 'competence-columns', columnWrapperClass: 'competence-columns-wrapper' });
    }

    if ($('#detail-tabs').length) {

        $('#detail-tabs').tabs({
            add: function (event, ui) {
                $tabs.tabs('select', '#phcentercolumn_1_liTab4');
            }
        });

        if (args.get_isPartialLoad()) {
            var count = $('#filter4').parent().index() - 1;
            $('#detail-tabs').tabs('select', count)
        }

    }
}


