var My;

if ( !My ) My = {};

My.ALPIQ_Topstory = Class.create();

My.ALPIQ_Topstory.topstoryCounter = 0;

My.ALPIQ_Topstory.prototype =
{	
	initialize: function( id, opts, firstTopstoryId, topStories ) 
	{
		this.options = 
		{
			duration:			2,
			topstoryId:			'topstorycurrentid',
			fadeDuration:		0.5,
			topstoryId:			'topstoryItemContent',
			uniqueId:			'topstoryItemId',
			activeClassName:	'topstoryActive',
			autoLoad:			true,
			periodForAutoLoad:	5
		}
		
		Object.extend(this.options, opts || {});
		
		this.id = id;
		this.running = false;
		this.lastTopStory = 0;
		this.topStories = topStories;
		
		this.currentReplaceEffectActive = false;
		
		document.observe( 'dom:loaded', function() {
			this.replaceTopStory( firstTopstoryId, this.options.autoLoad );
			new PeriodicalExecuter( function( pe ){ if ( this.options.autoLoad ){ this._autoLoad( -1 ); }else{ pe.stop(); } }.bind( this ), this.options.periodForAutoLoad );
		}.bind( this ) );
	},
	
	_autoLoad: function ( topStoryId )
	{
		if ( topStoryId === -1 )
		{
			for ( var i = 0; i < this.topStories.length; i++ )
			{
				if ( this.topStories[i].topstoryId === this.lastTopStory )
				{
					if ( i + 1 < this.topStories.length )
					{
						topStoryId = this.topStories[i + 1].topstoryId;
					}
					else
					{
						topStoryId = this.topStories[0].topstoryId;
					}
					break;
				}
			}
		}
		this.replaceTopStory( topStoryId, true );
	},
	
	replaceTopStory: function( topstoryId, autoLoad )
	{
		this.options.autoLoad = autoLoad;
		if( ( this.lastTopStory != topstoryId || autoLoad ) && !this.currentReplaceEffectActive )
		{
			this.currentReplaceEffectActive = true;
			
			var topStory = null;
			for ( var i = 0; i < this.topStories.length; i++ )
			{
				if ( this.topStories[i].topstoryId === topstoryId )
				{
					topStory = this.topStories[i]
					break;
				}
			}
			var newSource = topStory.img;
			var text = topStory.subtitle;
			var title = topStory.title;
			var link = topStory.link;
			
			var topstorycontent = $( "topstoryContent" );
			var topstoryItemContentTitle = $("topstoryItemContentTitle");
			var topstoryItemContentLead = $("topstoryItemContentLead");
			topstoryItemContentTitle.update(title);
			topstoryItemContentLead.update(text);
			
			var oldTopstory = $( this.options.topstoryId + this.lastTopStory );
			
			var newContent, textHolder, mycontainer, newTitle;
			
			newContent = new Element( "img", { src: newSource, alt: "", style: "z-index: 2; position: absolute;" } );
			/*
			var linkHolder;
			if( link != "" && !Object.isUndefined( link ) )
			{
				linkHolder = new Element( "a", { href: link } ).addClassName( "topstoryLink" ).update( new Element( "img", { src: "../standard/img/empty.gif", style: "width: 674px; height: 302px;", alt: "" } ) );
				linkHolder.id = "topstoryLink" + topstoryId;
			}
			*/
			$( "topstoryInfoImgLink" ).href = topStory.link;
			$( "topstoryInfoMoreLink" ).href = topStory.link;
			
			newContent.id = this.options.topstoryId + topstoryId;
			newContent.setOpacity( 0 ).hide();
			topstorycontent.insert( { "top": newContent } );
			/*
			if( !Object.isUndefined( linkHolder ) )
			{
				topstorycontent.insert( { top: linkHolder } );	
			}
			*/
			newContent.show();
			
			if( oldTopstory != null && !Object.isUndefined( oldTopstory ) )
			{
				newContent.show();

				new Effect.Parallel( [
					new Effect.Fade( oldTopstory, { 
						from: 1.0, 
						to: 0.0, 
						sync: true
					} ),
					new Effect.Appear( newContent, { 
						from: 0.0, 
						to: 1.0, 
						sync: true
					} )
				], { 
					duration: this.options.fadeDuration,
					afterFinish: function( e )
									{ 
										oldTopstory.remove(); 
										if( Element.myIsExisting( "topstoryLink" + this.lastTopStory ) )
										{
											$( "topstoryLink" + this.lastTopStory ).remove();	
										}
										this.currentReplaceEffectActive = false;
										$( this.options.uniqueId + topstoryId ).addClassName( "activeTopstoryItemContainer" );
										$( this.options.uniqueId + this.lastTopStory ).removeClassName( "activeTopstoryItemContainer" );
										this.lastTopStory = topstoryId;
									}.bind(this)
				} );
			}
			else
			{
				this._appearContent( oldTopstory, newContent );
				this.lastTopStory = topstoryId;
			}
		}
	},
	
	_appearContent: function( oldContent, newContent )
	{
		if( oldContent )
		{
			oldContent.remove();
		}
		newContent.show();
		new Effect.Appear( newContent, { 
			from: 0.0, 
			to: 1.0, 
			duration: this.options.fadeDuration,
			afterFinish: function( e )
							{
								this.currentReplaceEffectActive = false; 
							}.bind(this)
		} );
	}
};