/*
________________________________________
¤¤¤ 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() {
	fixColumnClasses();
	fixColumns();

	//synk box heights
	/*if($("table tr.boxSynkHeight:visible").size() > 1) {
		var iBoxMaxHeight = 0;
		$("table tr.boxSynkHeight:visible").each(function(){
			var iBoxHeight = $(this).parent("tbody").parent("table").height();
			if(iBoxHeight > iBoxMaxHeight){
				iBoxMaxHeight = iBoxHeight;
			}
		});
		
		
		$("table tr.boxSynkHeight:visible").each(function(){
			var iBoxHeight = $(this).parent("tbody").parent("table").height();
			if(iBoxHeight < iBoxMaxHeight){
				$objCell = $(this).find("td");
				if($objCell) {
					var iCellHeight = $objCell.height();
					var iNewCellHeight = iCellHeight + iBoxMaxHeight - iBoxHeight;
					//if (jQuery.browser.firefox) {
					//	var iCellVerticalPadding = parseInt($objCell.css("padding-top").replace("px", "")) + parseInt($objCell.css("padding-bottom").replace("px", ""))
					//	iNewCellHeight += iCellVerticalPadding;
					//}
					//alert(iCellHeight + " + " + iBoxMaxHeight + " - " + iBoxHeight + " + " + " = " + iNewCellHeight);
					$objCell.height(iNewCellHeight);
				}
			}
		});
		
	}
	*/
	
	//fix list 
	$(".list td table").each(function(){
		$(this).find(".list-row:first").addClass("list-row-first");
	});
	
	
	//fix error alert boxes
	if($("div.boxMsg .errMsg").size() > 0){
		$("div.boxMsg").each(function(){
			if($(this).find(".errMsg").attr("class")) {
				var objClass = $(this).attr("class");
				$(this).attr("class", objClass + " errMsgBox");
			}
			
		});
	}
	//fix info alert boxes
	if($("div.boxMsg .infoMsg").size() > 0){
		$("div.boxMsg").each(function(){
			if($(this).find(".infoMsg").attr("class")) {
				var objClass = $(this).attr("class");
				$(this).attr("class", objClass + " infoMsgBox");
			}
			
		});
	}
	//fix warn alert boxes
	if($("div.boxMsg .alertMsg").size() > 0){
		$("div.boxMsg").each(function(){
			if($(this).find(".alertMsg").attr("class")) {
				var objClass = $(this).attr("class");
				$(this).attr("class", objClass + " alertMsgBox");
			}
			
		});
	}
	
		
	//fix rounded corners for image
	/*$(".rightcontent img.rounded").each(function(){
		$(this).before('<div class="SideBarContainer"><div class="SideBarBorderTop"></div><div class="SideBarBorderBottom"></div>');
		$(this).after('</div>');
	});
	*/
	
	
	//fix image box width
	/*$("table.boxGreyImage:visible").each(function(){
		var iBoxWidth = $(this).width();
		var iBoxLeftWidth = parseInt($(this).find(".boxTopLeft").css("width").replace("px", ""));
		var iBoxRightWidth = parseInt($(this).find(".boxTopRight").css("width").replace("px", ""));
		
		$objContent = $(this).find(".boxImage");
		var iContentColspan = $objContent.attr("colspan");
		if(!isNaN(iContentColspan) && iContentColspan >= 3) {
			var iBoxTopWidth = iBoxWidth - iBoxLeftWidth - iBoxRightWidth;
			$(this).find(".boxTop").width(iBoxTopWidth + 'px');
			//alert(iBoxWidth + "\nL:" + iBoxLeftWidth + "\nR:" + iBoxRightWidth + "\nTop width:" + iBoxTopWidth);
		}
	});
	*/
}

$(document).ready(function(){
	//style fix
	if(jQuery.browser.safari) {
		$("#content").attr("class", $("#content").attr("class") + " safari");
		$("#page").attr("class", $("#page").attr("class") + " safari");
		$("#startpage").attr("class", $("#startpage").attr("class") + " safari");
		//$("table.search .btn_default").parent("td").width("35px");
	}
	/*
	$.browser.addSelectors("#content");
	$.browser.addSelectors("#page");
	$.browser.addSelectors("#startpage");
	*/
	
	
	$(".faq a").click(function(){
		$(".faq .faq_answer").hide();
		$(this).next().show();
	});
	


	//add input button image end
	$("input.btn_default").after('<div class="btn_default_end"></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);
					}
				}
			}
		});
	}
	
});


var iNrOfColumnBlocks = 0;
var blockClassExt = "-block";
//var colName = "column";
var aColName = "column,column-dynamic".split(",");
function fixColumnClasses() {
	var msg = "";
	var iCnt = 0;
	for(var j=0; j < aColName.length; j++) {
		msg = "";
		iCnt = 0;
		
		var colName = aColName[j];
		var blockClass = colName + blockClassExt;
		
		//$("." + colName + ":visible").each(function(){
		$("." + colName).each(function(){
			iCnt++;
			$(this).addClass(blockClass + iNrOfColumnBlocks);
			if($(this).next().attr("class") != colName){
				iCnt = 0;
				iNrOfColumnBlocks++;
			}
		});
		
		if(iNrOfColumnBlocks > 0){
			
			for(var i=0; i < iNrOfColumnBlocks; i++) {
				var blockName = blockClass + i;
				var iNrOfColumns = $("." + blockName).size();
				if(iNrOfColumns > 0) {
					//$("." + blockName).removeClass(colName);
					$("." + blockName).addClass(colName + iNrOfColumns);
					$("." + blockName + ":first").addClass(colName + "-first");
					$("." + blockName + ":last").addClass(colName + "-last");
					$("." + blockName + ":last").after('<div class="clear"></div>');
				}
			
			}
			
			
			//$("." + colName + ":visible").each(function(){
			/*$("." + colName ).each(function(){
				msg += $(this).attr("class") + "\n";
			});
			
			alert(msg + "\n" + iNrOfColumnBlocks);
			*/
		}
	}
}


function fixColumns() {	//and box-border height if only one box in each column
	for(var j=0; j < aColName.length; j++) {
		//alert(aColName[j]);
		var colName = aColName[j];
		var blockClass = colName + blockClassExt;
		for(var i=0; i < iNrOfColumnBlocks; i++) {
			var blockName = blockClass + i;
			var iNrOfColumns = $("." + blockName).size();
			if(iNrOfColumns > 0) {
			
				//fix height
				var iMaxHeight = 0;
				var iMaxBoxHeight = 0;
				var bOnlySingleBoxes = true;
				$("." + blockName).each(function(){
					var iHeight = $(this).height();
					iMaxHeight = (iHeight > iMaxHeight ? iHeight : iMaxHeight);
					
					//box-border
					if($(this).find(".box-main").size() > 1) {
						bOnlySingleBoxes = false;
					}
					var iBoxHeight = $(this).find(".box-main").height();
					iMaxBoxHeight = (iBoxHeight > iMaxBoxHeight ? iBoxHeight : iMaxBoxHeight);
					
				});
				if(iMaxHeight > 0){
					$("." + blockName).height(iMaxHeight + "px");
				}
				
				if(iMaxBoxHeight > 0 && bOnlySingleBoxes){
					$("." + blockName + " .box-main").height(iMaxBoxHeight + "px");
				}
			}
		}
	}
}

//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 (jQuery.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;
		}
		//$(this).fixSubNav();
	}
	else
	{
		// For other good browsers.
		$('iframe').load(function()
			{
				this.style.height = (this.contentWindow.document.body.offsetHeight) + 'px';
				//$(this).fixSubNav();
			}
		);
	}
}




//tab handler (with corresponding tab containers)
$(document).ready(function(){
		
	var DEFAULT_TAB = 0;	//0 based
	
	$(".tabContainer").hide();	//hide all; done by css
	$(".tabContainer:eq("+DEFAULT_TAB+")").show();
	activateTab($(".tabmenu a:eq(" + DEFAULT_TAB + ")").attr("id"));
	
	$(".tabmenu a").click(function(){
		
		var tabId = $(this).attr("id");
		activateTab(tabId);
		var currentIndex = $(".tabmenu a").index(this);
		$(".tabContainer").hide();	//hide all

		var contentId = $(".tabContainer:eq("+ currentIndex +")").attr("id");
		$("#"+contentId).show();
		
		
		if($("iframe").size() > 0){
			//iframe exist
			
			if($("#"+contentId + " #f2kContentIframe").size() > 0){
				LoadIFrame($("#"+contentId + " #f2kContentIframe"), $("#"+contentId + " #f2kContentIframe").attr("url"));
			}
			else{
				LoadIFrame(null, ""); //empty all iframes
			}
		}
		
	});
	
	
	function activateTab(tabId){
		$(".tabmenu a").removeAttr("class");
		$("#" + tabId).attr("class", "active");
	}
	
	var strPrevLoadedUrl = "";
	function LoadIFrame(objIframe, url){
		
		var bLoadIframe = false;
		if(objIframe){
			if(url.lenght > 0){
				bLoadIframe = true;
			}
		}
		if(bLoadIframe) {
			$(objIframe).attr("src", url);
		}
		else {
			if(strPrevLoadedUrl != url){
				$(objIframe).attr("src", url);
			}
		}
		$(this).fixIfameHeight();
		strPrevLoadedUrl = url;
		
	}
});
//END: tab handler



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 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";
		fixAltTextPos(objName);
	}
}

//fix altTextInfo boxes position
function fixAltTextPos(objName){
	if((jQuery.browser.msie && jQuery.browser.version.major > 7) || jQuery.browser.firefox) {
		$obj = $("#" + objName);
		if($obj){
			var iObjWidth = $obj.width();
			var iObjMarginLeft = parseInt($obj.css("margin-left").replace("px", ""));
			//alert(objWidth + "::" + objMarginLeft)
			
			if(iObjWidth > 0) {
				var iNewMarginLeft = iObjMarginLeft + iObjWidth;
				if(iNewMarginLeft <= 0) {
					$obj.css("margin-left", iNewMarginLeft + "px");
				}
				//alert($obj.css("margin-left"))
			}
		}
		/* width is not set yet
		$("div.altTextInfo").each(function(){
			var objWidth = $(this).width();
			var objMarginLeft = parseInt($(this).css("margin-left").replace("px", ""));
			alert(objWidth + "::" + objMarginLeft)
			if(objWidth && objWidth > 0) {
				$(this).css("margin-left", objMarginLeft + objWidth + "px");
				alert($(this).css("margin-left"))
			}
		});*/
	}
}

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("theme-pic-holder")
	if (objThemePicHolder){
		objThemePicHolder.style.backgroundImage = "url(" + aThemes[RndPic] + ")";
	}
	prevThemePic = RndPic;
	
	if(iLoadNextPictureAfterSec > 0){
		LoadThemePic();
	}
}

function LoadThemePic() {
	setTimeout('ThemePic()', iLoadNextPictureAfterSec*1000);
}

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 unescapeXmlText(str){
	// ċ: unescape("%E5")
	// ä: unescape("%E4")
	// ö: unescape("%F6")
	// Ċ: unescape("%C5")
	// Ä: unescape("%C4")
	// Ö: unescape("%D6")
	
	str = str.replace(/&#229;/g, unescape("%E5"));
	str = str.replace(/&#228;/g, unescape("%E4"));
	str = str.replace(/&#246;/g, unescape("%F6"));
	str = str.replace(/&#197;/g, unescape("%C5"));
	str = str.replace(/&#196;/g, unescape("%C4"));
	str = str.replace(/&#214;/g, unescape("%D6"));
	return str;
}
