/***
	Author: Reynaldo Castellano III
	Date: June 12, 2007	
***/

var CaptchaLoader = Class.create();
CaptchaLoader.prototype = {
	attributes : {},
	initialize : function(params){		
		this.attributes = {
			handler 			: (Tool.objectKeyExists(params,'handler'))?params['handler']:'/captcha/captchaHandler.php',
			serverVariable 		: (Tool.objectKeyExists(params,'serverVariable'))?params['serverVariable']:'',
			serverVariableScope	: (Tool.objectKeyExists(params,'serverVariableScope'))?params['serverVariableScope']:'',
			imageTagID			: (Tool.objectKeyExists(params,'imageTagID'))?params['imageTagID']:'',
			onLoad				: (Tool.objectKeyExists(params,'onLoad') && Tool.isFunction(params['onLoad']) )?params['onLoad']:function(){},
			imgAttributes		: (Tool.objectKeyExists(params,'imgAttributes') && Tool.isObject(params['imgAttributes']) )?params['imgAttributes']:{},
			loadingImg			: (Tool.objectKeyExists(params,'loadingImg'))?params['loadingImg']:'/images/load_gray.gif'
		};
	},
	load : function(){
		this.showLoading();
		var ajax = new Ajax.Request(this.attributes['handler'],{
			method		:'post',
			postBody	: 'imgAttr='+encodeURIComponent(JSON.stringify(this.attributes['imgAttributes'])),
			requestHeaders : {
				'Accept' : '*/*',
				'X-Requested-With' : 'XMLHttpRequest',
				'Referer': document.location
			},
			onSuccess 	: function(transport){
				var data = JSON.parse(transport.responseText);
				if($(this.attributes['imageTagID'])){				
					$(this.attributes['imageTagID']).src = data.src;
				}				
				this.attributes['onLoad'](data);
				this.hideLoading();
			}.bind(this),
			onFailure 	: function(transport){
				this.attributes['onLoad'](transport);
				this.hideLoading();
			}.bind(this)
		});
	},
	showLoading : function(){
		$(this.attributes['imageTagID']).src = this.attributes['loadingImg'];
	},
	setAttribute : function(attr,val){

	},
	getAttribute : function(){
		
	}
};

/******
	Usage:
		var Captcha = new CaptchaLoader({			
			serverVariable 		: 'captchaEnc',
			serverVariableScope : '$_SESSION',
			imageTagID			: 'captchaImg',
			beforeLoad			: function(){},
			afterLoad			: function(){}
		});
******/
