//avash - AjaxViaAdobeflaSH
/* JSON BEGIN */
/*
    http://www.JSON.org/json2.js
    2009-04-16
*/
if(!this.JSON){JSON={};}
(function(){function f(n){return n<10?'0'+n:n;}
if(typeof Date.prototype.toJSON!=='function'){Date.prototype.toJSON=function(key){return this.getUTCFullYear()+'-'+
f(this.getUTCMonth()+1)+'-'+
f(this.getUTCDate())+'T'+
f(this.getUTCHours())+':'+
f(this.getUTCMinutes())+':'+
f(this.getUTCSeconds())+'Z';};String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(key){return this.valueOf();};}
var cx=new RegExp("[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]","g");var escapable=new RegExp("[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]","g");var gap,indent,meta={'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','"':'\\"','\\':'\\\\'},rep;function quote(string){escapable.lastIndex=0;return escapable.test(string)?'"'+string.replace(escapable,function(a){var c=meta[a];return typeof c==='string'?c:'\\u'+('0000'+a.charCodeAt(0).toString(16)).slice(-4);})+'"':'"'+string+'"';}
function str(key,holder){var i,k,v,length,mind=gap,partial,value=holder[key];if(value&&typeof value==='object'&&typeof value.toJSON==='function'){value=value.toJSON(key);}
if(typeof rep==='function'){value=rep.call(holder,key,value);}
switch(typeof value){case'string':return quote(value);case'number':return isFinite(value)?String(value):'null';case'boolean':case'null':return String(value);case'object':if(!value){return'null';}
gap+=indent;partial=[];if(Object.prototype.toString.apply(value)==='[object Array]'){length=value.length;for(i=0;i<length;i+=1){partial[i]=str(i,value)||'null';}
v=partial.length===0?'[]':gap?'[\n'+gap+
partial.join(',\n'+gap)+'\n'+
mind+']':'['+partial.join(',')+']';gap=mind;return v;}
if(rep&&typeof rep==='object'){length=rep.length;for(i=0;i<length;i+=1){k=rep[i];if(typeof k==='string'){v=str(k,value);if(v){partial.push(quote(k)+(gap?': ':':')+v);}}}}else{for(k in value){if(Object.hasOwnProperty.call(value,k)){v=str(k,value);if(v){partial.push(quote(k)+(gap?': ':':')+v);}}}}
v=partial.length===0?'{}':gap?'{\n'+gap+partial.join(',\n'+gap)+'\n'+
mind+'}':'{'+partial.join(',')+'}';gap=mind;return v;}}
if(typeof JSON.stringify!=='function'){JSON.stringify=function(value,replacer,space){var i;gap='';indent='';if(typeof space==='number'){for(i=0;i<space;i+=1){indent+=' ';}}else if(typeof space==='string'){indent=space;}
rep=replacer;if(replacer&&typeof replacer!=='function'&&(typeof replacer!=='object'||typeof replacer.length!=='number')){throw new Error('JSON.stringify');}
return str('',{'':value});};}
if(typeof JSON.parse!=='function'){JSON.parse=function(text,reviver){var j;function walk(holder,key){var k,v,value=holder[key];if(value&&typeof value==='object'){for(k in value){if(Object.hasOwnProperty.call(value,k)){v=walk(value,k);if(v!==undefined){value[k]=v;}else{delete value[k];}}}}
return reviver.call(holder,key,value);}
cx.lastIndex=0;if(cx.test(text)){text=text.replace(cx,function(a){return'\\u'+
('0000'+a.charCodeAt(0).toString(16)).slice(-4);});}
if(/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,'@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,']').replace(/(?:^|:|,)(?:\s*\[)+/g,''))){j=eval('('+text+')');return typeof reviver==='function'?walk({'':j},''):j;}
throw new SyntaxError('JSON.parse');};}}());
/* JSON END */

/*AVASH START*/
//Use global namespace to make js closure possible
function _AjaxViaAdobeFlash () {
	this.ajax_requests = [];
	this.isAccesible = null;
	this.cbReadyIsFired = false;
	this.versionString = "";
	this.WaitForFlashTicks = 6000;
	this.FlashTimerPeriod = 500;
	this.flashObjId = "flashFuploader";
	if (typeof gFlashTimeout == "number") {
		this.WaitForFlashTicks = gFlashTimeout;
	}
	this.WaitForFlashTicksOrg = this.WaitForFlashTicks;
	var _this = this;
	if ((typeof swfobject != "undefined" && swfobject.hasFlashPlayerVersion("9.0.28")) || typeof swfobject == "undefined")  {
		this.WaitForFlashTimer = setInterval(function () {_this.WaitForFlash()}, _this.FlashTimerPeriod);
	}
}

_AjaxViaAdobeFlash.prototype = {
	_ajaxHandleStateChange : function(uId, httpStatus, data) {
		for(var k = 0; k < this.ajax_requests.length; k++) {
			if (this.ajax_requests[k].uId == uId) {
				var request = this.ajax_requests[k];
				this.ajax_requests.splice(k, 1);
				try {
					request.ready(httpStatus, data);
				} catch (e) {}
			}
		}
	},
	SendRequest : function(argObj) {
		if (!argObj.method) argObj.method = "post";
		if (!argObj.flashObjectId) argObj.flashObjectId = this.flashObjId ;
		this.method = argObj.method.toLowerCase();
		var request = new AjaxFlashRequest(argObj.flashObjectId, argObj.method, argObj.uri, argObj.funcName, argObj.funcArgs, argObj.callbackFunc, argObj.callbackObject, argObj.dirtyCookies);
		var res;
		if (res = request.send()) {
			this.ajax_requests.push(request);
			return true;
		} else {
			jswarn_l(2, 'Ajax falied uId=' + res);
			return false;
		}
	},
	SetAjaxReadyCallback : function(func) {
		var t = "SetAjaxReadyCallback ";
		if (this.isAccesible === true) {
			jswarn_l(3, t + " ApplyCb")
			jswarn_l(1, "onAjaxReady:D!:FlshOk=" + this.isAccesible + "/V=" + this.versionString);
			func.apply(window, [this.isAccesible, this.versionString]);
			this.cbReadyIsFired = true;
		} else {
			jswarn_l(3, t + " AssignCb")
			this.onAjaxReadyCallback = func;
		}
	},
	WaitForFlash: function(func) {
		var obj;
		if (AjaxFlash.WaitForFlashTicks >= 0) {
			jswarn_l(2, "WaitForFlashTicks=" + AjaxFlash.WaitForFlashTicks);
			AjaxFlash.WaitForFlashTicks -= AjaxFlash.FlashTimerPeriod;
		} else {
			_AjaxViaAdobeFlashIsReady("noflash", true, 0);
			if (AjaxFlash.WaitForFlashTimer) clearInterval(AjaxFlash.WaitForFlashTimer);
		}
	},
	GetFlashObj: function(movieName) {
		var t = "GetAvash via SWF - ";
		var anyIssues = false;
		if (typeof swfobject != "undefined" && !swfobject.XFilesUploaderSkipFlag) {
			var isIE = navigator.appName.indexOf("Microsoft") != -1;
			obj =  (isIE) ? window[movieName] : document[movieName];

			if (typeof obj == "undefined") anyIssues = true;
			if (!anyIssues) {
				jswarn_l(1, t + 'OK: '  + typeof obj);
				return obj;
			} else {
				jswarn_l(0, t + 'failed: '  + typeof obj);
				swfobject.XFilesUploaderSkipFlag = true;
			}
		}
		t = "Avash via GEBI - ";
		jswarn_l(5, 'Trying ' + t + '/as=' + anyIssues + ', /swf=' + typeof swfobject);
		anyIssues = false;
		obj = document.getElementById(movieName);

		if (typeof obj != "object") anyIssues = true;
		if (!anyIssues) {
			jswarn_l(1, t + 'OK: ' + typeof obj);
		} else {
			jswarn_l(0, t + 'failed: ' + typeof obj);
		}
		return obj;
	}
}

var AjaxFlash = new _AjaxViaAdobeFlash();

function _AjaxViaAdobeFlashIsReady (versionString, timerCall, counter) {
	jswarn_l(5, "FlashSync/Rtr=" + counter + ';Ver' +versionString);
	//FF fix: try to enable reborned flash anytime
	if (!timerCall && AjaxFlash) {
		var mc = AjaxFlash.GetFlashObj(AjaxFlash.flashObjId);
		if (typeof mc != "undefined") {mc.enableUploader()};
	}
	// If timer has been fired, then flash awaked, break it off
	if (AjaxFlash.cbReadyIsFired == true) return false;
	if (timerCall) {
		AjaxFlash.isAccesible = false;
	} else {
		AjaxFlash.isAccesible = true;
	}
	AjaxFlash.versionString = versionString;
	if (AjaxFlash.onAjaxReadyCallback) {
		jswarn_l(1, "onAjaxReady:FlshOk=" + AjaxFlash.isAccesible + "/V=" + AjaxFlash.versionString);
		AjaxFlash.cbReadyIsFired = true;
		try {
			AjaxFlash.onAjaxReadyCallback.apply(window, [AjaxFlash.isAccesible, AjaxFlash.versionString]);
		} catch (e) {}	
	} else {
		jswarn_l(1, "onAjaxReady: BadLuck!/Rtr=" + counter + ';Ver' +AjaxFlash.versionString);
	}
	if (AjaxFlash.WaitForFlashTimer) clearInterval(AjaxFlash.WaitForFlashTimer);
	return false;
}

	function AjaxFlashRequest(flashObjectId, method, script_uri,func_name,args,callback,callback_object, dirty_cookies)
	{
	this.method = method.toLowerCase();
	this.flashObjectId = flashObjectId;
	this.callback = callback;
	this.func_name = func_name;
	this.callback_object = callback_object;
	this.args = args;
	this.uri = script_uri;
	this.timeout = 15000;
	if (dirty_cookies) {
		this.cookies = dirty_cookies  + "";
	} else {
		this.cookies = document.cookie + "";
	}
	this.request_data = "ajax_call=1&func_name="+this.escapePlus(func_name)+"&back="+this.escapePlus(window.location)+"&data="+this.escapePlus(JSON.stringify(this.args));
	}

	AjaxFlashRequest.prototype = {
	getObj : function() {
		return AjaxFlash.GetFlashObj(this.flashObjectId);
	},
	send : function() {
		var res;
		if (this.uri.indexOf("http://" + escape(location.hostname) + "/") != -1 || !AjaxFlash.isAccesible) {
			res = this.send_native();
		} else {
			res = this.send_flash();
		}
		var cbObject = this.callback_object;
		if (cbObject && cbObject.cbAjaxIndicator && cbObject.ajaxIndicatorAutoOn) {
				cbObject.cbAjaxIndicator.apply(cbObject, [1, cbObject.ajaxOperand]);
		}
		return res;
	},
	send_flash : function() {
		var request = this.getObj();
		if (request) {
			this.request = request;
			var uId  = -1;
			jswarn_l(2, ('Lv.Cookies[' + this.method + ']' + this.cookies))
			this.request_data = "lavash_ver=1&forcedcookies=" + escape(this.cookies) + "&" + this.request_data;
			jswarn_l(5, "more:" + this.request_data)
			if (this.method == "post") {
				try {
					uId = this.request.ajax(this.uri, this.request_data, "AjaxFlash._ajaxHandleStateChange", this.cookies);
				} catch(err) {
					return false;
				}
			} else {
				var uri = this.uri + '?'+ this.request_data;
				try {
					uId = this.request.ajax(uri, "", "AjaxFlash._ajaxHandleStateChange", this.cookies);
				} catch(err) {
					return false;
				}
			}
			jswarn_l(5, "uID=" + uId);
			jswarn_l(5, "Avash:" + uId + "/" + this.uri);
			this.uId = uId;
			if (this.timeout) {
				this.timeoutID = setTimeout(function() {
					AjaxFlash._ajaxHandleStateChange(uId, 600, null)
				},this.timeout);
			}
			return uId > 0 ? uId : false;
		} else {
			return false;
		}
	},
	ready : function(httpStatus, responseText) {
		this.timeoutID ? clearTimeout(this.timeoutID) : null;

		var cbObject = this.callback_object ? this.callback_object : {};
		cbObject.responseText = responseText;
		cbObject.responseError = null;
		
		if (cbObject && cbObject.cbAjaxIndicator && cbObject.ajaxIndicatorAutoOff) {
				cbObject.cbAjaxIndicator.apply(cbObject, [0, cbObject.ajaxOperand]);
		}

		var evalError = false;
		try {
			var data = eval(responseText);
		} catch (e) { evalError = true; }

		if(httpStatus != 200 || evalError == true) {
			if(this.callback) {
				if (httpStatus == 600) {
					cbObject.responseError = {'reason': "timeout", 'status': 600};
				} else if (evalError == true) {
					cbObject.responseError = {'reason': "!eval", 'status': httpStatus};
				} else {
					cbObject.responseError = {'reason': "!200", 'status': httpStatus};
				}
				this.callback.apply(this.callback_object?this.callback_object:window,[httpStatus]);
			}
			return;
		}
		var magick = data.shift();
		if(magick != "AjaxResponse") return;
		var result = data[0];
		var result_apdx = null;
		if (data.length>1) result_apdx = data[1];
		data[0] = httpStatus;
			if(result != "OK" && result != "Redirect" && result != "Error") return;
			if(result == "OK") {
				if (result_apdx == "Redirect") {
					this.handleExpired(data.splice(1));
				} else {
					if(this.callback) {
						this.callback.apply(this.callback_object?this.callback_object:window,data);
					}
				}
			} else if(result == "Error") {
				if(this.callback) {
					cbObject.responseError = {reason: "error", status: httpStatus};
					this.callback.apply(this.callback_object?this.callback_object:window,[500]);
				}
			} else if(result == "Redirect") {
				this.handleExpired(data);
			}
		return;
	},
	getObj_native : function() {
		var XMLHttpFactories = [
			function () {return new XMLHttpRequest()},
			function () {return new ActiveXObject("Msxml2.XMLHTTP")},
			function () {return new ActiveXObject("Msxml3.XMLHTTP")},
			function () {return new ActiveXObject("Microsoft.XMLHTTP")}
		];
		var xmlhttp = false;
		for (var i=0;i<XMLHttpFactories.length;i++) {
			try {
				xmlhttp = XMLHttpFactories[i]();
			}
			catch (e) {
				continue;
			}
			break;
		}
		return xmlhttp;
	},
	send_native : function() {
		var req = this.getObj_native();
		if (req) {
			this.request = req;
			var uId  = (new Date().valueOf() + Math.round(Math.random() * 1000000));

			if (this.method == "get") {
				this.uri = this.uri + '?'+ this.request_data;
				this.request_data = null;
			}
			try {
				req.open(this.method.toUpperCase(), this.uri, true);
			} catch(e) {}
			req.setRequestHeader('User-Agent','XMLHTTP/1.0');
			if (this.method == "post") req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			req.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 1970 00:00:00 GMT");
			
			this.request.onreadystatechange = function() {
				if (req.readyState != 4) return;
				AjaxFlash._ajaxHandleStateChange(uId, req.status, req.responseText);
			};
			if (req.readyState == 4) return;
			try {
				req.send(this.request_data);
			} catch(e) {}

			jswarn_l(5, "NtvAjax:" + uId + "/" + this.uri);
			this.uId = uId;
			if (this.timeout) {
				this.timeoutID = setTimeout(function() {
					AjaxFlash._ajaxHandleStateChange(uId, 600, null);
				},this.timeout);
			}
			return uId > 0 ? uId : false;
		}
		return false;
	},
	handleExpired : function(data) {
		if (typeof AjaxFlash.cbOnExiredSession == "function") {
			AjaxFlash.cbOnExiredSession.apply(window, data);
		} else if (typeof AjaxFlash.cbOnExpiredSession == "function") {
			AjaxFlash.cbOnExpiredSession.apply(window, data);
		} else {
			window.location.reload();
		}
	},
	escapePlus : function(param) {
		param = escape(param);
		return String(param).replace(/\+/g,'%2B');
	}
}
/*AVASH END*/

try {
	gJsReady({'name': 'avash'});
} catch (e) {};