var ADS = function()
{	
	var _FLASH = Flash, _old_height = null, timers = {}, bannerTimeout = (27 * 1000);
	
	var types = {};
	types["5"] = {id: "5", path: "http://static.clicatribuna.com/ads/372x250/", width: "372", height: "250", container: "#ads-content"};
	types["4"] = {id: "4", path: "http://static.clicatribuna.com/ads/flutuantes/", width: "", height: "", container: "#banner-flutuante"};
	types["1"] = {id: "1", path: "http://static.clicatribuna.com/ads/150x296/", width: "150", height: "296", container: "#ads-column-1"};
	types["2"] = {id: "2", path: "http://static.clicatribuna.com/ads/150x296/", width: "150", height: "296", container: "#ads-column-2"};
	types["3"] = {id: "3", path: "http://static.clicatribuna.com/ads/800x80/", width: "800", height: "", container: "#ads-topo"};
	
	var $banners = {};
	
	return {
		
		init: function()
		{
			var $self = this;
			$("a.for-ads").each(function(){ $self.create(this); });
			
			for(var i in types)
			{
				var type = types[i];
				
				$self.showBanner( $(type.container + " div:first-child"), type.container );
				$self.registerTimer(type.id, type.container);
			}
		},
		
		showBanner: function(b, container)
		{
			if(b.length > 0 && $banners[container])
			{
				var bns = $banners[container];
				var bs = bns[b.index()];
				
				b.html( bs ).show();
			}
		},
		
		registerTimer: function(type, container)
		{
			var $self = this;
			if($(container + " div").length > 1)
			{
				timers["timer_" + type] = setInterval(function(){
				
					var nb = $(container + " div:visible").next();
					if(nb.length < 1) nb = $(container + " div:first-child");
					$self.showBanner(nb, container);
				
					try {
						var nb_id = nb.attr("id").split("-")[2];
						var nbf = FlashTag.getObject({id:"banner-" + type + "-" + nb_id})
						nbf.Stop();
						nbf.Rewind();
						nbf.Play();
					} catch(ex) {}
					
					$(container + " div").not(nb).hide();
				
				}, bannerTimeout);
			}
		},
		
		create: function(a)
		{
			var $a = $(a), $rel = $a.attr("rel").split(",");
			var _clicktag = $a.attr("href"), _type = types[$rel[1]], _src = _type.path + $rel[2], _container = $a.parent();
			
			var _width = "100%";
			if(_type.width != null && _type.width != "")
			{
				_width = _type.width + "px";
			}
			
			var _height = "100%";
			if(_type.height != null && _type.height != "")
			{
				_height = _type.height + "px";
			}
			
			var __params = [];
			if($rel.length > 3)
			{
				__params = $rel[3].split("&");
			}
			
			_params = {'flashvars': {'clicktag': _clicktag}};
			for(var i=0; i<__params.length; i++)
			{
				var p = __params[i].split("=");
				_params[p[0]] = p[1];
			}
			
			var spc = "#" + _container.parent().attr("id");
			if(!$banners[spc])
			{
				$banners[spc] = [];
			}
			
			var $banner = new _FLASH(_src, "banner-" + _type.id + "-" + $rel[0], _width, _height, _params);
			$banners[spc].push( $banner.toString() );
		},
		
		hideBanner: function()
		{
			$(types["4"].container).hide().remove();
		},
		
		expandBanner: function(h)
		{
			clearInterval(timers["timer_3"]);
			
			var $banner = $(types["3"].container);

			_old_height = $banner.height();

			$banner.height(h).addClass("expanded");
		},
		
		expandBannerClose: function()
		{
			ADS.registerTimer("3", types["3"].container);
			
			var $banner = $(types["3"].container);
			
			$banner.height(_old_height).removeClass("expanded");

			_old_height = $banner.height();
		}
		
	};
	
}();

var someBanner = ADS.hideBanner;
var hideBannerFlutuante = ADS.hideBanner;
var expandBanner = ADS.expandBanner;
var expandBannerClose = ADS.expandBannerClose;

$(function(){ ADS.init(); });
