var My;
if ( !My ) My = {};

My.Riegger_Topstory = Class.create();

My.Riegger_Topstory.topstoryCounter = 0;

My.Riegger_Topstory.prototype =
{	
	IMAGE: 1,
	FLASH: 2,
	IMAGEANDTEXT: 3,
	initialize: function( id, height, txtOpen, txtClose, opts, firstTopstoryId ) 
	{
		this.options = 
		{
			duration:			0.5,
			isOpen:				false,
			showHideElement:	'showHideTopStory',
			classOpen:			'arrowOpen',
			classClose:			'arrowClose',
			topstoryId:			'topstorycurrentid',
			fadeDuration:		1.0,
			topstoryId:			'topstoryItemContent'
		}
		
		Object.extend(this.options, opts || {});
		
		this.id = id;
		this.height = height;
		this.txtOpen = txtOpen;
		this.txtClose = txtClose;
		this.running = false;
		this.lastTopStory = firstTopstoryId;
		this.lastTopStoryObject = null;
		
		this.currentReplaceEffectActive = false;
	},
	
	showHideTopStory: function ()
	{
		if ( !this.running )
		{
			this.running = true;
			
			var height = this.height;
			if ( this.options.isOpen )
			{
				height = 0;
			}
			
			new Effect.Move( 'mainContainer', { y: height, mode: 'absolute', duration: this.options.duration, afterFinish: this._afterShowHideTopStory.bindAsEventListener( this ) } );
		}
	},
	
	_afterShowHideTopStory: function()
	{
		this.options.isOpen = !this.options.isOpen;
		if ( this.options.isOpen )
		{
			$(this.options.showHideElement).removeClassName( this.options.classOpen );
			$(this.options.showHideElement).addClassName( this.options.classClose );
			
			$(this.options.showHideElement).title = this.txtOpen;
		}
		else
		{
			$(this.options.showHideElement).removeClassName( this.options.classClose );
			$(this.options.showHideElement).addClassName( this.options.classOpen );
			
			$(this.options.showHideElement).title = this.txtClose;
		}
		mySetCookie( 'topstorystatus', this.options.isOpen );
		this.running = false;
	},
	
	replaceTopStory: function( topstoryId, newSource, type, text, title, link )
	{
		if( Object.isUndefined( type ) )
		{
			type = this.IMAGE;
		}
		
		if( !this.options.isOpen )
		{
			this.showHideTopStory();
		}
		
		if( this.lastTopStory != topstoryId && !this.currentReplaceEffectActive )
		{	
			var topstorycontent = $( "topstoryContent" );
				
			var oldTopstory;	
			if( this.lastTopStoryObject == null )
			{
				oldTopstory = $( this.options.topstoryId + this.lastTopStory );
			}
			else
			{
				oldTopstory = this.lastTopStoryObject;
			}
			
			this.currentReplaceEffectActive = true;
			
			var newContent, textHolder, mycontainer;
			
			switch( type )
			{
				case this.IMAGE:
					
					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: 990px; height: 270px;", alt: "" } ) );
					}
					
					newContent.id = this.options.topstoryId + topstoryId;
					newContent.setOpacity( 0 ).hide();
					topstorycontent.insert( { "top": newContent } );
					if( !Object.isUndefined( linkHolder ) )
					{
						topstorycontent.insert( { top: linkHolder } );	
					}
					break;
				case this.FLASH:
					var newContent = new Element( "div", { id: "flashholder", style: "position: absolute; z-index: 2" } ).setOpacity( 0 ).hide();
					newContent.setStyle( { position: "absolute", zIndex: 2 } ).setOpacity( 0 ).hide();
					topstorycontent.insert( { "top": newContent } );
					var swfObj = new SWFObject( newSource, this.options.topstoryId + topstoryId, 990, 270, '9.0.28', '#000000');
					swfObj.addParam('quality', 'high');
					swfObj.addParam('scale', 'noscale');
					swfObj.addParam('allowFullscreen','true');
					swfObj.addParam('allowScriptAccess','always');
					swfObj.addParam('align', 'middle');
					swfObj.write(newContent);
					break;
				case this.IMAGEANDTEXT:
					var boxHolder = new Element( "div", { style: "z-index: 2; position: absolute" } );
					var imageHolder = new Element( "img", { src: newSource, alt: "" } );
					
					// Hacky way to prevent text opacity
					var textHolder = new Element( "div", { id: "topstoryTextHolder" + topstoryId, style: "z-index: 3; position: absolute" } ).addClassName( "topstoryTextBackground" ).setOpacity( 0 );
					
					var tTitle = new Element( "div" ).update( title ).addClassName( "topstoryTitleHidden" );
					textHolder.insert( { "top": tTitle } );
					var tText = new Element( "div" ).update( text ).addClassName( "topstoryTextHidden" );
					textHolder.insert( { "bottom": tText } );
					
					var mycontainer = new Element( "div", { id: "topstoryTextContainer" + topstoryId, style: "z-index: 3; position: absolute" } ).addClassName( "topstoryTextContainer" ).setOpacity( 0 );
					var cTitle = new Element( "div" ).update( title ).addClassName( "topstoryTitle" );
					mycontainer.insert( { "top": cTitle } );
					var cText = new Element( "div" ).update( text ).addClassName( "topstoryText" );
					mycontainer.insert( { "bottom": cText } );
					
					var imageWithLink = imageHolder, linkHolder;
					if( link != "" && !Object.isUndefined( link ) )
					{
						linkHolder = new Element( "a", { id: "topstoryLink" + topstoryId, href: link } ).addClassName( "topstoryLink" ).update( new Element( "img", { src: "../standard/img/empty.gif", style: "width: 990px; height: 270px;", alt: "" } ) );
					}
					boxHolder.insert( { "bottom": imageWithLink } );
					newContent = boxHolder;
					newContent.id = this.options.topstoryId + topstoryId;
					newContent.setOpacity( 0 ).hide();
					topstorycontent.insert( { "top": newContent, "before": textHolder } );
					topstorycontent.insert( { "before": mycontainer } );
					if( !Object.isUndefined( linkHolder ) )
					{
						topstorycontent.insert( { top: linkHolder } );	
					}
					break;
			}
			
			var faOpacity = parseFloat( $( "topstoryNaviContainer" ).getStyle( "opacity" ) );
			
			this.lastTopStoryObject = newContent;
			
			if( !Object.isUndefined( oldTopstory ) )
			{
				newContent.show();
				if( type != this.FLASH )
				{
					if( !Element.myIsExisting( "topstoryTextHolder" + this.lastTopStory ) )
					{
						new Effect.Parallel( [
							new Effect.Fade( oldTopstory, { 
								from: 1.0, 
								to: 0.0, 
								sync: true
							} ),
							
							new Effect.Appear( $( "topstoryTextHolder" + topstoryId ), {
								from: 0.0,
								to: 0.5,
								sync: true
							} ),
							new Effect.Appear( $( "topstoryTextContainer" + topstoryId ), {
								sync: true
							} ),
							new Effect.Appear( newContent, { 
								from: 0.0, 
								to: 1.0, 
								sync: true
							} ),
							new Effect.Fade( $( "topstoryNaviContainer" ), { 
	
								from: faOpacity, 
								to: 0.5, 
								sync: true 
							} ),
							new Effect.Morph( $( "topstoryNaviContainer" ), {
								style: {
									backgroundColor: "#000000"
								},
								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.lastTopStory = topstoryId;
											}.bind(this)
						} );
					}
					else
					{
						new Effect.Parallel( [
							new Effect.Fade( oldTopstory, { 
								from: 1.0, 
								to: 0.0, 
								sync: true
							} ),
							new Effect.Fade( $( "topstoryTextHolder" + this.lastTopStory ), { 
								from: 0.5, 
								to: 0.0, 
								sync: true
							} ),
							new Effect.Fade( $( "topstoryTextContainer" + this.lastTopStory ), { 
								sync: true
							} ),
							new Effect.Appear( $( "topstoryTextHolder" + topstoryId ), {
								from: 0.0,
								to: 0.5,
								sync: true
							} ),
							new Effect.Appear( $( "topstoryTextContainer" + topstoryId ), {
								sync: true
							} ),
							new Effect.Appear( newContent, { 
								from: 0.0, 
								to: 1.0, 
								sync: true
							} ),
							new Effect.Fade( $( "topstoryNaviContainer" ), { 
	
								from: faOpacity, 
								to: 0.5, 
								sync: true 
							} ),
							new Effect.Morph( $( "topstoryNaviContainer" ), {
								style: {
									backgroundColor: "#000000"
								},
								sync: true
							} )
						], { 
							duration: this.options.fadeDuration,
							afterFinish: function( e )
											{ 
												oldTopstory.remove(); 
												$( "topstoryTextContainer" + this.lastTopStory ).remove();
												$( "topstoryTextHolder" + this.lastTopStory ).remove();
												if( Element.myIsExisting( "topstoryLink" + this.lastTopStory ) )
												{
													$( "topstoryLink" + this.lastTopStory ).remove();	
												}
												this.currentReplaceEffectActive = false; 
												this.lastTopStory = topstoryId;
											}.bind(this)
						} );
					}
				}
				else
				{
					new Effect.Parallel( [
						new Effect.Fade( oldTopstory, { 
							from: 1.0, 
							to: 0.0,
							sync: true
						} ), 
						new Effect.Fade( $( "topstoryTextHolder" + this.lastTopstory ), {
							from: 0.5,
							to: 0.0,
							sync: true
						} ),
						new Effect.Appear( $( "topstoryTextHolder" + topstoryId ), {
							sync: true
						} ),
						new Effect.Appear( newContent, { 
							from: 0.0, 
							to: 1.0, 
							sync: true
						} ),
						new Effect.Appear( $( "topstoryNaviContainer" ), { 
							from: faOpacity, 
							to: 1.0, 
							sync: true 
						} ),
						new Effect.Morph( $( "topstoryNaviContainer" ), {
							style: {
								backgroundColor: "#141414"
							},
							sync: true
						} )
					], { 
						duration: this.options.fadeDuration,
						afterFinish: function( e )
										{ 
											oldTopstory.remove(); 
											if( Element.myIsExisting( "topstoryTextHolder" + this.lastTopStory ) )
											{
												$( "topstoryTextContainer" + this.lastTopStory ).remove();
												$( "topstoryTextHolder" + this.lastTopStory ).remove();
											}
											this.currentReplaceEffectActive = false; 
											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)
		} );
	}
}