/*
*	MudiBlox Framework.
*	Javascript functions
*	Author : Jeffrey van Hal (Digitien), Muhamed Durakovic (Mubis)
*	CopyRight 2008 
*	Date: 30-07-2008
*/
//var default_init_page = 49;
var default_init_page = 'Band-home';

var QueryString = {
   params : $(window.location.search.toQueryParams()),
  
   get : function(key, defaultValue) {
       if (defaultValue == null) defaultValue = null;
       var value = this.params[key]
       if (value==null) value = defaultValue;
       return value;
   },
  
   set : function(key, value) {
       this.params[key] = value;
   },
  
   remove : function(key) {
       this.params = this.params.collect(function(param) {
           if (key != param.key) return param;
       }).compact();
   },
  
   make : function() {
       return "?" + this.params.collect(function(param) {
           return escape(param.key) +"="+ escape(param.value);
       }).join("&");
   },
  
   go : function() {
       window.location.href = location.pathname + this.make();
   }
}

	function setHeight(){
			
		 var viewportwidth;
		 var viewportheight;

		 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
		 if (typeof window.innerWidth != 'undefined')
		 {
			  viewportwidth = window.innerWidth,
			  viewportheight = window.innerHeight
		 }
		// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
		 else if (typeof document.documentElement != 'undefined'
			 && typeof document.documentElement.clientWidth !=
			 'undefined' && document.documentElement.clientWidth != 0)
		 {
			   viewportwidth = document.documentElement.clientWidth,
			   viewportheight = document.documentElement.clientHeight
		 }
		 // older versions of IE
		 else
		 {
			   viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
			   viewportheight = document.getElementsByTagName('body')[0].clientHeight
		 }
		//document.write('<p>Your viewport width is '+viewportwidth+'x'+viewportheight+'</p>');
		
			var cssHeaderHeight = 150; 
			var cssMargins = 10;
			var pageHeight = $('mainContent');
			var fillHeight = $('container');
			var contentHeight = $('thiscontent');
			
			var defaultSize = 650;
			var contentOffset = contentHeight.offsetHeight;
			//available height is offset minus viewport height -1px
			var container = viewportheight;
			var total = cssHeaderHeight + contentOffset + cssMargins;
			
			//if not full page view you can interchange "container" by "defaultSize"
			if(total < container){

				fillHeight.style.height = container.toString() + 'px';
			}else{
			
				fillHeight.style.height =	total.toString() + 'px';
			}
		}


var currSelectedMenu = null;
var currContent = new Array();
/*
	AJAX FrameWork Functions 	
*/

function openAjax(){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	
	if (!ajaxRequest) { 
   		alert("Unfortunately your browser doesn’t support this feature."); 
   		return false; 
 	} 

	return ajaxRequest;
}

function closeAjax(objAjax){	
	objAjax.send(null); 
}

function imgdivloader(objAjax, strImg)
{
	if(objAjax.readyState == 1){
	document.getElementById(strImg).style.display="block";	
	}
	if(objAjax.readyState == 2){
	document.getElementById(strImg).style.display="none";	
	}	
}

function callfunction(url, callbackFunction, strImgId ,return_xml){

	var ajaxRequestObj = openAjax();	
	ajaxRequestObj.onreadystatechange = function(){
	imgdivloader(ajaxRequestObj,strImgId);
	
		if(ajaxRequestObj.readyState == 4){
			
			if (return_xml) { 
				 response = ajaxRequestObj.responseXML; 
			}else{ 
				 response = ajaxRequestObj.responseText; 
			} 
			if (typeof callbackFunction == 'function') {				
				callbackFunction(response);
			}
			else{
				alert("callbackFunction: "+callbackFunction+" not defined");
			}
			

		}
	}
	if(isEmptyStr(url)){
		alert("No Url defined for this function.");
	}
	ajaxRequestObj.open("POST", url, true);
	closeAjax(ajaxRequestObj); 
}

/* 
	AJAX Wrapper Functions
*/
	function callMenu2(id){
		callfunction("?stype=majax&action=menu&cid="+id, mycallback, "imgload",false);
		currSelectedMenu = id;		
	}
	
	function callMenu(id){
		callfunction("?stype=majax&action=page&page="+id, mycallback, "imgload",false);
		currSelectedMenu = id;		
	}
	
	var globPageId;
	function callArea(id){
		callfunction("?stype=majax&action=area&area="+id, productAreaCallBack, "imgload",false);
		globPageId = id;
	}
	
	
	function loadPage(id){
		$('page').value = id;
		$('pageloader').submit();
	}
	

/* 
	AJAX callBackFunctions
*/
	function mycallback(ajxresponseText){
		//alert(ajxresponseText);	
		document.getElementById('contentblock').innerHTML = ajxresponseText;
		currContent.push = [ {currSelectedMenu:ajxresponseText} ];
	}
	
	function productAreaCallBack(ajxresponseText){
			document.getElementById('div_area_description').innerHTML = ajxresponseText;
			if(globPageId == 60){
			document.getElementById('backlink').style.display = 'none';
			}else{
			document.getElementById('backlink').style.display = 'block';
			}
			
		}
		

/* 
	init function 
*/
function init2(){
	var cid = QueryString.get('cid',null);
	if(cid != null){
		id=cid;			
	}else{
		id=default_init_page; //default startpage				
	}
	
	if(typeof currContent == 'object'){
		if(currContent.length > 0){
		alert(currContent[cid]);
		}
	}
	if(currSelectedMenu != null && currSelectedMenu == cid){
			document.getElementById('contentblock').innerHTML = currContent;
	}else{
		callMenu(id);
	}
	
	
}

function init(){
	var cid = QueryString.get('page',null);
	if(cid != null){
		id=cid;			
	}else{
		id=default_init_page; //default startpage				
	}
	
	if(typeof currContent == 'object'){
		if(currContent.length > 0){
		alert(currContent[cid]);
		}
	}
	if(currSelectedMenu != null && currSelectedMenu == cid){
			document.getElementById('contentblock').innerHTML = currContent;
	}else{
		callMenu(id);
	}
	
	
}

/*
	Custom string and validation functions
*/

	
function addMenuItem(){

	var str = $('newmenuitem').value;
	if(!isEmptyStr(str)){
		$('menuform').submit();
	}else{
		alert('Vul aub eerst een naam in het veld!');
		$('menuitem').focus();
	}
	
}

function isEmptyStr(s)
{
   var whitespace = " \t\n\r";
   var i;
   if((s == null) || (s.length == 0))
      return true;
   // Search string looking for characters that are not whitespace
   for (i = 0; i < s.length; i++)    
    {   
      var c = s.charAt(i);
      if (whitespace.indexOf(c) == -1) 
        return false;
    }
    // At this point all characters are whitespace.
    return true;
}

function urlencode(text) 
{		
	
	text=text.replace("/\//g","%2F");
	text=text.replace("/?/g","%3F");
	text=text.replace("/=/g","%3D");
	text=text.replace("/&/g","%26");	

		
	/*
	//text=ReplaceAll(text, "/\", "%2F"); 
	text=ReplaceAll(text, "?", "%3F");
	text=ReplaceAll(text, "=", "%3D");
	text=ReplaceAll(text, "&", "%26");
	text=ReplaceAll(text, " ", "+");
	text=ReplaceAll(text, ",", "%2c");	
	*/
	return text;
}


/************** TABS *****************************/
function toggleDisp() {
    for (var i=0;i<arguments.length;i++){
        var d = $(arguments[i]);
        if (d.style.display == 'none')
            d.style.display = 'block';
        else
            d.style.display = 'none';
    }
}
/*-----------------------------------------------------------
    Toggles tabs - Closes any open tabs, and then opens current tab
    Input:     1.The number of the current tab
                    2.The number of tabs
                    3.(optional)The number of the tab to leave open
                    4.(optional)Pass in true or false whether or not to animate the open/close of the tabs
    Output: none 
    ---------------------------------------------------------*/
function toggleTab(num,numelems,opennum,animate) {
	
    if ($('tabContent'+num).style.display == 'none'){
        for (var i=1;i<=numelems;i++){
            if ((opennum == null) || (opennum != i)){
                var temph = 'tabHeader'+i;
                var h = $(temph);
                if (!h){
                    var h = $('tabHeaderActive');
                    h.id = temph;
                }
                
                var tempc = 'tabContent'+i;
                var c = $(tempc);
                if(c.style.display != 'none'){
                    if (animate || typeof animate == 'undefined'){
                     	Effect.toggle(tempc,'appear',{duration:0.5, queue:{scope:'menus', limit: 3}});
                    }else{
                        toggleDisp(tempc);
                    }
                }
            }
        }
        
        var h = $('tabHeader'+num);
        if (h)
            h.id = 'tabHeaderActive';
        h.blur();
        var c = $('tabContent'+num);
        c.style.marginTop = '2px';
        if (animate || typeof animate == 'undefined'){
            Effect.toggle('tabContent'+num,'appear',{duration:0.5, queue:{scope:'menus', position:'end', limit: 3}});
            }else{
            toggleDisp('tabContent'+num);
        }
    }
}


sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" ie_does_hover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" ie_does_hover\\b"), "");
			
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);



