/******************************************************************************/
/*
    © 2005-2008 Grafikstudio
    All rights reserved

    Making *ANY* changes in this document will cause loss of 
    12-month warranty for faultless site functioning and
    free of charge service during warranty period will be no 
    longer available!
    Any changes shoud be first consulted with and accepted by
    Grafikstudio representative.

    Stylesheet for ProCMS visitor-side pages.
    Version 3.0 or greater only.

    * File encoding: UTF-8
	* Script compatibility: Gecko 1.8
	* General functions are stored in scripts.js
	* Function used while building menus are stored in menus.js
*/
/******************************************************************************/
//	GLOBAL VARIABLES DEFINITIONS
var httpRequest = null;
//	FUNCTION DETECTING MSIE BROWSER
function isIE(){
	if(-1 != navigator.userAgent.indexOf("MSIE")) { return true; } else { return false; }
}
//	CROSS-BROWSER FUNCTIONS USED TO DETECT VARIOUS OBJECTS' PROPERTIES 
function screenWidth() {
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		return window.innerWidth;
	} else {
		if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			return document.documentElement.clientWidth;
		} else {
			if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			return document.body.clientWidth;
			}
		}
	}
}
function screenHeight() {
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		return window.innerHeight;
	} else {
		if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			return document.documentElement.clientHeight;
		} else {
			if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			return document.body.clientHeight;
			}
		}
	}
}
function getTextContent(elm) {
	return ( elm.textContent || elm.innerText || elm.text );
}
function getVerticalOffset() {
	if ( window.pageYOffset ) {
		return window.pageYOffset;
	} else if ( document.body.scrollTop ) {
		return document.body.scrollTop;
	} else if ( document.documentElement.scrollTop ) {
		return document.documentElement.scrollTop;
	} else {
		return 0;
	}
}
function createNewElement(sTag){
	if (isIE()) { obj = document.createElement(sTag.toUpperCase()); }
	else { obj = document.createElement(sTag); }
	return obj;
}
//	GENERAL DHTML FUNCTIONS MODIFYING CSS CLASSES AND STYLESD
function div_display (name) {
	document.getElementById(name).style.display="block";
}
function div_hide (name) {
	document.getElementById(name).style.display="none";
}
function hoverClass(obj) {
	var is_selected = obj.className.indexOf("Hover");
	if (  is_selected == -1 ) {
		n = obj.className;
		obj.className = n+'Hover';
	}
}
function unhoverClass(obj) {
	var n = obj.className.indexOf("Hover");
	if ( n != -1 ) {
		cls = obj.className.substring(0,n);
		obj.className = cls;
	}
}

//	FUNCTIONS USED TO SHOW PICTURE PREVIEW FLOATING WINDOW
function pictureViewFullscreenStateChange(picID) {
	if ( httpRequest.readyState == 4 ) {
		if ( httpRequest.status == 200 ) {
			ch = httpRequest.responseXML.documentElement;
			for ( i=0;i<ch.childNodes.length;i++  ) {
				switch ( ch.childNodes[i].nodeName ){
					case "width": var imageX = getTextContent (ch.childNodes[i]); break;
					case "height":  var imageY = getTextContent (ch.childNodes[i]); break;
					case "description":  var imageDescription = getTextContent (ch.childNodes[i]); break;
				}
			}
			pictureViewFullscreenAppend(picID,imageX,imageY,imageDescription);
		} else {
			pictureViewFullscreenError();
		}
	} else {
		pictureViewFullscreenLoading();
	}
}
function pictureViewFullscreenLoading() {
	objDescription = document.getElementById ('imagePreviewDescription');
	objDescription.innerHTML = "<img class=\"imagePreviewLoading\" src=\""+jsPageAdress+"/img/loading.gif\" width=\"150\" height=\"15\" border=\"0\" alt=\"\" />";
}
function pictureViewFullscreenError() {
	alert ( "XMLHttpRequest: Error while loading XML data file." );
	objDiv = document.getElementById ('imagePreviewDiv');
	document.body.removeChild (objDiv);
}
function pictureViewFullscreen(picID) {
	if ( httpRequest ) { return false; }
	objDiv = createNewElement('div');
	objDiv.id = 'imagePreviewDiv';
	objDiv.className = 'imagePreview';
	document.body.appendChild (objDiv);
	objDescription = createNewElement('div');
	objDescription.id = 'imagePreviewDescription';
	objDescription.className = 'imagePreviewDescription';
	objDiv.appendChild (objDescription);
	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;
	}
	httpRequest.open('GET', jsPageAdress+'/xml.picture.php?gid='+picID, true);
	httpRequest.send('gid='+picID);
	httpRequest.onreadystatechange = function(){pictureViewFullscreenStateChange(picID);};
}
function pictureViewFullscreenAppend(picID,imgWidth,imgHeight,imgAlt) {
	if ( imgWidth > (screenWidth()-25) ) {
		imgHeight = Math.floor((imgHeight/imgWidth)*(screenWidth()-25));
		imgWidth = screenWidth()-25;
	}
	if ( imgHeight > (screenHeight()-50) ) {
		imgWidth = Math.floor((imgWidth/imgHeight)*(screenHeight()-50));
		imgHeight = screenHeight()-50;
	}
	objDiv = document.getElementById ('imagePreviewDiv');
	objDiv.style.height = document.body.clientHeight+'px';
	objDescription = document.getElementById ('imagePreviewDescription');
	objDescription.innerHTML = "&nbsp;";
	objImg = createNewElement('img');
	objImg.id = 'imagePreviewImg';
	objImg.className = 'imagePreview';
	objImg.src = jsPageAdress+'/module_thumbnail.php?gid='+picID+'&x='+imgWidth+'&y='+imgHeight+'&out=jpg';
	objImg.width = imgWidth;
	objImg.height = imgHeight;
	objImg.alt = 'Click on the image to close preview.';
	objImg.title = imgAlt;
	objImg.border = '0';
	objImg.style.top = Math.floor((screenHeight()-imgHeight)/2)+getVerticalOffset()+'px';
	objImg.style.left = Math.floor((screenWidth()-imgWidth)/2)+'px';
	objImg.onclick = function(){pictureViewFullscreenClose();};
	objDiv.appendChild (objImg);
}
function pictureViewFullscreenClose() {
	httpRequest = null;
	objDiv = document.getElementById('imagePreviewDiv');
	document.body.removeChild(objDiv);
}
// CALENDAR PRINTING FUNCIONS
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()+7 );
	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()-7 );
	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
	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() + 31 )/7 ) + 1;
	
	// get information about previous and next month
	nextDate = new Date ( currentDate.getFullYear(), currentDate.getMonth()+1, 1 );
	nextMonth = nextDate.getMonth() + 1;
	nextYear = nextDate.getFullYear();
	previousDate = new Date ( currentDate.getFullYear(), currentDate.getMonth()-1, 1 );
	previousMonth = previousDate.getMonth() + 1;
	previousYear = previousDate.getFullYear();
	
	// insert calendar title
	if ( document.getElementById('calendarTitle') != undefined ) {
		calendarTitle = document.getElementById('calendarTitle');
	} else {
		calendarTitle = document.createElement ('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 + "\" 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'));
	}
	calendarTable = document.createElement ('table');
	calendarTable.id = 'calendarTable';
	calendarTable.className = 'calendar';
	calendarTable.cellSpacing = 1;
	calendarTable.width = '100%';
	
	// insert calendar table heading with short names of days of weeks
	tr = document.createElement ('tr');
	tr.className = 'calendar';
	for ( dow=0;dow<7;dow++ ) {
		th = document.createElement ('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 = document.createElement ('div');
			td.appendChild (div);
			var z = ( w * 7 + dd ) - fdate.getDay();
			var 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);
	}
	calendarObject.appendChild (calendarTable);

	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;
	}
	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 = "";
			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++ ) {
					gid = getTextContent (ch.childNodes[i].childNodes[2]);
					t = document.createElement ('div');
					t.className = "calendarEventTitle";
					t.innerHTML = "<a href=\""+jsPageAdress+"/index.php?cmd=event&amp;gid="+gid+"\" class=\"calendarEventTitle\" target=\"_self\">" + getTextContent ( ch.childNodes[i].childNodes[0] ) + "</a>";
					d = document.createElement ('div');
					d.className = "calendarEventContent";
					d.innerHTML = "<a href=\""+jsPageAdress+"/index.php?cmd=event&amp;gid="+gid+"\" class=\"calendarEventContent\" target=\"_self\">" + getTextContent ( ch.childNodes[i].childNodes[1] ) + "</a>";
					calendarEvents.appendChild (t);
					calendarEvents.appendChild (d);
				}
			}
		} 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." );
}
