/*
________________________________________
¤¤¤ Common: Template byBrick™ ¤¤¤
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
*/

/*
 * runOnLoad.js: portable registration for onload event handlers.
 * 
 * This module defines a single runOnLoad() function for portably registering
 * functions that can be safely invoked only when the document is fully loaded
 * and the DOM is available.
 *
 * Functions registered with runOnLoad() will not be passed any arguments when
 * invoked. They will not be invoked as a method of any meaningful object, and
 * the this keyword should not be used.  Functions registered with runOnLoad()
 * will be invoked in the order in which they were registered.  There is no
 * way to deregister a function once it has been passed to runOnLoad().
 *
 * In old browsers that do not support addEventListener() or attachEvent(),
 * this function relies on the DOM Level 0 window.onload property and will not
 * work correctly when used in documents that set the onload attribute
 * of their <body> or <frameset> tags.
 */
function runOnLoad(f) {
    if (runOnLoad.loaded) f();    // If already loaded, just invoke f() now.
    else runOnLoad.funcs.push(f); // Otherwise, store it for later
}

runOnLoad.funcs = []; // The array of functions to call when the document loads
runOnLoad.loaded = false; // The functions have not been run yet.

// Run all registered functions in the order in which they were registered.
// It is safe to call runOnLoad.run() more than once: invocations after the
// first do nothing. It is safe for an initialization function to call
// runOnLoad() to register another function.
runOnLoad.run = function() {
    if (runOnLoad.loaded) return;  // If we've already run, do nothing

    for(var i = 0; i < runOnLoad.funcs.length; i++) {
        try { runOnLoad.funcs[i](); }
        catch(e) { /* An exception in one function shouldn't stop the rest */ }
    }
    
    runOnLoad.loaded = true; // Remember that we've already run once.
    delete runOnLoad.funcs;  // But don't remember the functions themselves.
    delete runOnLoad.run;    // And forget about this function too!
};

// Register runOnLoad.run() as the onload event handler for the window
if (window.addEventListener)
    window.addEventListener("load", runOnLoad.run, false);
else if (window.attachEvent) window.attachEvent("onload", runOnLoad.run);
else window.onload = runOnLoad.run;




runOnLoad(function(){ //fix for FireFox; when <img-tags doesn't have width and height set the "$(document).ready(function(){" will run before the images are loaded
	
	/*if($("div.???").size() > 0){ 
		setTimeout('fixHtml()', 1000);
	}
	else {*/
		fixHtml();
	//}
	
});	


function fixHtml() {


}


//iframe height fix
jQuery.fn.fixIfameHeight = function()
{
  //IE fix
  /*if($("#mdMainContent").size() > 0){
    $("#mdMainContent").width(($("#mdMainContent").width()-2)+'px');
  } 
  if($(".mdMainContent").size() > 0){
    var mainWidth = $(".mdMainContent").width()-2;
    $(".mdMainContent").width(mainWidth+'px');
  } */
 
  
	// Safari is no good.
	if ($.browser.safari) //$.browser.msie)
	{
		// Set specific variable to represent all iframe tags.
		var iFrames = document.getElementsByTagName('iframe');
		
		// Resize heights.
		function iResize()
		{
			for (var i = 0; i < iFrames.length; i++)
			{
				iFrames[i].style.height = iFrames[i].contentWindow.document.body.offsetHeight + 'px';
				
				//alert(iFrames[i].style.height)
			}
		}

		// Start timer when loaded.
		$('iframe').load(function()
			{
				setTimeout(iResize, 0);
			}
		);

		// For Safari to realize iframes loaded.
		for (var i = 0; i < iFrames.length; i++)
		{
			var iSource = iFrames[i].src;
			iFrames[i].src = '';
			iFrames[i].src = iSource;
		}
	}
	else
	{
		// For other good browsers.
		$('iframe').load(function()
			{
				this.style.height = (this.contentWindow.document.body.offsetHeight) + 'px';
			}
		);
	}
}


function hideFaqBox(){
	
	for(i=1;i<8;i++)
	{
		$("#Faq_" + i).hide();
	}

}

function ShowFaqBox(id){
	
	hideFaqBox();
	
	$(id).show("fast");

}

$(document).ready(function(){
	$.browser.addSelectors("#content");
	$.browser.addSelectors("#page");
	$.browser.addSelectors("#startpage");
	
	
	$(".FaqContent").hide();	
	$(".FaqBox").click(function(){
				
		
		if($(this).next(".FaqContent").css("display") == "none"){
			$(this).next(".FaqContent").show("fast");
		}
		else {
			$(this).next(".FaqContent").hide("fast");
		}
		
	
	});
	
	//**********************
	
	
	$(".faq a").click(function(){
		$(".faq .faq_answer").hide();
		$(this).next().show();
		$(this).fixSubNav();
	});
	
	//add input button image end
	$("input.btn_default").after('<div class="btn_default_end"></div><div class="clear"></div>');
	$("input.btn_search").after('<div class="btn_search_end"></div><div class="clear"></div>');
	
	//add width to input button container to make sure that the button image end doesn't wrap
	if($("#startpage input.btn_default").parent(".btnContainer").size() < 1){	//not on startpage
		$("input.btn_default").parent(".btnContainer").each(function(){
			var iBtnWidth = $(this).width();
			if($(this).children(".btn_default_end").size() > 0){
				iBtnWidth += $(this).children(".btn_default_end").width();
				$(this).css("width",  iBtnWidth + 'px');
			}
		});
	}
	
	
	
	// kontrakt flickar
	
	$("#KontraktMenu li.KontraktInActive").after('<div class="KontraktRight"></div><div class="clear"></div>');
	$("#KontraktMenu li.KontraktActive").after('<div class="KontraktRightActive"></div><div class="clear"></div>');


	$("#KontraktMenu li").click(function(){
		
		//Inaktivera alla flikar
		makeMenuInactive();
		
		// sätter aktiv flik
		
		$(this).addClass("KontraktActive");
						
		$(this).after('<div class="KontraktRightActive"></div><div class="clear"></div>');
	
	});
	
	
	
	//fix iframe height
	if($("#cmsContentIframe").size() > 0){
		$(this).fixIfameHeight();
	}
	
	
	//if forced template is set; add forcedTempladeId to all links that doesnt have target set
	var strFindParam = "forcedTemplateId";
	var strForcedTemplateId = GetUrlParam(strFindParam);
	if(strForcedTemplateId.length > 0){
		var extraParams = strFindParam + "=" + strForcedTemplateId;

		//add forcedTempladeId to all links that doesnt have target set
		$("a").each(function(){
			var bTargetExist = false;
			var strTarget = ""; 
			if($(this).attr("target")){
				strTarget = $(this).attr("target");
				if(strTarget.length > 0){
					bTargetExist = true;
				}
			}
			
			if(bTargetExist){
				switch(strTarget){
					case "_blank":
						break;
				}
			}
			else {
				if($(this).attr("href")){
					var url = $(this).attr("href");
					if(url.indexOf("index.php") > -1){
						if(url.indexOf("index.php?") > -1){
							url += "&" + extraParams;
						}
						else {
							url += "?" + extraParams;
						}
						$(this).attr("href", url);
					}
				}
			}
		});
	}
	
});

function GetUrlParam( paramName )
{
	var oRegex = new RegExp( '[\?&]' + paramName + '=([^&]+)', 'i' ) ;
	var oMatch = oRegex.exec( window.location.search ) ;
	
	if ( oMatch && oMatch.length > 1 )
		return oMatch[1] ;
	else
		return '' ;
}

function makeMenuInactive()
{
	
	for(i=1;i<10;i++)
	{
		//alert("delete" + i);
		$("#KontraktMenu li#KontraktMenu_" + i).removeClass("KontraktRightActive");
		$("#KontraktMenu li#KontraktMenu_" + i).removeClass("KontraktActive");
				
	}

}


function closeMsg(objMsg){
  if(objMsg){ objMsg.style.display = 'none'; }
}

var newWin;
var previousURL;
var previousWinName
function OpenWindow(URL,Width,Height,scrollbars,winname,resizable){
	if(scrollbars == "yes" || scrollbars == "1"){
	  scrollbars = "yes"
  }
  else{
	  scrollbars = "no"
  }
  if(resizable == "yes" || resizable == "1"){
	  resizable = "yes"
  }
  else{
	  resizable = "no"
  }
  
  if (!newWin || newWin.closed || winname != previousWinName){
		var winParams = 'toolbar=0,scrollbars='+scrollbars+',status=no,resizable='+resizable;
		if(Width.length > 0 && !isNaN(Width) && Height.length > 0 && !isNaN(Height)){
			if(parseInt(Width) > 0 && parseInt(Height) > 0){
				winParams += ',width='+Width+',height='+Height;
			}
		}
		newWin = window.open(URL,winname,winParams);
			
		previousURL = URL;
		if (!newWin.opener){
			newWin.opener = window;
		}
	}
	else if(URL != previousURL){
		newWin.location.href = URL;
		newWin.focus();
		previousURL = URL;
	}
	else {
		newWin.focus();
	}
	
	previousWinName = winname
}


function getForm(){
	for(var i=0; i < document.forms.length; i++){
		if(document.forms[i]){
			if(document.forms[i].name.indexOf('j_id') > -1){
				return document.forms[i];
			}
		}
	}
	
	return document.forms[0];
}

function getFirstChildByName(obj, childName){
	var objChildHierarchy = obj;
	for(var i=0; i < 10; i++){ //max depth 10
		objChildHierarchy = objChildHierarchy.firstChild;
		if(objChildHierarchy){
			if(objChildHierarchy.name && objChildHierarchy.name.indexOf(childName) > -1){
				return objChildHierarchy;
			}
		}
		else {
			break;
		}
	}
	
	return null;
}





function showAltTextF2K(objName, txt, bToggleHideShowSelecteBoxes){
	var objAltMsg = document.getElementById(objName);
	if(objAltMsg){
		objAltMsg.innerHTML = txt;
		objAltMsg.style.width = '200px';
		if(txt.length > 100){
			objAltMsg.style.width = '400px';
		}
		objAltMsg.style.display = "block";
		if(bToggleHideShowSelecteBoxes){
			toggleHideShowAllSelectBoxesF2K(false);
		}
	}
}


function hideAltTextF2K(objName){
	var objAltMsg = document.getElementById(objName);
	if(objAltMsg){
		objAltMsg.style.display = "none";
		toggleHideShowAllSelectBoxesF2K(true);
	}
}

function PopupToggleVisibleF2K(objName) {
	

	var obj = document.getElementById(objName);
	if (!obj)
		return null;
	
	//alert ("obj.style.display: " + obj.style.display);

	if (obj.style.display.length <= 0)
		obj.style.display= "none";

	if (obj.style.display == "none") {
		obj.style.display = "block";
	} else {
		obj.style.display = "none";
	}
}

function PopupShowF2k(objName) {
	var obj = document.getElementById(objName);
	if (!obj)
		return null;

	obj.style.display = "inline";
}

function PopupHideF2k(objName) {
	var obj = document.getElementById(objName);
	if (!obj)
		return null;

	obj.style.display = "none";
}

function toggleHideShowAllSelectBoxesF2K(bShow){
	var appName = navigator.appName;
	if(appName.indexOf("Microsoft") > -1){
		var i = 0;
	  var selectObjects = document.getElementsByTagName("select")
	  if(selectObjects){
	  	for(var i=0; i < selectObjects.length; i++){
	  		if(selectObjects[i].type == "select-one"){
					if(bShow){  			
		  			selectObjects[i].style.visibility = "visible";
		  		}
		  		else {
		  			selectObjects[i].style.visibility = "hidden";
		  		}
	  		}
	  	}
	  }
  }
}

function showAltTextForceWidth(objName, txt, boxWidth){
	var objAltMsg = document.getElementById(objName);
	if(objAltMsg){
		if (txt.length > 0) {
			objAltMsg.innerHTML = txt;
		}
		
		objAltMsg.style.width = boxWidth.toString() + 'px';
		objAltMsg.style.display = "block";
	}
}

var strMaxWidth = 0;
function showAltText(objName, txt){
	var objAltMsg = document.getElementById(objName);
	if(objAltMsg){
		if (txt.length > 0) {
			objAltMsg.innerHTML = txt;
		}
		else {
		  txt = objAltMsg.innerHTML;
		}
		
		if(strMaxWidth.length > 0 && !isNaN(strMaxWidth)){
			objAltMsg.style.width = strMaxWidth + 'px';
		}
		else {
			objAltMsg.style.width = '200px';
			if(txt.length > 100){
				objAltMsg.style.width = '400px';
			}
		}
		objAltMsg.style.display = "block";
	}
}



function hideAltText(objName){
	var objAltMsg = document.getElementById(objName);
	if(objAltMsg){
		objAltMsg.style.display = "none";
	}
}

function listProperties(obj, strFind){
  var i = 0;
  var msg = "";
  for(itm in obj){
    if(i > 50){
      alert(msg);
      i = 0;
      msg = "";
    }	
		if(strFind.length > 0){
			if(itm.toLowerCase().indexOf(strFind.toLowerCase()) > -1){	//search for lower case part of item name
				msg += itm + "\n";	
				i++;
			}
		}
		else {
			msg += itm + "\n";	
			i++;
		}

  }
  alert(msg);
}


 var prevThemePic = -1;
 function ThemePic() {
	/* IMAGES IS LOADED ON defatul.htm */
	/*var aThemes = new Array(2);
            aThemes[0]= 'images/pic_page_start_theme1a.jpg';
            aThemes[1]= 'images/pic_page_start_theme1a.jpg';*/
    if(prevThemePic == -1){
		RndPic = Math.floor(Math.random()*aThemes.length);
	}
	else{
		RndPic = prevThemePic + 1;
		if(RndPic >= aThemes.length) { RndPic = 0;}
	}
	var objThemePicHolder = document.getElementById("themePicHolder")
	if (objThemePicHolder){
		objThemePicHolder.style.backgroundImage = "url(" + aThemes[RndPic] + ")";
	}
	prevThemePic = RndPic;
	
	if(iLoadNextPictureAfterSec > 0){
		LoadThemePic();
	}
}

function LoadThemePic() {
	setTimeout('ThemePic()', iLoadNextPictureAfterSec*1000);
}
