/**
 * SiteJay2 
 * @link http://www.sitejay.com
 * @author Jozef Sokol <jozef.sokol@sitejay.com>
 * @copyright Copyright (c) 2005 Jozef Sokol Absolutio
 *
 * @name SiteJay.common.js
 * @package SiteJay
 */

var SiteJay=new Object();

/**
 * BrowserInfo
 */ 
var sAgent = navigator.userAgent.toLowerCase();
SiteJay.IsIE=sAgent.indexOf("msie") != -1;
SiteJay.IsGecko=!SiteJay.IsIE;
SiteJay.IsNetscape=sAgent.indexOf("netscape") != -1;
try
{
	SiteJay.ScreenWidth=screen.width;
	SiteJay.ScreenHeight=screen.height;
}
catch (e) 
{
	SiteJay.ScreenWidth=800;
	SiteJay.ScreenHeight=600;
}

/**
* Object SiteJay.ScriptLoader
*/ 
SiteJay.ScriptLoader = new Object() ;
SiteJay.ScriptLoader.IsLoading = false ;
SiteJay.ScriptLoader.Queue = new Array() ;

SiteJay.ScriptLoader.AddScript = function( scriptPath )
{
	SiteJay.ScriptLoader.Queue[ SiteJay.ScriptLoader.Queue.length ] = scriptPath ;
	
	if ( !this.IsLoading )
		this.CheckQueue() ;
}

SiteJay.ScriptLoader.CheckQueue = function() 
{
	if (this.Queue.length>0)
	{
		this.IsLoading=true;
		var sScriptPath=this.Queue[0];
		var oTempArray=new Array();
		for (i=1;i<this.Queue.length;i++)
			oTempArray[i-1]=this.Queue[i];
		this.Queue = oTempArray;
		var e ;
		if (sScriptPath.lastIndexOf('.css')>0)
		{
			e=document.createElement('LINK') ;
			e.rel='stylesheet';
			e.type='text/css';
		}
		else
		{
			e=document.createElement("script");
			e.type="text/javascript";
		}
		document.getElementsByTagName("head")[0].appendChild(e); 
		// Start downloading it.
		if (e.tagName=='LINK')
		{
			// IE must wait for the file to be downloaded.
			if (SiteJay.IsIE)
				e.onload=SiteJay.ScriptLoader_OnLoad ;
			// Gecko doens't fire any event when the CSS is loaded, so we can't wait for it.
			else
				SiteJay.ScriptLoader.CheckQueue();
				
			e.href=sScriptPath ;
		}
		else
		{
			// Gecko fires the "onload" event and IE fires "onreadystatechange"
			e.onload = e.onreadystatechange = SiteJay.ScriptLoader_OnLoad;
			e.src = sScriptPath ;
		}
	}
	else
	{
		this.IsLoading=false ;
	}
}
SiteJay.ScriptLoader_OnLoad=function()
{
	// Gecko doesn't have a "readyState" property
	if (this.tagName=='LINK' || !this.readyState || this.readyState=='loaded')
		// Load the next script available in the queue
		SiteJay.ScriptLoader.CheckQueue() ;
}

function sjGetUrlParam(paramName)
{
	var oRegex=new RegExp('[\?&]'+paramName+'=([^&]+)', 'i');
	var oMatch=oRegex.exec(window.top.location.search);
	if (oMatch && oMatch.length>1)
		return oMatch[1];
	else
		return '';
}

function sjEscapeHTML(text) {
	text=text.replace('\n','');
	text=text.replace('&','&amp;');
	text=text.replace('<','&lt;');
	text=text.replace('>','&gt;');
	return text;
}

function sjGetE(elementId)
{
	return document.getElementById(elementId);
}

function sjShowE(element,isVisible)
{
	if (typeof(element)=='string')
		element=sjGetE(element);
	element.style.display=isVisible?'':'none';
}

function sjShowHideE(element)
{
	if (typeof(element)=='string')
		element=sjGetE(element);
	if (element.style.display.length==0)
		element.style.display='none';
	else
		element.style.display='';
}

function sjRemoveChilds(element)
{
	if (!element)
		return;
	if (element.hasChildNodes())
	{
		for ( var i = 0 ; i < element.childNodes.length ; i++ )
		{
			sjRemoveChilds(element.childNodes[i]);
			element.removeChild(element.childNodes[i]);
		}
		// jebe mu to alebo mne ... nie vzdy odstrani vsetko
		if (element.hasChildNodes())
			sjRemoveChilds(element);
	}
}

function sjOpenWindow(url,width,height,name,scrollbars)
{
	var iLeft=(SiteJay.ScreenWidth-width)/2;
	var iTop=(SiteJay.ScreenHeight-height)/2;

	var sOptions="toolbar=no,status=no,resizable=yes,dependent=yes";
	sOptions+=scrollbars?",scrollbars=yes":",scrollbars=no";
	sOptions+=",width="+width;
	sOptions+=",height="+height;
	sOptions+=",left="+iLeft;
	sOptions+=",top="+iTop;
		
	if (name)
		name="sjWindow";
	if (SiteJay.IsIE)
	{
		var oWindow=window.open(url,name,sOptions);
		if (oWindow)
			oWindow.opener=window;
		else
			alert("ups") ;
    }
    else
		window.open(url,name,sOptions);
}

/**
 * Events
 */
function sjAddLoadEvent(func) 
{
	if (typeof window.addEventListener != "undefined")
	{
		window.addEventListener("load",func,false);
	}
	else if (typeof window.attachEvent != "undefined") 
	{
		window.attachEvent("onload",func);
	}
	else 
	{
		if (window.onload != null) {
			var oldOnload = window.onload;
			window.onload = function ( e ) {
				oldOnload( e );
				func();
			};
		}
		else 
			window.onload = func;
	}
}

function before_unload(evt) {
    try {return confirmleaving();}
    catch (e) {}
}
window.onbeforeunload=before_unload


/**
 * Popup menu
 */
sjMenuPop = function() {
	var menus=document.getElementsByTagName("UL");
	for (var m=0; m<menus.length; m++) 
	{
		if (menus[m].className=="sjMenu")
		{
			var sfEls = menus[m].getElementsByTagName("LI");
			for (var i=0; i<sfEls.length; i++) 
			{
				sfEls[i].onmouseover=function(){this.className+=" sfhover";}
				sfEls[i].onmouseout=function(){this.className=this.className.replace(new RegExp(" sfhover\\b"), "");}
			}
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sjMenuPop);

/**
 * Common functions
 */
function sjPopupImage(sURL)
{
	sjOpenWindow("/image.html?"+sURL,200,200,"sjImagePopup");
	return false;
}

function sjMnuOC(btn,objid)
{
	var obj=document.getElementById(objid);
	if (!obj)
		return false;
	if (obj.style.display=='block')
	{
		obj.style.display='none';
		sjRemoveCookie(objid);
		if (btn.tagName!="A")
			btn.className="pmp";
	}
	else
	{
		obj.style.display='block';
		sjSetCookie(objid,"1");
		if (btn.tagName!="A")
			btn.className="pmm";
	}
}

function sjSetCookie ( sName, sValue, nDays ) 
{
	var expires = "";
	if (nDays) 
	{
		var d = new Date();
		d.setTime( d.getTime() + nDays * 24 * 60 * 60 * 1000 );
		expires = "; expires=" + d.toGMTString();
	}

	document.cookie = sName + "=" + sValue + expires + "; path=/";
}

function sjRemoveCookie(name)
{
	sjSetCookie( name, "", -1 );
}
