if(FOLDER_PREFIX == undefined) FOLDER_PREFIX = "";

String.prototype.trim = function()
{
	return this.replace(/\s+/g,"");
}

var niceBanner = {
	
	ZONAS: new Array(),
	INTERVALOS: new Array(),
	
	goGoGo: function(info)
	{
		niceBanner.ZONAS = info;
		var banners = $$("div.niceBanner");

		if(banners.length > 0) banners.each(function(banner)
		{
			var clases = banner.classNames().toArray();
			Element.extend(clases);
			clases.each(function(clase){
				if(clase.indexOf("niceBanner___") != -1)
				{
					var zoneType = clase.substr(13);
					niceBanner.procesar(zoneType);
				}
			});
		});		
	},
	
	getZona: function(id)
	{
		var x = 0;
		for(x=0;x<niceBanner.ZONAS.length;x++) if(niceBanner.ZONAS[x].nombre == id) return niceBanner.ZONAS[x];
		return false;
	},
	
	procesar: function(zoneType)
	{
		var banners = $$("div.niceBanner___" + zoneType);
		var zona = niceBanner.getZona(zoneType);
		if(zona.useCount == undefined) zona.useCount = 0;
		if(((banners.length > 0) && (zona !== false)) && (zona.banners.length))
		{
			banners.each(function(banner)
			{
				zona.useCount++;
				if(zona.useCount > zona.banners.length) zona.useCount = 1;
				
				var output = $div();
				output.addClassName("niceBanner");
				output.addClassName("niceBanner___" + zoneType);
				var outputSource = zona.banners[zona.useCount-1];
				
				
				switch(outputSource.type)
				{
					case "gif":
					case "jpg":
						var outputBanner = $img({src:FOLDER_PREFIX + outputSource.url});
						outputBanner.writeAttribute("alt",outputSource.nombre);
						outputBanner.writeAttribute("title",outputSource.nombre);
						if(outputSource.behavior.link != undefined) outputBanner = $a({href:'javascript:' + outputSource.behavior.link + ';'},outputBanner);
						output.appendChild(outputBanner);
						if(banner.outputSource != undefined) niceBanner.stopObservingBanner(banner);
						
						Element.replace(banner,output);

						if(outputSource.behavior.count != undefined) output.observe("click",function() { new Ajax.Request(FOLDER_PREFIX + "admin/includes/php/sys/helpers/webrequest.php",{method:"post",parameters:{action:'count_banner_click', id: outputSource.id}}); });
					break;
					
					case "swf":
					case "flv":
						var flashdiv = $div();
						flashdiv.writeAttribute("style",'width:' + zona.width + 'px;height:' + zona.height + 'px;overlay:hidden;');
						var overlay = $div();
						overlay.writeAttribute("style",'position:absolute;z-index:10;width:' + zona.width + 'px;height:' + zona.height + 'px;overlay:hidden;cursor:pointer;background-color:ffffff;*filter:alpha(opacity=0); -moz-opacity:0; opacity:0;');
						overlay.writeAttribute("alt",outputSource.nombre);
						overlay.writeAttribute("title",outputSource.nombre);
						
						output.appendChild(overlay);
						output.appendChild(flashdiv);

						Element.replace(banner,output);
						
						var swf = "";
						if(outputSource.type == "swf") swf = outputSource.url;						
						else swf = "flash/player.swf";
						var so = new SWFObject(FOLDER_PREFIX + swf,flashdiv.identify() + "__flash",zona.width + "px",zona.height + "px","7","#FFFFFF");
						var so = new SWFObject(FOLDER_PREFIX + swf,flashdiv.identify() + "__flash",zona.width + "px",zona.height + "px","7","#FFFFFF");
						so.addParam("allowfullscreen","false");
						so.addParam("wmode","transparent");
						so.addParam("allowscriptaccess","always");
						if(outputSource.type == 'flv') so.addParam("flashvars","controlbar=none&autostart=true&&repeat=always&link&file=../" + outputSource.url);
						so.write(flashdiv.identify());
						
						if(outputSource.behavior.count != undefined) overlay.observe("click",function() { new Ajax.Request(FOLDER_PREFIX + "admin/includes/php/sys/helpers/webrequest.php",{method:"post",parameters:{action:'count_banner_click', id: outputSource.id}}); });
						if(outputSource.behavior.link != undefined) overlay.observe("click",function() {eval(outputSource.behavior.link);});
					break;
				}
				if((zona.rotative) && (zona.banners.length > 1))
				{
					var intervalo = new PeriodicalExecuter(function(pe) {
						pe.stop();
						var ZONA = pe.zona;
						niceBanner.procesar(ZONA.nombre);
						}, zona.rotationtime);
					intervalo.zona = zona;
				}
				
				output.outputSource = outputSource;
				niceBanner.observeBanner(output);
			});			
		}		
	},
	
	observeBanner: function(output)
	{
		if(output.outputSource.behavior.onclick != undefined) output.observe("click",output.outputSource.behavior.onclick);
		if(output.outputSource.behavior.onmouseover != undefined) output.observe("mouseover",output.outputSource.behavior.onmouseover);
		if(output.outputSource.behavior.onmouseout != undefined) output.observe("mouseout",output.outputSource.behavior.onmouseout);
	},
	
	stopObservingBanner: function(output)
	{
		if(output.outputSource.behavior.onclick != undefined) output.stopObserving("click",output.outputSource.behavior.onclick);
		if(output.outputSource.behavior.onmouseover != undefined) output.stopObserving("mouseover",output.outputSource.behavior.onmouseover);
		if(output.outputSource.behavior.onmouseout != undefined) output.stopObserving("mouseout",output.outputSource.behavior.onmouseout);
	}
};

document.observe("dom:loaded", function()
{
	niceBanner.goGoGo(BANNERSYS);
});