var showcase = liwe.module ( 'showcase' );
	
//function ShowCaseBox ( cnt_name, tags ) tags no longer supperted use id_conf
function ShowCaseBox ( cnt_name, id_conf, timer )
{
	this.cnt_name = cnt_name;
	this.cback = showcase.show; 
	//this._tags = tags; no longer supported use id_conf
	this._id_conf = id_conf;

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

	this._timer = 3000;
	if ( timer > 0 ) this._timer = timer * 1000 ;

	this._base_index = 0; //$( cnt_name ).style.zIndex;
	this._upper = 0;
	this._lower = 0;

	this._base_index = $( this.cnt_name ).style.zIndex;
	this._upper = this._base_index + 10;
	this._lower = this._base_index + 5;

	this._img1 = this.cnt_name + '-image-1';
	this._img2 = this.cnt_name + '-image-2';

	$( this._img1 ).style.zIndex = this._upper;
	$( this._img2 ).style.zIndex = this._lower;

	showcase._instances [ cnt_name ] = this;
};

showcase._instances = {};

//showcase._new_instance = function ( cnt_name, tags ) tags no longer supported use id_conf
showcase._new_instance = function ( cnt_name, id_conf )
{
	showcase.ajax ( { action : 'showcase.ajax.get_conf', id : id_conf }, function ( data )
	{
		var timer = data [ 'config' ] [ 'playtime' ] ;	
		var box = new ShowCaseBox ( cnt_name, id_conf, timer );

		if ( box._items.length < 1 ) 
			showcase._list_items  ( cnt_name );
	});
};

showcase.set_timer = function ( cnt_name, tm )
{
	if ( ! tm ) return;
	showcase._instances [ cnt_name ]._timer = tm;
};

showcase._list_items = function ( cnt_name )
{
	var box = showcase._instances [ cnt_name ];

	//if ( ! cback ) cback = showcase.show;
	showcase.ajax ( { action : "showcase.ajax.list", id_conf : box._id_conf }, function ( data )
				{
					if ( data [ 'items' ].length < 1 ) return;
					box._items = data.get ( 'items' , [] );
					if ( box.cback )  box.cback ( cnt_name );
				} );
};

showcase.show = function ( cnt_name, dest_div )
{
	var box = showcase._instances [ cnt_name ];

	if ( ( box._items.length < 1 ) )
	{
		showcase._list_items ( cnt_name );
	} else {
		showcase.start ( cnt_name );
	}

	console.debug ( box._items );
};

showcase.init = function ()
{
	showcase.load_templates ();
};

//showcase._switch_div = function ( cnt_name, tags ) tags no longer supported use id_conf
showcase._switch_div = function ( cnt_name, id_conf )
{
	var box = showcase._instances [ cnt_name ];
	if ( box._items.length == 1 ) return;

	// if display property is set to none showcase wont render animation and it will pause
	if (  $( box.cnt_name ) == null ) return;
	var display  =  $( box.cnt_name ).style.display;
	if ( display == 'none' ) 
	{
		showcase.start ( cnt_name, id_conf ); 
		return;
	}

	box._cur = ( ( box._cur + 1 ) >= box._items.length ) ? 0 : box._cur + 1;
	if ( $( box._img1 ).style.zIndex < $( box._img2 ).style.zIndex )
	{
		var fade_div = box._img1;
		var dest_div = box._img2;
	} else {
		var fade_div = box._img2;
		var dest_div = box._img1;
	}

	var dict = {
			id : box._items [ box._cur ] [ 'id' ],
			descr : box._items [ box._cur ] [ 'descr' ],
			lnk : box._items [ box._cur ] [ 'lnk' ]
			};

	dict [ 'descr-div' ] = '';
	dict [ 'title' ] = '';
	var descr = dict.get ( 'descr', '' ).strip ();
	if ( descr != '' ) 
	{
		dict [ 'descr-class' ] = 'descr';
		dict [ 'title' ] =  String.formatDict ( showcase.templates [ 'SC-title' ] , dict ); 
		dict [ 'descr-div' ] = String.formatDict ( showcase.templates [ 'SC-descr' ] , dict );
	}
	$( dest_div , String.formatDict ( showcase.templates [ 'SC-image' ] , dict ) );

	liwe.fx.fade_out ( $( fade_div ), function ()
			{
				$( dest_div ).style.zIndex = box._upper;
				$( fade_div ).style.zIndex = box._lower;
				liwe.fx.set_opacity ( $( fade_div ), 100 );
				
				//console.debug ( 'shocase: animation end ( display : %s )', display );
				showcase.start ( cnt_name, id_conf );
			} );
};

//showcase.start = function ( cnt_name, tags ) tags no longer supprted use id_conf
showcase.start = function ( cnt_name, id_conf )
{
	if ( showcase._instances.get ( cnt_name, '' ) == '' ) 
	{
		showcase._new_instance ( cnt_name, id_conf );
		return;
	}
	showcase.start_roll ( cnt_name, id_conf );
}; 

showcase.stop_roll = function ( cnt_name ) 
{
	clearTimeout ( showcase._instances [ cnt_name ].t );
};

//showcase.start_roll = function ( cnt_name, tags ) tags no longer supportes use id_conf
showcase.start_roll = function ( cnt_name, id_conf )
{
	var box = showcase._instances [ cnt_name ];

	box.t = setTimeout ( function () { showcase._switch_div ( cnt_name, id_conf ); } , box._timer );
};

