var showMessTab = new Array();
var imagesMaxZIndex = 50;

var NETSC = (navigator.appName =="Netscape") ? true : false;
var IE = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;
var OPERA = (window.opera) ? true : false;

if ((! getCookie("isIE")) && (! IE))
	setCookie("isIE","yes",365);


//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function recalcAll() {
	recalcMessDives();
}


//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function getElem(elemId) {
	var elem = null;
	
	if ( document.getElementById )	
		elem = document.getElementById(elemId);  
		
	if ((elem == null) && (document.getElementsByName))
		elem = document.getElementsByName(elemId)[0];
	
	if ((elem == null) && (document.all))
		elem = document.all[elemId];  
	
	if ((elem == null) && ( document.layers )) 	
		elem = document.layers[elemId];

	return elem;
}

//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function time() {
	return Math.floor(new Date().getTime()/1000);
}

//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function getPeriod(t) {
	return getPeriod(t,false);
}


//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function getPeriod(t ,withHour) {

	var acttime = time();
	var czas = ""; 
	if (date('Y-m-d',t) == date('Y-m-d',acttime))
		czas = "dzisiaj o "+date('H:i',t);
	
	else if (date('Y-m-d',t) == date('Y-m-d',acttime- 24*60*60))
		czas = "wczoraj "+date('H:i',t);
	
	else if (date('Y-m-d',$) == date('Y-m-d',acttime- 2*24*60*60))
		czas = "przedwczoraj "+date('H:i',$t);
	
	else	{
		czas = date('Y-m-d',t);
		if (withHour != false)
			czas = czas + " o godz: "+date('H:i',t);
		czas = czas+", "+Math.floor((acttime-t)/(24*60*60))+" dni temu";
	}
	
	//."".($withHour != false?" o godz: ".date('H:i',$t):"").", ".floor((time()-$t)/(24*60*60))." dni temu";

	return czas;

}

//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function getPos(ofObj) {

	var xPos = 0;
	var yPos = 0;
	var lastAddedxPos=0;
	var lastAddedyPos=0;
	if (ofObj.style.position != "absolute") {
		while ((typeof ofObj != "undefined") && (ofObj.tagName != "HTML"|"BODY")) {
			//alert(ofObj.tagName+" "+ofObj.offsetTop);
			
			if (lastAddedxPos != parseInt(ofObj.offsetLeft + 0)) {
				lastAddedxPos = parseInt(ofObj.offsetLeft + 0);
				xPos += lastAddedxPos;
			}
			if (lastAddedyPos != parseInt(ofObj.offsetTop + 0)) {
				lastAddedyPos = parseInt(ofObj.offsetTop + 0);
				yPos += lastAddedyPos;
			}
			if (ofObj.tagName == "iframe") 
				break;
			ofObj = (typeof ofObj.parentNode != "undefined")?ofObj.parentNode:ofObj.parentElement;
		}
	} else {
		xPos = parseInt(ofObj.offsetLeft + 0);
		yPos = parseInt(ofObj.offsetTop + 0);

	}

	var ret = new Array();
	ret["xPos"] = xPos;
	ret["yPos"] = yPos;
	return ret;
}

//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function urlencode(str) {
	return escape(str).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
}

//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function replaceAll(what,to,str) {
	while (str.indexOf(what) > -1)
		str = str.replace(what,to);
	return str;
}

//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function checkFilename() {
	var addImgForm = getElem("addImgForm");
	var addImgSubmit = getElem("addImgSubmit");
	var addImgFilename = getElem("addImgFilename");

 	var imgFile = addImgFilename.value.toLowerCase();
 
	if (! ((imgFile.lastIndexOf(".") != -1) &&  (imgFile.substring(imgFile.lastIndexOf(".")+1) == "jpg" || imgFile.substring(imgFile.lastIndexOf(".")+1) == "jpeg")))
		alert("Podany plik nie jest plikiem \".jpg\"!\n\nSystem przyjmuje tylko pliki JPG.\nProsimy o podanie poprawnego pliku.");
	else { 
		alert('Po kliknięciu OK plik zostanie wysłany do serwera.\nMoże to portwać do kilkudziesięciu sekund w zależności od rozmiaru pliku ze zdjęciem.\nProsimy cierpliwie czekać na przesłanie zdjęcia.\n\nKliknij OK w celu potwierdzenia.'); 
		addImgSubmit.disabled = true;
		addImgSubmit.value="... wysyłanie ...";
		addImgForm.submit();
	}
}

//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function setVisibleMenu() {
	var messImg="no";
	
	if (nick == "") {
		getElem("menu2").style.display='none';
		getElem("menu1").style.display='';
		return;
	}
					
	mailMess = 	'<b>Skrzynka:</b> Brak nowych wiadomości.';
	settingsMess = 	'Twoje konto.';
	logoutMess = 	'Wyloguj się.';
	if (mailNew != 0) {
		mailMess = 	'Masz <b>'+mailNew+' wiadomość/wiadomości</b> - kliknij aby odczytać.';
		messImg="new";
	}
	getElem("menu2txt").innerHTML = '<h4 class="m">Zalogowany jako: <a href="http://profile.'+mDom+'/'+nick+'/" target="_top" style="color: white"><b>'+nick+'</b></a> '+
	'<img id="mailm1" 	src="http://'+fDom+'navigationTop/'+messImg+'message.gif" 	onMouseOver="javasript: showMess(\'mailm1\',mailMess,3,0,1);" 		onMouseOut="javasript: hiddeMess();" onClick="javascript:top.location = \'http://user.'+mDom+'/?show=inbox\';"> '+
	'<img id="settingsm" 	src="http://'+fDom+'navigationTop/home.gif"  				onMouseOver="javasript: showMess(\'settingsm\',settingsMess,3,0,1);"	onMouseOut="javasript: hiddeMess();" onClick="javascript:top.location = \'http://user.'+mDom+'/\';"> '+
	'<img id="logoutm" 	src="http://'+fDom+'navigationTop/logout.gif"  			onMouseOver="javasript: showMess(\'logoutm\',logoutMess,3,0,1);" 	onMouseOut="javasript: hiddeMess();" onClick="javascript:top.location = \'http://user.'+mDom+'/login.html?logout=true\';"> '+
	'</h4>';
	getElem("menu1").style.display='none';
	getElem("menu2").style.display='';
}

//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

function setCookie(c_name,value,expiredays) { 
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
	//alert(c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString()));
}

//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) { 
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) 
				c_end=document.cookie.length;
    			return unescape(document.cookie.substring(c_start,c_end));
    		} 
  	}
	return false;
}



//***************************************************************************************************************
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//  MMESSAGES MESSAGES MESSAGES MESSAGES MESSAGES MESSAGES MESSAGES MESSAGES MESSAGES MESSAGES MESSAGES MESSAGES

//***************************************************************************************************************
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function deleMess() {
	location.replace("/?show=fotosmess&delete=true&piId="+pictId);
}
function saveMess() {

	var imgDiv = getElem("mainpict");
	if (imgDiv == null)
		return;
	var messDiv = getElem("mess");
	if (messDiv == null)
		return;
	var messText = getElem("messarea");
	var saveMessElem = getElem("saveMess");

	if (messText.value.length > 200) {
		alert("Tekst opisu nie może przekraczać 200 znaków.\nAktualnie wprowadzony opis zawiera "+messText.value.length+" znaków.\n\nProszę poprawić opis przed zapisaniem");
		return;
	}

	var uri = "&piId="+pictId;
	uri += "&mType="+mType;

	var xcrd=0;
	var ycrd=0;

	switch(mType) {
		case 1: 
			ycrd = parseInt(messDiv.offsetHeight + 0); 
			break;
		case 2: 
			ycrd = parseInt(messDiv.offsetHeight + 0); 
			xcrd = parseInt(messDiv.offsetWidth + 0);
			break;
		case 3: 
			xcrd = parseInt(messDiv.offsetWidth + 0); 
			break;
	}



	var xsm = (parseInt(messDiv.style.left+0) - parseInt(imgDiv.offsetLeft+0) + xcrd)/parseInt(imgDiv.offsetWidth + 0);
	uri += "&xsm="+Math.min(Math.max(0,xsm),1);


	var ysm = (parseInt(messDiv.style.top+0) - parseInt(imgDiv.offsetTop+0) + ycrd)/parseInt(imgDiv.offsetHeight + 0);
	uri += "&ysm="+Math.min(Math.max(0,ysm),1);

	uri += "&mess=" + encodeMess(messText.value);
	saveMessElem.disabled = true;
	saveMessElem.value="... zapisywanie ...";
	location.replace("/?show=fotosmess&save=true"+uri);
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

function recalcMessDives(xx) {

	var i=0;
	for (i=0;i<showMessTab.length;i++) {

		//if (xx==true) {
		//	alert(showMessTab[i]["divName"]+" "+showMessTab[i]["xPosDiv"]+" "+showMessTab[i]["yPosDiv"]+" "+showMessTab[i]["messName"]+" "+showMessTab[i]["xPosMess"]+" "+showMessTab[i]["yPosMess"]);
		//}
		if ((objDiv = getElem(showMessTab[i]["divName"])) == null)
			continue;

		if ((messDiv = getElem(showMessTab[i]["messName"])) == null)
			continue;

		var pos = getPos(objDiv);

		messDiv.style.left = ((-showMessTab[i]["xPosDiv"]+pos["xPos"]) + showMessTab[i]["xPosMess"]) + "px";
		messDiv.style.top  = ((-showMessTab[i]["yPosDiv"]+pos["yPos"]) + showMessTab[i]["yPosMess"]) + "px";

	}
}

//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function hiddeMess() {
	var messDiv = getElem("mess");
	if (messDiv == null)
		return;
	messDiv.style.visibility = "hidden";
}

//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function showMess(idElem, mess, typedymek, xsm, ysm) {
	showMess(idElem, mess, typedymek, xsm, ysm, false);
}

//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function showMess(idElem, mess, typedymek, xsm, ysm, messDymekCreate) {

	var newDivMess = new Array();
	var messDivName = "mess";
	if (messDymekCreate)
		mess = decodeMess(mess,true);
	else	mess = decodeMess(mess,false);

	if (messDymekCreate) {

		messDivName = idElem+"mess";
		var newDiv = document.createElement("div");
		newDiv.setAttribute("id", messDivName);
		newDiv.setAttribute("name", messDivName);
		document.body.appendChild(newDiv);

	}

	if ((messDiv = getElem(messDivName)) == null)
		return;

	messDiv.style.visibility = "hidden";
	messDiv.style.position = "absolute";
	messDiv.style.zIndex = 99999;

	if (mess=="")	{
		messDiv.style.visibility = "hidden";
		return;
	}

	if ((objDiv = getElem(idElem)) == null)
		return;

	if ((typeof xsm == "undefined") || (typeof ysm == "undefined") || (typeof typedymek == "undefined")) {
		xsm=0;
		ysm=0;
		typedymek=0;
	}


	var width = parseInt(objDiv.offsetWidth + 0);
	var height = parseInt(objDiv.offsetHeight + 0);
	var pos = getPos(objDiv);

	if (messDymekCreate) {
		newDivMess["divName"] = idElem;
		newDivMess["xPosDiv"] = parseInt(pos["xPos"] + 0);
		newDivMess["yPosDiv"] = parseInt(pos["yPos"] + 0);
	}

	messDiv.innerHTML = getDymek("<h4>"+mess+"</h4>",typedymek);
	messDiv.style.visibility = "visible";
	switch(typedymek) {
		case 1: 
			pos["yPos"] -= parseInt(messDiv.offsetHeight + 0); 
			break;
		case 2: 
			pos["yPos"] -= parseInt(messDiv.offsetHeight + 0); 
			pos["xPos"] -= parseInt(messDiv.offsetWidth + 0);
			break;
		case 3: 
			pos["xPos"] -= parseInt(messDiv.offsetWidth + 0); 
			break;
	}
	messDiv.style.left = pos["xPos"] + xsm * width + "px";
	messDiv.style.top  = pos["yPos"] + ysm * height + "px";

	if (messDymekCreate) {
		newDivMess["messName"] = messDivName;
		newDivMess["xPosMess"] = parseInt(messDiv.style.left + 0);
		newDivMess["yPosMess"] = parseInt(messDiv.style.top + 0);
		showMessTab.push(newDivMess);
	}
	
	recalcMessDives();
}



//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function getDymek(mess,type) {
	mess = '<h3>'+mess+'</h3>';

	switch(type) {
		case 0:
		return '<table class="dymek"><tr><td><img src="http://'+fDom+'/dymek/0/dymek1.gif" width="19" height="23"></td><td background="http://'+fDom+'/dymek/0/dymek2.gif" height="23"></td><td><img src="http://'+fDom+'/dymek/0/dymek3.gif" width="11" height="23"></td></tr><tr><td background="http://'+fDom+'/dymek/0/dymek4.gif"></td><td background="http://'+fDom+'/dymek/0/dymek5.gif">'+mess+'</td><td background="http://'+fDom+'/dymek/0/dymek6.gif"></td></tr><tr><td><img src="http://'+fDom+'/dymek/0/dymek7.gif" width="19" height="6"></td><td background="http://'+fDom+'/dymek/0/dymek8.gif" height="6"></td><td><img src="http://'+fDom+'/dymek/0/dymek9.gif" width="11" height="6"></td></tr></table>';
		break;

		case 1:
		return '<table class="dymek"><tr><td><img src="http://'+fDom+'/dymek/1/dymek1.gif" width="19" height="6"></td><td background="http://'+fDom+'/dymek/1/dymek2.gif" height="6"></td><td><img src="http://'+fDom+'/dymek/1/dymek3.gif" width="11" height="6"></td></tr><tr><td background="http://'+fDom+'/dymek/1/dymek4.gif"></td><td background="http://'+fDom+'/dymek/1/dymek5.gif">'+mess+'</td><td background="http://'+fDom+'/dymek/1/dymek6.gif"></td></tr><tr><td><img src="http://'+fDom+'/dymek/1/dymek7.gif" width="19" height="23"></td><td background="http://'+fDom+'/dymek/1/dymek8.gif" height="23"></td><td><img src="http://'+fDom+'/dymek/1/dymek9.gif" width="11" height="23"></td></tr></table>';
		break;

		case 2:
		return '<table class="dymek"><tr><td><img src="http://'+fDom+'/dymek/2/dymek1.gif" width="11" height="6"></td><td background="http://'+fDom+'/dymek/2/dymek2.gif" height="6"></td><td><img src="http://'+fDom+'/dymek/2/dymek3.gif" width="19" height="6"></td></tr><tr><td background="http://'+fDom+'/dymek/2/dymek4.gif"></td><td background="http://'+fDom+'/dymek/2/dymek5.gif">'+mess+'</td><td background="http://'+fDom+'/dymek/2/dymek6.gif"></td></tr><tr><td><img src="http://'+fDom+'/dymek/2/dymek7.gif" width="11" height="23"></td><td background="http://'+fDom+'/dymek/2/dymek8.gif" height="23"></td><td><img src="http://'+fDom+'/dymek/2/dymek9.gif" width="19" height="23"></td></tr></table>';
		break;

		case 3:
		return '<table class="dymek"><tr><td><img src="http://'+fDom+'/dymek/3/dymek1.gif" width="11" height="23"></td><td background="http://'+fDom+'/dymek/3/dymek2.gif" height="23"></td><td><img src="http://'+fDom+'/dymek/3/dymek3.gif" width="19" height="23"></td></tr><tr><td background="http://'+fDom+'/dymek/3/dymek4.gif"></td><td background="http://'+fDom+'/dymek/3/dymek5.gif">'+mess+'</td><td background="http://'+fDom+'/dymek/3/dymek6.gif"></td></tr><tr><td><img src="http://'+fDom+'/dymek/3/dymek7.gif" width="11" height="6"></td><td background="http://'+fDom+'/dymek/3/dymek8.gif" height="6"></td><td><img src="http://'+fDom+'/dymek/3/dymek9.gif" width="19" height="6"></td></tr></table>';
		break;
	}

}

//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function encodeMess(mess) {
	return replaceAll("'","--apos1--",replaceAll("\"","--apos--",replaceAll("\r","--r--",replaceAll("\n","--n--",replaceAll(";","--sred--",mess)))));
}


//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function decodeMess(mess,isHTML) {
	var dividder = "-mEsS-";
	mess = replaceAll("--apos1--","'",replaceAll("--apos--","\"",replaceAll("--r--","\r",replaceAll("--n--","\n",replaceAll("--sred--",";",mess)))))
	if (mess.indexOf(dividder) === -1) {
		if (! isHTML)
			return mess;
		else return replaceAll("\n","<br>",replaceAll("\r","<br>",replaceAll("\r\n","<br>",replaceAll("\r\n","<br>",mess))));
	} else {
		var ddiv = mess.indexOf(dividder);
		var m1 = mess.substr(0,ddiv);
		var m2 = mess.substr(ddiv+dividder.length);
		if (! isHTML)
			return m1+wordwrap(m2,50,"\n");
		else return m1+wordwrap(replaceAll("\n","<br>",replaceAll("\r","<br>",replaceAll("\r\n","<br>",replaceAll("\r\n","<br>",m2)))),50,"<br>");
	}
}


//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	if  (document.getElementById) {
		(function() {

			if (window.opera) 
				document.write("<input type='hidden' id='Q' value=' '>");

			var dragok = false;
			var y,x,d,dy,dx;

			function move(e){
				if (!e) e = window.event;
				if (dragok) {
					d.style.left = dx + e.clientX - x + "px";
					d.style.top  = dy + e.clientY - y + "px";
					var navi = getElem("navi");
					if (navi) {
						currentDivName = d.id;
						navi.style.visibility = "visible";
						navi.style.left = dx + e.clientX - x + "px";
						navi.style.top  = dy + e.clientY - y + "px";
					}
					return false;
				}
			}

			function down(e) {
				if (!e) e = window.event;
				var temp = (typeof e.target != "undefined")?e.target:e.srcElement;
				while ((typeof temp != "undefined") && (temp.tagName != "HTML"|"BODY") && (temp.className != "dragclass")) 
					temp = (typeof temp.parentNode != "undefined")?temp.parentNode:temp.parentElement;


				if (temp.className == "dragclass") {
					if (window.opera) 
						document.getElementById("Q").focus();
					dragok = true;
					imagesMaxZIndex++;
					temp.style.zIndex = imagesMaxZIndex;
					d = temp;
					dx = parseInt(temp.style.left+0);
					dy = parseInt(temp.style.top+0);
					x = e.clientX;
					y = e.clientY;
					var navi = getElem("navi");
					if (navi) {
						currentDivName = d.id;
						navi.style.visibility = "visible";
						navi.style.left = dx + "px";
						navi.style.top  = dy + "px";
					}
					document.onmousemove = move;
					return false;
				}
			}

			function up() {
				dragok = false;
				document.onmousemove = null;
			}


			document.onmousedown = down;
			document.onmouseup = up;

		})();
	}



