/**
 * 
 * 
 */

var RemoteHandler = new Class({
	initialize: function() {},

	executeSimpleRequestForText: function(url, successHandler, errorHandler) {
		new Ajax(this.fixUrl(url) + '&ajaxType=html', {
			onComplete: successHandler,
			onFailure: this.fixHandler(errorHandler),
			method: 'get'
		}).request();
	},
	executeSimpleRequestForJson: function(url, successHandler, errorHandler) {
		new Json.Remote(this.fixUrl(url) + '&ajaxType=json', {
			onComplete: successHandler,
			onFailure: this.fixHandler(errorHandler),
			method: 'get'
		}).send();
	},
	postSimpleFormForText: function(url, data, successHandler, errorHandler) {
		new Ajax(this.fixUrl(url) + '&ajaxType=html', {
			data: data,
			onComplete: successHandler,
			onFailure: this.fixHandler(errorHandler),
			method: 'post'
		}).request();
	},
	postSimpleFormForJson: function(url, form, successHandler, errorHandler) {
		form.ajaxType = 'json';
		new Ajax(url, {
			method: 'post',
			data: form,
			onComplete: function(text) {
				var jsonResult = null;
				try {
					jsonResult = Json.evaluate(text);
				}
				catch(e) {
				}
				this.successHandler(jsonResult);
			}.bind({
				successHandler: successHandler
			}),
			onFailure: function() {
				this.fixHandler(this.errorHandler)
			}.bind({fixHandler: this.fixHandler, errorHandler: errorHandler})
		}).request();
	},
	fixUrl: function(url) {
		if (url.indexOf('://') == -1)
			return UNIENCE_HOST + this.addNoCacheQuery(url);
		else
			return this.addNoCacheQuery(url);
	},
	fixHandler: function(handler) {
		if (!$defined(handler)) {
			return Class.empty;
		}
		else {
			return handler;
		}
	},
	addNoCacheQuery: function(url) {
		if (url.indexOf('?') >= 0) {
			return url + '&rnd=' + Math.random();
		}
		else {
			return url + '?' + Math.random();
		}
	}
});

Ajax = Ajax.extend({
    request: function(data){
    	//this.timeoutTimer = window.setTimeout(this.callTimeout.bindAsEventListener(this), AJAX_TIMEOUT);
		//this.addEvent('onComplete', this.removeTimer);
	    this.parent(data);
    },

    callTimeout: function () {
        this.transport.abort();
        this.onFailure();
        //if (this.options.onTimeout) {
            //this.options.onTimeout();
        //}
    },

    removeTimer: function() {
        window.clearTimeout(this.timeoutTimer);
    }
});


var remoteHandler = new RemoteHandler();

String.extend({
	evalScripts: function evalScripts(){
		var script, scripts;

		scripts = [];
	
		var regexp = /<script[^>]*>([\s\S]*?)<\/script>/gi;
		while ((script = regexp.exec(this))) scripts.push(script[1]);
	
		scripts = scripts.join('\n');

		if (scripts) (window.execScript) ? window.execScript(scripts) : window.setTimeout(scripts, 0);
	},
	escapeUTF8: function () {
		string = this.replace(/\r\n/g,"\n");
		var utftext = "";
	
		for (var n = 0; n < string.length; n++) {
			var c = string.charCodeAt(n);
	
			if (c == '%'.charCodeAt(0)) {
				utftext += '%25';
			}
			else if (c == '&'.charCodeAt(0)) {
				utftext += '%26';
			}
			else if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
		}
	
		return utftext;
	}
});

