// JavaScript Document
var tagHeaders = function(){
	
	this.headers = new Array( );
	this.index = 0;
	this.bulletActive = new Image ( );
	this.bulletActive.src = '/images/site/bulletRed.png';

	this.bulletNotActive = new Image ( );
	this.bulletNotActive.src = '/images/site/bulletWhite.png';
	this.slideTimeout = '10000';
		
	this.createheader = function ( image,firstTwo,taglineTop,taglineBottom,refLink )
	{
		var tmp = new Array( )
		tmp['image'] = image;
		tmp['tagLineTop'] = taglineTop;
		tmp['tagLineBottom'] = taglineBottom;
		tmp['tagLineTopFirst'] = firstTwo;
		tmp['link'] = refLink;
		var index = this.headers.length++;
		this.headers[ index ] =  tmp;
	}
	
	this.initHeaders = function ( basePath,containerEl,tagContainer,noSlide,randomHeader )
	{
		if ( randomHeader )
		{
			rndNum = parseInt ( Math.random() * this.headers.length);

			if ( this.initAHeader( rndNum,basePath,containerEl,tagContainer,noSlide ) );
				this.displayHeader ( rndNum);	
		}
		else
			for ( var x in this.headers	)
				this.initAHeader( x,basePath,containerEl,tagContainer,noSlide );
		
		var tmpHead= this;
		if ( !noSlide )
		{
				//init sliders
			$( '.navigationBoxLeft' ).click( function() { tmpHead.openHeader( tmpHead.index - 1,tmpHead  ) } );
			$( '.navigationBoxRight' ).click( function() { tmpHead.openHeader( tmpHead.index + 1,tmpHead ) } );
			tmpHead.openHeader ( tmpHead.index,tmpHead );
		}
		
	 }
	 
	this.initAHeader= function ( x,basePath,containerEl,tagContainer,noSlide )
	{

		var tmpEl = document.createElement( 'img' );
		tmpEl.src = basePath + this.headers[x].image;
		tmpEl.style.position = 'absolute';
		tmpEl.style.zIndex = '-1';
		tmpEl.className= 'header' + x;
		if ( x > 0 )
			tmpEl.style.display = 'none';
			
		$( containerEl ).append ( tmpEl );
		
		//add the taglne
		var tmp = $( '#tagLineWrapperBase' ).clone();
		tmp.addClass( 'header' + x );
		tmp.attr( 'id','header' +x )
		$( '.tagLineTitle',$( tmp )).html( '<a href="'+this.headers[x].link+'">'+this.headers[x].tagLineTopFirst+'</a>'+this.headers[x].tagLineTop );
		$( '.tagLineLink',$( tmp )).html( this.headers[x].tagLineBottom );
		
		if ( x > 0 )
			tmp.css ( 'display', 'none' );
		else
			tmp.css ( 'display', 'block' );
		$( tagContainer ).append ( tmp );

		var tmpHead = this.headers[x];
		$( '.tagLineLinkWrapper').click( function( ){ location.href=tmpHead.link; } );

		//add the bullet
		var tmp = document.createElement( 'div' );
		tmp.className= 'bulletImage';
		if ( !noSlide )
		{
			var tmpBullet = (  x > 0 ? this.bulletNotActive.cloneNode( true ) : this.bulletActive.cloneNode( true ) );
			tmpBullet.id = "bullet" + x;
			tmp.appendChild ( tmpBullet );
			$( '.bulletWrapper' ).append( tmp );
		}	
		
		return true;
	}
	
	this.openHeader = function ( indexSlide,tmpHead,noSlide )
	{
		if (  this.slideHeaderTimer  )
			clearTimeout ( this.slideHeaderTimer );
		hObj = tmpHead.headers;
		$( '.header' +tmpHead.index ).fadeOut( 'fast', function()
		{ 
			if ( $(this).attr( 'id' ) )
			{	
				if ( !noSlide ) //change the bullet
					$( "#bullet" + tmpHead.index ).attr ( 'src',tmpHead.bulletNotActive.src );

				if ( !hObj[ indexSlide ] && indexSlide < 0 )
					indexSlide = hObj.length-1;	
				else if ( !hObj[ indexSlide ] )
					indexSlide = 0;
					
				$( "#bullet" +indexSlide ).attr ( 'src',tmpHead.bulletActive.src );
				
				tmpHead.index = indexSlide;	
				$( '.header' + indexSlide ).fadeIn ( 'fast', function() 
				{ 
					if ( $(this).attr( 'id' ) )
					{
						if(typeof sIFR == "function" && !sIFR.UA.bIsIEMac && (!sIFR.UA.bIsWebKit || sIFR.UA.nWebKitVersion >= 100))
						{
							sIFR.replaceElement(".header" + indexSlide + " .tagLineTitle", named(
								 {sFlashSrc: "/js/site/dinbold.swf", sColor: "#ffffff",sLinkColor:"#ff0000", sWmode:"transparent"}
							  ));  
							sIFR.replaceElement(".header" + indexSlide + " .tagLineLink", named(
								 {sFlashSrc: "/js/site/dinlight.swf", sColor: "#ffffff", sWmode:"transparent"}
							  )); 
							sIFR.replaceElement(".header" + indexSlide + " .tagLineLinkContent", named(
								 {sFlashSrc: "/js/site/dinbold.swf", sColor: "#ffffff", sWmode:"transparent"}
							  ));  
							
							sIFR.replaceElement(".header" + indexSlide + " .tagLineTitleContent", named(
								 {sFlashSrc: "/js/site/dinbold.swf", sColor: "#ffffff", sWmode:"transparent"}
							  )); 
						}	
						if ( !noSlide )
							tmpHead.slideHeaderTimer = setTimeout ( function() { tmpHead.openHeader ( indexSlide + 1,tmpHead ); },tmpHead.slideTimeout );
					}
				});		
			}
		});
	}
	
	this.displayHeader =function ( indexSlide )
	{
		$( '.header' + indexSlide ).css( 'display','block' );
		
		if(typeof sIFR == "function" && !sIFR.UA.bIsIEMac && (!sIFR.UA.bIsWebKit || sIFR.UA.nWebKitVersion >= 100))
		{
			sIFR.replaceElement(".header" + indexSlide + " .tagLineTitle", named(
				 {sFlashSrc: "/js/site/dinbold.swf", sColor: "#ffffff",sLinkColor:"#ff0000", sWmode:"transparent"}
			  ));  
			sIFR.replaceElement(".header" + indexSlide + " .tagLineLink", named(
				 {sFlashSrc: "/js/site/dinlight.swf", sColor: "#ffffff", sWmode:"transparent"}
			  )); 
			sIFR.replaceElement(".header" + indexSlide + " .tagLineLinkContent", named(
				 {sFlashSrc: "/js/site/dinbold.swf", sColor: "#ffffff", sWmode:"transparent"}
			  ));  
			
			sIFR.replaceElement(".header" + indexSlide + " .tagLineTitleContent", named(
				 {sFlashSrc: "/js/site/dinbold.swf", sColor: "#ffffff", sWmode:"transparent"}
			  )); 
		}
	}
}
