var homePage = {};

;( function( $ ) {

	// Detect if using IE6.
	var msie6 = ( typeof XMLHttpRequest == 'undefined' );

	homePage = {

		// ---------- Adds share icons to the news links.
		//
		initNews : function() {

			// Do not continue for IE6.
			if ( msie6 ) return;

			$( '#my_news ul li' )
				.each( function( index ) {

					var li = $( this );
					var linkUrl = $( 'a.my_newsLink', li ).get( 0 ).href; // Do not use attr(). We want to get the full URL.
					linkUrl += ( ( linkUrl.indexOf( '?' ) >= 0 ) ? '&' : '?' ) + 'source=share';
					var facebookUrl = 'http://www.facebook.com/share.php?u=' + escape( linkUrl );
					var twitterUrl = 'http://twitter.com/home?status=' + escape( $( '.my_twitterText', li ).text() );

					// Give the share links their URLs.
					$( 'a.my_facebookIcon', li ).attr( 'href', facebookUrl );
					$( 'a.my_twitterIcon', li ).attr( 'href', twitterUrl );

					// Remove the Twitter text container.
					$( '.my_twitterText', li ).remove();
					
				} )
				.hover(
					function() { $( 'div.my_newsIcons', this ).css( 'visibility', 'visible' ); },
					function() { $( 'div.my_newsIcons', this ).css( 'visibility', 'hidden'  ); }
				);

		},
			
		// ---------- Loads the main Flash component.
		//
		initFlash : function( options ) {

			var swf = 'flash/home/mainContent_smaller_v2.swf';
			if ( options.overrideSwf != '' ) swf = options.overrideSwf;

			var flashvars = {
			};

			if ( options.clickUrl   != '' ) flashvars.clickUrl = options.clickUrl;
			if ( options.promoValue != '' ) flashvars.promoValue = options.promoValue;

			var params = {
				menu    : 'false',
				wmode   : 'transparent',
				bgcolor : '#ffffff'
			};

			swfobject.embedSWF( swf,
					'my_bannerFlash', '938', '390', '10.0.0',
					null, flashvars, params, null );

		},

		// ---------- Gives the links their events.
		//
		initLinks : function( retailerMode ) {
			
			$( '.my_link' )
				.each( function() {
					showCallout( this ); // Bugfix for IE: quickly show then hide the callouts.
					hideCallout( this );
				} )
				.css( 'cursor', 'pointer' )
				.click( function( e ) {
					window.location.href = $( 'a', this ).attr( 'href' );
				} )
				.hover(
					function() {
						showCallout( this );
					},
					function() {
						hideCallout( this );
					}
				);

			$('#my_link1 .my_linkInside').retailerTypeSwitch(
				function() { $( this ).removeClass( 'my_linkInsideGeneral' ).addClass( 'my_linkInsideTHD' ); },
				function() { $( this ).removeClass( 'my_linkInsideGeneral' ).addClass( 'my_linkInsideWM'  ); },
				function() { $( this ).removeClass( 'my_linkInsideGeneral' ).addClass( 'my_linkInsideIDD' ); });

		}

	};

	function showCallout( link ) {

		// Do not continue for IE6.
		if ( msie6 ) return;
		
		var callout = $( '.my_linkCallout', link );

		$( link ).addClass( 'my_linkOn' );

		callout
			.clearQueue()
			.css( 'display', 'block' )
			.css( 'opacity', 0 )
			.offset( callout.parent().offset() )
			.width( callout.parent().width() )
			.animate( {
				opacity : 1,
				top     : '-=20'
			}, 200 );

	};

	function hideCallout( link ) {

		// Do not continue for IE6.
		if ( msie6 ) return;
		
		var callout = $( '.my_linkCallout', link );

		$( link ).removeClass( 'my_linkOn' );

		callout
			.clearQueue()
			.animate( {
				opacity : 0,
				top     : '+=20'
			}, 200 );
	};

} )( jQuery );

