/*
________________________________________
¤¤¤ 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() {
	fixStartPageHeights();
	fixLogin();
}

function fixStartPageHeights(){
	if($("#startpage").size() > 0){
		var iMaxHeight = 0;
		var iTmpHeight = $("#mainnews").height();
		if(iTmpHeight && iTmpHeight > iMaxHeight){
			iMaxHeight = iTmpHeight;
		}
		iTmpHeight = $("#mainpuff").height();
		if(iTmpHeight && iTmpHeight > iMaxHeight){
			iMaxHeight = iTmpHeight;
		}
		iTmpHeight = $("#mainlogin").height();
		if(iTmpHeight && iTmpHeight > iMaxHeight){
			iMaxHeight = iTmpHeight;
		}
		
		if(iMaxHeight > 0){
			$("#mainnews").height(iMaxHeight + "px");
			$("#mainpuff").height(iMaxHeight + "px");
			$("#mainlogin").height(iMaxHeight + "px");
		}
	}
}

function fixLogin(){
	if($("#loginDefault").size() > 0 && $("#startpage").size() > 0) {
		$("#loginDefault").remove();
	}
	if($("#loginStartPage").size() > 0 && $("#startpage").size() < 1){
		$("#loginStartPage").remove();
	}
}


$(document).ready(function(){
	loadImagePlayer();

});

var images = null;
function loadImagePlayer() {
	if(images && images.length > 0 && $("#picture").size() > 0) {
		$("#picture").f2ImagePlayer({imageList:images});
	}
}


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 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() {
	if(prevThemePic == -1){
		RndPic = Math.floor(Math.random()*aThemes.length);
	}
	else{
		RndPic = prevThemePic + 1;
		if(RndPic >= aThemes.length) { RndPic = 0;}
	}
	var $themPicHolder = $(".themePicHolder");
	if(iFadeInOutSec > 0 && prevThemePic > -1 && aThemes.length > 1){
		$themPicHolder.fadeOut(iFadeInOutSec*1000, function(){
				$themPicHolder.css("background-image", "url(" + aThemes[RndPic] + ")");
				$themPicHolder.fadeIn(iFadeInOutSec*1000);
		});
	}
	else {
		$themPicHolder.css("background-image", "url(" + aThemes[RndPic] + ")");
	}


	prevThemePic = RndPic;

	if(iLoadNextPictureAfterSec > 0){
		LoadThemePic();
	}
}

function LoadThemePic() {
	setTimeout('ThemePic()', iLoadNextPictureAfterSec*1000);
}