/* Javascript PT 5/05/2009 */
/*
 *
 *  Argenta Base Javascript functions.
 *  Written and maintained by Amplexor NV
 *  support-argenta@amplexor.com
 *
 *  Original Author: Navigation: Kevin Vaesen 
 *  Comments are mixed language.
 *
 * Enter the Sandman
 *
 **/
 
 
// Deze var disabled tijdelijk alle animaties, om ervoor te zorgen dat we geen animatie verstoren
g_bGlobalDisableAnimations = false;

// Global timer handle -- wordt gebruikt om setTimeout handle op de slaan om timeouts te cancelen etc
g_hTimer = null; 
g_hTimerOut = null;

/*
 *
 *  This function will convert the regular navigation menu into a collapsible one,
 *  where the current section is visible and the rest is collapsed. Yahoo is used
 *  for animating the different parts.
 *
 *  This function expects elMenu to be a UL element, whose children should be LI.
 *  Each LI has a text element, followed by UL > LI > A
 *
 **/
function CreateMenuNavigation( elMenu, elSkip )
{
    //
    //  Sanity checks
    //
    if( !elMenu || elMenu.firstChild == null )
        return;
    
    //
    //  Enumerate the menu
    //
    for( var elCurrent = elMenu.firstChild; elCurrent; elCurrent = elCurrent.nextSibling )
    {
        if( elCurrent.nodeName != "LI" )
            continue;
        
        //  Skip the active item -- this one is to remain visible at all times.
        //  TODO: expand this to be an array of ID's to keep visible instead of just one item!
        if( elSkip && elCurrent.id ==  elSkip.id )
            continue;
            
        ProcessCategory( elCurrent, elMenu );
    }
}


/*
 *  Processes each block of links with a title and an UL element
 *
 **/
function ProcessCategory( elNavItem, elMenu )
{
    var elTitle, elChild;

    elTitle = elNavItem.firstChild;
    elChild  = elNavItem.firstChild.nextSibling;
   
    //
    //  Zoek een A als titel
    //
     for( elTitle = elNavItem.firstChild; elTitle && elTitle.nodeName != "A"; elTitle = elTitle.nextSibling )
        ; /* <-- puntkomma! */

    //
    //  Zoek een UL kindje en verberg het (want het is een lelijk kindje).
    //
    for( elChild = elNavItem.firstChild; elChild && elChild.nodeName != "UL"; elChild = elChild.nextSibling )
        ; /* <-- puntkomma! */

    // Misschien hebben we d'r wel eentje?
    if( elChild && elChild.nodeName == "UL" ) // only do lists for now
    {    
        if( !elChild.id || ! elChild.id.length )    // we need an ID, so fix one if we don't have one
            elChild.id = GenerateID( "nav" ) ;
        
        ShowBlock( elChild, false, false ); // Hide this block
    }
    
    if( elTitle.nodeName == "A" ) // only do plain-text or A-elements for now
        GenerateHoverCode( elTitle, elChild, elMenu ); 
}


/*
 *  GenerateHoverCode -- Generates the code needed to allow for expansion
 *  of items when the mouse passes over them. This function will convert 
 *  all plain-text items to <a> elements.
 *  Next, it will add mouseover and mouseout events, which will hide or
 *  show the appropriate items in the navigation menu.
 *
 **/
 
 function nav_onmouseover( strMenu, strBlock )
 {
    if( g_hTimer ) 
        clearTimeout( g_hTimer ); 
    
    g_hTimer = setTimeout( function() { DoAccordeon( strMenu,strBlock ); }, 450);
 }

function nav_onmouseout( strBlock )
{
    //if( g_hTimerOut ) 
     //   clearTimeout( g_hTimerOut ); 
       
    if( g_hTimer ) 
        clearTimeout( g_hTimer ); 
           
    //g_hTimerOut = setTimeout( function() { ShowBlock( strBlock, false, true ) }, 2000);
}

function GenerateHoverCode( elTitle, elBlockToToggle, elMenu )
{
    var elNew, elText, elParent, strJSMouseOver, strJSMouseOut;

    if( !elTitle )
        return null;
 
 
    if( !(elParent = elTitle.parentNode) )
        return null;
    
    if( !elBlockToToggle )
        return null;

    //
    //  Zorg dat we zeker en vast een <a> tag hebben om aan te binden.
    //
    
    // Do tha funk: wrap plain text elements with a link
    if( elTitle.nodeType == 3 )
    {
        elNew = document.createElement( "a" );
        elText = document.createTextNode( elTitle.nodeValue );
        elNew.appendChild( elText );      
        elNew.id = GenerateID( "lnk" );
        elParent.replaceChild( elNew, elTitle ); 
        elTitle = elNew;
    }
    
    YAHOO.util.Dom.addClass( elTitle, "nav-expandable" );

    //
    //  Bind de code aan de mouseevents 
    //
    
   // Wacht een halve seconde met openklappen, om mensen die enkel de landingpage moeten hebben niet nodeloos verkeerd te laten klikken door een schuivende link
    strJSMouseOver = "nav_onmouseover('"+elMenu.id +"','"+ elBlockToToggle.id  +"')";
    strJSMouseOut = "nav_onmouseout( '" + elBlockToToggle.id  +"')"; 
    
    // Mouseover event: expand the navigatiebar
    elTitle.setAttribute( "onmouseover", "javascript:" + strJSMouseOver + ";return true;" );
    elTitle.onmouseover = function() { eval( strJSMouseOver ); }; // here be dragons!

    elTitle.setAttribute( "onfocus", "javascript:" + strJSMouseOver + ";return true;" );
    elTitle.onfocus = function() { eval( strJSMouseOver ); }; // enter the dragon!

    // Mouseout event: collapse the navigatiebar na enkele seconden
    // BUGBUG -- dit werkt nog niet 100%
    elTitle.parentNode.setAttribute( "onmouseout", "javascript:" + strJSMouseOut + ";return true;" );
    elTitle.parentNode.onmouseout = function() { eval( strJSMouseOut ); }; // fix voor IE
    
    
    return elNew;
}

/*
 *  ShowBlock -- Toont of verbergt een block met of zonder animatie
 *
 **/
function ShowBlock( elBlock, bShow, bAnimate )
{
    var ANIM_SPEED = 0.2;
    var strID = elBlock.id;
    var strHeight, nHeight;
    var animOpacity;
    var animHeight;
    
    // Convert string naar element
    if( typeof( elBlock ) != 'object')
        elBlock  = document.getElementById( elBlock );
    
    // Sanity check: is het wel nodig dat we iets doen ?
    if( !YAHOO.util.Dom.hasClass( elBlock, 'nav-hidden') == bShow )
        return;
        
    // Vraag de hoogte van het element op voor animatie
    var strHeight = YAHOO.util.Dom.getStyle(elBlock,'height');
    
    if( strHeight === 'auto' )
        nHeight = parseInt( elBlock.offsetHeight );
    else
        nHeight = Math.max( parseInt(strHeight), elBlock.offsetHeight );

    if( bAnimate )
    {
        animOpacity = (new YAHOO.util.Anim( elBlock, { opacity: { from: bShow ? 0 : 1,          to: bShow ? 1  : 0      }  }, ANIM_SPEED * 2, YAHOO.util.Easing.easeBoth));
        animHeight = (new YAHOO.util.Anim( elBlock,  { height:  { from: bShow ? 0 : nHeight,    to: bShow ? nHeight : 0 }  }, ANIM_SPEED, YAHOO.util.Easing.easeBoth));
    }
    
   // Moeten we de block verbergen of tonen?
   if( bShow )
   {
        //  Tonen, dus we moven de block naar de juiste plaats, zetten de hoogte op 0, 
        //  en herstellen de handel vooraleer we beginnen met de animatie
        var fnCallback = function( a, b, obj ) 
        {  
            YAHOO.util.Dom.removeClass( obj, 'nav-hidden') ;
            YAHOO.util.Dom.addClass( obj, 'nav-shown' );
            
            YAHOO.util.Dom.setStyle( obj,'height', '0px' );  // reset de animatie params
            YAHOO.util.Dom.setStyle( obj,'opacity', 0 );
            
        };
        
        if( bAnimate )  
            animHeight.onStart.subscribe( fnCallback, elBlock );
        else
            fnCallback( 0,0, elBlock );
    }
    else
    {
        // Verbergen, dus NA de animatie herstellen we de hoogte, maar moven we onze block uit het zicht.
        // Op die manier blijft onze hoogte behouden, wat een feest

        var fnCallback = function( a, b, obj ) 
        {  
            YAHOO.util.Dom.removeClass( obj, 'nav-shown') ;
            YAHOO.util.Dom.addClass( obj, 'nav-hidden' );
            
            YAHOO.util.Dom.setStyle( obj,'height', 'auto' ); //nHeight + 'px' );  // reset de animatie params
            YAHOO.util.Dom.setStyle( obj,'opacity', 1 );
        };

        if( bAnimate )
            animHeight.onComplete.subscribe( fnCallback, elBlock );
        else
            fnCallback( 0,0, elBlock );
    }


    if( bAnimate )
    {
        //  Zorg ervoor dat we geen twee animaties terzelfdertijd starten (geeft lelijke effectjes voor de usert)
        animHeight.onStart.subscribe( function() { g_bGlobalDisableAnimations = true; } );
        animHeight.onComplete.subscribe( function() { g_bGlobalDisableAnimations = false; } );
        
        animOpacity.animate();
        animHeight.animate();
    }        
}

/*
 *  DoAccordeon: Collapses all other items when a certain menuitem is expanded
 *
 **/
function DoAccordeon( strMenu, strToExpand )
{
    //
    //  Sanity checks
    //
    var elMenu = document.getElementById( strMenu );
    
    if( !elMenu || elMenu.firstChild == null )
        return false;
    
    if( g_bGlobalDisableAnimations )
        return;
        
    //
    //  Enumerate the menu
    //
    var elCurrent = elMenu.firstChild;
    
    for( ; elCurrent; elCurrent = elCurrent.nextSibling )
    {
       if( elCurrent.nodeName != "LI" || !elCurrent.firstChild )
            continue;
       
        for( var objCurrChild = elCurrent.firstChild; objCurrChild && objCurrChild.nodeName != "UL"; objCurrChild = objCurrChild.nextSibling )
            ; /* <-- semicolon OJA! */
            
        if(  objCurrChild && objCurrChild.nodeName == "UL" && objCurrChild.id )
            ShowBlock( objCurrChild, ( objCurrChild.id == strToExpand ), true );
    }
}


/*
 *  GenerateID: Returned voor ons een (semi-unieke) ID
 **/
function GenerateID( strPrefix )
{
    return strPrefix + (new Date).getTime() + Math.floor( Math.random() * 1024 );
}

/* FAQ */
function collapse( strID )
{
	var collection = YAHOO.util.Dom.getElementsByClassName( "question" );
	for(var i=0;i<collection.length;i++)
	{
	    close( 
			document.getElementById( collection[i].id ), 
			document.getElementById( GetAnswerID( collection[i].id ) )
		);
	}
	
	open( 
		document.getElementById( strID ), 
		document.getElementById( GetAnswerID( strID ) ) 
	);
}

function InitFAQ()
{
	var collection = YAHOO.util.Dom.getElementsByClassName( "question" );
	for(var i=0;i<collection.length;i++)
	{
		var s = collection[i].id ;
		YAHOO.util.Event.addListener(
			collection[i], 
			"click", 
			function(e,a){
					collapse( a);
				}, 
			collection[i].id);
	
//		cssjs('add', document.getElementById(GetAnswerID( collection[i].id )) ,'shown');
	}
	collapse( "faq1-dt" );
}

function GetAnswerID( strQuestionID )
{
	return strQuestionID.replace( "-dt", "-dd" );
}
	
	function cssjs(a,o,c1,c2)
	{
    	switch (a)
    	{
		    case 'swap':
			    o.className=!cssjs('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
				break;
			case 'add': 
				if(!cssjs('check',o,c1))
				{
				    o.className+=o.className?' '+c1:c1;
				}
				break;
			case 'remove':
			    var rep=o.className.match(' '+c1)?' '+c1:c1;
				o.className=o.className.replace(rep,'');
				break;
			case 'check':
				return new RegExp('\\b'+c1+'\\b').test(o.className)
				break;
		}
	}
	
	function open(q,a)
	{
	//	alert("opening " + a.id );
		cssjs('add',q,'active');
		cssjs('remove',a,'hidden');
		//cssjs('swap',a,'hidden','shown');
		q.style.fontWeight = "bold";
	}


	function close(q,a)
	{
		//alert("closing " + a.id );
	    cssjs('remove',q,'active');
	    cssjs('add',a,'hidden');
		//cssjs('swap',a,'shown','hidden');
		q.style.fontWeight = "normal";
	}




//============
// Font Sizer
//============

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=/;" + GetCookieDomainString();
}

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 setFontSize(dimension){
	var area = document.getElementById("maincontainer");
	area.style.fontSize = dimension;
	createCookie("fontsize", dimension, 1);
	setFontStyle(dimension);
}

function setFontStyle(dimension){
	document.getElementById("fontsizeswitch1").className = '';
	document.getElementById("fontsizeswitch2").className = '';
	document.getElementById("fontsizeswitch3").className = '';

	if (dimension != ''){
		if (dimension == '1em')
			document.getElementById("fontsizeswitch1").className = 'active';
		else{
			if (dimension == '1.15em' )
				document.getElementById("fontsizeswitch2").className = 'active';
			else
				document.getElementById("fontsizeswitch3").className = 'active';
		}
	}
	else
		document.getElementById("fontsizeswitch1").className = 'active';
}

function SetLangCookie( strLang )
{
   createCookie( "language", strLang, 100 );
}

function GetCookieDomainString(){
  e = document.domain.split(/\./);
  if(e.length > 2) {
    return("domain=" + "." + e[1] + "." +  e[2]) + ";"  ;
  }else{
    return("");
  }
}
