﻿function Ajax(){
	this.XHR = createXHR();
	this.contentHandler = null;
	this.contentHandlerPath = null;
	this.XHR.target = new String();
	this.XHR.returnInnerHTML = true;
	this.XHR.showLoader = true;
	this.stateVar = "nothing";
	this.loaded = true;
	
	this.XHR.onreadystatechange=function() {
		if(this.readyState==4) {
			if (this.status == 200 || this.status == 304){
				if(this.returnInnerHTML) {
					//alert(document.getElementById(this.target));
					//alert(this.responseText);
					if(this.showLoader)
						this.loadingSwitch("show");
					
					document.getElementById(this.target).innerHTML = this.responseText;
					
					var textareas = document.getElementsByTagName("textarea");
					if (textareas.length > 0) {
						for (i=0; i<textareas.length;i++){
							if(textareas[i].className == "htmleditor") {
								var oFCKeditor = new FCKeditor( textareas[i].id ) ;
								oFCKeditor.BasePath = "../library/javascript/Fckeditor/";
								oFCKeditor.Width = 685;
								oFCKeditor.Height = 800;
								oFCKeditor.ReplaceTextarea() ;
							}
						}
					}
										
					runScripts(document.getElementById(this.target));
				} else {
					//alert(this.responseText);
					this.loadingSwitch("show");
					
					this.returnInnerHTML = true;
					Registry.refresh();
				}
			} else if(this.status == 404) {
				document.getElementById(this.target).innerHTML = "404";
			} else if(this.status == 500) {
				document.getElementById(this.target).innerHTML = this.responseText;
			} else if(this.status == 0) {
				void(0);
			} else {
				alert("Hiba történt: " + this.statusText);
			}
		}
	};
	
	this.setDefaultTarget = function(aTarget) {
		this.XHR.target = aTarget;
	};
	
	this.setContentHandler = function(aContentHandler,aContentHandlerPath) {
		this.contentHandler = aContentHandler;
		this.contentHandlerPath = aContentHandlerPath;
	};
	
	this.open = function(aMethod, aUrl, aAsync) {
		this.XHR.open(aMethod, aUrl, aAsync);
	};
	
	this.send = function(aBody) {
		this.XHR.send(aBody);
	};
	
	this.request = function(aUrl, aMethod, aAsync, aBody) {
		if(this.XHR.showLoader)
			this.XHR.loadingSwitch("hide");
		
		aMethod = typeof(aMethod) != 'undefined' ? aMethod : "GET";
		aAsync = typeof(aAsync) != 'undefined' ? aAsync : true;
		aBody = typeof(aBody) != 'undefined' ? aBody : null;
		
		this.open(aMethod, aUrl, aAsync);
		this.send(aBody);
	};
	
	this.load = function(aVariable, aValue, aTarget) {
		//this.XHR.returnInnerHTML = true;
		//this.XHR.target = typeof(aTarget) != 'undefined' ? aTarget : "target";
		if (aValue != 'news_older' && aValue != 'news_newer') {
			unFocus.History.addHistory(aVariable+"/"+aValue+"/"+aTarget);
		} else {
			this.request(this.contentHandlerPath+this.contentHandler + '?' + aVariable + '=' + aValue);
		}
	};
	
	this.init = function(aVariable, aValue, aTarget) {
		this.XHR.returnInnerHTML = true;
		this.XHR.target = typeof(aTarget) != 'undefined' ? aTarget : "target";
		this.request(this.contentHandlerPath+this.contentHandler + '?' + aVariable + '=' + aValue);
	};
	
	this.refresh = function() {
		this.XHR.returnInnerHTML = true;
		this.request(this.contentHandlerPath+this.contentHandler);
	};
	
	this.submit = function(form) {
		this.XHR.returnInnerHTML = false;
		
		if(this.XHR.showLoader)
			this.XHR.loadingSwitch("hide");
		
		var body = this.createPostRequestBody(form);
		
		this.open("post", form.action, true);
		this.XHR.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		this.send(body);
	};
	
	this.XHR.loadingSwitch = function(action) {
		var loading = document.getElementById("loading");
		var aHide = document.getElementsByTagName("div");
		
		switch(action) {
		case "hide":
			loading.style.display = "block";
			for (var i=0; i < aHide.length; i++) {
				if (aHide[i].className == "hide")
					aHide[i].style.display = "none";
			}
			break;
			
		case "show":
			loading.style.display = "none";
			for (var i=0; i < aHide.length; i++) {
				if (aHide[i].className == "hide")
					aHide[i].style.display = "block";
			}
			break;
		}
	};
	
	this.encodeNameAndValue = function(aName, aValue) {
		var param = encodeURIComponent(aName);
		param += "=";
		param += encodeURIComponent(aValue);
		return param;
	};
	
	this.createPostRequestBody = function(aForm) {
		var params = new Array();
		for (var i = 0; i< aForm.elements.length; i++) {
			var field = aForm.elements[i];
			
			switch (field.type) {
			case "button":
			case "submit":
			case "reset":
				break;
				
			case "checkbox":
			case "radio":
				if(!field.checked) {
					break;
				}
				
			case "text":
			case "hidden":
			case "password":
				params.push(this.encodeNameAndValue(field.name, field.value));
				break;
				
			default:
				switch(field.tagName.toLowerCase()) {
				case "select":
					params.push(this.encodeNameAndValue(field.name, field.options[field.selectedIndex].value));
					break;
					
				default:
					if (field.className=='htmleditor') {
						params.push(this.encodeNameAndValue(field.name, FCKeditorAPI.GetInstance(field.id).GetHTML(true)));
					} else {
						params.push(this.encodeNameAndValue(field.name, field.value));
					}
					break;
				}
			}
		}
		
		return params.join("&");
	};
	
	this.historyListener = function(historyHash) {
		if (historyHash == "") {
			historyHash = "content/home/content";
		}
		historyHashArray = historyHash.split("/");
		try {
			ajax.init(historyHashArray[0],historyHashArray[1],historyHashArray[2]);
		} catch(e) {
		}
		
		this.stateVar = historyHash;
	};
	
	unFocus.History.addEventListener('historyChange', this.historyListener);
	
	this.historyListener(unFocus.History.getCurrent());
};

function createXHR() {
	if (typeof XMLHttpRequest != "undefined") {
		return new XMLHttpRequest();
	} else if (windows.ActiveXObject) {
		var aVersions = ["MSXML2.XMLHttp.6.0", "MSXML2.XMLHttp.3.0"];
		
		for (var i = 0; i < aVersions.length; i++) {
			try {
				var oXHR = new ActiveXObject(aVersions[i]);
				return oXHR;
			} catch(oError) {
				//DO NOTHING
			}
		}
	}
	throw new Error("Sajnos az oldal nem kompatibilis az ön böngészöjével!");
};

function runScripts(e) {
	if (e.nodeType != 1) return; //if it's not an element node, return
	
	if (e.tagName.toLowerCase() == 'script') {
		eval(e.text); //run the script
	}
	else {
		var n = e.firstChild;
		while ( n ) {
			if ( n.nodeType == 1 ) runScripts( n ); //if it's an element node, recurse
			n = n.nextSibling;
		}
	}
};
