ImageLauncher = function(name, cookUrl){
	this.name			= name;
	this.aCommands 			= new Array();
	this.oCommandStatus		= new Object();
	this.iDebug  			= 0;
	this.iImgProcess		= 0;
	this.iImgNotId			= 0;
	this.ImgSrcPixelTranparent 	= '';
	this.cookUrl			= cookUrl;

	this.userData 			= new Object();
	this.divMsg			= null;
	
	this.html = new Object();
	this.html.divEvents = document.createElement('div');
	this.eventTarget = this.html.divEvents;
	this.oComponent = new component(this.name + '.oComponent', this);
}

ImageLauncher.prototype = {
	processQueue: function(){
		if (this.oCommandsTmp == null) { this.oCommandsTmp = new Object(); }
		
		for (var i=0;i<this.aCommands.length; i++){
			if (this.getCommandStatus(this.aCommands[i].ObjTo.id) != true) {
				this.setCommandStatus(this.aCommands[i].ObjTo.id, true);
				this.oCommandsTmp[this.aCommands[i].ObjTo.id] = this.aCommands[i].clone();
				setTimeout(this.name + '.exec(' + this.name + '.oCommandsTmp[\'' + this.aCommands[i].ObjTo.id + '\']);', 0);
				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
			if (oExec.sSrc.length >= 2048) {
				this.getCookUrl(oExec); return;
			}
			oExec.ObjTo.src = oExec.sSrc;
			return;
		}
		
		if (oExec.ObjTo.userData == null) { oExec.ObjTo.userData = new Object(); }
		
		oExec.ObjTo.userData.error = 'elementLoadErrorHandler';
		oExec.ObjTo.userData.name = this.name;

		//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) {
			this.ImgSrcPixelTranparent = "/images/album_designer/transparent.gif";

			this.setCommandStatus(oExec.ObjTo.id, true);
			oExec.ObjTo.onload                   = function(e) { var oTmp = eval(this.userData.name); oTmp.imgLoad(e, 'load'); };
			oExec.ObjTo.onerror                  = function(e) { var oTmp = eval(this.userData.name); oTmp.imgLoad(e, 'error'); };
			
			oExec.ObjTo.src                      = this.ImgSrcPixelTranparent + (oBrowserSniffer.isMsie == true ? '?cb=' + Math.random() : '');
			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('before: this.aCommands.length: ' + this.aCommands.length);
		this.clearQueue(oExec, aLength);
		//dbg.add('later: this.aCommands.length: ' + this.aCommands.length);

		this.loadingShow();
	},

	exec: function(oExec) {
		if (oExec == null || oExec == "undefined") { return;}

		// If the url is already encoded (it starts with $) we assume
		// it already has a cachebuster parameter (cb=), so we do not
		// append another
		if ( (String(oExec.sSrc).indexOf('PIS?$')) == -1 ) {
			oExec.sSrc = oExec.sSrc + (oBrowserSniffer.isMsie == true ? (String(oExec.sSrc).indexOf('?') == -1 ? '?' : '') + '&cb=' + Math.random() : '');
		}

		if ((oExec.sSrc.length >= 2048 && oBrowserSniffer.isMsie == true) || (oExec.sSrc.length >= 2048 && oBrowserSniffer.isWindows == true && oBrowserSniffer.isFirefox == true) || (oExec.sSrc.length >= 7999)){
			this.setCommandStatus(oExec.ObjTo.id, false);
			this.getCookUrl(oExec); 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('Requests to the Img ' + oExec.ObjTo.id + ' : ' + oExec.ObjTo.iCount);
		
		setTimeout(this.name + '.setExtarEvents({}, \'load\');', 0);
		
		this.setCommandStatus(oExec.ObjTo.id, true);
		this.iImgProcess++;
		this.iDebug++;

		oExec.ObjTo.src = oExec.sSrc;
	},

	getCookUrl: function(oExec) {
		var oAjax = new sendFormAjax();
		oAjax.setActionUrl(this.cookUrl);
		oAjax.setFormBody("&url=" + encodeURIComponent(oExec.sSrc));
		oAjax.userData.oExec = oExec;
		oAjax.userData.imageLauncherName = this.name;
		oAjax.oEventHandle = this;
		oAjax.fEventHandle = 'handleGetCookUrl';
		oAjax.FormSubmitToAjax();
	},	
	
	handleGetCookUrl: function(oAjax) {
		return function(){
			if (oAjax.XmlHttp.readyState==4) {
				oAjax.userData.oExec.sSrc = oAjax.XmlHttp.responseXML.getElementsByTagName('cooked-url')[0].firstChild.data;
				//dbg.add('oAjax.userData.oExec.sSrc: ' + oAjax.userData.oExec.sSrc);
				var oTmp = eval(oAjax.userData.imageLauncherName);
				oTmp.execAdd(oAjax.userData.oExec);
			}
		}
	},

	imgLoad: function(e, sEvent){
		if (!e) e = normalizeEvent(window.event);
		if (oBrowserSniffer.isMsie == true) { Obj = e.target; } else { Obj = e.currentTarget; }
		if (Obj.userData.preload == true) { this.iImgProcess--; }

		Obj.userData.imgLoad = (Obj.userData.preload == true ? true : false);
		Obj.userData.preload = true;
		
		this.setCommandStatus(Obj.id, false);
		this.loadingShow();
		this.eventTarget = Obj;
		if (Obj.userData.imgLoad == true) { Obj.style.background = 'none'; this.setExtarEvents(e, sEvent); }
	},

	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);

		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; }
	}
}
