/**
*
*  WebToolKit.utf8 (UTF-8 encode / decode) component
*  Compiled by Justas Vinevicius <justas.vinevicius(at)gmail.com>
*  Original code by Tobias Kieslich <tobias(at)justdreams.de>
*
*  Homepage:
*  http://www.webtoolkit.info/
*
**/

if (typeof(WebToolKit) == "undefined") {
	var WebToolKit = {};
};

WebToolKit.utf8 = {

	encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {
			var c = string.charCodeAt(n);
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
		}

		return utftext;
	},

	decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
		while ( i < utftext.length ) {
			c = utftext.charCodeAt(i);
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1); c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
		}
		return string;
	}

};

if (typeof(String.prototype.utf8encode) == "undefined") {
	String.prototype.utf8encode = function () {
		return WebToolKit.utf8.encode(this);
	};
};

if (typeof(String.prototype.utf8decode) == "undefined") {
	String.prototype.utf8decode = function () {
		return WebToolKit.utf8.decode(this);
	};
};

/**
*
*  WebToolKit URL encode / decode component
*  Compiled by Justas Vinevicius <justas.vinevicius(at)gmail.com>
*  Original code by Tyler Akins <fidian(at)rumkin.com>
*
*  Dependencies:
*  WebToolKit.utf8 (UTF-8 encode / decode) component for correct UTF-8 handling
*
*  Homepage:
*  http://www.webtoolkit.info/
*
**/

if (typeof(WebToolKit) == "undefined") {
	var WebToolKit = {};
};

WebToolKit.url = {

	encode : function (string) {
		if (typeof(String.prototype.utf8encode) != "undefined") {
			return escape(string.utf8encode());
		} else {
			return escape(string);
		}
	},

	decode : function (string) {
		if (typeof(String.prototype.utf8decode) !== "undefined") {
			return unescape(string).utf8decode();
		} else {
			return unescape(string);
		}
	}

};

if (typeof(String.prototype.urlencode) == "undefined") {
	String.prototype.urlencode = function () {
		return WebToolKit.url.encode(this);
	};
};

if (typeof(String.prototype.urldecode) == "undefined") {
	String.prototype.urldecode = function () {
		return WebToolKit.url.decode(this);
	};
};


/**
*
*  WebToolKit.base64 (Base64 encode / decode) component
*  Compiled by Justas Vinevicius <justas.vinevicius(at)gmail.com>
*  Original code by Tyler Akins <fidian(at)rumkin.com>
*
*  Dependencies:
*  WebToolKit.utf8 (UTF-8 encode / decode) component for correct UTF-8 handling
*
*  Homepage:
*  http://www.webtoolkit.info/
*
**/

if (typeof(WebToolKit) == "undefined") {
	var WebToolKit = {};
};

WebToolKit.base64 = {

	keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;

		if (typeof(String.prototype.utf8encode) !== "undefined") {
			input = input.utf8encode();
		}

		do {
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);

			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;

			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}

			output = output + this.keyStr.charAt(enc1) + this.keyStr.charAt(enc2) +
			this.keyStr.charAt(enc3) + this.keyStr.charAt(enc4);
		} while (i < input.length);

		return output;
	},

	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;

		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

		do {
			enc1 = this.keyStr.indexOf(input.charAt(i++));
			enc2 = this.keyStr.indexOf(input.charAt(i++));
			enc3 = this.keyStr.indexOf(input.charAt(i++));
			enc4 = this.keyStr.indexOf(input.charAt(i++));

			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;

			output = output + String.fromCharCode(chr1);

			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}
		} while (i < input.length);

		if (typeof(String.prototype.utf8decode) !== "undefined") {
			return output.utf8decode();
		} else {
			return output;
		}
	}

};

if (typeof(String.prototype.base64encode) == "undefined") {
	String.prototype.base64encode = function () {
		return WebToolKit.base64.encode(this);
	};
};

if (typeof(String.prototype.base64decode) == "undefined") {
	String.prototype.base64decode = function () {
		return WebToolKit.base64.decode(this);
	};
};


function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function get_element(id)
{
  var itm = null;
  if (document.getElementById) {
	itm = document.getElementById(id);
  } else if (document.all){
	itm = document.all[id];
  } else if (document.layers){
	itm = document.layers[id];
  }
  return itm;
}

function getElement(id)
{
  var itm = null;
  if (document.getElementById) {
	itm = document.getElementById(id);
  } else if (document.all){
	itm = document.all[id];
  } else if (document.layers){
	itm = document.layers[id];
  }
  return itm;
}

function resize_spacer_div(d)
{
	var itm = get_element(d);
	var itm2 = get_element('footer');

	if (itm2 && itm) {
		var lt2 = findPosY(itm2);
		var lt1 = findPosY(itm);
		var nh  = lt2 - lt1;
		//alert(nh);
		if (itm.style)
		{
				itm.style.height = nh + "px";
		}
	}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function cfm(id, disp) {
  var itm = null;
  if (document.getElementById) {
	itm = document.getElementById(id);
  } else if (document.all){
	itm = document.all[id];
  } else if (document.layers){
	itm = document.layers[id];
  }

  if (!itm) {
   // 
  }
  else if (itm.style) {
	itm.style.display = disp;
  }
}

function myPopUp(url)
{
	window.open(url, '', 'Width=820, Height=630, menubar=0, status=0');
}

function GetSWVer()
{
	var flashinstalled = 0;
	var flashversion = 0;
	MSDetect = "false";
	if (navigator.plugins && navigator.plugins.length)
	{
		x = navigator.plugins["Shockwave Flash"];
		if (x)
		{
			flashinstalled = 2;
			if (x.description)
			{
				y = x.description;
				flashversion = y.charAt(y.indexOf('.')-1);
			}
		}
		else
			flashinstalled = 1;
		if (navigator.plugins["Shockwave Flash 2.0"])
		{
			flashinstalled = 2;
			flashversion = 2;
		}
	}
	else if (navigator.mimeTypes && navigator.mimeTypes.length)
	{
		x = navigator.mimeTypes['application/x-shockwave-flash'];
		if (x && x.enabledPlugin)
			flashinstalled = 2;
		else
			flashinstalled = 1;
	}
	else
		MSDetect = "true";
		
	return flashversion;
}

var ajaxRequest = ajaxGetRequest();
var targetObject= null;

function ajaxCallback()
{ 
	//document.ajax.dyn.value = "Wait server...";
	if (ajaxRequest)
	{
		if(ajaxRequest.readyState == 4)
		{
			if(ajaxRequest.status == 200)
			{
				var status = ajaxRequest.responseXML.getElementsByTagName('status').item(0);
				var code   = ajaxRequest.responseXML.getElementsByTagName('code').item(0);
				var data   = ajaxRequest.responseXML.getElementsByTagName('data').item(0);
				if (status)
				{
					targetObject.innerHTML = status.firstChild.data;
					if (code.firstChild.data == "1")
					{
						var commData = getElement("commData");
						if (commData && data)
						{
							if (data.firstChild)
							{
								commData.innerHTML = WebToolKit.base64.decode(data.firstChild.data);
								setTimeout("cfm('komentarze', '');cfm('dodaj', 'none');cfm('inne', 'none');", 1000);
							} else {
								targetObject.innerHTML = "Komentarz dodany, odswiez strone recznie.";	
							}
						} else {
							targetObject.innerHTML = "Komentarz dodany, odswiez strone recznie.";
						}
						
						var sourceForm   = getElement("commForm");
						if (sourceForm)
						{
							sourceForm.komentarz.value = "";
						}
					}
				} else {
					targetObject.innerHTML = "Blad";
				}
			}	
			else	
			{
				targetObject.innerHTML = "Blad: " + ajaxRequest.status + " " + ajaxRequest.statusText;
			}	
			
			getElement("commFormDodaj").disabled = false;
			getElement("commFormText" ).disabled = false;
			
			if (getElement("commFormNick"))
			{
				getElement("commFormNick" ).disabled = false;
			}			
		} 
	}
}

function ajaxCallbackPage()
{ 
	if (ajaxRequest)
	{
		if(ajaxRequest.readyState == 4)
		{
			if(ajaxRequest.status == 200)
			{
				var status = ajaxRequest.responseXML.getElementsByTagName('status').item(0);
				var code   = ajaxRequest.responseXML.getElementsByTagName('code').item(0);
				var data   = ajaxRequest.responseXML.getElementsByTagName('data').item(0);

				if (status)
				{
					if (code.firstChild.data == "1")
					{
						var commData = getElement("commData");
						if (commData && data)
						{
							if (data.firstChild)
							{
								commData.innerHTML = WebToolKit.base64.decode(data.firstChild.data);
							}
						}
					}
				} else {
					alert("Wystapil nieokreslony blad.");
				}
			}	
			else	
			{
				alert("Blad: " + ajaxRequest.status + " " + ajaxRequest.statusText);
			}	
		} 
	}
}

function ajaxGetRequest()
{
	var req = null; 

	if(window.XMLHttpRequest) {
		req = new XMLHttpRequest(); 
	} else if (window.ActiveXObject) {
		req  = new ActiveXObject('Microsoft.XMLHTTP'); 	
	}
	
	return req;
}

function ajaxPostComment()
{
	var sourceForm  = null;
	targetObject	= null;
	ajaxRequest 	= ajaxGetRequest();
	
	targetObject = getElement("commPlaceholder");
	sourceForm   = getElement("commForm");
	
	if (!ajaxRequest || !targetObject || !sourceForm)
	{
		if (!sourceForm)
		{
			alert("Nie mozna dodac komentarza!");
			return false;
		} else {
			alert("Nie mozna dodac komentarza!");
			sourceForm.submit();
			return false;
		}
	}
	
	var content = "";
	if (sourceForm.nick)
	{
		content = "ajax=1&id=" + WebToolKit.url.encode(sourceForm.id.value) + "&nick=" + WebToolKit.url.encode(sourceForm.nick.value) + "&komentarz=" + WebToolKit.url.encode(sourceForm.komentarz.value);
	} else {
		content = "ajax=1&id=" + WebToolKit.url.encode(sourceForm.id.value) + "&komentarz=" + WebToolKit.url.encode(sourceForm.komentarz.value);
	}
	
	ajaxRequest.onreadystatechange = ajaxCallback;
	ajaxRequest.open("POST", "/komentarze/", true); 
	ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	ajaxRequest.send(content); 
	
	targetObject.innerHTML = "Wysylam ...";
	
	getElement("commFormDodaj").disabled = true;
	getElement("commFormText" ).disabled = true;
	
	if (getElement("commFormNick"))
	{
		getElement("commFormNick" ).disabled = true;
	}
	
	return true;
}

function ajaxSwitchPage(id, page)
{
	targetObject	= null;
	ajaxRequest 	= ajaxGetRequest();
	
	if (!ajaxRequest)
	{
		alert("Nie mozna zmienic strony!");
		return false;
	}
	
	ajaxRequest.onreadystatechange = ajaxCallbackPage;
	ajaxRequest.open("GET", "/komentarze_strona.php?id=" + id + "&pg=" + page, true); 
	ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	ajaxRequest.send(null); 
	
	return true;
}

function ajaxCallbackRate()
{ 
	if (ajaxRequest)
	{
		var commData = getElement("rateStatus");
		if(ajaxRequest.readyState == 4)
		{
			if(ajaxRequest.status == 200)
			{
				var status = ajaxRequest.responseXML.getElementsByTagName('status').item(0);
				var code   = ajaxRequest.responseXML.getElementsByTagName('code').item(0);
				var data   = ajaxRequest.responseXML.getElementsByTagName('data').item(0);

				if (status)
				{
					if (code.firstChild.data == "1")
					{
						if (commData && data)
						{
							if (data.firstChild)
							{
								commData.innerHTML = WebToolKit.base64.decode(data.firstChild.data);
							}
						}
					}
				} else {
					if (commData)
					{
						commData.innerHTML = "Wystapil nieokreslony blad.";
					}
				}
			}	
			else	
			{
				commData.innerHTML = "Blad: " + ajaxRequest.status + " " + ajaxRequest.statusText;
			}	
		} 
	}
}

function ajaxRate(id, rate)
{
	targetObject	= null;
	ajaxRequest 	= ajaxGetRequest();
	
	if (!ajaxRequest)
	{
		alert("Nie mozna glosowac!");
		return false;
	}
	
	var commData = getElement("rateStatus");
	commData.innerHTML = "Poczekaj chwile ...";
	
	ajaxRequest.onreadystatechange = ajaxCallbackRate;
	ajaxRequest.open("GET", "/ocena/" + id + "/" + rate + "/", true); 
	ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	ajaxRequest.send(null); 
	
	return true;
}