var ajax = {
	history: [],
	historyIndex: 0,
	show_loader: '',
	run: function(php, id, act, property, session_id, request_id, use_history){
		var requestString = 'req=' + (AJAX_HTTP_KEY ? encodeURIComponent(ajax.rc4(AJAX_HTTP_KEY, php)) : php) + '<!AJAX!>' + session_id + '<!AJAX!>' + (request_id || '');
		requestString += '&rnd=' + Math.random();
		ajax.sendRequest(id, act, property, requestString);
	},
	sendRequest: function(id, act, property, requestString){
		if (ajax.show_loader != '') AJAX_SHOW_LOADER = ajax.show_loader;
		var update = (id) ? $(id) : $(document.body);
		var loader = (id) ? '/images/loader-small.gif' : '/images/loader-big.gif';
		var duration = (id) ? 500 : 0;
		var loader_width = (id) ? '32' : '66';
		var loader_height = (id) ? '32' : '66';
		var loader_overlay = (id) ? '#fff' : '#000';
		var options_hw = (id) ? '' : '100%';
		var req = new Request.LOAD({
			method: 'post',
			url: AJAX_PATH + 'process.php',
			useWaiter: AJAX_SHOW_LOADER,
			waiterTarget: update,
			waiterOptions: {
				containerProps: {styles: {position: 'absolute', 'text-align': 'left'}},
				img: {src: loader, styles: {width: loader_width, height: loader_height, opacity: 1}},
				layer: {styles: {position: 'absolute', display: 'block', left: 0, top: 0, opacity: 0.5, background: loader_overlay}, 'height': options_hw, 'width': options_hw},
				useIframeShim: true,
				fxOptions: {duration: duration}
			},
			headers: {'Content-type': 'application/x-www-form-urlencoded', 'X-Requested-With': 'XMLHttpRequest', 'X-Community-Version': '1.0', 'Accept': 'text/javascript, text/html, application/xml, text/xml, */*'},
			onSuccess: function(responseText, responseXML){
				if (responseText != 'process_error'){
					actions = responseText.split('<ajax_split>');
					if (id) ajax.put(actions[0], id, act, property);
					if (actions[1]) eval(actions[1]);
				}else{
					if (AJAX_SHOW_LOADER) alert(AJAX_UNKNOWN_ERROR);
					window.location.reload();
				}
			}
		}).send(requestString);
	},
	put: function(content, id, act, property){
		if (!id) return;
		if (property){
			try {
				var ob = document.getElementById(id);
			}
			catch(e){}

			if (act == 'p'){
				ob[property] = content + ob[property];
			}else if(act == 'a'){
				ob[property] += content;
			}else if(property.split('.')[0] == 'style'){
				ob.style[property.split('.')[1]] = content;
			}else if(ob){
				ob[property] = content;
			}
		}else{
			window[id] = content;
		}
	},
	get: function(id, property){
		if (!property) return ajax.phpSerialize(id);
		return ajax.phpSerialize(document.getElementById(id)[property]);
	},
	phpSerialize: function(v){
		var ret = '';
		if (typeof(v) == 'object'){
			var len = v.length;
			if (len){
				ret = 'a:' + len + ':{';
				for (var i = 0; i < len; i++){
					ret += 'i:' + i +';'
					ret += 's:' + (v[i]+'').length + ':"' + encodeURIComponent(v[i]) +'";'
				}
				ret += '}';
			}else{
				len = 0;
				for (var i in v){
					len++;
				}
				ret = 'a:' + len + ':{';

				for (var i in v){
					ret += 's:' + i.length + ':"' + i +'";'
					ret += 's:' + v[i].length + ':"' + encodeURIComponent(v[i]) +'";'
				}
				ret += '}';
			}
		}else{
			ret += 's:' + (v+'').length + ':"' + encodeURIComponent(v) +'";'
		}
		return ret;
	},
	setStyle: function(ob, styleString){
		document.getElementById(ob).style.cssText = styleString;
	},
	rc4: function(pwd, data){
		var pwd_length = pwd.length;
		var data_length = data.length;
		var key = [];
		var box = [];
		var cipher = '';
		var k;
		for (var i = 0; i < 256; i++){
			key[i] = pwd.charCodeAt(i % pwd_length);
			box[i] = i;
		}

		for (var j = i = 0; i < 256; i++){
			j = (j + box[i] + key[i]) % 256;
			tmp = box[i];
			box[i] = box[j];
			box[j] = tmp;
		}

		for (var a = j = i = 0; i < data_length; i++){
			a = (a + 1) % 256;
			j = (j + box[a]) % 256;
			tmp = box[a];
			box[a] = box[j];
			box[j] = tmp;
			k = box[((box[a] + box[j]) % 256)];
			cipher += String.fromCharCode(data.charCodeAt(i) ^ k);
		}
		return cipher;
	},
	getForm: function(f){
		var vals = {};
		for (var i = 0; i < f.length; i++){
			if (f[i].id){
				vals[f[i].id] = f[i].value;
			}
		}
		return vals;
	},
	addHistory: function(value){
		if (!document.getElementById('ajaxHistory')){
			var iframe = new Element('iframe', {id: 'ajaxHistory', name: 'ajaxHistory', src: AJAX_PATH + "ajax.php/1/", styles: {'position': 'absolute', 'top': '-1000px'}}).inject(document.body).contentWindow;
		}
		ajax.historyIndex = -1;
		ajax.history = [];
		ajax.ajaxHistory = window.frames["ajaxHistory"];
		ajax.lastHash = ajax.ajaxHistory.location.hash;

		var historyURL1 = AJAX_PATH + 'ajax.php/1/';
		var historyURL2 = AJAX_PATH + 'ajax.php/2/';
		ajax.historyValue = value;
		ajax.lastHistoryURL = ajax.lastHistoryURL == historyURL2 ? historyURL1 : historyURL2;
		ajax.ajaxHistory.location.href = ajax.lastHistoryURL + '#' + (ajax.historyIndex - 0 + 1);
		window.addEvent('load', function(){
			ajax.monitorHistory();
		});
	},
	monitorHistory: function(){
		ajax.lastHistoryURL = ajax.ajaxHistory.location.pathname;
		var currentHash = ajax.ajaxHistory.location.hash;
		var index = currentHash.split('#')[1];
		if (ajax.lastHash != currentHash){
			if (ajax.historyValue == null && index < ajax.history.length){
				ajax.historyIndex = index;
			}else{
				if (ajax.historyIndex < ajax.history.length - 1){
					ajax.history.splice(ajax.historyIndex - 0 + 1, ajax.history.length - ajax.historyIndex - 1);
				}
				ajax.historyIndex = ajax.history.length;
				ajax.history[ajax.historyIndex] = ajax.historyValue;
			}

			if (!currentHash && ajax.history.length && ajax.onBackToStart){
				ajax.onBackToStart();
			}else if (ajax.historyValue != ajax.history[index]){
				ajax.onHistoryChange(index);
			}
			ajax.lastHash = currentHash;
			ajax.historyValue = null;
		}
		ajax.historyInterval = window.setTimeout(ajax.monitorHistory, 0);
	},
	onHistoryChange: function(i){
		ajax.history[i]();
	}
}