// ----- iBrowser

var iBrowser={Family:false};
if (typeof(window.opera)!='undefined') iBrowser.Family='opera';
else if (navigator.plugins.length>0) iBrowser.Family='mozilla';
else if (navigator.appName=='Microsoft Internet Explorer') iBrowser.Family='msie';

if(document.getElementsByTagName){
	var elems=document.getElementsByTagName('base');
	if(elems.length){document.base=elems[0].href;}
}

function iDocumentReload() {
	var form=document.forms[0];
	if(!form)return;
	document.forms[0]['Step'].value='Update';
	form.submit();
}

function iLeft(text,separator) {
	var p=text.indexOf(separator);
	if (p>=0)return(text.substr(0,p));
	else return(text);
}

function iRight(text,separator) {
	var p=text.indexOf(separator);
	if (p>=0)return(text.substr(p+separator.length));
	else return('');
}

function iLeftBack(text,separator) {
	var p=text.lastIndexOf(separator);
	if (p>=0)return(text.substr(0,p));
	else return('');
}

function iRightBack(text,separator) {
	var p=text.lastIndexOf(separator);
	if (p>=0)return(text.substr(p+separator.length));
	else return(text);
}

function iDump(v) {
	var t='';
	for(e in v) {
		x=v[e]
		if (typeof(x)=='function'){}//t+=e+'=[function]; ';
		else if (typeof(x)=='object')t+=e+"=[object];\n";
		else if (typeof(x)=='string')t+=e+"="+x+";\n";
		else t+=e+'='+x+";\n";
	}
	alert(t);
}

function iPosition(obj) {
	var curleft=curtop=0;
	if(obj.offsetParent)
	{
		curleft=obj.offsetLeft;
		curtop=obj.offsetTop;
		while(obj=obj.offsetParent)
		{
			curleft+=obj.offsetLeft;
			curtop+=obj.offsetTop;
		}
	}
	return({X:curleft,Y:curtop});
}

function iCookieSet(name1, value1, expire1) {
  document.cookie = name1 + "=" + escape(value1) + (!expire1? "" : ("; expires=" + expire1.toGMTString()))+"; path=/";
}

function iCookieGet(name1) {
	var k1,j1,text1=name1+"=";
	if (document.cookie.length > 0){
		j1 = document.cookie.indexOf(text1);
		if (j1 != -1){
			j1 += text1.length;
			k1 = document.cookie.indexOf(";", j1);
			if (k1 == -1) k1 = document.cookie.length;
			return unescape(document.cookie.substring(j1, k1))
		}
	}
}

// --- iFlash ---

function iFlashHtml(src,width,height,id,bgcolor,flashvars) {
	var html='';
	if (id) {
		html+='<SCRIPT LANGUAGE=VBScript\> \non error resume next \nSub '+id+'_FSCommand(ByVal command, ByVal args)\n  call '+id+'_DoFSCommand(command, args)\nend sub\n</SCRIPT\> \n';
	}
	html+='<object type="application/x-shockwave-flash" width="'+width+'px" height="'+height+'px"';
	if (id){html+=' id="'+id+'" name="'+id+'"';}
	html+=' data="'+src+'"><param name=movie value="'+src+'"><param name=flashvars value="'+flashvars+'"><param name=quality value=high><param name=menu value=false>';
	if (bgcolor=='transparent') html+='<param name=wmode value=transparent>';
	else if (bgcolor) html+='<param name=bgcolor value='+bgcolor+'>';
	html+='</object>';
	return(html);
}

function iFlashGenerate(src,width,height,id,bgcolor,flashvars,install) {
	var html='';
	if ((!install)||(iFlashVersion()>=6)) {
		html+=iFlashHtml(src,width,height,id,bgcolor,flashvars);
	} else if (install) {
		html+='<p>Do prawidłowego działania serwisu wymagany jest plugin Macromedia Flash 6 lub nowszy.</p><p>';
		html+=iFlashHtml('layout/flash.swf',70,70,'href=index.php');
		html+='</p><p>Nie widzisz animacji? Zainstaluj plugin:</p><p><a href="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" target=_blank><img src="layout/flash.gif" width=75 height=75></a></p>';
	}
	document.write(html);
}

function iFlashVersion() {
	var i,v=0;
	if (navigator.plugins.length) {
		var p=navigator.plugins;
		if ((p)&&(p.length>0)&&(p['Shockwave Flash']))	{
			var w=p['Shockwave Flash'].description.split(' ');
				for(i=0;i<w.length;i++) {
					var j=parseInt(w[i]);
					if (!isNaN(j))
						v=Math.max(v,j);
				}
		}
		return(v);
	} else {
		for(v=10;v>0;v--) {
			var x=null;
			try {
				x=new ActiveXObject("ShockwaveFlash.ShockwaveFlash."+v);
			} catch(e) {}
			if (x) return(v);
		}
		return(0);
	}
}

function iBannerGenerate(banners)
{
	var r=Math.floor(Math.random()*(banners.length-1));
	banner_width=iLeft(banners[r]['banner_size'],'x');
	banner_height=iRight(banners[r]['banner_size'],'x');
	if(iRight(banners[r]['banner_source'],'.')=='swf') {
		iFlashGenerate(banners[r]['banner_source'],banner_width,banner_height,false,'transparent','click='+escape(banners[r]['banner_target'])+'&clicktag='+escape(banners[r]['banner_target'])+'&gdaclick='+escape(banners[r]['banner_target']),false);
	} else {
		document.write('<a href="'+banners[r]['banner_target']+'"><img src="'+banners[r]['banner_source']+'" alt="" style="width:'+banner_width+'px;height:'+banner_height+'px;"></a>');
	}
}

function iElementSwitch(element)
{
	(t=document.getElementById(element)).style.display=='block'?t.style.display='none':t.style.display='block';
	if(element=='vt_menu') iTypesSetPosition();
}

// ------ WINDOW

var IE = document.all?true:false
if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove=getMouseXY;

function getMouseXY(e)
{
	if (IE)
	{
		mouseX=event.clientX+document.body.scrollLeft;
		mouseY=event.clientY+document.body.scrollTop;
	}
	else
	{
		mouseX=e.pageX;
		mouseY=e.pageY;
	}

	if(mouseX<0){mouseX=0};
	if(mouseY<0){mouseY=0};

	return true;
}

function iDec2Hex(Dec)
{
	var hexChars = "0123456789ABCDEF";
	Dec=Math.round(Dec);
	var a=Dec%16;
	var b=(Dec-a)/16;
	hex=""+hexChars.charAt(b)+hexChars.charAt(a);
	return hex;
}

function iPageOnLoad() {
}

function iCurrency(value,symbol) {
	if(isNaN(value))value=0;
	var value_integer=Math.floor(value);
	value_float=value-value_integer;
	value_float*=10000000000;
	value_float=value_float.toString();
	while(value_float.length<10)
		value_float='0'+value_float;
	symbol=symbol.replace(/_/,value_integer);
	var mask='##########';
	for(d=1;d<=10;d++) {
		symbol=symbol.replace(new RegExp(mask,'ig'),value_float);
		mask=mask.substr(1);
		value_float=value_float.substr(1);
	}
	return(symbol);
}

function iForumPostNew(page,parent,newPost){
  //Run WithOUT Prototype
  if(newPost){
    $('dpm').innerHTML = '<form id="new_post"><input type="hidden" name="Forum.Page" value="'+page+'"><input type="hidden" name="Forum.Step" value="Save"><input type="text" name="Forum.Name" value=""><input type="textarea" name="Forum.Body" value=""><input type="text" name="Forum.Author" value="">';
  }else{
    $('dpm').innerHTML = '<form id="new_post"><input type="hidden" name="Forum.Page" value="'+page+'"><input type="hidden" name="Forum.Step" value="Save"><input type="hidden" name="Forum.Hierarchy" value="'+parent+'"><input type="textarea" name="Forum.Body" value=""><input type="text" name="Forum.Author" value="">';  }
  $('dpm').innerHTML += '<button onmousedown="ForumPostNewSave()">Zapisz</button></form>';
  $('dpm').style.display="block";
}

function ForumPostNewSave(){
	var post = '';
	if (g('new_post')){
		var f = g('new_post');
		var i = 0;
		for(i=0;i<f.elements.length;i++){
			post += '&'+f.elements[i].name+'='+encodeURI(f.elements[i].value);
		}
	}
	makePostRequest(document.SiteURL+'?Page=ForumNew',post);
}

function g(o){return document.getElementById(o)}
function remove(id){
	var el = document.getElementById(id);
	el.parentNode.removeChild(el);
}

function ForumPostNew(page,parent,newPost)
{
	e=document.createElement('div');
	e.setAttribute('id','PostNew');
	(es=e.style).top='0';
	es.width=es.height='100%';
	es.position='absolute';
	es.textAlign='center';

	if(newPost){
		content='<h2>Nowa wiadomość</h2><form id="new_post"><input type="hidden" name="Forum.Page" value="'+page+'"><input type="hidden" name="Forum.Step" value="Save">Temat:<br><input type="text" name="Forum.Name" value=""><br>Treść</br><textarea rows=5 cols=20 type="textarea" name="Forum.Body" value=""></textarea><br>Podpis</br><input type="text" name="Forum.Author" value="">';
	}else{
		content='<h2>Odpowiedz</h2><form id="new_post"><input type="hidden" name="Forum.Page" value="'+page+'"><input type="hidden" name="Forum.Step" value="Save"><input type="hidden" name="Forum.Hierarchy" value="'+parent+'">Treść<br><textarea rows=5 cols=20 type="textarea" name="Forum.Body" value=""></textarea><br>Podpis</br><input type="text" name="Forum.Author" value="">';}
		
	content+='<a class=submit onmousedown="remove(\'PostNew\');">Anuluj</a>';
	content+='<a class=submit style="text-align:center;margin-right:5px"onmousedown="ForumPostNewSave();">Wyślij</a></form>';

	e.innerHTML='<div style="margin:0 auto;position:absolute;top:50%;left:50%;margin:0 auto;width:1px;height:1px;overflow:visible"><div style="width:380px;height:310px;position:relative;bottom:150px;right:190px;padding:5px;background:#00114B"><div style="background:#002565;width:370px;height:300px;padding:10px;text-align:left" class="PostNewx" id=dpm>'+content+'</div></div></div>';
	document.getElementsByTagName('body')[0].appendChild(e);
	return false;
}

function makePostRequest(URL,Query){
	vMakeRequest(URL,'POST',Query,'text/plain');
}

function vMakeRequest(url,method,action,type) { //Based on Mozzila Documentation
	var http_request = false; // HTTP Request Handler
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType(type); // Avoiding MIME error (text /or/ xml)
		}
	}else if (window.ActiveXObject) { // Loved IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) { // First try
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) { // Faild to create ActiveXObject
				alert('Caught Exception (IE): ' + e.toString());
			}
		}
	}
	if (!http_request) {
		alert('Giving up! Cannot create an XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = function() { vGetContents(http_request); }; // Process the requested site
	http_request.open(method, url, true);
	if(method=='POST'){
		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http_request.setRequestHeader("Content-length", action.length);
		http_request.setRequestHeader("Connection", "close");
	}
	if(method=='GET') http_request.send(null);
	else if(method=='POST') http_request.send(action);
}
function vGetContents(http_request) { // Processing resieved data
	try {
		if (http_request.readyState == 4 || http_request.readyState == "complete") {
			if (http_request.status == 200) { // Grabbing data from the server
				try{
					if(http_request.responseXML){
						if(http_request.responseXML.documentElement){ //IE
							vProcessDataXML(http_request.responseXML.documentElement);
						}else if(http_request.getResponseHeader("Content-Type")=='text/xml'){
							vProcessDataXML(http_request.responseXML);
						}else
							vProcessDataText(http_request.responseText);
					}else{
						vProcessDataText(http_request.responseText);
					}
				}catch(e){
					vProcessDataText(http_request.responseText);
				}
			} else if (http_request.status == 404) {
				alert('Requested object NOT found!');
			} else if (http_request.status == 500) {
				alert('Internal server error');
			}else {
				alert('There was a problem with the request.');
			}
		}else {
			// --- Put "LOADING" Div Here;
		}
	} catch( e ) {
		alert('Caught Exception on main alert: ' + e.toString());
	}
}
function vProcessDataText(Data){
	vGetRequest(Data,false);
}
function vProcessDataXML(Data){
	vGetRequest(Data,true);
}

function vGetRequest(d,isXML) {
	if(isXML){
		//Do nothing
		var c = d.firstChild;
		setTimeout("remove(\'PostNew\')",3000);
		return true;
	}else {
		d = d.replace("\r\n", "", "g");
		if(d=='OK')
			g('dpm').innerHTML = '<h2 style="text-align:center;margin-top:120px"> Wiadomość wysłana do moderatora. Prosze czekać na jej edycje w serwisie.</h2>';
		else
			g('dpm').innerHTML = '<h2 style="text-align:center;margin-top:120px">Nie zapisano!</h2>';
	 	setTimeout("remove(\'PostNew\')",3000);
	}
}

