/**
 * Create engine objects
 */
var main = null;
var picture = null;
var ajax = null;

/**
 * Create main site objects
 */
var header = null;
var frame = null;

/**
 * Create admin area objects
 */
var adminCommon = null;

/**
 * Initializes everything
 */
function init(pArea) {
	
	/**
	 * Main module
	 * @type  Main
	 */
	main = new Main();
	
	/**
	 * Picture module
	 * @type  Picture
	 */
	picture = new Picture();
	
	/**
	 * Ajax module
	 * @type  Ajax
	 */
	ajax = new Ajax();
	
	/**
	 * Header module
	 * @type  Header
	 */
	header = new Header();
	header.init(pArea);
	
	/**
	 * Frame module
	 * @type  Frame
	 */
	frame = new Frame();
	frame.init();
	
	/**
	 * AdminCommon module
	 * @type  AdminCommon
	 */
	adminCommon = new AdminCommon();
	adminCommon.init(pArea);
}
/**
 * AdminCommon module
 * @constructor
 * @author   Sperg Ádám
 * @version  1.01, 08/31/08
 */
function AdminCommon() {

	/**
	 * Pointer to this
	 * @type  Header
	 */
	var self = this;
	
	/**
	 * Last opened city in civil forum list
	 * @type  Number
	 */
	var lastOpenedList = null;
	
	/**
	 * Submit deleting form width selected value in user list
	 * @param  Number  Id
	 */
	this.userListDel = function(pId) {
		var delIdInput = main.getElt("userdel");
		delIdInput.value = pId;
		document.userlist.submit();
	}
	
	/**
	 * Submit deleting form width selected value in civil list
	 * @param  Number  Id
	 */
	this.civilListDel = function(pId) {
		var delIdInput = main.getElt("civillistdel");
		delIdInput.value = pId;
		document.civillist.submit();
	}
	
	/**
	 * Submit deleting form width selected value in document list
	 * @param  Number  Id
	 */
	this.docListDel = function(pId) {
		var delIdInput = main.getElt("doclistdel");
		delIdInput.value = pId;
		document.doclist.submit();
	}
	
	/**
	 * Submit deleting form width selected value in news list
	 * @param  Number  Id
	 */
	this.newsListDel = function(pId) {
		var delIdInput = main.getElt("newslistdel");
		delIdInput.value = pId;
		document.newslist.submit();
	}
	
	/**
	 * Submit deleting form width selected value in job list
	 * @param  Number  Id
	 */
	this.jobListDel = function(pId) {
		var delIdInput = main.getElt("joblistdel");
		delIdInput.value = pId;
		document.joblist.submit();
	}
	
	/**
	 * Submit deleting form width selected value in assembly
	 * @param  Number  Id
	 */
	this.assemblyDel = function(pId) {
		var delIdInput = main.getElt("assemblydel");
		delIdInput.value = pId;
		document.assembly.submit();
	}
	
	/**
	 * Submit deleting form width selected value in round list
	 * @param  Number  Id
	 */
	this.roundListDel = function(pId) {
		var delIdInput = main.getElt("roundlistdel");
		delIdInput.value = pId;
		document.roundlist.submit();
	}
	
	/**
	 * Submit file adding form in news or round
	 * @param  Number  Id
	 */
	this.fileAdd = function(pId) {
		var addIdInput = main.getElt("fileadd");
		addIdInput.value = pId;
		if(document.newsmodify) {
			document.newsmodify.submit();
		} else if (document.roundmodify) {
			document.roundmodify.submit();
		} else if (document.payoff) {
			document.payoff.submit();
		} else if (document.newsmod) {
			document.newsmod.submit();
		} else if (document.work) {
			document.work.submit();
		}
	}
	
	/**
	 * Submit image adding form in round
	 * @param  Number  Id
	 */
	this.imageAdd = function(pId) {
		var addIdInput = main.getElt("imageadd");
		addIdInput.value = pId;
		if(document.roundmodify) {
			document.roundmodify.submit();
		} else if (document.newsmod) {
			document.newsmod.submit();
		}
	}
	
	/**
	 * Submit file deleting form in news or round
	 * @param  Number  Id
	 */
	this.fileDel = function(pId) {
		var delIdInput = main.getElt("filedel");
		delIdInput.value = pId;
		if(document.newsmodify) {
			document.newsmodify.submit();
		} else if (document.roundmodify) {
			document.roundmodify.submit();
		} else if (document.payoff) {
			document.payoff.submit();
		} else if (document.newsmod) {
			document.newsmod.submit();
		} else if (document.work) {
			document.work.submit();
		}
	}
	
	/**
	 * Submit deleting form width selected value in program list
	 * @param  Number  Id
	 */
	this.programListDel = function(pId) {
		var delIdInput = main.getElt("programlistdel");
		delIdInput.value = pId;
		document.programlist.submit();
	}
	
	/**
	 * Show and hide list
	 * @param  Number  Table tbody id to show
	 * @param  String  Type name
	 */
	this.showHideList = function(pId, pType) {
		var showTbody = main.getElt(pType + pId.toString());
		var showHref = main.getElt(pType + "href" + pId.toString());
		if(!showTbody) {
			return;
		}
		showTbody.className = "";
		showHref.innerHTML = "";
		if(lastOpenedList != null) {
			var hideTbody = main.getElt(pType + lastOpenedList.toString());
			var hideHref = main.getElt(pType + "href" + lastOpenedList.toString());
			hideTbody.className = "synthesisCity";
			hideHref.innerHTML = "Megnyit";
		}
		lastOpenedList = pId;
	}
	
	/**
	 * Set reservation
	 * Use Ajax request, callback: this.reservationCallback();
	 * @param  Object  pObj   Checkbox
	 * @param  String  pDate  Date to set
	 */
	this.reservation = function(pObj, pDate) {
		if(!pObj) {
			return false;
		}
		var checked = pObj.checked;
		ajax.send('POST', '../php/ajax/AjaxReservation.php', 'func=reservation&checked=' + checked + '&rdate=' + pDate, 'adminCommon.reservationCallback');
	}
	
	/**
	 * Callback for set reservation
	 * @param  String  Ajax response text
	 */
	this.reservationCallback = function(pResponse) {
		if(pResponse.indexOf('Error') != -1 || pResponse.indexOf('error') != -1) {
			alert(pResponse);
			return;
		}
		var response = pResponse.split('|');
		var rid = response[0];
		var rclassname = response[1];
		var td = main.getElt(rid);
		td.className = rclassname;
	}
	
	/**
	 * Initializes AdminCommon
	 */
	this.init = function(pArea) {
		if(pArea != "admin") {
			return;
		}
	}					
}/**
 * Ajax module
 * @constructor
 * @author   Sperg Ádám
 * @version  2.04
 * TODO CHECK http://www.brainjar.com/dhtml/ajax/default4.asp
 */
function Ajax() {
	
	/**
	 * Status code:
	 * 200 : "OK." 
	 * 404 : "Not Found"
	 * 500 : "Server Error."
	 */
	
	/**
	 * Pointer to this
	 * @type  Ajax
	 */
	var self = this;
	
	/**
	 * Loader gif directory and base filename
	 * @type  String
	 */
	this.loader = picture.imgDir + 'ajax/loader';

	/**
	 * Encodes special characters
	 * @param   String  pText  Text to encode
	 * @return  String  Encoded text
	 */
	function encodeSpecialChars(pText) {
		text = new String(pText);
		text = text.replace(/"/g, "*#22*");
		text = text.replace(/'/g, "*#27*");
		text = text.replace(/`/g, "*#27*");
		text = text.replace(/´/g, "*#27*");
		text = text.replace(/‘/g, "*#27*");
		text = text.replace(/’/g, "*#27*");
		text = text.replace(/,/g, "*#2c*");
		text = text.replace(/\{/g, "*#7b*");
		text = text.replace(/\}/g, "*#7d*");
		text = text.replace(/\[/g, "*#5b*");
		text = text.replace(/\]/g, "*#5d*");
		text = text.replace(/\\/g, "*#5c*");
		return text;
	}
	
	/**
	 * Decodes special characters
	 * @param   String  pText  Text to decode
	 * @return  String  Decoded text
	 */
	function decodeSpecialChars(pText) {
		text = new String(pText);
		text = text.replace(/\*#28\*/g, "(");
		text = text.replace(/\*#29\*/g, ")");
		text = text.replace(/\*#5b\*/g, "[");
		text = text.replace(/\*#5d\*/g, "]");
		text = text.replace(/\*#22\*/g, "\"");
		text = text.replace(/\*#27\*/g, "'");
		text = text.replace(/\*#2c\*/g, ",");
		text = text.replace(/\*#7b\*/g, "{");
		text = text.replace(/\*#7d\*/g, "}");
		text = text.replace(/\*\#\*/g, " ");
		text = text.replace(/\*#5c\*/g, "\\");
		text = text.replace(/\*#3b\*/g, ";");
		return text;
	}
	
	/**
	 * Sends Ajax request
	 * @param  String  pMethod  Sending method
	 * @param  String  pPage    
	 * @param  String  pData
	 * @param  String  pTodo
	 */
	this.send = function(pMethod, pPage, pData, pTodo) {
		var xmlHttpRequest = self.createXmlHttpRequest();
		var method = pMethod.toUpperCase() == "GET" ? "GET"
				: pMethod.toUpperCase() == "POST" ? "POST"
				: null;
		if(method == null) {
			return;
		}
		if(pPage == null || typeof(pPage) == "undefined") {
			return;
		}
		var page = method == "POST" ? pPage
				: pData != null && typeof(pData) != "undefined" ? pPage + "?" + encodeSpecialChars(pData)
				: pPage;
		xmlHttpRequest.open(method, page, true);
		xmlHttpRequest.onreadystatechange = function() {
			if(xmlHttpRequest.readyState == 4) {
				if(xmlHttpRequest.status == 200) {
					if(pTodo != null && typeof(pTodo) != "undefined") {
						var response = decodeSpecialChars(xmlHttpRequest.responseText);
						eval(pTodo)(response);
					}
				} else {
					alert('Ajax.send(): status = ' + xmlHttpRequest.status + ', statusText = ' + xmlHttpRequest.statusText);
				}
			}
		}
		if(method == "POST") {
			xmlHttpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=ISO-8859-2');
			xmlHttpRequest.send(encodeSpecialChars(pData));
		} else {
			xmlHttpRequest.send(null);
		}
	}
	
	/**
	 * Creates and returns a new XMLHttpRequest object
	 * @return  XMLHttpRequest object
	 */
	this.createXmlHttpRequest = function() {
		var xmlHttpRequestMsProgIds = new Array("Msxml2.XMLHTTP.7.0", 
												"Msxml2.XMLHTTP.6.0", 
												"Msxml2.XMLHTTP.5.0", 
												"Msxml2.XMLHTTP.4.0", 
												"MSXML2.XMLHTTP.3.0", 
												"MSXML2.XMLHTTP", 
												"Microsoft.XMLHTTP");
		if(window.XMLHttpRequest != null)
			xmlHttpRequest = new window.XMLHttpRequest();
		else if(window.ActiveXObject != null) {
			var success = false;
			for(var i = 0; i < xmlHttpRequestMSProgIds.length && !success; i++) {
				try {
					xmlHttpRequest = new ActiveXObject(xmlHttpRequestMSProgIds[i]);
					success = true;
				} catch(e) {}
			}
		}
		if(!xmlHttpRequest) {
			alert("AjaxManager.createXmlHttpRequest(): Couldn't create XMLHttpRequest object.");
		}
		return xmlHttpRequest;
	}
	
	/**
	 * Show loader image in specified div
	 * @param  String   pDiv     Div id
	 * @param  Boolean  pCenter  Need the image in centered position
	 * @param  String   pName    Set it if need other then default gif
	 */
	this.showLoader = function(pDiv, pCenter, pName) {
		if(!pDiv) {
			return false;
		}
		var div = engine.main.getElt(pDiv);
		if(!div) {
			return false;
		}
		var loaderGif = pName && typeof pName != 'undefined' ? this.loader + pName : this.loader;
		if(pCenter) {
			div.innerHTML = '<center><img src="' + loaderGif + '.gif"></center>';
		} else {
			div.innerHTML = '<img src="' + loaderGif + '.gif">';
		}
	}
}/**
 * Main module
 * @constructor
 * @author   Sperg Ádám
 * @version  1.03, 08/28/08
 */
function Main() {
	
	/**
	 * Pointer to this
	 * @type  Main
	 */
	var self = this;
	
	/**
	 * Site base url
	 * @type  String
	 */
	this.baseUrl = "http://varpalotakisterseg.hu/";
	
	/**
	 * Browser types data
	 * @type  Array
	 */
	var browserTypesData = [{string: navigator.userAgent, subString: "OmniWeb", versionSearch: "OmniWeb/", identity: "OmniWeb"}, 
							{string: navigator.vendor, subString: "Apple", identity: "Safari"}, 
							{prop: window.opera, identity: "Opera"}, 
							{string: navigator.vendor, subString: "iCab", identity: "iCab"}, 
							{string: navigator.vendor, subString: "KDE", identity: "Konqueror"}, 
							{string: navigator.userAgent, subString: "Firefox", identity: "Firefox"}, 
							{string: navigator.vendor, subString: "Camino", identity: "Camino"}, 
							{string: navigator.userAgent, subString: "Netscape", identity: "Netscape"}, 
							{string: navigator.userAgent, subString: "MSIE", identity: "Explorer", versionSearch: "MSIE"}, 
							{string: navigator.userAgent, subString: "Gecko", identity: "Mozilla", versionSearch: "rv"}, 
							{string: navigator.userAgent, subString: "Mozilla", identity: "Netscape", versionSearch: "Mozilla"}];
	
	/**
	 * OS types data
	 * @type  Array
	 */
	var osTypesData = [{string: navigator.platform, subString: "Win", identity: "Windows"}, 
					   {string: navigator.platform, subString: "Mac", identity: "Mac"}, 
					   {string: navigator.platform, subString: "Linux", identity: "Linux"}];
	
	/**
	 * Browser version search string
	 * @type  String
	 */
	var browserVersionSearchString = "";
	
	/**
	 * Browser type
	 * @type  String
	 */
	this.browserType = searchBrowserOrOsType(browserTypesData) || "unknown";
	
	/**
	 * Browser version
	 * @type  String
	 */
	this.browserVersion = searchBrowserVersion(navigator.userAgent) || searchBrowserVersion(navigator.appVersion) || "unknown";
	
	/**
	 * OS Type
	 * @type  String
	 */
	this.osType = searchBrowserOrOsType(osTypesData) || "unknown";
	
	/**
	 * Search browser or os type
	 * @param   Array   pData  Browser or os type data
	 * @return  String  Browser or os type
	 */
	function searchBrowserOrOsType(pData) {
		for(var i=0; i < pData.length; i++) {
			var dataString = pData[i].string;
			var dataProp = pData[i].prop;
			browserVersionSearchString = pData[i].versionSearch || pData[i].identity;
			if(dataString) {
				if(dataString.indexOf(pData[i].subString) != -1) {
					return pData[i].identity;
				}
			} else if (dataProp) {
				return pData[i].identity;
			}
		}
	}
	
	/**
	 * Search browser version
	 * @param   String  pDataString  Browser userAgent or appVersion
	 * @return  String  Browser version
	 */
	function searchBrowserVersion(pDataString) {
		var index = pDataString.indexOf(browserVersionSearchString);
		if (index == -1) {
			return;
		}
		return parseFloat(pDataString.substring(index + browserVersionSearchString.length + 1));
	}
	
	/**
	 * Get element by id
	 * @param   String  pId  DOM element id
	 * @return  Object  DOM element
	 */
	this.getElt = function(pId) {
		return (document.getElementById ? document.getElementById(pId)
			: document.all ? document.all[pId]
			: null);
	}
	
	/**
	 * Get location parameter
	 * @parma   String  pParam  Parameter to find value
	 * @return  String  Parameter value
	 */
	this.getUrlParam = function(pParam) {
		pParam = pParam.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		var regexS = "[\\?&]" + pParam + "=([^&#]*)";
		var regex = new RegExp(regexS);
		var results = regex.exec(window.location.href);
		if(results == null) {
			return "";
		} else {
			return results[1];
		}
	}
	
	/**
	 * Is value in array
	 * @param   Array    pArray    Array where need to found element
	 * @param   String   pElement  Element to found
	 * @return  Boolean  true if found,
	 * 					 false if not
	 */
	this.inArray = function(pArray, pElement) {
		for(var i = 0; i < pArray.length; i++) {
			if(pArray[i] == pElement) {
				return true;
			}
		}
		return false;
	}
}/**
 * Picture module
 * @constructor
 * @author   Sperg Ádám
 * @version  1.01, 08/18/08
 */
function Picture() {

	/**
	 * Pointer to this
	 * @type  Header
	 */
	var self = this;
	
	/**
	 * Images directory
	 * @type  String
	 */
	var imgDir = main.baseUrl + "images/";
	
	/**
	 * Show backgraound transparency in Internet Explorer for PNG files
	 * @param  String  pEltId  DOM element id to change background
	 */
	this.ieAlphaImage = function(pEltId) {
		var element = main.getElt(pEltId);
		var backgroundImg = element.style.backgroundImage.substring(4, element.style.backgroundImage.length - 1);
		element.style.backgroundImage = "none";
		element.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + backgroundImg + "', sizingMethod='scale')";
	}
}/**
 * Frame module
 * @constructor
 * @author   Sperg Ádám
 * @version  1.01, 08/18/08
 */
function Frame() {

	/**
	 * Pointer to this
	 * @type  Header
	 */
	var self = this;
	
	/**
	 * Show backgraound transparency in Internet Explorer for PNG borders
	 */
	this.init = function() {
		if(main.browserType == "Explorer" && main.browserVersion >= 5.5) {
			main.getElt("frameBrdLeft").style.backgroundImage = "url('images/frame/frameBrdLeft.png')";
			picture.ieAlphaImage("frameBrdLeft");
			main.getElt("frameBrdLeft").style.backgroundPosition = "left top";
			main.getElt("frameBrdRight").style.backgroundImage = "url('images/frame/frameBrdRight.png')";
			picture.ieAlphaImage("frameBrdRight");
		}
	}
}/**
 * Header module
 * @constructor
 * @author   Sperg Ádám
 * @version  1.03, 08/28/08
 */
function Header() {

	/**
	 * Pointer to this
	 * @type  Header
	 */
	var self = this;
	
	/**
	 * Header images directory
	 * @type  String
	 */
	var imgDir = main.baseUrl + "images/header/";
	
	/**
	 * Header images file names
	 * @type  Array
	 */
	var imgNames = new Array("berhida", "osku", "osi", "varpalotaleft", "varpalotacenter", "varpalotaright", "tes", "jasd", "petfurdo");
	
	/**
	 * Header images extension
	 * @type  String
	 */
	var imgExt = ".jpg";

	/**
	 * Header images Array
	 * @type  Array
	 */
	var images = new Array();
	
	/**
	 * Content names
	 * @type  Array
	 */
	var contentNames = new Array("berhida", "osku", "osi", "varpalota", "tes", "jasd", "petfurdo");
	
	/**
	 * Content id-s
	 * @type Array
	 */
	var contentIds = new Array(0, 1, 2, 4, 6, 7, 8);

	/**
	 * Actual header image id
	 * @type  Array
	 */
	var actualImg = {id: "header4"};
	
	/**
	 * Change header to selected 
	 * @param  String  pChangeTo  DOM element
	 */
	this.change = function(pChangeTo) {
		if(!pChangeTo) {
			self.change(actualImg);
			return;
		}
		var changeTo = pChangeTo.id;
		var header = main.getElt("header");
		header.style.backgroundImage = "url(" + images[changeTo].src + ")";
	}
	
	/**
	 * Initializes Header
	 */
	this.init = function(pArea) {
		if(main.browserType == "Explorer" && main.browserVersion >= 5.5) {
			main.getElt("headerBrd").style.backgroundImage = "url('images/header/headerShadow.png')";
			picture.ieAlphaImage("headerBrd");
		}
		if(pArea == 'admin') {
			return;
		}
		for(var i = 0; i < imgNames.length; i++) {
			var newImg = new Image(765, 100);
			newImg.src = imgDir + imgNames[i] + imgExt;
			images["header" + i] = newImg;
		}
		if(main.getUrlParam("c") != "" && main.inArray(contentNames, main.getUrlParam("c"))) {
			for(var i = 0; i < contentNames.length; i++) {
				if(contentNames[i] == main.getUrlParam("c")) {
					actualImg.id = "header" + contentIds[i];
					break;
				}
			}
		}
		self.change();
	}					
}

