/* ==================================================================
FILE:   Clock.js
DESCR:  Zegar z GIF'ów na strony e-FRIENDS.
NOTES:  
================================================================== */

function convToStr(data) {
	data = "s" + data;
	data = data.substring(1, data.length);
	return data;
}

function getPDate(times, what) {
	var monn = new Array(12);
	
	monn[0] = "styczeń";
	monn[1] = "luty";
	monn[2] = "marzec";
	monn[3] = "kwiecień";
	monn[4] = "maj";
	monn[5] = "czerwiec";
	monn[6] = "lipiec";
	monn[7] = "sierpień";
	monn[8] = "wrzesień";
	monn[9] = "paĽdziernik";
	monn[10] = "listopad";
	monn[11] = "grudzień";

	var dayy = new Array(6);
	
	dayy[0] = "niedziela";
	dayy[1] = "poniedziałek";
	dayy[2] = "wtorek";
	dayy[3] = "¶roda";
	dayy[4] = "czwartek";
	dayy[5] = "pi±tek";
	dayy[6] = "sobota";

	if (times != "now") {
		var cdate = new Date(times);
	} else {
		var cdate = new Date();
	}

	if (what != null) {

		// zamień wszystkie wystąpienia <dddd> na nazwę dnia tygodnia
		whatL = what.toLowerCase();
		posL = whatL.indexOf("<dddd>", 0);
		strL = dayy[cdate.getDay()];
		while (posL != -1) {
			whatL = what.toLowerCase();
			what = what.substring(0, posL) + strL + what.substring(posL + 6, what.length);
			whatL = what.toLowerCase();
			posL = whatL.indexOf("<dddd>", 0);
		}
		
		// zamień wszystkie wystąpienia <dd> na nazwę dnia tygodnia
		whatL = what.toLowerCase();
		posL = whatL.indexOf("<dd>", 0);
		strL = cdate.getDate();
		while (posL != -1) {
			whatL = what.toLowerCase();
			what = what.substring(0, posL) + strL + what.substring(posL + 4, what.length);
			whatL = what.toLowerCase();
			posL = whatL.indexOf("<dd>", 0);
		}
		
		// zamień wszystkie wystąpienia <mmmm> na nazwę miesiąca
		whatL = what.toLowerCase();
		posL = whatL.indexOf("<mmmm>", 0);
		strL = monn[cdate.getMonth()];
		while (posL != -1) {
			whatL = what.toLowerCase();
			what = what.substring(0, posL) + strL + what.substring(posL + 6, what.length);
			whatL = what.toLowerCase();
			posL = whatL.indexOf("<mmmm>", 0);
		}
		
		// zamień wszystkie wystąpienia <mm> na numer miesiąca
		whatL = what.toLowerCase();
		posL = whatL.indexOf("<mm>", 0);
		strL = ((cdate.getMonth() < 9) ? ('0' + (cdate.getMonth() + 1)) : (cdate.getMonth() + 1));
		while (posL != -1) {
			whatL = what.toLowerCase();
			what = what.substring(0, posL) + strL + what.substring(posL + 4, what.length);
			whatL = what.toLowerCase();
			posL = whatL.indexOf("<mm>", 0);
		}
		
		// zamień wszystkie wystąpienia <yyyy> na numer roku (4 cyfry)
		whatL = what.toLowerCase();
		posL = whatL.indexOf("<yyyy>", 0);
		strL = convToStr(cdate.getFullYear());
		while (posL != -1) {
			whatL = what.toLowerCase();
			what = what.substring(0, posL) + strL + what.substring(posL + 6, what.length);
			whatL = what.toLowerCase();
			posL = whatL.indexOf("<yyyy>", 0);
		}

		// zamień wszystkie wystąpienia <yy> na numer roku (2 cyfry)
		whatL = what.toLowerCase();
		posL = whatL.indexOf("<yy>", 0);
		strL = convToStr(cdate.getFullYear());
		strL = strL.substring(strL.length - 2, strL.length);
		while (posL != -1) {
			whatL = what.toLowerCase();
			what = what.substring(0, posL) + strL + what.substring(posL + 4, what.length);
			whatL = what.toLowerCase();
			posL = whatL.indexOf("<yy>", 0);
		}

		out = what;
	}
	else {
		out = dayy[cdate.getDay()] + ", " + cdate.getDate() + " " + monn[cdate.getMonth()] + " " + cdate.getFullYear() + " r.";
	}
	return out;
}
