var evt;
document.onmousemove=function(e){
 evt=window.event || e;
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}


window.onload = function(){
	if (!document.getElementsByClassName)
	{
		document.getElementsByClassName = function(classname)
		{
			var elArray = [];
			var tmp = document.getElementsByTagName("*");
			var regex = new RegExp("(^|\\s)" + classname + "(\\s|$)");
			for ( var i = 0; i < tmp.length; i++ ) {
				if ( regex.test(tmp[i].className) ) {
					elArray.push(tmp[i]);
				}
			}
			return elArray;
		};
	}
};


function test_page_load(){
	if(!page_loaded){setTimeout("test_page_load();", 200); return false;}
	return true;
}

function checkCookie(){
	var time=new Date();
	time.setTime(time.getTime()+(60*1000));
	setcookie("testCookie","bla bla",time);
	contents=getcookie("testCookie");
	if(contents=="bla bla"){
		return true;
	}else{
		return false;
	}
}
function setcookie(name,val,exp){
	document.cookie=name+"="+escape(val)+"; expires="+exp.toGMTString()+"; path=/";
}

function getcookie(val){
	var rgx=new RegExp(val+"=([^;]+)");
	var cookie=rgx.exec(document.cookie);
	return(cookie!=null)?unescape(cookie[1]):null;
}

noweOkienko=null;
function galeria (src, w, h) {
	if (window.screen){
		aw=screen.availWidth;
		ah=screen.availHeight;
	}else{
		aw=640;
		ah=450;
	}
	if (noweOkienko!=null && !noweOkienko.closed){
		noweOkienko.close();
	}
	if (noweOkienko==null || noweOkienko.closed){
		ustawienia="left=" + (aw-w)/2 + "," + "top=" + (ah-h)/2 + "," + "screenX=" + (aw-w)/2 + "," + "screenY=" + (ah-h)/2 + "," + "width=" + w + "," + "height=" + h + "," + "innerWidth=" + w + "," + "innerHeight=" + h + "," + "toolbar=no," + "location=no," + "directories=no," + "status=no," + "menubar=no," + "scrollbars=no," + "resizable=no"
		noweOkienko = window.open ('','obrazek',ustawienia);
	}
	noweOkienko.document.open();
	noweOkienko.document.clear();
	noweOkienko.document.write("<html><head>\n"+"<style><!--\n"+"body {background-repeat:no-repeat}\n"+"--></style>\n"+"</head>\n"+"<body bgcolor=\"#FFFFFF\" topmargin=\"0\" leftmargin=\"0\" bottommargin=\"0\" rightmargin=\"0\" marginwidth=\"0\" marginheight=\"0\"><img src=\""+src+"\" width="+w+" height="+h+"></body>\n"+"</html>");
	noweOkienko.document.close();
	noweOkienko.focus();
}


function okienko (src, w, h) {
	if (window.screen){
		aw=screen.availWidth;
		ah=screen.availHeight;
	}else{
		aw=640;
		ah=450;
	}
	if (noweOkienko!=null && !noweOkienko.closed){
		noweOkienko.close();
	}
	if (noweOkienko==null || noweOkienko.closed){
		ustawienia="left=" + (aw-w)/2 + "," + "top=" + (ah-h)/2 + "," + "screenX=" + (aw-w)/2 + "," + "screenY=" + (ah-h)/2 + "," + "width=" + w + "," + "height=" + h + "," + "innerWidth=" + w + "," + "innerHeight=" + h + "," + "toolbar=no," + "location=no," + "directories=no," + "status=no," + "menubar=no," + "scrollbars=yes," + "resizable=no"
		noweOkienko = window.open (src,'',ustawienia);
	}
	noweOkienko.focus();
}



function okienko_r (src, w, h) {
	if (window.screen){
		aw=screen.availWidth;
		ah=screen.availHeight;
	}else{
		aw=640;
		ah=450;
	}
	if (noweOkienko!=null && !noweOkienko.closed){
		noweOkienko.close();
	}
	if (noweOkienko==null || noweOkienko.closed){
		ustawienia="left=" + (aw-w)/2 + "," + "top=" + (ah-h)/2 + "," + "screenX=" + (aw-w)/2 + "," + "screenY=" + (ah-h)/2 + "," + "width=" + w + "," + "height=" + h + "," + "innerWidth=" + w + "," + "innerHeight=" + h + "," + "toolbar=no," + "location=no," + "directories=no," + "status=no," + "menubar=no," + "scrollbars=yes," + "resizable=yes"
		noweOkienko = window.open (src,'',ustawienia);
	}
	noweOkienko.focus();
}


function showHide(text, dispProp){
	if($(text).style.display!=dispProp ){
		$(text).style.display=dispProp;
	}else{
		$(text).style.display='none';
	}
}


function clearDiv(name, timeout) {
	setTimeout("$('"+name+"').innerHTML = '';$('"+name+"').style.display='none';", timeout);
}

function doDelay(fun, timeout) {
	setTimeout(fun, timeout);
}


function formatTime(seconds){
	var hh='00';
	var mm='00';
	var ss='00';
	if(seconds==null){
		return '0';
	}
	if(seconds>=3600){
		var t = Math.floor(seconds/3600);
		if(t<10) hh = '0'+t;
		else hh = t;
		seconds = seconds - (t*3600);
	}else hh='';
	if(seconds>=60){
		var t2 = Math.floor(seconds/60);
		if(t2<10) mm = '0'+t2;
		else mm = t2;
		seconds = seconds - (t2*60);
	}
	if(seconds<10) ss = '0'+seconds;
	else ss = seconds;
	if(hh!='') return hh+':'+mm+':'+ss;
	else return mm+':'+ss;
}

function copyToClipboard(s){
	if( window.clipboardData && clipboardData.setData ){
		clipboardData.setData("Text", s);
	}else{
		// You have to sign the code to enable this or allow the action in about:config by changing
		user_pref("signed.applets.codebase_principal_support", true);
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
		var clip = Components.classes['@mozilla.org/widget/clipboard;[[[[1]]]]'].createInstance(Components.interfaces.nsIClipboard);
		if (!clip) return;
		// create a transferable
		var trans = Components.classes['@mozilla.org/widget/transferable;[[[[1]]]]'].createInstance(Components.interfaces.nsITransferable);
		if (!trans) return;
		// specify the data we wish to handle. Plaintext in this case.
		trans.addDataFlavor('text/unicode');
		// To get the data from the transferable we need two new objects
		var str = new Object();
		var len = new Object();
		var str = Components.classes["@mozilla.org/supports-string;[[[[1]]]]"].createInstance(Components.interfaces.nsISupportsString);
		var copytext=meintext;
		str.data=copytext;
		trans.setTransferData("text/unicode",str,copytext.length*[[[[2]]]]);
		var clipid=Components.interfaces.nsIClipboard;
		if (!clip) return false;
		clip.setData(trans,null,clipid.kGlobalClipboard);	   
	}
}



var show_div_timer=null, ns4=document.layers, ns6=document.getElementById&&!document.all, ie4=document.all, mm=document.onmousemove, img1;

//żeby layer był wyśrodkowany to e=null
//sw==1 zamyka diva i tworzy nowego
function show_div(type, w, h, title, shift_top, shift_left, e, sw, div_id) {
	if(div_id==null || typeof(div_id)=='undefined'){
		div_id = 'show_div';
	}
	var x=0, y=0, o_div=document.getElementById(div_id), t1='', div_class='', winW=0, winH=0, dt=new Date();
	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") { winW = window.innerWidth-16; winH = window.innerHeight-16; }
		if (navigator.appName.indexOf("Microsoft")!=-1) { winW = document.body.offsetWidth-20; winH = document.body.offsetHeight-20;}
	}else{ 
		winW = screen.availWidth;
		winH = screen.availHeight;
	}

	if (show_div_timer!=null) clearTimeout(show_div_timer);
	if (e!=null) {
		if (e.clientX) {
			if ((ns4) || (ns6)) {
				x=e.pageX;
				y=e.pageY;
			} else {
				x=e.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
				y=e.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
			}
		} else if (e.x) {
			x=e.x;
			y=e.y;
		} else {
			if (parseInt(navigator.appVersion)>3) {
				x=Math.round((winW-w)/2);
				y=Math.round((winH-h)/2);
			}else{ 
				x=Math.round((screen.availWidth-w)/2);
				y=Math.round((screen.availHeight-h)/2);
			}
		}
	} else {
		if (parseInt(navigator.appVersion)>3) {
			x=Math.round((winW-w)/2);
			y=Math.round((winH-h)/2);
		}else{ 
			x=Math.round((screen.availWidth-w)/2);
			y=Math.round((screen.availHeight-h)/2);
		}
	}
	if(x<0) x=0;
	if(y<0) y=0;
	if ((!o_div) || (sw==1)) {
		var uniqZ = dt.getTime();
		close_div(div_id);

		b_div=document.createElement('div');
		b_div.setAttribute('id', div_id+'_bcg');
		b_div.className = 'show_div_bcg';
		//b_div.style.zIndex = uniqZ;
		//b_div.style.height = (winH + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop))+'px';
		document.body.appendChild(b_div);

		o_div=document.createElement('div');
		o_div.setAttribute('id', div_id);
		o_div.className = 'show_div';
		//o_div.style.zIndex = uniqZ+1;
		o_div.style.top=eval(y+shift_top)+'px';
		o_div.style.left=eval(x+shift_left)+'px';
		document.body.appendChild(o_div);
	}
	if (w>0) o_div.style.width=w+'px';
	if (h>0) o_div.style.height=h+'px'; else o_div.style.minHeight='75px';
	o_div.innerHTML='<div id="'+div_id+'_top" class="show_div_top">'+
		'<div style="float: left;">'+title+'</div>'+
		'<div style="float: right;"><a href="#" id="'+div_id+'_a" class="show_div_a" onclick="close_div(\''+div_id+'\'); return false;"><img src="/images/icon_close.gif" style="margin-top: 1px;" alt="" /></a></div></div>'+
		'<div id="'+div_id+'_main" class="show_div_main" style="height: '+(h-40)+'px;">'+
		'<img src="/i/loader1.gif" id="img1" style="margin-top: '+eval(Math.round((Math.max(h, 75)-18)/2)-35)+'px;" />'+
		'</div>';
	if (type=='div') {
		document.getElementById(div_id+'_main').onclick=function () { };
		//http_post('http://'+base_host+uri, params, 'show_div_result');
	} else if (type=='no_top_div') {
		document.getElementById(div_id+'_main').onclick=function () { };
		document.getElementById(div_id+'_a').style.display = 'none';
		//http_post('http://'+base_host+uri, params, 'show_div_result');
	} else {
		img1=new Image();
		img1.src='/'+uri;
	}
	tr_div(0, type, div_id);
}

function hide_close(div_id){
	if(document.getElementById(div_id+'_a')) document.getElementById(div_id+'_a').style.display = 'none';
}

function show_close(div_id){
	if(document.getElementById(div_id+'_a')) document.getElementById(div_id+'_a').style.display = '';
}

function tr_div(n, type, div_id) {
	var max1=10, v=n/max1, o_div=document.getElementById(div_id);
	if (!o_div) return;
	o_div.style.opacity=v;
	o_div.style.MozOpacity=v;
	o_div.style.filter='alpha(opacity: '+eval(v*100)+')';
	if (n<max1) {
		setTimeout('tr_div('+eval(n+1)+', \''+type+'\', \''+div_id+'\')', 25);
	} else if (type=='img') {
		if (img1.complete) {
			var i=document.getElementById('img1');
			i.src=img1.src;
			i.style.marginTop='';
		} else {
			setTimeout('tr_div('+eval(n)+', \''+type+'\', \''+div_id+'\')', 25);
		}
	}
}

function close_div(div_id) {
	if(div_id==null || typeof(div_id)=='undefined'){
		div_id = 'show_div';
	}
	var o_div=document.getElementById(div_id);
	if (o_div) o_div.parentNode.removeChild(o_div);
	var b_div=document.getElementById(div_id+'_bcg');
	if (b_div) b_div.parentNode.removeChild(b_div);
}

function show_div_result(txt, div_id) {
	if(div_id==null || typeof(div_id)=='undefined'){
		div_id = 'show_div';
	}
	var o_div_main=document.getElementById(div_id+'_main');
	o_div_main.style.textAlign='';
	o_div_main.innerHTML=txt;
}

function append_div_result(obj, div_id) {
	if(div_id==null || typeof(div_id)=='undefined'){
		div_id = 'show_div';
	}
	var o_div_main=document.getElementById(div_id+'_main');
	o_div_main.style.textAlign='';
	o_div_main.appendChild(obj);
}

function make_login(txt){
	var from_char = new Array('ę','ó','±','¶','ł','ż','Ľ','ć','ń'), to_char = new Array('e','o','a','s','l','z','z','c','n'), t=new Array();
	txt = txt.substr(0,32).toLowerCase();
	for(var i=0; i<txt.length; i++){
		t[i] = txt[i];
		for(var j=0; j<from_char.length; j++){
			if(from_char[j]==txt[i]){ t[i]=to_char[j]; break; }
		}
	}
	txt = t.join('');
	return txt.replace(/[^a-z0-9_.@-]/ig, '_');
}

function date_from_unix(sec, hours){
	var nd = new Date(sec*1000);
	var year = nd.getFullYear();
	var month = nd.getMonth()+1;
	if(month<10) month = '0'+month;
	var day = nd.getDate();
	if(day<10) day = '0'+day;
	var hr = nd.getHours();
	if(hr<10) hr = '0'+hr;
	var min = nd.getMinutes();
	if(min<10) min = '0'+min;

	if(hours) return day+'.'+month+'.'+year+' '+hr+':'+min;
	else return day+'.'+month+'.'+year;
}


function test_int(obj){
	obj.style.background="";
	if(obj.value=='') obj.value=0;
	var txt = obj.value;
	if( txt.match(/\D/g) ){
		obj.value = txt.replace(/\D/g, "");
		obj.style.background="#ff0000";
		//setTimeout('$(\''+obj.id+'\').style.background=\'\';',2000);
	}
}


function rzCC(s){
	for(var exp=/-([a-z])/; 
		exp.test(s); 
		s=s.replace(exp,RegExp.$1.toUpperCase())
	);
	return s;
}

function _setStyle(element, declaration) {
   if (declaration.charAt(declaration.length-1)==';')
     declaration = declaration.slice(0, -1);
   var k, v;
   var splitted = declaration.split(';');
   for (var i=0, len=splitted.length; i<len; i++) {
      k = rzCC(splitted[i].split(':')[0]);
      v = splitted[i].split(':')[1];
      eval("element.style."+k+"='"+v+"';");
   }
}

function addEvent(obj, type, handler) {
	if(!obj.events) obj.events = [];
	
	if(!obj.events["on"+type]) {
		obj.events["on"+type] = [];
		if(obj["on"+type]) obj.events["on"+type].push(obj["on"+type]);
	}
	
	obj.events["on"+type].push(handler);

	obj["on"+type] = function(event) {
		event = event || window.event;
		
		var returnValue = true;
		var eventHandlers = this.events["on"+event.type];
		
		if(eventHandlers) {
			for(var i = 0; i < eventHandlers.length; ++i) {
				this.$eventHandler = eventHandlers[i];
				returnValue = this.$eventHandler.call(this, event);
			}
		}
		
		return returnValue;
	}

}


