WGSN = { GFX:{} };
WGSN.VideoPlayer = { skin:'flash/skin.swf', streamer:'rtmpt://cp46106.edgefcs.net/ondemand/flashondemand', video:'publicsite/creative_intelligence.flv' };


// Navigation
WGSN.Navigation =
{
	setup: function()
	{
		var page = window.location.toString().match( /([^\/]+)$/ );

		if ( page )
		{
			jQuery( '#PrimaryNavigation li a' ).each( function()
			{
				var link = jQuery( this ).attr( 'href' ).match( /([^\/]+)$/ );
				if ( link && link[1] == page[1] )
				{
					jQuery( this ).addClass( 'textColour' ).parent().addClass( 'active' );
					return false;
				}
			} );
		}
		else jQuery( '#PrimaryNavigation li:first a' ).addClass( 'textColour' ).parent().addClass( 'active' );
	}
}
jQuery( document ).ready( WGSN.Navigation.setup );


// Homepage Video Player
WGSN.HomepageVideo =
{
	image: null,
	height: 0,

	setup: function()
	{
		WGSN.HomepageVideo.image = jQuery( '#CreativeVideo' ).bind( 'click', WGSN.HomepageVideo.open );
	},

	open: function(e)
	{
		var container = jQuery( '#CreativeVideoContent' );
		WGSN.HomepageVideo.height = container.height();

		WGSN.HomepageVideo.image.unbind( 'click' );

		container.animate( { opacity:0 }, 750, 'easeInOutQuad', function()
		{
			container.animate( { height:452 }, 500, 'easeInOutQuad', function()
			{
				// create video layer
				var popup_video = jQuery( '<div id="FlashVideo"><div id="FlashVideoPlayer">Flash is required to play this video</div></div>' ).css( { top:container.offset().top, left:container.offset().left } );
				popup_video.append( jQuery( '<a href="#" class="close"></a>' ).bind( 'click', WGSN.HomepageVideo.close ) );
				jQuery( 'body' ).append( popup_video );

				// embed player
				swfobject.embedSWF( 'flash/player.swf', 'FlashVideoPlayer', 750, 422, '9.28.0', null, { file:WGSN.VideoPlayer.home, autostart:'true', controlbar:'over', skin:WGSN.VideoPlayer.skin }, { allowfullscreen:'true' } );
			} );
		} );

		e.preventDefault();
	},

	close: function(e)
	{
		jQuery( '#FlashVideo' ).remove();

		var container = jQuery( '#CreativeVideoContent' );
		container.animate( { height:WGSN.HomepageVideo.height }, 750, 'easeInOutQuad', function()
		{
			container.animate( { opacity:1 }, 500, 'easeInOutQuad', function()
			{
				WGSN.HomepageVideo.setup();
			} );
		} );

		e.preventDefault();	
	}
}
jQuery( document ).ready( WGSN.HomepageVideo.setup );

// Quote Boxes
WGSN.QuoteBoxLists =
{
	list:null,

	setup: function()
	{
		WGSN.QuoteBoxLists.list = jQuery( '#Content .quoteBoxWide .quoteBoxList' );
		WGSN.QuoteBoxLists.list.find( 'li:gt(0)' ).addClass( 'hidden' );

		setTimeout( WGSN.QuoteBoxLists.rotate, 3000 );
	},

	rotate: function()
	{
		var list = WGSN.QuoteBoxLists.list;
		var items = list.find( 'li' );
		var current = list.find( 'li:not(.hidden)' );
		var current_index = items.index( current );
		var next = ++current_index < items.length ? current_index : 0;

		current.css( { position:'absolute', top:0, left:0, width:current.width() } ).animate( { opacity:0 }, 1000, 'easeInOutQuad', function() { jQuery( this ).addClass( 'hidden' ).css( { position:'', width:'' } ) } );
		items.filter( ':eq(' + next + ')' ).removeClass( 'hidden' ).css( { opacity:0 } ).animate( { opacity:1 }, 1000, 'easeInOutQuad', function() { setTimeout( WGSN.QuoteBoxLists.rotate, 5000 ); } );
	}
}
jQuery( document ).ready( WGSN.QuoteBoxLists.setup );

// Event Quote Boxes
WGSN.EventBoxLists =
{
	list:null,

	setup: function()
	{
		WGSN.EventBoxLists.list = jQuery( '#EventTicker .hmpgTickerEvents .eventBoxList' );
		WGSN.EventBoxLists.list.find( 'li:gt(0)' ).addClass( 'hidden' );

		setTimeout( WGSN.EventBoxLists.rotate, 3000 );
	},

	rotate: function()
	{
		var list = WGSN.EventBoxLists.list;
		var items = list.find( 'li' );
		var current = list.find( 'li:not(.hidden)' );
		var current_index = items.index( current );
		var next = ++current_index < items.length ? current_index : 0;

		current.css( { position:'absolute', top:0, left:0, width:current.width() } ).animate( { opacity:0 }, 1000, 'easeInOutQuad', function() { jQuery( this ).addClass( 'hidden' ).css( { position:'', width:'' } ) } );
		items.filter( ':eq(' + next + ')' ).removeClass( 'hidden' ).css( { opacity:0 } ).animate( { opacity:1 }, 1000, 'easeInOutQuad', function() { setTimeout( WGSN.EventBoxLists.rotate, 5000 ); } );
	}
}
jQuery( document ).ready( WGSN.EventBoxLists.setup );

// Info Section Navigation
WGSN.InfoSectionNavigation =
{
	content:null,

	setup: function()
	{
		jQuery( '#Content ul.infoSectionNav' ).removeClass( 'column' ).each( function()
		{
			var parent = jQuery( this );

			// reformat
			parent.removeClass( 'column-one column-two column-three' ).addClass( 'textSpecial' );

			// create content area
			parent.after( WGSN.InfoSectionNavigation.content = jQuery( '<div class="infoSectionNavContent"></div>' ) );

			// create links
			parent.children( 'li' ).each( function(i) 
			{
				if ( i == 0 )
				{
					// reformat navigation links
					jQuery( this ).find( 'h3.tabHeader' ).replaceWith( '<strong class="tabHeader">' + jQuery( this ).find( 'h3' ).html() + '</strong>' );

					// move content
					parent.parent().find( '.infoSectionNavContent' ).append( jQuery( this ).find( 'div:first' ) );
				}
				else
				{
					jQuery( this ).find( 'h3.tabHeader' ).replaceWith( jQuery( '<a href="#" class="tabHeader backgroundColour">' + jQuery( this ).find( 'h3' ).html() + '</a>' ).bind( 'click', WGSN.InfoSectionNavigation.show ) );

					// move content
					parent.parent().find( '.infoSectionNavContent' ).append( jQuery( this ).find( 'div:first' ).addClass( 'hidden' ) );
				}

				jQuery( this ).addClass( 'textSmall' );
			} );
		} );
	},

	show: function(e)
	{
		var list_item = jQuery( this ).parent();
		var index = list_item.parent().find( 'li' ).index( list_item[0] );
		var previous = list_item.parent().find( 'li strong' );

		// change navigation selection
		jQuery( this ).replaceWith( '<strong class="tabHeader">' + jQuery( this ).html() + '</strong>' );
		previous.replaceWith( jQuery( '<a href="#" class="tabHeader backgroundColour">' + previous.html() + '</a>' ).css( { backgroundPosition:'0 0' } ).bind( 'click', WGSN.InfoSectionNavigation.show ) );

		// animate close
//		WGSN.InfoSectionNavigation.content.stop();

		var current = WGSN.InfoSectionNavigation.content.children( 'div:not(.hidden)' );
		var current_height = WGSN.InfoSectionNavigation.content.height();
		var next = WGSN.InfoSectionNavigation.content.children( 'div:eq(' + index + ')' );
		var next_height = next.removeClass( 'hidden' ).height();
		next.addClass( 'hidden' );
		var height = current_height > next_height ? current_height : next_height;

		if ( current_height >= next_height )
		{
			next.removeClass( 'hidden' ).css( { opacity:0 } ).animate( { opacity:1 }, 750, 'easeInOutQuad' );
			current.css( { position:'absolute', top:0, left:0, height:current_height } ).animate( { opacity:0 }, 750, 'easeInOutQuad', function()
			{
				jQuery( this ).addClass( 'hidden' ).css( { position:'' } );
				WGSN.InfoSectionNavigation.content.animate( { height:next_height }, 500, 'easeInOutQuad' );
			 } );
		}
		else
		{
			WGSN.InfoSectionNavigation.content.animate( { height:height }, 500, 'easeInOutQuad', function()
			{
				next.removeClass( 'hidden' ).css( { opacity:0 } ).animate( { opacity:1 }, 750, 'easeInOutQuad' );
				current.css( { position:'absolute', top:0, left:0, height:current_height } )
					.animate( { opacity:0 }, 750, 'easeInOutQuad', function() { jQuery( this ).addClass( 'hidden' ).css( { position:'' } ) } );
			} );
		}

		e.preventDefault();
	}
}
jQuery( document ).ready( WGSN.InfoSectionNavigation.setup );

// Carousel JS
WGSN.Carousels =
{
	setup: function()
	{
		jQuery( '#Content .carousel' ).each( function() { new WGSN.Carousels.Class( this ) } );
	},

	Class: function( element )
	{
		var _carousel = jQuery( element );
		var _container = _carousel.find( 'ul' );
		var _navigation;
		var _items = _container.find( 'li' ).length;
		var _container_width = _items * 240 + ( ( _items - 1 ) * 15 );
		var _page = 0;
		var _width = 750;//_carousel.width();
		var _pages = Math.ceil( _items/3 );//Math.ceil( parseInt( ( _container_width / _width ) * 10, 10 ) / 10 );

		function setup()
		{
			_container.css( { width:_container_width } );

			// create navigation
			_container.after ( _navigation = jQuery( '<div class="carouselNavigation"><div class="carouselNavigationContent"><a href="#" class="previous backgroundColour"></a><span class="active"></span><a href="#" class="next backgroundColour"></a></div></div>' ) );
			for ( var x = 0; x < _pages - 1; x++ ) _navigation.find( 'a.next' ).before( '<span></span>' );

			// bind events to buttons
			_navigation.find( 'a.next' ).bind( 'click', next );
			_navigation.find( 'a.previous' ).bind( 'click', previous );
		}

		function next(e)
		{
			_page = ++_page < _pages ? _page : 0;
			var offset = _width * _page  * -1 + -14 * _page;

			_navigation.find( 'span.active' ).removeClass( 'active' );
			_navigation.find( 'span:eq(' + _page + ')' ).addClass( 'active' );

			_container.stop().animate( { left:offset }, 500, 'easeInOutQuad' );

			e.preventDefault();
		}

		function previous(e)
		{
			_page = --_page >= 0 ? _page : _pages - 1;
			var offset = _width * _page  * -1 + -14 * _page;

			_navigation.find( 'span.active' ).removeClass( 'active' );
			_navigation.find( 'span:eq(' + _page + ')' ).addClass( 'active' );

			_container.stop().animate( { left:offset }, 500, 'easeInOutQuad' );

			e.preventDefault();
		}

		setup();
	}
}
jQuery( document ).ready( WGSN.Carousels.setup );


// People Bio Section
WGSN.PeopleBio =
{
	domain_data: [],

	setup: function()
	{
		// save domain info
		jQuery( '#PeopleDetail .bio .bioHeading' ).each( function()
		{
			WGSN.PeopleBio.domain_data.push( { title:jQuery( this ).find( 'h2' ).text(), intro:jQuery( this ).find( '.bioIntro' ).html() } );
		} );

		jQuery( '#PeopleDetail .accordion a' ).bind( 'click', WGSN.PeopleBio.load );
		jQuery( '#PeopleDetail .accordion h4' ).bind( 'click', WGSN.PeopleBio.displayDomain );
	},

	load: function(e)
	{
		var url = jQuery( this ).attr( 'href' );

		// get bio
		jQuery.ajax( { url:url, type:'GET', success:WGSN.PeopleBio.view, error:WGSN.PeopleBio.error } );

		// update links
		jQuery( this ).parents( 'ul' ).find( 'a.textColour' ).removeClass( 'textColour' );
		jQuery( this ).addClass( 'textColour' );

		e.preventDefault();
	},

	displayDomain: function(e)
	{
		var bio = jQuery( '#PeopleDetail .bio' );
		var bio_title = bio.find( '.bioHeading:first h2' );
		var bio_name = bio.find( '.bioHeading h3' );
		var bio_intro = bio.find( '.bioIntro' );
		var bio_detail = bio.find( '.bioDetail:first' );
		var bio_buttons = bio.find( '.next, .previous' );

		var index = jQuery( '#PeopleDetail .accordion > li' ).index( jQuery( this ).parent() );

		// update titles
		bio_title.text( WGSN.PeopleBio.domain_data[index].title );
		bio_intro.html( WGSN.PeopleBio.domain_data[index].intro );

		var old_height = bio_intro.height();
		var height = bio_intro.css( { height:'' } ).height();
		bio_intro.css( { height:old_height } ).animate( { height:height }, 500, 'easeInOutQuad' );

		if ( bio_detail.length && bio_name.length )
		{
			bio_name.animate( { height:0 }, 500, 'easeInOutQuad', function() { jQuery( this ).remove() } );
			bio_detail.animate( { height:0 }, 500, 'easeInOutQuad', function() { jQuery( this ).remove() } );
			bio_buttons.animate( { opacity:0 }, 500, 'easeInOutQuad', function() { jQuery( this ).remove() } );
		}
	},

	view: function( xml )
	{
		if ( xml.getElementsByTagName( 'bio' ).length )
		{
			// parse data
			var title = jQuery( 'title', xml ).text();
			var name  = jQuery( 'name', xml ).text();
			var content = jQuery( 'detail', xml );

			var bio = jQuery( '#PeopleDetail .bio' );
			var bio_heading = bio.find( '.bioHeading:first' );
			var bio_title = bio_heading.find( 'h2' );
			var bio_name = bio_heading.find( 'h3 em' );
			var bio_intro = bio.find( '.bioIntro:first' );
			var bio_detail = bio.find( '.bioDetail:first' );

			// render structure
			if ( !bio_name.length && bio_intro.length && !bio_detail.length )
			{
				// set up pagination
				var bio_next = jQuery( '<a href="#" class="next backgroundColour"></a>' ).css( { opacity:0 } ).bind( 'click', WGSN.PeopleBio.next );
				var bio_previous = jQuery( '<a href="#" class="previous backgroundColour"></a>' ).css( { opacity:0 } ).bind( 'click', WGSN.PeopleBio.previous );
				var bio_detail_next = bio_next.clone( true );
				var bio_detail_previous = bio_previous.clone( true );

				bio_intro.before( bio_name = jQuery( '<h3 class="textLarge"><em>' + name + '</em></h3>' ).css( { height:0 } ) );
				bio_name.animate( { height:20 }, 500, 'easeInOutQuad' );
				bio_heading.after( bio_detail = jQuery( '<div class="bioDetail textSmall clear">' + content.text() + '</div>' ) );
				bio_intro.css( { overflow:'hidden' } ).animate( { height:0 }, 500, 'easeInOutQuad', function()
				{
					//jQuery( this ).remove();

					// fade in bio navigation
					bio_heading.append( bio_next ).append( bio_previous );
					bio_next.animate( { opacity:1 }, 1000, 'easeInOutSine' );
					bio_previous.animate( { opacity:1 }, 1000, 'easeInOutSine' );
				} );

				// animate bio detail
				var bio_detail_height = bio_detail.height();
				bio_detail.css( { height:0 } ).animate( { height:bio_detail_height }, 500, 'easeInOutQuad', function()
				{
					jQuery( this ).css( { height:'' } )

					// fade in bio navigation
					bio_detail.append( bio_detail_next ).append( bio_detail_previous );
					bio_detail_next.animate( { opacity:1 }, 1000, 'easeInOutSine' );
					bio_detail_previous.animate( { opacity:1 }, 1000, 'easeInOutSine' );
				} );
			}
			else
			{
				// render data
				bio_name.text( name );
				bio_detail.html( content.text() );

				var bio_detail_next = jQuery( '<a href="#" class="next backgroundColour"></a>' ).bind( 'click', WGSN.PeopleBio.next );
				var bio_detail_previous = jQuery( '<a href="#" class="previous backgroundColour"></a>' ).bind( 'click', WGSN.PeopleBio.previous );
				bio_detail.append( bio_detail_next ).append( bio_detail_previous );
			}

			// render data
			bio_title.text( title );

			// animate scroll to top
			jQuery( 'html,body' ).stop().animate( { scrollTop:jQuery( '#PeopleDetail' ).offset().top }, 500, 'easeInOutQuad' );
		}
		else WGSN.PeopleBio.error;
	},

	next: function(e)
	{
		var accordion = jQuery( '#PeopleDetail .accordion' );
		var current = accordion.find( 'a.textColour' );
		var next = current.parent().next( 'li' ).find( 'a' ).click();
		if ( !next.length )
		{
			if ( current.parent().parent().hasClass( 'ui-accordion-content' ) ) next = current.parent().parent().find( 'li:first a' ).click();
			else
			{
				top_current = current.parent().parents( 'li:first' );

				next = top_current.next( 'li' ).find( 'a:first' ).click();
				
				if ( !next.length ) next = top_current.parent().find( 'li:first a:first' ).click();
			}
		}

		e.preventDefault();
	},

	previous: function(e)
	{
		var accordion = jQuery( '#PeopleDetail .accordion' );
		var current = accordion.find( 'a.textColour' );
		var previous = current.parent().prev( 'li' ).find( 'a' ).click();
		if ( !previous.length )
		{
			if ( current.parent().parent().hasClass( 'ui-accordion-content' ) ) previous = current.parent().parent().find( 'li:last a' ).click();
			else
			{
				top_current = current.parent().parents( 'li:first' );

				previous = top_current.prev( 'li' ).find( 'a:first' ).click();
				
				if ( !previous.length ) previous = top_current.parent().find( 'li:last a:first' ).click();
			}
		}

		e.preventDefault();
	},

	error: function()
	{
	
	}
}
jQuery( document ).ready( WGSN.PeopleBio.setup );

// Locations Section
WGSN.Locations =
{
	setup: function()
	{
		jQuery( '#Locations .accordion a' ).bind( 'click', WGSN.Locations.show );
	},

	show: function(e)
	{
		var location_list = jQuery( '#Locations .locationsList' );
		var location = jQuery( this ).attr( 'href' ).replace( /#/, '' );
		var item = location_list.find( 'a[name=' + location + ']' ).parent();

		// animate
		location_list.stop().animate( { scrollTop:item[0].offsetTop }, 500, 'easeInOutQuad' );

		e.preventDefault();
	}
}
jQuery( document ).ready( WGSN.Locations.setup );

// Store Page Colours
WGSN.GFX.schemes =
{
	colour:'',

	setup: function()
	{
		var e = jQuery( '<div id="WGSN-GFX-COLOUR" class="textColour" style="position:absolute; top:-1000px; left:-1000px; width:1px; height:1px"></div>' )
		jQuery( '#Container' ).append( e );
		WGSN.GFX.schemes.colour = e.css( 'color' );
		e.remove();
	}
}
jQuery( document ).ready( WGSN.GFX.schemes.setup );

// Accordions
WGSN.GFX.accordions =
{
	setup: function()
	{
		jQuery( '.accordion h4' ).bind( 'click', WGSN.GFX.accordions.clear );
		jQuery( '.accordion' ).accordion( { active:0, animated:'swing', autoHeight:false, collapsible:false, header:'h4', navigation:true } );
	},

	clear: function()
	{
		// clear previous header background colour
		jQuery( this ).parents( 'ul' ).find( 'li h4.ui-state-active' ).css( { backgroundColor:'' } );
	}
}
jQuery( document ).ready( WGSN.GFX.accordions.setup );

/* Fancy Rollovers: Blue - Black
WGSN.GFX.rollovers = 
{
	setup: function()
	{
		if ( !jQuery.browser.msie || jQuery.browser.version > 6 )
		{
			jQuery( '#Content a.backgroundColour, h4.backgroundColour.ui-state-default' ).css( { backgroundColor:WGSN.GFX.schemes.colour } )
				.live( 'mouseover', function() { jQuery( this ).stop().css( { opacity:1 } ).animate( { backgroundColor:'#000' }, 750, 'easeOutQuart' ) } )
				.live( 'mouseout', function() { jQuery( this ).stop().animate( { backgroundColor:WGSN.GFX.schemes.colour }, 750, 'easeOutQuart' ) } );
	
			jQuery( '#InlinePopup a.close' )
				.live( 'mouseover', function() { jQuery( this ).stop().animate( { color:'#000' }, 750, 'easeOutQuart' ).find( 'span.backgroundColour' ).stop().animate( { backgroundColor:'#000' }, 750, 'easeOutQuart' ) } )
				.live( 'mouseout', function() { jQuery( this ).stop().animate( { color:WGSN.GFX.schemes.colour }, 750, 'easeOutQuart' ).find( 'span.backgroundColour' ).stop().animate( { backgroundColor:WGSN.GFX.schemes.colour }, 750, 'easeOutQuart' ) } )
				.find( 'span.backgroundColour' ).css( { backgroundColor:WGSN.GFX.schemes.colour } );
		}
	}
}
jQuery( document ).ready( WGSN.GFX.rollovers.setup );
*/
