// :: display
// displayIndicator
function displayIndicator(indicator)
{
	var div = $(indicator);
	if (div) {
		div.style.display = "block";
	}
}
// displayIndicators
function displayIndicators(indicators)
{
	if (typeof(indicators) == "object") {
		var length = indicators.length;
		for (var i=0; i<length; i++) {
			displayIndicator(indicators[i]);
		}
	}
}
// hideIndicator
function hideIndicator(indicator)
{
	var div = $(indicator);
	if (div) {
		//new Rico.Effect.FadeTo(indicator,0,500,4);
		div.style.display = "none";
	}
}
// hideIndicators
function hideIndicators(indicators)
{
	if (typeof(indicators) == "object") {
		var length = indicators.length;
		for (var i=0; i<length; i++) {
			hideIndicator(indicators[i]);
		}
	}
}
// :: tools
function strToJSON(str)
{
	var temp = {};
	eval("temp = " + $('json_accounts').innerHTML);
	if (typeof(temp) == "object") {
		return temp;
	}
	else {
		return false;
	}
}
// :: calls
// ajaxcall
function ajaxElementUpdatersSuccess(obj)
{
	if (obj.status == "200")
	{
		var xml = obj.responseXML;
		var pouet = xml.firstChild.childNodes[1];
		//var length = ;
		var response = xml.getElementsByTagName("response");
		var length = response.length;
		for (var i=0;i<length;i++) {
			var temp = response[i];
			var type = temp.getAttribute("type");
			if (type) {
				if (temp.textContent) {
					var id = temp.getAttribute("id");
					if ($(id)) {
						$(id).innerHTML = temp.textContent;
					}
				}
			}
		}
	}
}
function ajaxElementUpdaters(serverLocation, indicators, options)
{
	// indicators
	displayIndicators(indicators);
	// ajax call
	var callback = options['onComplete'];
	var temp = "pouet";
	options['onComplete'] = function (t) {
		ajaxElementUpdatersSuccess(t, temp);
		callback();
	};
	new Ajax.Request(location.protocol+ '//' + window.location.host + serverLocation, options);
}
function ajaxElementUpdater(serverLocation, indicator, element, options) 
{
	new Ajax.Updater(element, serverLocation, options);
}
// :: old
// ajaxGetRequestId
/*
function ajaxGetRequestId() 
{
    var length=8;
    var sPassword = "";
     
    for (i=0; i < length; i++) 
    {
        numI = getRandomNum();
        while (checkPunc(numI)) { numI = getRandomNum(); }
        sPassword = sPassword + String.fromCharCode(numI);
    } 
    return sPassword;
}
*/
// getRandomNum
/*
function getRandomNum()
{    
    // between 0 - 1
    var rndNum = Math.random()
    // rndNum from 0 - 1000    
    rndNum = parseInt(rndNum * 1000);
    // rndNum from 33 - 127        
    rndNum = (rndNum % 94) + 33;           
    return rndNum;
}
*/
// checkPunc
/*
function checkPunc(num) 
{
    if ((num >=33) && (num <=47)) { return true; }
    if ((num >=58) && (num <=64)) { return true; }    
    if ((num >=91) && (num <=96)) { return true; }
    if ((num >=123) && (num <=126)) { return true; }
    return false;
}
*/
/*
function ajaxcall2(serverLocation, indicator, element, options)
{
	// variables
	if (options == undefined) options = "";
	// object instantiation
	var ajaxEngine = new Rico.AjaxEngine();
	// register request
	var requestId = ajaxGetRequestId();
	ajaxEngine.registerRequest(requestId, serverLocation);
	// display indicator
	if (typeof indicator == "object")
	{
		var length = indicator.length;
		for (var i=0;i<length;i++)
		{
			displayIndicator(indicator[i]);
		}
	}
	// register elements
	if (typeof element == "object")
	{
		var length = element.length;
		for (var i=0;i<length;i++)
		{
			ajaxEngine.registerAjaxElement(element[i]);
		}
	}
	//
	ajaxEngine.(requestId, options);
}
*/
// :: kAjax object
// - object constructor
function kAjax() {
	var serverLocation = "";
	var indicators     = "";
	var options        = "";
}
// - object definition
kAjax.prototype = {
	addIndicators: function (indicators) {
		// variables
		if (indicators.length > 0) {
			for (var i in indicators) {
				if ($(indicators[i])) {
					var el = $(indicators[i]);
					try {
						var width  = parseInt(el.getStyle('width'));
						var height = parseInt(el.getStyle('height'));
						var paddingLeft   = parseInt(el.getStyle('paddingLeft'));
						var paddingRight  = parseInt(el.getStyle('paddingRight'));
						var paddingTop    = parseInt(el.getStyle('paddingTop'));
						var paddingBottom = parseInt(el.getStyle('paddingBottom'));
					}
					catch (e) {}
					// add opac frame and modify its original position (padding correction)
					var frameName = 'ajax_frame_'+indicators[i];
					var frameHtml = '<div id="'+frameName+'" style="background-color: #ADD8E6; opacity: 0.5; position: absolute; width: '+(width+paddingLeft+paddingRight)+'px;height: '+(height+paddingTop+paddingBottom)+'px"></div>';
					try {
						el.insertAdjacentHTML("AfterBegin", frameHtml);
					}
					catch (e) {}
					if ($(frameName)) $(frameName).setStyle({top: (parseInt($(frameName).getStyle('top'))-paddingTop)+'px', left: (parseInt($(frameName).getStyle('left'))-paddingLeft)+'px'});
					// add ajax loader
					var imageHtml = '<img src="file.php?f=ajax-loader.gif" style="position:absolute" />';
					try {
						el.insertAdjacentHTML("AfterBegin", imageHtml);
					}
					catch (e) {}
				}
				else
				{
					//console.log("element '" + indicators[i] + "' does not exists");
				}
			}
		}
		else
		{
			//console.log("no indicator");
		}
	},
	call: function (serverLocation, indicators, options) {
		// - setters
		// options
		var callback = options['onComplete'];
		options['onComplete'] = function (t) {
			kAjax.callSuccess(t);
			callback(t);
		};
		this.set(serverLocation, indicators, options);
		// - ajax call
		this.request();
	},
	callSuccess: function (obj) {
		if (obj.status == "200") {
			var xml = obj.responseXML;
			var length = 0;
			if (xml) {
				var response = xml.getElementsByTagName("response");
				length = response.length;
			}
			var toReturn = new Object;
			for (var i=0;i<length;i++) {
				var tagResponse = response[i];
				var type = tagResponse.getAttribute("type");
				if (type) {
					
					if (tagResponse.textContent != undefined) {
						var textContent = tagResponse.textContent;
                    }
                    else {
                       var textContent = tagResponse.text;
                    }
					if (textContent) {
						var id = tagResponse.getAttribute("id");
						toReturn[id] = eval("(" +textContent+ ")");
					}
				}
			}
			// return result
			if (length == 1){
				for (var i in toReturn) {
					var toReturn = toReturn[i];
				}
			}
			obj.kJSON = toReturn;
		}
	},
	elementUpdaters: function (serverLocation, indicators, options, executeJS)
	{
		// - setters
		if (executeJS == undefined) executeJS = false;
		// options
		var callback = options['onComplete'];
		options['onComplete'] = function (t) {
			kAjax.elementUpdatersSuccess(t, executeJS);
			callback();
		};
		this.set(serverLocation, indicators, options);
		// - ajax call
		this.request();
	},
	elementUpdaters2: function (serverLocation, indicators, options, executeJS)
	{
		//console.log("enter in elementUpdaters2");
		// - setters
		if (executeJS == undefined) executeJS = false;
		// options
		var callback = options['onComplete'];
		options['onComplete'] = function (t) {
			kAjax.elementUpdatersSuccess(t, executeJS);
			callback();
		};
		this.set(serverLocation, indicators, options);
		// - ajax call
		this.request2();
	},
	elementUpdatersSuccess: function (obj, executeJS)
	{
		if (obj.status == "200") {
			var xml = obj.responseXML;
			var response = xml.getElementsByTagName("response");
			var length = response.length;
			for (var i=0;i<length;i++) {
				var tagResponse = response[i];
				var type = tagResponse.getAttribute("type");
				if (type) {
					if (tagResponse.textContent != undefined) {
						var textContent = tagResponse.textContent;
                    }
                    else {
                       var textContent = tagResponse.text;
                    }
					if (textContent) {
						var id = tagResponse.getAttribute("id");
						if ($(id)) {	
							// html
							if (executeJS) {
								// print response without js
								$(id).innerHTML = textContent.stripScripts();
								// execute scripts on the page
								var scripts = textContent.extractScripts(); // modified function of prototype
								if (scripts.length > 0) {
									this.executeScripts(scripts);
								}
							}
							else {
								$(id).innerHTML = textContent;
							}
						}
					}
				}
			}
		}
	},
	executeScripts: function (scripts) 
	{
		_this = this;
		if (scripts[0][0] == "external"){
			var filename = scripts[0][1];
			if (scripts.length > 1) {
				scripts.shift();
				new Ajax.Request(filename, { method: 'post', evalJS: true, onComplete:function(){_this.executeScripts(scripts)} });
			}
			else {
				new Ajax.Request(filename, { method: 'post', evalJS: true });
			}
		}
		else{
			try {
				global.eval(scripts[0]); // global comes from prototype.js
			}
			catch (e) {console.log(e)};
			if (scripts.length > 1) {
				scripts.shift();
				this.executeScripts(scripts);
			}
		}
	},
	request: function () {
		displayIndicators(this.indicators);
		new Ajax.Request(this.serverLocation, this.options);
	},
	request2: function () {
		this.addIndicators(this.indicators);
		new Ajax.Request(this.serverLocation, this.options);
	},
	set: function (serverLocation, indicators, options) {
		this.setServerLocation(serverLocation);
		this.setIndicators(indicators);
		this.setOptions(options);
	},
	setServerLocation: function(serverLocation) {
		if ((serverLocation != "undefined") && (serverLocation != null))
//			this.serverLocation = location.protocol+ '//' + window.location.host + window.location.pathname + serverLocation;
			this.serverLocation = location.protocol+ '//' + window.location.host + "/" + serverLocation;
	},
	setIndicators: function(indicators) {
		if ((indicators != "undefined") && (indicators != null)) {
			this.indicators = indicators;
		}
		else {
			this.indicators = "";
		}
	},
	setOptions: function(options) {
		if ((options != "undefined") && (options != null))
			this.options = options;
	}
}
kAjax = new kAjax();
