/**/
/*  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 h2');
    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 currSize =  header.css('font-size');
    var currSizeNr = currSize.substring( 0,2 );
    var newSizeNr = Math.floor( currSizeNr*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});
        }
    }); 
    
};

// this function is called when the DOM is loaded
$(document).ready(function() {

    
// initialize banner pop-up
if ($('#ABMbanner').length != 0) 
{  
    openABMbannerPopup(); 
}

    // initialize pulldown menu's				
    $("#nav ul").createPullDown("#nav");
    $("#sitesNavigation ul").createPullDown("#sitesNavigation");
    $("#locationNavigation ul").createPullDown("#locationNavigation");
    $("#contextMenuNoTabs ul").createPullDown("#contextMenuNoTabs");

    $("#contextMenu ul").createPullDown("#contextMenu");
    $("#contextMenu2 ul").createPullDown("#contextMenu2");
	
	// tabbed submenu
	//contextMenu();
	//contextMenu2();
	
	// initialize tabs
	if( $("div#productTabs").length > 0 )
	{
        createTabs();
    }

    // only run the tabs script if needed
    if ($("div#filterTabs").length > 0) {
        createFilterTabs();
    }

    if ($("a.quickLinksPulldown").length > 0) {
        createPulldownContainers();
    }

    if ($("a.testLabPulldownMenu").length > 0) {
        createTestMenu();
    }

    if ($("a.faq-detail").length > 0) {
        prepareFaq();
    }

    if ($('div#content h2').length > 0) {
        resizeMainHeader(22);
    }

    if ($("#contextMenu ul").length > 0) {
        $("#contextMenu ul").createDoubleColumnList("#contextMenu");
    }
    if ($("#contextMenu2 ul").length > 0) {
        $("#contextMenu2 ul").createDoubleColumnList("#contextMenu2");
    }
    if ($("#contextMenuNoTabs ul").length > 0) {
        $("#contextMenuNoTabs ul").createDoubleColumnList("#contextMenuNoTabs");
    }


});

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");
}
*/





