/**
 *  Janmedia Intractive Inc
 *  $Id: jmscript.js,v 1.26 2009-03-20 15:50:11 pkulik Exp $
 *  Janmedia Java Script
 *  Author: fwo
 */

// IMPORT FEATURE --------------------------------------------------------------


// do przeechowywania wew pol stanu
if (typeof(ImportScript) == "undefined")
{
	ImportScript = {
		bOpe: (navigator.appName == "Opera"),
		bMoz: (navigator.appName == "Netscape"),
		bIE:  (navigator.userAgent.toLowerCase().indexOf('msie')!=-1)
	}
}

/**
 *	@return <tt>true</tt> if all libs loaded
 */
function importScriptDone()
{
	if (!document.jmscriptLoad) return false;
	//var bIE = (navigator.userAgent.indexOf('msie')!=-1);
	var importedScripts = window.importedScripts;
	if (typeof(importedScripts)=='undefined') {return true; }
	var i;

	for (i=0; i<importedScripts.length; i++)
	{
		var e = document.getElementById('jmscript' + i);
		if (e==null) continue;
//		if (e==null) { log(importedScripts[i] + " " + importedScripts.length); return false; }
		if (typeof(e.readyState)=='undefined') continue;
		if (e.readyState!='loaded' && e.readyState!='complete') return false;
		if (e.funcionOnLoad) return false;
	}
	return true;
}

/**
 *	Waits for load all libs and execute <tt>fn</tt>
 */
function importScriptWait(fn)
{
	if (importScriptDone()) fn();
	else setTimeout(function() {importScriptWait(fn)} ,100);
}

/**
 *	Imports lig form url <tt>scriptName</tt>
 */
function importScript(scriptName,functionOnLoad)
{
	var importedScripts = window.importedScripts;
	if (!importedScripts)
	{
		importedScripts = new Array();
		window.importedScripts = importedScripts;
	}

	var i;
	for (i=0; i<importedScripts.length; i++)
	{
		if (importedScripts[i]==scriptName)
		{
			if (!functionOnLoad) return;
			var e = document.getElementById('jmscript' + i);
			if (typeof(e.readyState)=='undefined' || e.readyState!='loaded' || e.readyState!='complete')
			{
				functionOnLoad();
			}
			else
			{
				alert('NOT IMPLEMENTTED!');
			}
			return;
		}
	}

	importedScripts[importedScripts.length] = scriptName;

	// Opera 9.2
	// T#5069 - fwo, Dodalem sprawdzanie, czy mamy document.body
	if (document.jmscriptLoad && document.body )
	{
		var s = document.createElement('script');
		if 	(ImportScript.bMoz)
		{
			s.readyState='loading';
			if(functionOnLoad) s.funcionOnLoad = functionOnLoad;
			addEvent(s, 'load', function() {this.readyState='loaded'; if (this.funcionOnLoad) { this.funcionOnLoad(); this.funcionOnLoad=null}},false); // FF
		}
		else
		{
			if(functionOnLoad)
			{
				s.funcionOnLoad = functionOnLoad;
				addEvent(s, 'readystatechange', importScriptAfterIE,false); // IE
			}
		}

		s.setAttribute('id','jmscript' + (importedScripts.length-1));
		s.setAttribute('src',scriptName);
		s.setAttribute('language','Javascript1.2');
		s.setAttribute('type','text/javascript');
		// alert(s + " " + document.body + " " + document.jmscriptLoad);
		document.body.appendChild(s);
	}
	else
	{
		// IE
		if (functionOnLoad && !ImportScript.bMoz)
		{
			importScript_addFunction(scriptName,functionOnLoad);
			document.write('<scrip' + 't onreadystatechange="importScriptAfterIE2(this);" id="jmscript' + (importedScripts.length-1) + '" type="text/javascript" src="' + scriptName + '"></scrip' + 't>');
		}
		else
		{
			document.write('<scrip' + 't id="jmscript' + (importedScripts.length-1) + '" type="text/javascript" src="' + scriptName + '"></scrip' + 't>');
		}
		// document.write(s);

		if (functionOnLoad && ImportScript.bMoz)
		{
			window.importedScripts_timer = functionOnLoad;
			document.write('<scrip' + 't type="text/javascript">window.importedScripts_exec();</scrip' + 't>');
		}

	}
}

importScriptAfterIE = function(e) {importScriptAfterIE2(e.srcElement);}
importScriptAfterIE2 = function(o) {
	if (o.readyState!='loaded' && o.readyState!='complete') return;
	importScript_executeFunction(o.src);
	if (o.funcionOnLoad) { o.funcionOnLoad(); o.funcionOnLoad=null; }
}


function importScript_addFunction(scriptName,functionOnLoad)
{
	var fs = window.importedScriptFunctions;
	if (!fs)
	{
		fs = new Array();
		window.importedScriptFunctions = fs;
	}
	var a = fs[scriptName];
	if (!a)
	{
		a = new Array();
		fs[scriptName] = a;
	}
	a.push(functionOnLoad);
}
function importScript_executeFunction(scriptName)
{
	var fs = window.importedScriptFunctions;
	if (!fs) return;
	var a = fs[scriptName];
	if (!a) return;
	var ai;
	for (ai=0; ai<a.length; ai++)
	{
		var f = a[ai];
		if (f==null) continue;
		a[ai]=null;
		f();
	}
}

function importStyle(src)
{
	if(document.createStyleSheet)
	{
		setTimeout(function() { document.createStyleSheet(src); }, 100);
	}
	else
	{
		var s = document.createElement('link');
		s.setAttribute('href',src + "?" + Math.round());
		s.setAttribute('rel','stylesheet');
		s.setAttribute('type','text/css');
		document.getElementsByTagName('head')[0].appendChild(s);
	}
//		below does not work in Fx 3.0 It could be related to the doctype.
// 		CSS <link tag is a part of the HEAD sectionnot the body
//		document.write('<link href=\'' + src + '\' rel=\'stylesheet\' type=\'text/css\'/>');
}

function addEvent(obj, evt, func, capture)
{
	if(obj.addEventListener)
		obj.addEventListener(evt, func, capture);
	else if(obj.attachEvent)
		obj.attachEvent('on' + evt, func);
	else
		alert('This site is optimized for the latest browsers. The older browser you are using may cause parts of the site to display incorrectly. We apologize for any inconvenience.');
}

function jmscript_load()
{
	document.jmscriptLoad = true; // set we are loaded
	if (!ImportScript.bOpe)
		document.write = null; // specjalnie, 2007-12-20, fwo, ale nie dla Opera 9.2 -> T#5069
}

addEvent(window, 'load', jmscript_load, true);
/*
problem w IIR+IE - szczegoly zamowien
var JMScript = {
	onContent:function(f){//(C)webreflection.blogspot.com http://webreflection.blogspot.com/2006/11/my-domcontentloaded-final-solution.html
var a,b=navigator.userAgent,d=document,w=window,
c="__onContent__",e="addEventListener",o="opera",r="readyState",
s="<scr".concat("ipt defer src='//:' on",r,"change='if(this.",r,"==\"complete\"){this.parentNode.removeChild(this);",c,"()}'></scr","ipt>");
w[c]=(function(o){return function(){w[c]=function(){};for(a=arguments.callee;!a.done;a.done=1)f(o?o():o)}})(w[c]);
if(d[e])d[e]("DOMContentLoaded",w[c],false);
if(/WebKit|Khtml/i.test(b)||(w[o]&&parseInt(w[o].version())<9))
(function(){/loaded|complete/.test(d[r])?w[c]():setTimeout(arguments.callee,1)})();
else if(/MSIE/i.test(b))d.write(s);
}
};
JMScript.onContent(jmscript_load);
*/