/**
 * Zarif ana javascript dosyası
 * Orjinal dosyadan sadece ihtiyaç duyulan kısımlar alınmıştır.
 * @author Fatih Tolga Ata
 * @copyright 2007 - Fatih Tolga Ata
 * @version 0.1
 */

var zarif = {};
zarif.VERSION = "0.1";

zarif.isDefined = function(obj)
{
	return ((typeof(obj) === "undefined") || (obj === null)) ? false : true;
};

zarif.extend = function(subClass, baseClass)
{
	if (!zarif.isDefined(baseClass))
		baseClass = zarif.Object;
	function inheritance() {};
	inheritance.prototype = baseClass.prototype;
	subClass.prototype = new inheritance();
	subClass.prototype.constructor = subClass;
	subClass.baseConstructor = baseClass;
	subClass.base = baseClass.prototype;
}

zarif.Object = function(){};

zarif.core = {};

zarif.core.Request = function (file, type, async)
{
	if ((typeof(type) == "undefined") || (type == null) || (type == ""))
		type = "text/plain";
	if ((typeof(async) == "undefined") || (async == null))
		async = true;
	//privates
	
	/**
	 * @private
	 */
	this._file = file;
	/**
	 * @private
	 */	
	this._type = type;
	/**
	 * @private
	 */	
	this._async = async;
	/**
	 * @private
	 */	
	this._request = null;
	/**
	 * @private
	 */	
	this._status = 0;//0=init, 1=progress, 2= complete, 3= timeout, 4= error;
	this._createRequest();

	//properties
	this.getStatus = function(){return this._status;};
	
	//Events
	this.onInit = function(){};
	this.onComplete = function(data){};
	this.onProgress = function(total, loaded){};
	this.onError = function(code, msg){};
};
zarif.extend(zarif.core.Request);

zarif.core.Request.prototype._createRequest = function ()
{
	try
	{
		// Mozilla , Safari , IE7
		this._request = new XMLHttpRequest();
	}
	catch (e)
	{
		// IE
		var XMLHTTP_IDS = new Array('MSXML2.XMLHTTP.5.0',
									'MSXML2.XMLHTTP.4.0',
									'MSXML2.XMLHTTP.3.0',
									'MSXML2.XMLHTTP',
									'Microsoft.XMLHTTP');
		var success = false;
		for (var i=0;i < XMLHTTP_IDS.length && !success; i++)
		{
			try
			{
				this._request = new ActiveXObject(XMLHTTP_IDS[i]);
				success = true;
			}
			catch (e){}
		}
		if (!success) {
		  throw new Error('Unable to create XMLHttpRequest.');
		}
	 }
};

zarif.core.Request.prototype._execute = function ()
{
	if (typeof(this._postdata) == "undefined")
		return;
	
	//XmlHttpRequest'i ayarlayıp çalıştırıyoruz.
	if (this._postdata == "")
		var method = "GET";
	else
		var method = "POST";

	var self = this;
	//her zaman için async çalışır ama onComplete, onError eventları async olduğunda direk,
	//sync olduğunda zarif.core.RequestQueue ile çalıştırılır 
	this._request.open(method, this._file, this._async);
	if (this._async)
	{
		this._request.onreadystatechange = function ()
		{
			switch(self._request.readyState)
			{
				case 0:
				case 1:
					self._status = 1; //progress
					self.onInit();
					break;
				case 2:
					try
					{
						if (self._request.status != 200)
						{
							self._status = 4;//error
							self.doError();
							self._request.abort();
						}
					}
					catch (e){}
					break;
				case 3:
					try
					{ 
						var contentLength = self._request.getResponseHeader("Content-Length");
						var total = self._request.responseText.length; 
					}
					catch (e)
					{
						var contentLength = 0;
						var total = 0;
					}
					self.onProgress(total, contentLength);
					self._status = 1;//progress
					break;
				case 4:
					if(self._request.status == 200)
					{
						self._status = 2;//complete
						self.doComplete();
					}
					else
					{
						self._status = 4;//error
						self.doError();
					}
					break;
			}
		};
	}
	if(this._request.overrideMimeType)
		this._request.overrideMimeType(this._type);
	this._request.setRequestHeader("Content-Type",
		"application/x-www-form-urlencoded; charset=utf-8");
	this._request.send(this._postdata);
	if (!this._async)
	{
		if (this._request.status == 200)
			this.doComplete();
		else
			this.doError();
	}
};

zarif.core.Request.prototype.doComplete = function()
{
	this.onComplete({text: this._request.responseText, 
					xml: this._request.responseXML});
}

zarif.core.Request.prototype.doError = function()
{
	this.onError(this._request.status, this._request.statusText);
}

zarif.core.Request.prototype.send = function(data)
{
	if ((data == null) || (typeof(data) === "undefined"))
		data = [];
	//POST verilerini arrayden dönüştürüyoruz.
	var postData = null;
	var postData_arr = [];
	for (var i = 0; i < data.length; i++)
	{
		postData_arr.push(data[i].join("="));
	}
	if (postData_arr.length > 0);
		postData = postData_arr.join("&");
	
	this._postdata = postData;

	this._execute();
};