
/* 
	(c) 2005-2006 Grafikstudio
	Wszystkie prawa zastrzeżone

	UWAGA!
	Dokonanie *jakichkolwiek* zmian w obrębie kodu źródłowego strony
	doprowadzi do utraty gwarancji na poprawne działanie kodu i
	uniemożliwi nieodpłatne naprawy usterek przez wykonawcę, jeżeli 
	takowe wystąpią.

*/
//	GLOBAL VARIABLES DEFINITIONS
var current_link_menu = "";
var current_link_submenu = "";
var visible_submenu = "";
var hovered_category = "";
var hovered_subcategory = "";
var httpRequest = null;
//var jsHolidaysString = "/1-1/1-5/3-5/12-4/13-4/11-6/15-8/1-11/11-11/25-12/26-12/";
var jsHolidaysString = "/1-1/6-1/1-5/3-5/25-4/23-6/15-8/1-11/11-11/25-12/26-12/";
var jsMonthsFullArray = ["Styczeń","Luty","Marzec","Kwiecień","Maj","Czerwiec","Lipiec","Sierpień","Wrzesień","Październik","Listopad","Grudzień"];
var jsDaysShortArray = ["Nd","Pn","Wt","Śr","Cz","Pt","So"];
var jsDaysFullArray = ["Niedziela","Poniedziałek","Wtorek","Środa","Czwartek","Piątek","Sobota"];

var viewTime = 5000;

$(document).ready(function(){
	var imgParent = $("div#headerpicture");
	var timeId = window.setTimeout(slideShowC,viewTime);
	function slideShowC(){
		$.get("xml.header.php", function(data){
			var i = $('<img style="display: none;" src="'+data+'" width="250" height="187" border="0" alt="" />');
			i.appendTo(imgParent);
			i.load(function(){
				$(imgParent).children(":visible").remove();
				i.css("display","block");
			});
		});
		window.clearTimeout(timeId);
		timeId = window.setTimeout(slideShowC,viewTime);
	}
});

function isIE(){
	if(-1 != navigator.userAgent.indexOf("MSIE")) { return true; } else { return false; }
}

function createNewElement(sTag){
	if (isIE()) { obj = document.createElement(sTag.toUpperCase()); }
	else { obj = document.createElement(sTag); }
	return obj;
}

function getTextContent(elm) {
	return ( elm.textContent || elm.innerText || elm.text );
}

function set_visibility (name) {
	if ( name == visible_submenu) {
		visible_submenu = "";
	} else {
		visible_submenu = name;
	}
}

function div_display (name) {
	if ( document.getElementById(name) ) { 
	document.getElementById(name).style.display="block";
	}
}

function div_hide (name) {
	if ( document.getElementById(name) ) { 
		if ( name != visible_submenu ) {
			document.getElementById(name).style.display="none";
		}
	}
}

function mainmenu_hover (name) {
	if ( document.getElementById(name) ) { 
	document.getElementById(name).className="block_menu_link_hover";
	}
}

function mainmenu_unhover (name) {
	if ( document.getElementById(name) ) { 
		if (name != current_link_menu) {
			document.getElementById(name).className="block_menu_link";
		}
	}
}

function submenu_hover (name) {
	if ( document.getElementById(name) ) { 
		document.getElementById(name).className="block_menu_sublink_row_hover";
	}
}

function submenu_unhover (name) {
	if ( document.getElementById(name) ) { 
		if ( name != current_link_submenu ) {
			document.getElementById(name).className="block_menu_sublink_row";
		}
	}
}

function search_select_all() {
	for (var c=0;c<document.forms["search_form"].elements.length;c++) {
		var e = document.forms["search_form"].elements[c];
		if (e.name != 'search_option_allbox') {
			e.checked = document.search_form.search_option_allbox.checked;
		}
	}
}

function open_image_window (id, width, height) {
	
	var window_width = 0, window_height = 0;


	if ( width < ( window.screen.availWidth - 30 ) ) {
		window_width  = width + 30;
		scrolls='no';
	} else {
		window_width = window.screen.availWidth - 30;
		scrolls='yes';
	}
	if ( height < ( window.screen.availHeight - 130 ) ) {
		window_height =  height + 120;
		scrolls='no';
	} else {
		window_height =  window.screen.availHeight - 130;
		scrolls='yes';
	}
	
	var position_left = 0, position_top = 0;

	position_left = Math.round( (window.screen.availWidth  - window_width ) /2 );
	position_top = Math.round( (window.screen.availHeight - window_height) /2 );
	
	window.open("inc.picture.view.php?gid="+id+"", "null", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars="+scrolls+",resizable=yes,copyhistory=no,width="+window_width+",height="+window_height+",left="+position_left+",top="+position_top+"");

}

function category_hover (id) {
	document.getElementById('mm_prd_'+id+'').className="block_menu_prd_category_hover";
//	if ( document.getElementById('mm_prd_'+id+'_sub') != null ) {
//		document.getElementById('mm_prd_'+id+'_sub').className="block_menu_prd_category_sub_hover";
//	}
}

function category_unhover (id) {
	if ( id != hovered_category ) {
		document.getElementById('mm_prd_'+id+'').className="block_menu_prd_category";
//		if ( document.getElementById('mm_prd_'+id+'_sub') != null ) {
//			document.getElementById('mm_prd_'+id+'_sub').className="block_menu_prd_category_sub";
//		}
	}
}

function subcategory_hover (id) {
	document.getElementById('mm_prd_sub_'+id+'').className="block_menu_prd_subcategory_hover";
}

function subcategory_unhover (id) {
	if ( id != hovered_subcategory ) {
		document.getElementById('mm_prd_sub_'+id+'').className="block_menu_prd_subcategory";
	}
}

function category_rolldown(id) {
	if (document.getElementById('mm_prd_'+id+'_sub') != null) {
		if ( document.getElementById('mm_prd_'+id+'_sub').style.display == "block" ) {
			document.getElementById('mm_prd_'+id+'_sub').style.display = "none";
		} else {
			document.getElementById('mm_prd_'+id+'_sub').style.display = "block";
		}
	}
}
/*
function subcategory_rolldown(id) {
	if (document.getElementById('mm_prd_sub_'+id+'_prdlist').style.display == "block") {
		document.getElementById('mm_prd_sub_'+id+'_prdlist').style.display = "none";
	} else {
		document.getElementById('mm_prd_sub_'+id+'_prdlist').style.display = "block";
	}
}
*/
function products_rolldown() {
	if (document.getElementById('mm_prd_list').style.display == "block") {
		document.getElementById('mm_prd_list').style.display = "none";
	} else {
		document.getElementById('mm_prd_list').style.display = "block";
	}
}

function hoverClass ( obj ) {
	var is_selected = obj.className.indexOf("_select");
	if (  is_selected == -1 ) {
		n = obj.className;
		obj.className = n+'_hover';
	}
}
function hoverClassE(obj){
	var is_selected = obj.className.indexOf("_select");
	if (  is_selected == -1 ) {
		n = obj.className;
		obj.className = n+'_hover';
		if (obj.childNodes.length>0){
			for (i=0;i<obj.childNodes.length;i++){
				if (obj.childNodes[i].tagName == "a" || obj.childNodes[i].tagName == "A"){
					obj.childNodes[i].className += '_hover';
				}
			}
		}
	}
}
function unhoverClassE(obj){
	var n = obj.className.indexOf("_hover");
	if ( n != -1 ) {
		cls = obj.className.substring(0,n);
		obj.className = cls;
		if (obj.childNodes.length>0){
			for (i=0;i<obj.childNodes.length;i++){
				if ( ( obj.childNodes[i].tagName == "a" || obj.childNodes[i].tagName == "A" ) && obj.childNodes[i].className.indexOf('_hover') ) {
					obj.childNodes[i].className = obj.childNodes[i].className.substring(0,obj.childNodes[i].className.indexOf('_hover'));
				}
			}
		}
	}
}

function selectClass ( obj ) {
	n = obj.className;
	obj.className = n+'_select';
}

function unselectClass ( obj ) {
	var n = obj.className.indexOf("_select");
	if ( n != -1 ) {
		cls = obj.className.substring(0,n);
		obj.className = cls;
	}

}

function unhoverClass ( obj ) {
	var n = obj.className.indexOf("_hover");
	if ( n != -1 ) {
		cls = obj.className.substring(0,n);
		obj.className = cls;
	}
}

function rollDown (obj) {
	for ( i=0; i<obj.childNodes.length ;i++  ) {
		if ( obj.childNodes[i].id.indexOf('submenu')!=-1) { 
			obj.childNodes[i].style.display = "block";
		}
	}
}

function rollUp (obj) {
	for ( i=0; i<obj.childNodes.length ;i++  ) {
		if ( obj.childNodes[i].id.indexOf('submenu')!=-1) { 
			obj.childNodes[i].style.display = "none";
		}
	}
}

function changeChildsColor (obj,col) {
	if ( window.navigator.userAgent.indexOf('IE') != -1 ) { 
	for ( i=0; i<obj.childNodes.length ; i++ ) {
		if ( obj.childNodes[i].nodeName == 'a' || obj.childNodes[i].nodeName == 'A' ) { 
			obj.childNodes[i].style.color = col;
		}
	}
	}
}

function pollVote(formname,maxpicks){
	var f=document.forms[formname];
	var picks=0;
	for (i=0;i<f.elements.length;i++) {
		if ( f.elements[i].getAttribute("type")=="checkbox" && f.elements[i].checked ) { picks++; }
		if ( picks >= maxpicks ) { f.submit(); return true; }
	}
}

function jsNextDay ( date ) {
	var newdate = new Date ( date.getFullYear(), date.getMonth(), date.getDate()+1 );
	var month = newdate.getMonth()+1;
	var s = newdate.getDate()+","+month+","+newdate.getFullYear();
	return s;
}
function jsPreviousDay ( date ) {
	var newdate = new Date ( date.getFullYear(), date.getMonth(), date.getDate()-1 );
	var month = newdate.getMonth()+1;
	var s = newdate.getDate()+","+month+","+newdate.getFullYear();
	return s;
}
function jsNextWeek ( date ) {
	var newdate = new Date ( date.getFullYear(), date.getMonth(), date.getDate()+30);
	var month = newdate.getMonth()+1;
	var s = newdate.getDate()+","+month+","+newdate.getFullYear();
	return s;
}
function jsPreviousWeek ( date ) {
	var newdate = new Date ( date.getFullYear(), date.getMonth(), date.getDate()-30 );
	var month = newdate.getMonth()+1;
	var s = newdate.getDate()+","+month+","+newdate.getFullYear();
	return s;
}
function getTextualRepresentation ( argDate ) {
	var ereg = /^(\d{1,}),(\d{1,}),(\d{4})$/i;
	var result = ereg.exec(argDate);
	if ( result == null ) { return "?"; }
	var s = result[1] + " " + jsMonthsFullArray[result[2]-1] + " " + result[3];
	return s;
}

function printCallendar(startDay,startMonth,startYear) {
	// get element into variable
	var calendarObject = document.getElementById('calendarObject');
	// get current date
	if ( startDay == 0 ) {
		var currentDate = new Date (startYear,startMonth-1,1);
	} else {
		var currentDate = new Date (startYear,startMonth-1,startDay);
	}
	// get date of the first day in current month
	var fdate = new Date ( currentDate.getFullYear(), currentDate.getMonth(), 1 );
	// get number of weeks in current month
	var weekNumber = Math.floor ( ( fdate.getDay() + 30 )/7 ) + 1;
	//alert ( fdate.getDay() );
	
	// get information about previous and next month
	var nextDate = new Date ( currentDate.getFullYear(), currentDate.getMonth()+1, 1 );
	var nextMonth = nextDate.getMonth() + 1;
	var nextYear = nextDate.getFullYear();
	var previousDate = new Date ( currentDate.getFullYear(), currentDate.getMonth()-1, 1 );
	var previousMonth = previousDate.getMonth() + 1;
	var previousYear = previousDate.getFullYear();
	
	// insert calendar title
	if ( document.getElementById('calendarTitle') != undefined ) {
		var calendarTitle = document.getElementById('calendarTitle');
	} else {
		var calendarTitle = createNewElement ('div');
	}
	calendarTitle.className = 'calendarTitle';
	calendarTitle.id = "calendarTitle";
	calendarTitle.innerHTML = 
	"<img src=\""+jsPageAdress+"/default/calendar_previous.png\" class=\"calendarTitle\" width=\"16\" height=\"16\" "+
		"alt=\"" + getTextualRepresentation(jsPreviousWeek(currentDate)) + "\" "+
		"title=\"" + getTextualRepresentation(jsPreviousWeek(currentDate)) + "\" border=\"0\" style=\"left:0px;\" "+
		"onclick=\"printCallendar("+jsPreviousWeek(currentDate)+");\" />"+
	"<img src=\""+jsPageAdress+"/default/calendar_step_previous.png\" class=\"calendarTitle\" width=\"16\" height=\"16\" "+
		"alt=\"" + getTextualRepresentation(jsPreviousDay(currentDate)) + "\" "+
		"title=\"" + getTextualRepresentation(jsPreviousDay(currentDate)) + "\" border=\"0\" style=\"left:16px;\" "+
		"onclick=\"printCallendar("+jsPreviousDay(currentDate)+");\" />"+
	currentDate.getDate() + " <a href=\"" + jsPageAdress + "/index.php?cmd=month&amp;date=" + startMonth + "-"+ currentDate.getFullYear() +"\" class=\"calendarTitle\" target=\"_self\">" + jsMonthsFullArray[currentDate.getMonth()] + "</a> " + currentDate.getFullYear() +
	"<img src=\""+jsPageAdress+"/default/calendar_step_next.png\" class=\"calendarTitle\" width=\"16\" height=\"16\" "+
		"alt=\"" + getTextualRepresentation(jsNextDay(currentDate)) + "\" "+
		"title=\"" + getTextualRepresentation(jsNextDay(currentDate)) + "\" border=\"0\" style=\"right:16px;\" "+
		"onclick=\"printCallendar("+jsNextDay(currentDate)+");\" />"+
	"<img src=\""+jsPageAdress+"/default/calendar_next.png\" class=\"calendarTitle\" width=\"16\" height=\"16\" "+
		"alt=\"" + getTextualRepresentation(jsNextWeek(currentDate)) + "\" "+
		"title=\"" + getTextualRepresentation(jsNextWeek(currentDate)) + "\" border=\"0\" style=\"right:0px;\" "+
		"onclick=\"printCallendar("+jsNextWeek(currentDate)+");\" />";
	calendarObject.appendChild (calendarTitle);
	
	// insert calendar table
	if ( document.getElementById('calendarTable') != undefined ) {
		 document.getElementById('calendarTable').parentNode.removeChild(document.getElementById('calendarTable'));
	}
	
	calendarTableParent = createNewElement('table');
	calendarTableParent.id = 'calendarTable';
	calendarTableParent.className = 'calendar';
	calendarTableParent.cellSpacing = 1;
	//calendarTableParent.width = '100%';

	calendarTable = createNewElement('tbody');
	calendarObject.appendChild(calendarTableParent);
	calendarTableParent.appendChild(calendarTable);

	// insert calendar table heading with short names of days of weeks
	tr = createNewElement('tr');
	tr.className = 'calendar';
	for ( var dow = 0; dow < 7; dow++ ) {
		th = createNewElement('th');
		th.className = 'calendar';
		th.innerHTML = jsDaysShortArray [dow];
		tr.appendChild (th);
	}
	calendarTable.appendChild (tr);

	// print callendar itself
	for ( w = 0; w < weekNumber; w++ ) {
		tr = document.createElement('tr');
		tr.className = 'calendar';
		for ( dd = 1; dd <= 7; dd++ ) {
			td = document.createElement('td');
			td.className = 'calendar';
			div = createNewElement('div');
			td.appendChild (div);
			z = ( w * 7 + dd ) - fdate.getDay();
			date = new Date (currentDate.getFullYear(),currentDate.getMonth(),z);
			if ( date.getMonth() == currentDate.getMonth() ) {
				if ( date.getDay() == 0 || jsHolidaysString.indexOf("/"+date.getDate()+"-"+(date.getMonth()+1)+"/")!=-1 ) 
				{ div.className = 'calendarDayHolyday'; } else { div.className = 'calendarDayMundane'; }
				if ( date.getDate() == currentDate.getDate() ) { div.className = 'calendarDayToday'; }
				div.innerHTML = "<a href=\"" + jsPageAdress + "/index.php?cmd=calendar&amp;date=" + date.getDate() + "-" + (date.getMonth()+1) + "-" + date.getFullYear() + "\" class=\"" + div.className + "\" target=\"_self\">" + date.getDate() + "</a>";
			} else {
				td.removeChild (div);
				td.innerHTML = "&nbsp;";
				td.className = "calendarEmptyCell";
			}
			tr.appendChild (td);
		}
		calendarTable.appendChild (tr);
	}

	var calendarEvents = document.getElementById ('calendarEventsList');
	while ( calendarEvents.firstChild ) {
		calendarEvents.removeChild ( calendarEvents.firstChild );
	}
	getEventsForDay ( currentDate );
}

function getEventsForDay ( date ) {
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		httpRequest = new XMLHttpRequest();
		if (!isIE()) { httpRequest.overrideMimeType('text/xml'); }
	} else if (window.ActiveXObject) { // IE
		httpRequest = new ActiveXObject ("Microsoft.XMLHTTP");
	}
	if (!httpRequest) {
		alert ( "XMLHttpRequest: Cannot create an XMLHTTP instance." );
		return false;
	}
	var m = date.getMonth()+1;
	httpRequest.open('GET', jsPageAdress+'/xml.events.php?date='+date.getDate()+'-'+m+'-'+date.getFullYear(), true);
	httpRequest.send( 'date='+date.getDate()+'-'+m+'-'+date.getFullYear() );
	httpRequest.onreadystatechange = function(){getEventsForDayStateChange();};
}

function getEventsForDayStateChange() {
	var calendarEvents = document.getElementById('calendarEventsList');
	if ( httpRequest.readyState == 4 ) {
		if ( httpRequest.status == 200 ) {
			calendarEvents.innerHTML = "";
			var ch = httpRequest.responseXML.documentElement; 
			if ( ch.childNodes.length == 0 ) {
				calendarEvents.innerHTML = 'Brak wydarzeń danego dnia.';
			} else {
				for (var i = 0; i < ch.childNodes.length; i++ ) {
					var dte = getTextContent (ch.childNodes[i].childNodes[2]);
					var gid = getTextContent (ch.childNodes[i].childNodes[3]);
					var t = createNewElement ('div');
					t.className = "calendarEventTitle";
					t.innerHTML = "<a href=\""+jsPageAdress+"/index.php?cmd=calendar&amp;date="+dte+"&amp;gid="+gid+"\" class=\"calendarEventTitle\" target=\"_self\">" + getTextContent ( ch.childNodes[i].childNodes[0] ) + "</a>";
					var d = createNewElement ('div');
					d.className = "calendarEventDate";
					d.innerHTML = dte+":";
					calendarEvents.appendChild (d);
					calendarEvents.appendChild (t);
				}
			}
		} else {
			getEventsForDayError();
		}
	} else {
		getEventsForDayLoading();
	}
}

function getEventsForDayLoading() {
	var calendarEvents = document.getElementById ('calendarEventsList');
	calendarEvents.innerHTML = "<img class=\"calendarEventsListLoading\" src=\""+jsPageAdress+"/default/loading.gif\" width=\"32\" height=\"32\" border=\"0\" alt=\"Loading...\" />";
}

function getEventsForDayError() {
	alert ( "XMLHttpRequest: Error while loading XML data file." );
}

function calendar_showEventContent(cID) {
	if ( document.getElementById('calendarEventContent_'+cID).style.display != "block" ) {
		document.getElementById('calendarEventContent_'+cID).style.display = "block";
		document.getElementById('calendarEventTitle_'+cID).firstChild.src = "img/calendar_up.png";
	} else {
		document.getElementById('calendarEventContent_'+cID).style.display = "none";
		document.getElementById('calendarEventTitle_'+cID).firstChild.src = "img/calendar_down.png";
	}
}

function calendar_showNewsContent(cID) {
	if ( document.getElementById('calendarNewsContent_'+cID).style.display != "block" ) {
		document.getElementById('calendarNewsContent_'+cID).style.display = "block";
		document.getElementById('calendarNewsTitle_'+cID).firstChild.src = "img/calendar_up.png";
	} else {
		document.getElementById('calendarNewsContent_'+cID).style.display = "none";
		document.getElementById('calendarNewsTitle_'+cID).firstChild.src = "img/calendar_down.png";
	}
}

function calendar_printDayOfMonth(timeValue) {
	date = new Date(timeValue*1000);
	//date.setTime();
	if ( date.getDay() == 0 || jsHolidaysString.indexOf("/"+date.getDate()+"-"+(date.getMonth()+1)+"/")!=-1 ) {
		document.getElementById('calendarID'+timeValue+'').style.color = "red";
		document.getElementById('calendarID'+timeValue+'').style.backgroundColor = "#fff4f4";
	} else {
		document.getElementById('calendarID'+timeValue+'').style.color = "black";
	}
	document.getElementById('calendarID'+timeValue+'').innerHTML = date.getDate();
}
