/**
 *
 */

var	NOT_ANIMATED		= 0,
	FADE				= 1,
	SLIDE_LEFT			= 2,
	SLIDE_RIGHT			= 3,
	SLIDE_TOP			= 4,
	SLIDE_BOTTOM		= 5,
	SCROLL_LEFT			= 6,
	SCROLL_RIGHT		= 7,
	BLOCKS_SLIDE_TOP	= 8,
	BLOCKS_SLIDE_BOTTOM	= 9,
	BLOCKS_SLIDE_LEFT	= 10,
	BLOCKS_SLIDE_RIGHT	= 11,
	BLOCKS_SWIRL		= 12,
	BLOCKS_RANDOM		= 13;
	
	
function Advert( name )
{
	this.name		= name;
	this.list		= new Array;
	this.index		= -1;
	this.currentAd	= null;
	this.nextAd		= null;
	this.prevAd		= null;
	this.timer		= null;
	this.tiles		= null;
	this.zindex		= 100;
	
	
	this.createTiles = function( obj )
	{
		var	obj		= $('#' + obj),
			width	= obj.width(),
			height	= obj.height(),
			tilesX	= parseInt( width / 10 ),
			tilesY	= parseInt( height / 7 );
		
		this.tiles	= new Array;
		this.sizeX	= tilesX;
		this.sizeY	= tilesY;
		
		for( var y = 0; y < 8; y++ )
		{
			this.tiles[y] = new Array;
			
			for( var x = 0; x < 11; x++ )
			{
				this.tiles[y][x] = 0;
				obj.parent().append( '<div id="tilein_' + this.name + '-' + x + '-' + y + '" style="margin:0;padding:0;left:' + (x*tilesX) + ';top:' + (y*tilesY) + ';width:' + tilesX + 'px;height:' + tilesY + 'px;float:left;position:absolute;z-index:' + this.zindex + '"></div>' );
				obj.parent().append( '<div id="tileout_' + this.name + '-' + x + '-' + y + '" style="margin:0;padding:0;left:' + (x*tilesX) + ';top:' + (y*tilesY) + ';width:' + tilesX + 'px;height:' + tilesY + 'px;float:left;position:absolute;z-index:' + this.zindex + '"></div>' );
			}
		}
	}
	
	/**
	 *
	 */
	this.add = function( obj )
	{
		switch( obj.type )
		{
			case NOT_ANIMATED:
				obj.transition = this.notAnimated;
				obj.opposite = this.notAnimated;
				break;
				
			case FADE:
				obj.transition = this.fade;
				obj.opposite = this.fade;
				break;
			
			case SLIDE_LEFT:
				obj.transition = this.slideLeft;
				obj.opposite = this.slideRight;
				break;
			
			case SLIDE_RIGHT:
				obj.transition = this.slideRight;
				obj.opposite = this.slideLeft;
				break;
			
			case SLIDE_TOP:
				obj.transition = this.slideTop;
				obj.opposite = this.slideBottom;
				break;
			
			case SLIDE_BOTTOM:
				obj.transition = this.slideBottom;
				obj.opposite = this.slideTop;
				break;
			
			case SCROLL_LEFT:
				obj.transition = this.scrollLeft;
				obj.opposite = this.scrollRight;
				break;
			
			case SCROLL_RIGHT:
				obj.transition = this.scrollRight;
				obj.opposite = this.scrollLeft;
				break;
			
			case BLOCKS_SLIDE_TOP:
				obj.transition = this.blocksSlideTop;
				obj.opposite = this.blocksSlideBottom;
				break;
			
			case BLOCKS_SLIDE_BOTTOM:
				obj.transition = this.blocksSlideBottom;
				obj.opposite = this.blocksSlideTop;
				break;
			
			case BLOCKS_SLIDE_LEFT:
				obj.transition = this.blocksSlideLeft;
				obj.opposite = this.blocksSlideRight;
				break;
			
			case BLOCKS_SLIDE_RIGHT:
				obj.transition = this.blocksSlideRight;
				obj.opposite = this.blocksSlideLeft;
				break;
				
			case BLOCKS_SWIRL:
				obj.transition = this.blocksSwirl;
				obj.opposite = this.blocksSwirl;
				break;
			
			case BLOCKS_RANDOM:
				obj.transition = this.blocksRandom;
				obj.opposite = this.blocksRandom;
				break;
		}
		
		obj.parent	= this;
		obj.zindex	= this.zindex;
		this.list.push( obj );
		
		this.zindex++;
		
		if ( (obj.type == BLOCKS_SLIDE_TOP || obj.type == BLOCKS_SLIDE_BOTTOM || obj.type == BLOCKS_SLIDE_LEFT || obj.type == BLOCKS_SLIDE_RIGHT || obj.type == BLOCKS_SWIRL || obj.type == BLOCKS_RANDOM) && ! this.tiles )
			this.createTiles( obj.object );
	}
	
	/**
	 *
	 */
	this.exec = function()
	{
		clearTimeout( this.timer );
		
		if ( ! this.currentAd && this.list[ 0 ].type != SCROLL_LEFT && this.list[ 0 ].type != SCROLL_RIGHT && this.list[ 0 ].type != NOT_ANIMATED )
		{
			this.index++;
			this.currentAd = this.list[ this.index ];
			this.currentAd.fade = this.fade;
			this.currentAd.duration /= 3;
			this.currentAd.fade( 1 );
			this.currentAd.duration *= 3;
		}
		else if ( ! this.currentAd )
			this.doNext();
		
		this.timer = setTimeout( this.name + '.doNext()', (this.currentAd.type == SCROLL_LEFT || this.currentAd.type == SCROLL_RIGHT ? this.currentAd.duration : this.currentAd.delay) );
	}
	
	/**
	 *
	 */
	this.pause = function()
	{
		if ( this.currentAd.type != SCROLL_LEFT && this.currentAd.type != SCROLL_RIGHT )
			clearTimeout( this.timer );
	}
	
	/**
	 *
	 */
	this.resume = function()
	{
		if ( this.currentAd.type != SCROLL_LEFT && this.currentAd.type != SCROLL_RIGHT )
			this.timer = setTimeout( this.name + '.doNext()', (this.currentAd.type == SCROLL_LEFT || this.currentAd.type == SCROLL_RIGHT ? this.currentAd.duration : this.currentAd.delay) );
	}
	
	/**
	 *
	 */
	this.doNext = function()
	{
		if ( ! this.currentAd || this.currentAd.type == SCROLL_LEFT || this.currentAd.type == SCROLL_RIGHT || this.currentAd && $('#'+this.currentAd.object).queue().length == 0 )
		{
			clearTimeout( this.timer );
			
			this.index++;
			this.next();
			this.exec();
		}
	}
	
	/**
	 *
	 */
	this.doPrevious = function()
	{
		if ( ! this.currentAd || this.currentAd.type == SCROLL_LEFT || this.currentAd.type == SCROLL_RIGHT || this.currentAd && $('#'+this.currentAd.object).queue().length == 0 )
		{
			clearTimeout( this.timer );
			
			this.currentAd = this.list[ this.index ];
			this.index--;
			this.previous();
			this.exec();
		}
	}
	
	/**
	 *
	 */
	this.next = function()
	{
		if ( this.index >= this.list.length )
			this.index = 0;
		
		this.nextAd = this.list[ this.index ];
		
		if ( this.currentAd && this.list.length > 1 && this.currentAd.opposite )
			this.nextAd.transition( null, this.currentAd );
			
		this.currentAd = this.list[ this.index ];
		this.currentAd.transition( 1 );
		
	//	if ( this.currentAd.type != SCROLL_LEFT && this.currentAd.type != SCROLL_RIGHT )
	//		$.post( '/library/ajax/advert-impression.php', 'id=' + this.currentAd.id );
	}
	
	/**
	 *
	 */
	this.previous = function()
	{
		if ( this.index == -1 )
			this.index = this.list.length - 1;
		
		this.nextAd = this.list[ this.index ];
		
		if ( this.currentAd && this.list.length > 1 && this.currentAd.opposite )
			this.nextAd.opposite( null, this.currentAd );
			
		this.currentAd = this.list[ this.index ];
		this.currentAd.opposite( 1 );
		
	//	if ( this.currentAd.type != SCROLL_LEFT && this.currentAd.type != SCROLL_RIGHT )
	//		$.post( '/library/ajax/advert-impression.php', 'id=' + this.currentAd.id );
	}
	
	/**
	 *
	 */
	this.notAnimated = function( start, obj )
	{
		var	obj = $( '#' + (obj ? obj.object : this.object) );
		
		obj.css( 'left', 0 );
		obj.css( 'top', 0 );
		
		if ( start )
			obj.show();
		else
			obj.hide();
	}
	
	/**
	 *
	 */
	this.fade = function( start, obj )
	{
		var	obj = $( '#' + (obj ? obj.object : this.object) );
		
		obj.css( 'left', 0 );
		obj.css( 'top', 0 );
		
		if ( start )
			obj.fadeIn( this.duration );
		else
			obj.fadeOut( this.duration );
	}
	
	/**
	 *
	 */
	this.scrollLeft = function()
	{
		var	obj = $('#'+this.object);
		
		obj.stop();
		obj.show();
		obj.css( 'left', 0 );
		obj.animate( {left:'-=' + (obj.width() / 2)}, this.duration, 'linear' );
	}
	
	/**
	 *
	 */
	this.scrollRight = function()
	{
		var	obj = $('#'+this.object);
		
		obj.stop();
		obj.show();
		obj.css( 'left', -(obj.width() / 2) );
		obj.animate( {left:'+=' + (obj.width() / 2)}, this.duration, 'linear' );
	}
	
	/**
	 *
	 */
	this.slideLeft = function( start, obj )
	{
		var	obj = $( '#' + (obj ? obj.object : this.object) );
		
		obj.css( 'top', 0 );
		
		if ( start )
		{
			obj.css( 'left', obj.width() );
			obj.show();
			obj.animate( {left:'-=' + obj.width()}, this.duration );
		}
		else
		{
			obj.css( 'left', 0);
			obj.animate( {left:'-=' + obj.width()}, this.duration, function() {obj.hide()} );
		}
	}
	
	/**
	 *
	 */
	this.slideRight = function( start, obj )
	{
		var	obj = $( '#' + (obj ? obj.object : this.object) );
		
		obj.css( 'top', 0 );
		
		if ( start )
		{
			obj.css( 'left', -obj.width() );
			obj.show();
			obj.animate( {left:'+=' + obj.width()}, this.duration );
		}
		else
		{
			obj.css( 'left', 0);
			obj.animate( {left:'+=' + obj.width()}, this.duration, function() {obj.hide()} );
		}
	}
	
	/**
	 *
	 */
	this.slideTop = function( start, obj )
	{
		var	obj = $( '#' + (obj ? obj.object : this.object) );
		
		obj.css( 'left', 0 );
		
		if ( start )
		{
			obj.css( 'top', obj.height() );
			obj.show();
			obj.animate( {top:'-=' + obj.height()}, this.duration );
		}
		else
		{
			obj.css( 'top', 0 );
			obj.animate( {top:'-=' + obj.height()}, this.duration, function() {obj.hide()} );
		}
	}
	
	/**
	 *
	 */
	this.slideBottom = function( start, obj )
	{
		var	obj = $( '#' + (obj ? obj.object : this.object) );
		
		obj.css( 'left', 0 );
		
		if ( start )
		{
			obj.css( 'top', 0 - obj.height() );
			obj.show();
			obj.animate( {top:'+=' + obj.height()}, this.duration );
		}
		else
		{
			obj.css( 'top', 0 );
			obj.animate( {top:'+=' + obj.height()}, this.duration, function() {obj.hide()} );
		}
	}
	
	/**
	 *
	 */
	this.setBlock = function( name, x, y, url )
	{
		$('#' + name + this.name + '-' + x + '-' + y ).css( {
			'top'					: y * this.sizeY,
			'left'					: x * this.sizeX,
			'background-image'		: 'url(' + url + ')',
			'background-position'	: -(x * this.sizeX) + 'px ' + (-(y * this.sizeY) + 'px'),
			'background-repeat'		: 'no-repeat',
			'z-index'				: this.zindex
		} );
	}
	
	/**
	 *
	 */
	this.blocksSlideTop = function( start, obj )
	{
		var	name	= ( obj ? obj.object : this.object ),
			obj		= $( '#' + name );
		
		obj.css( 'left', 0 );
		obj.css( 'top', 0 );
		
		if ( start )
		{
			for( var y = this.parent.tiles.length - 1, n = 0; y >= 0 ; y--, n++ )
				for( var x = 0; x < this.parent.tiles[y].length; x++ )
				{
					this.parent.setBlock( 'tilein_', x, y, obj.find('img').attr('src') );
					$('#tilein_' + this.parent.name + '-' + x + '-' + y ).hide();
					$('#tilein_' + this.parent.name + '-' + x + '-' + y ).fadeIn( n * (this.duration / 10) );
				}
			
			setTimeout( "$('#" + name + "').show();$(\"div[id*='tilein']\").hide();", this.duration );
		}
		else
		{
			for( var y = this.parent.tiles.length - 1, n = 0; y >= 0 ; y--, n++ )
				for( var x = 0; x < this.parent.tiles[y].length; x++ )
				{
					this.parent.setBlock( 'tileout_', x, y, obj.find('img').attr('src') );
					$('#tileout_' + this.parent.name + '-' + x + '-' + y ).show();
					$('#tileout_' + this.parent.name + '-' + x + '-' + y ).fadeOut( n * (this.duration / 10) );
				}
			
			setTimeout( "$('#" + name + "').hide();", this.duration );
		}
	}
	
	/**
	 *
	 */
	this.blocksSlideBottom = function( start, obj )
	{
		var	name	= ( obj ? obj.object : this.object ),
			obj		= $( '#' + name );
		
		obj.css( 'left', 0 );
		obj.css( 'top', 0 );
		
		if ( start )
		{
			for( var y = 0; y < this.parent.tiles.length; y++ )
				for( var x = 0; x < this.parent.tiles[y].length; x++ )
				{
					this.parent.setBlock( 'tilein_', x, y, obj.find('img').attr('src') );
					$('#tilein_' + this.parent.name + '-' + x + '-' + y ).hide();
					$('#tilein_' + this.parent.name + '-' + x + '-' + y ).fadeIn( y * (this.duration / 10) );
				}
			
			setTimeout( "$('#" + name + "').show();$(\"div[id*='tilein']\").hide();", this.duration );
		}
		else
		{
			for( var y = 0; y < this.parent.tiles.length; y++ )
				for( var x = 0; x < this.parent.tiles[y].length; x++ )
				{
					this.parent.setBlock( 'tileout_', x, y, obj.find('img').attr('src') );
					$('#tileout_' + this.parent.name + '-' + x + '-' + y ).show();
					$('#tileout_' + this.parent.name + '-' + x + '-' + y ).fadeOut( y * (this.duration / 10) );
				}
			
			setTimeout( "$('#" + name + "').hide();", this.duration );
		}
	}
	
	/**
	 *
	 */
	this.blocksSlideLeft = function( start, obj )
	{
		var	name	= ( obj ? obj.object : this.object ),
			obj		= $( '#' + name );
		
		obj.css( 'left', 0 );
		obj.css( 'top', 0 );
		
		if ( start )
		{
			for( var y = 0; y < this.parent.tiles.length; y++ )
				for( var x = this.parent.tiles[y].length - 1, n = 0; x >= 0 ; x--, n++ )
				{
					this.parent.setBlock( 'tilein_', x, y, obj.find('img').attr('src') );
					$('#tilein_' + this.parent.name + '-' + x + '-' + y ).hide();
					$('#tilein_' + this.parent.name + '-' + x + '-' + y ).fadeIn( n * (this.duration / 10) );
				}
			
			setTimeout( "$('#" + name + "').show();$(\"div[id*='tilein']\").hide();", this.duration );
		}
		else
		{
			for( var y = 0; y < this.parent.tiles.length; y++ )
				for( var x = this.parent.tiles[y].length - 1, n = 0; x >= 0 ; x--, n++ )
				{
					this.parent.setBlock( 'tileout_', x, y, obj.find('img').attr('src') );
					$('#tileout_' + this.parent.name + '-' + x + '-' + y ).show();
					$('#tileout_' + this.parent.name + '-' + x + '-' + y ).fadeOut( n * (this.duration / 10) );
				}
			
			setTimeout( "$('#" + name + "').hide();", this.duration );
		}
	}
	
	/**
	 *
	 */
	this.blocksSlideRight = function( start, obj )
	{
		var	name	= ( obj ? obj.object : this.object ),
			obj		= $( '#' + name );
		
		obj.css( 'left', 0 );
		obj.css( 'top', 0 );
		
		if ( start )
		{
			for( var y = 0; y < this.parent.tiles.length; y++ )
				for( var x = 0; x < this.parent.tiles[y].length; x++ )
				{
					this.parent.setBlock( 'tilein_', x, y, obj.find('img').attr('src') );
					$('#tilein_' + this.parent.name + '-' + x + '-' + y ).hide();
					$('#tilein_' + this.parent.name + '-' + x + '-' + y ).fadeIn( x * (this.duration / 10) );
				}
			
			setTimeout( "$('#" + name + "').show();$(\"div[id*='tilein']\").hide();", this.duration );
		}
		else
		{
			for( var y = 0; y < this.parent.tiles.length; y++ )
				for( var x = 0; x < this.parent.tiles[y].length; x++ )
				{
					this.parent.setBlock( 'tileout_', x, y, obj.find('img').attr('src') );
					$('#tileout_' + this.parent.name + '-' + x + '-' + y ).show();
					$('#tileout_' + this.parent.name + '-' + x + '-' + y ).fadeOut( x * (this.duration / 10) );
				}
			
			setTimeout( "$('#" + name + "').hide();", this.duration );
		}
	}
	
	/**
	 *
	 */
	this.blocksSwirl = function( start, obj )
	{
		var	name	= ( obj ? obj.object : this.object ),
			obj		= $( '#' + name );
		
		obj.css( 'left', 0 );
		obj.css( 'top', 0 );
		
		if ( start )
		{
			xstart	= 0;
			xfinish	= this.parent.tiles[0].length - 1;
			ystart	= 0;
			yfinish	= this.parent.tiles.length - 1;
			nn		= 1;
			
			for( var n = 0; n < 4; n++ )
			{
				for( var x = xstart; x <= xfinish; x++ )
				{
					this.parent.setBlock( 'tilein_', x, ystart, obj.find('img').attr('src') );
					$('#tilein_' + this.parent.name + '-' + x + '-' + ystart ).hide();
					$('#tilein_' + this.parent.name + '-' + x + '-' + ystart ).fadeIn( ++nn * (this.duration / 100) );
				}
				
				ystart++;
				
				for( var y = ystart; y <= yfinish; y++ )
				{
					this.parent.setBlock( 'tilein_', xfinish, y, obj.find('img').attr('src') );
					$('#tilein_' + this.parent.name + '-' + xfinish + '-' + y ).hide();
					$('#tilein_' + this.parent.name + '-' + xfinish + '-' + y ).fadeIn( ++nn * (this.duration / 100) );
				}
				
				xfinish--;
				
				for( var x = xfinish; x >= xstart; x-- )
				{
					this.parent.setBlock( 'tilein_', x, yfinish, obj.find('img').attr('src') );
					$('#tilein_' + this.parent.name + '-' + x + '-' + yfinish ).hide();
					$('#tilein_' + this.parent.name + '-' + x + '-' + yfinish ).fadeIn( ++nn * (this.duration / 100) );
				}
				
				yfinish--;
				
				for( var y = yfinish; y >= ystart; y-- )
				{
					this.parent.setBlock( 'tilein_', xstart, y, obj.find('img').attr('src') );
					$('#tilein_' + this.parent.name + '-' + xstart + '-' + y ).hide();
					$('#tilein_' + this.parent.name + '-' + xstart + '-' + y ).fadeIn( ++nn * (this.duration / 100) );
				}
				
				xstart++;
			}
			
			setTimeout( "$('#" + name + "').show();$(\"div[id*='tilein']\").hide();", this.duration );
		}
		else
		{
			xstart	= 0;
			xfinish	= this.parent.tiles[0].length - 1;
			ystart	= 0;
			yfinish	= this.parent.tiles.length - 1;
			nn		= 1;
			
			for( var n = 0; n < 4; n++ )
			{
				for( var x = xstart; x <= xfinish; x++ )
				{
					this.parent.setBlock( 'tileout_', x, ystart, obj.find('img').attr('src') );
					$('#tileout_' + this.parent.name + '-' + x + '-' + ystart ).show();
					$('#tileout_' + this.parent.name + '-' + x + '-' + ystart ).fadeOut( ++nn * (this.duration / 100) );
				}
				
				ystart++;
				
				for( var y = ystart; y <= yfinish; y++ )
				{
					this.parent.setBlock( 'tileout_', xfinish, y, obj.find('img').attr('src') );
					$('#tileout_' + this.parent.name + '-' + xfinish + '-' + y ).show();
					$('#tileout_' + this.parent.name + '-' + xfinish + '-' + y ).fadeOut( ++nn * (this.duration / 100) );
				}
				
				xfinish--;
				
				for( var x = xfinish; x >= xstart; x-- )
				{
					this.parent.setBlock( 'tileout_', x, yfinish, obj.find('img').attr('src') );
					$('#tileout_' + this.parent.name + '-' + x + '-' + yfinish ).show();
					$('#tileout_' + this.parent.name + '-' + x + '-' + yfinish ).fadeOut( ++nn * (this.duration / 100) );
				}
				
				yfinish--;
				
				for( var y = yfinish; y >= ystart; y-- )
				{
					this.parent.setBlock( 'tileout_', xstart, y, obj.find('img').attr('src') );
					$('#tileout_' + this.parent.name + '-' + xstart + '-' + y ).show();
					$('#tileout_' + this.parent.name + '-' + xstart + '-' + y ).fadeOut( ++nn * (this.duration / 100) );
				}
				
				xstart++;
			}
			
			setTimeout( "$('#" + name + "').hide();", this.duration );
		}
	}
	
	/**
	 *
	 */
	this.blocksRandom = function( start, obj )
	{
		var	name	= ( obj ? obj.object : this.object ),
			obj		= $( '#' + name );
		
		obj.css( 'left', 0 );
		obj.css( 'top', 0 );
		
		if ( start )
		{
			for( var y = 0; y < this.parent.tiles.length; y++ )
				for( var x = 0; x < this.parent.tiles[y].length; x++ )
				{
					this.parent.setBlock( 'tilein_', x, y, obj.find('img').attr('src') );
					$('#tilein_' + this.parent.name + '-' + x + '-' + y ).hide();
					$('#tilein_' + this.parent.name + '-' + x + '-' + y ).fadeIn( Math.floor( Math.random() * (this.duration / 2) ) );
				}
			
			setTimeout( "$('#" + name + "').show();$(\"div[id*='tilein']\").hide();", this.duration );
		}
		else
		{
			for( var y = 0; y < this.parent.tiles.length; y++ )
				for( var x = 0; x < this.parent.tiles[y].length; x++ )
				{
					this.parent.setBlock( 'tileout_', x, y, obj.find('img').attr('src') );
					$('#tileout_' + this.parent.name + '-' + x + '-' + y ).show();
					$('#tileout_' + this.parent.name + '-' + x + '-' + y ).fadeOut( Math.floor( Math.random() * (this.duration / 2) ) );
				}
			
			setTimeout( "$('#" + name + "').hide();", this.duration );
		}
	}
}
