var banner2  = new liwe.module ( "banner2" );

function BannerBox ( dest_div, tipo )
{
	var self = this;

	this._dest_div = $el ( dest_div );

	this._items = [];
	this._cur = 0;

	this._last_id = 0;

	this._timer = 3000;
	if ( tipo [ 'playtime' ] > 0 ) this._timer = tipo [ 'playtime' ] * 1000 ;
	this._tipo = tipo;
	this._banner = {};

	this._base_index = 0; 
	this._upper = 0;
	this._lower = 0;

	this._base_index = this._dest_div.style.zIndex;

	this._upper = this._base_index + 10;
	this._lower = this._base_index + 5;

	this._el1 = 'bn-box-el1-' + tipo [ 'id' ];
	this._el2 = 'bn-box-el2-' + tipo [ 'id' ];

	this._current = this._el1;

	//render
	if ( $( this._current ) )
	{
		this._last_id = $( this._current ).firstChild.getAttribute ( 'rel' ); 
	}
	else
	{
		this._dest_div.innerHTML = String.formatDict ( banner2.templates [ 'bn-box' ], tipo );
	}

	var tipo = tipo [ 'id' ];

	liwe.events.add ( $ ( this._el1 ), 'mouseover' ,function () { banner2.stop_roll ( tipo ); } );
	liwe.events.add ( $ ( this._el1 ), 'mouseout' ,function () { banner2.start_roll ( tipo ); } );
	liwe.events.add ( $ ( this._el2 ), 'mouseover' ,function () { banner2.stop_roll ( tipo ); } );
	liwe.events.add ( $ ( this._el2 ), 'mouseout' ,function () { banner2.start_roll ( tipo ); } );
	this._can_roll = true;

	this._timeoutid = setTimeout ( function () { self.refresh (); }, this._timer );
};

BannerBox.prototype.refresh = function ()
{
	var self = this;
	banner2.ajax ( { action: 'banner2.ajax.show_banner', last_id: this._last_id, id_tipo: this._tipo [ 'id' ] }, function ( data )
	{
		var b = data [ 'banner' ];

		if ( self._last_id == b [ 'id_banner' ] ) 
		{
			self._can_roll =  false;
			return;
		}
		self._last_id = b [ 'id_banner' ];

		self._can_roll =  true;

		var old = self._current;

		if ( self._current == self._el1 )
		{
			self._current = self._el2;
		}
		else
		{
			self._current = self._el1;
		}
		b [ 'w' ] = self._tipo [ 'w' ];
		b [ 'h' ] = self._tipo [ 'h' ];

		if ( $( old ).firstChild )
			liwe.events.del ( $( old ).firstChild, 'click', banner2.click );

		$( self._current, String.formatDict ( banner2.templates [ 'bn-box-img' ], b ) );

		self.animate ( old, self._current );
		self._timeoutid = setTimeout ( function () { self.refresh (); }, self._timer );
	});
};

BannerBox.prototype.animate  = function ( old, current ) 
{
	var effect = this._tipo [ 'effect' ];

	switch ( effect ) 
	{
		case 0:
			liwe.fx.set_opacity ( old, 0 );
			liwe.fx.set_opacity ( current, 100 );
			break;
		case 1:
			liwe.fx.fade_in ( current );
			liwe.fx.fade_out ( old );
			break;
		case 2:
			liwe.fx.push ( old, current, 'right' );
			break;
		case 3:
			liwe.fx.push ( old, current, 'top' );
			break;
		case 4:
			liwe.fx.push ( old, current, 'left' );
			break;
		case 5:
			liwe.fx.push ( old, current, 'bottom' );
			break;
	};
	
};

banner2.render = function ( dest_div, id_tipo )
{
	banner2.ajax ( { action : 'banner2.ajax.get_tipo', id : id_tipo }, function ( data )
	{
		tipo = data [ 'tipo' ];
		var box = new BannerBox ( dest_div, tipo );
		banner2._instances [ id_tipo ] = box;
	});
};


banner2.start_roll = function ( name )
{
	var b = banner2._instances [ name ];
	if (! b._can_roll ) return;
	if ( b._timeoutid ) return;
	b._timeoutid = setTimeout ( function () { b.refresh (); }, b._timer );
	
};

banner2.stop_roll = function ( name )
{
	
	var b = banner2._instances [ name ];
	clearTimeout ( b._timeoutid );
	b._timeoutid = null;
};

banner2._instances = {};

banner2.init = function ()
{
	banner2.load_templates ();
	console.debug ( "banner  2  init" );
};	

banner2.plugin = function ( el, params )
{
	if ( params.get ( 'id_tipo' ) )
	{
		banner2.render ( el, params [ 'id_tipo' ] );
	}
	else if ( params.get ( 'id' ) )
	{
		banner2.ajax ( { action: 'banner2.ajax.get_banner', id: params [ 'id' ] }, function ( res )
		{
			var b = res.banner;
			if ( ! b )
			{
				console.error ( 'Banner with id %d not found!', params [ 'id' ] );
				return;
			}
			b [ 'id_banner' ] = b [ 'id' ];
			b [ 'banner' ] = b [ 'descr' ];
			el.innerHTML = String.formatDict ( banner2.templates [ 'bn-box-img' ], b );
		} );
	}
};

