ImageLauncher = function(name){
	this.name			= name;
	this.aCommands 			= new Array();
	this.oCommandStatus		= new Object();
	this.iDebug  			= 0;
	this.iImgProcess		= 0;
	this.iImgNotId			= 0;
	this.ImgSrcPixelTranparent 	= '';
	this.ImgSrcPixelTranparentLoad	= false;
	
	this.userData 			= new Object();
	this.handlersEvent 		= new Object();
	this.divMsg			= null;
}

ImageLauncher.prototype = {
	processQueue : function(){
		for (var i=0;i<this.aCommands.length; i++){
			if (this.getCommandStatus(this.aCommands[i].ObjTo.id) != true) {
				this.exec(this.aCommands[i]);
				this.aCommands.splice(i, 1);
				i--;
			}
			//else { dbg.add("return!"); }
		}
	},

	clearQueue : function(oExec, iIndex){
		for (var i=this.aCommands.length-1; i>=0; i--){
			//dbg.add('this.aCommands[i]["ObjTo"].id: ' +  this.aCommands[i]["ObjTo"].id + ' - i: ' + i);
			if (oExec.ObjTo.id == this.aCommands[i]["ObjTo"].id && i < iIndex) {
				dbg.add('Delete - oExec.ObjTo.id: ' + oExec.ObjTo.id + ' - this.aCommands[iIndex]["ObjTo"].id: ' +  this.aCommands[iIndex]["ObjTo"].id + ' - i: ' + i + ' - iIndex: ' + iIndex);
				this.aCommands.splice(i, 1); iIndex--;
			}
		}
	},

	execAdd : function(oExec){
		if (oBrowserSniffer.isSafari == true) { //Disabled ImageLaunche in Safari
			oExec.ObjTo.src = oExec.sSrc;
			return;
		}
		
		
		if (oExec.ObjTo.userData == null) { oExec.ObjTo.userData = new Object(); }
		oExec.ObjTo.userData.load = 'elementLoadHandler';
		oExec.ObjTo.userData.error = 'elementLoadErrorHandler';
		
		//dbg.add('execAdd - oExec.ObjTo.id: ' + oExec.ObjTo.id);
		if (oExec.ObjTo.id == 'undefined' || oExec.ObjTo.id == null || trim(oExec.ObjTo.id) == ''){
			oExec.ObjTo.id = 'ImgId_' + this.iImgNotId;
			this.iImgNotId++;
		}
		
		if (oExec.ObjTo.userData.preload != true) {
			if (this.ImgSrcPixelTranparentLoad == false) {
				this.ImgSrcPixelTranparent 	= "/images/album_designer/transparent.gif" + (oBrowserSniffer.isMsie == true ? '?cb=' + Math.random() : '');
			}
			
			if ((oBrowserSniffer.isMsie == true && this.ImgSrcPixelTranparentLoad == false) || oBrowserSniffer.isMsie == false ) {
				this.setCommandStatus(oExec.ObjTo.id, true);
				oExec.ObjTo.onload = function() { ImageEventHandle(this, 'load'); };
			}
			
			this.ImgSrcPixelTranparentLoad = true;

			oExec.ObjTo.src = this.ImgSrcPixelTranparent;
			oExec.ObjTo.style.background ="url('/images/album_designer/loadingicon.gif')";
			oExec.ObjTo.style.backgroundRepeat = 'no-repeat';
			oExec.ObjTo.style.backgroundPosition = '50% 50%';
			oExec.ObjTo.userData.preload = false;
		}
		
		this.aCommands.push(oExec);
		aLength = this.aCommands.length-1;
		//dbg.add('Antes: this.aCommands.length: ' + this.aCommands.length);		
		this.clearQueue(oExec, aLength);
		//dbg.add('Despues: this.aCommands.length: ' + this.aCommands.length);
		
		this.loadingShow();
	},

	exec : function(oExec) {
		if (oExec == null || oExec == "undefined") { return;}	
		
		if ((oExec.sSrc.length >= 2048 && oBrowserSniffer.isMsie == true) || oExec.sSrc.length > 7999){
			var oAjax = new sendFormAjax();
			oAjax.setActionUrl(data.actionUrls.cookUrl);
			oAjax.setFormBody("&url=" + encodeURIComponent(oExec.sSrc));
			oAjax.userData.load 	= 'xmlHttpLoadHandler';
			oAjax.userData.oExec 	= oExec;
			oAjax.fEventHandle	= handleOnReadyStateChange;
			oAjax.FormSubmitToAjax();
			return;
		}
		
		
		//if (this.iDebug == 10) { oExec.sSrc+='1'; this.iDebug = 0;}		
		if (isNaN(oExec.ObjTo.iCount)) { oExec.ObjTo.iCount = 1; } else { oExec.ObjTo.iCount++; }
		//dbg.add('Peticiones a la Img ' + oExec.ObjTo.id + ' : ' + oExec.ObjTo.iCount);
		
		this.setCommandStatus(oExec.ObjTo.id, true);
		this.iImgProcess++;		
		this.iDebug++;
		oExec.ObjTo.src = oExec.sSrc + (oBrowserSniffer.isMsie == true ? (String(oExec.sSrc).indexOf('?') == -1 ? '?' : '') + '&cb=' + Math.random() : '');
		
		oExec.ObjTo.onload = function() { ImageEventHandle(this, 'load'); };
		oExec.ObjTo.onerror = function() { ImageEventHandle(this, 'error'); };
		
		this.setExtarEvents('load');
	},
	
	
	getCookUrl : function(oAjax, bErr) { 
		if (bErr==true) { }
		//dbg.add('oAjax.XmlHttp.readyState: ' + oAjax.XmlHttp.readyState);
		oAjax.userData.oExec.sSrc = oAjax.XmlHttp.responseXML.getElementsByTagName('cooked-url')[0].firstChild.data;
		//dbg.add('oAjax.userData.oExec.sSrc: ' + oAjax.userData.oExec.sSrc);
		this.execAdd(oAjax.userData.oExec);
	},
	
	imgLoad : function(Obj, bErr){
		if ((oBrowserSniffer.isMsie == true && this.ImgSrcPixelTranparentLoad == true) || Obj.userData.preload == true) { this.iImgProcess--; Obj.style.background = 'none'; }
		
		Obj.userData.preload = true;				
		this.setCommandStatus(Obj.id, false);
		//this.iImgProcess--;
		
		this.loadingShow();
		this.setExtarEvents((bErr == true ? 'error' : 'load'));		

		//TODO:
		return;
		if (document.getElementById("imgLoadError" + Obj.id)) {
			Obj.parentNode.removeChild(document.getElementById("imgLoadError" + Obj.id));
		}

		if (bErr) {
			Obj.innerHTML+= "<a id=\"imgLoadError" + Obj.id + "\" href=\"javascript:" + this.name + ".reloadImg('" + Obj.id + "');\">RELOAD!</a>";
		}				
	},
	
	reloadImg : function(ObjToId){
		var oExec = new Object();
		oExec.ObjTo 	= document.getElementById(ObjToId);
		oExec.sSrc 	=  document.getElementById(ObjToId).src;
		this.execAdd(oExec);		
	},

	loadingShow : function() {	
		if (this.iImgProcess<0) { this.iImgProcess=0;}
//		dbg.add('this.aCommands.length: ' + this.aCommands.length + ' this.iImgProcess: ' + this.iImgProcess);

		//this.divMsg = Main.gui.oNotificationAreaImgLauncher.html.div;
		if (this.divMsg != null) {
			if (this.iImgProcess <= 0) {
				this.divMsg.style.display = 'none';
			}
			else {
				this.divMsg.style.display = 'block';
				this.divMsg.innerHTML = "Loading (" + (this.iImgProcess + 1) + " images pending) ";
			}
		}
		this.processQueue();
	},

	setCommandStatus : function(ObjToId, bSatus) {
		this.oCommandStatus[ObjToId] = bSatus;
	},

	getCommandStatus : function(ObjToId) {
		try {
			if (trim(this.oCommandStatus[ObjToId]) == 'undefined') { return false; }
			return this.oCommandStatus[ObjToId]; 
		}
		catch(e){ return false; }
	},
	
	setEventHandlerData : function(name, value) {
		this.userData[name] = value;
	},

	setEventHandler : function(name, value, obj) {
		switch (name) {
			case 'load': case 'error':
				if (this.handlersEvent[name] == null) { this.handlersEvent[name] = new Object(); }				
				this.handlersEvent[name]['method'] = value;
				if (obj != null) { this.handlersEvent[name]['object'] = obj; }
				break;
			default:
				//alert('ERROR!');
		}
	},
	
	setExtarEvents : function(eventName) {
		if (this.handlersEvent[eventName] != null) {
			var e = new Object();			
			e.target = this; e.type = eventName;
			
			if (this.handlersEvent[eventName]['object'] != null) {
				var obj = this.handlersEvent[eventName]['object'];
				obj[this.handlersEvent[eventName]['method']](e);
			}
			else {
				this.handlersEvent[eventName]['method'](e);
			}
		}	
	}	
}

