/*
Title:				Careers Javascript
Date Modified: 		January 2010
Version:			1.0
*/

// Requires jQuery
if ( !window.jQuery ) jQuery = function() { return { ready:function() { void(null) } } }
else jQuery.noConflict();


if ( !window.Mars ) Mars = {};


// Careers Card Deck
Mars.CardDeck =
{
	parent:null,
	item:null,
	next:null,
	enabled:true,

	setup: function()
	{
		var link;
	
		Mars.CardDeck.parent = jQuery( '#CareersCardDeck .careersCardDeckContent' );
		
		if ( Mars.CardDeck.parent.length )
		{
			// select next element to swap
			Mars.CardDeck.next = Mars.CardDeck.parent.find( 'li:first' );
			
			// reset first element
			Mars.CardDeck.next.removeClass( 'first' ).css( { zIndex:200 } );
			 Mars.CardDeck.parent.find( 'li:gt(0)' ).hide();

			// create elements
			Mars.CardDeck.parent.find( 'li' ).each( function()
			{
				var link = jQuery( '<a href="#">Next fact ›</a>' );

				link.bind( 'mousedown', Mars.CardDeck.swap ).bind( 'click', function(e) { e.preventDefault() } );

				jQuery( this ).append( link );
			} );
		}
	},
	
	swap: function(e)
	{
		if ( Mars.CardDeck.enabled )
		{
			Mars.CardDeck.enabled = false;
		
			Mars.CardDeck.item = Mars.CardDeck.next;
			Mars.CardDeck.next = Mars.CardDeck.item.next( 'li' );
			if ( Mars.CardDeck.next.length == 0 ) Mars.CardDeck.next = Mars.CardDeck.item.parents( 'ul' ).find( 'li:first' );
	
			Mars.CardDeck.next.css( { zIndex:200 } ).show();
			Mars.CardDeck.item.css( { webkitAnimationName:'swap', webkitAnimationDuration:'500ms' } );
	
			if ( !jQuery.browser.webkit )
			{
//				Mars.CardDeck.item.find( 'a' ).hide();											/** Site Catalyst Fix **/
			
				Mars.CardDeck.item.animate( { left:-162 }, 250, 'easeInQuad', function()
				{
					Mars.CardDeck.item.css( { zIndex:199 } );
					
					Mars.CardDeck.item.animate( { left:47 }, 250, 'easeOutQuad', function()
					{
						Mars.CardDeck.item.css( { zIndex:199 } ).hide();
//						Mars.CardDeck.item.find( 'a' ).show();									/** Site Catalyst Fix **/
						Mars.CardDeck.next.css( { zIndex:201 } );
						
						Mars.CardDeck.enabled = true;
					} );
				} );
			}
			else
			{
				setTimeout( function() { Mars.CardDeck.item.css( { zIndex:199 } ) }, 250 );
				setTimeout( Mars.CardDeck.reset, 499 );
			}
		}

		e.preventDefault();
	},
	
	reset: function()
	{
		Mars.CardDeck.item.css( { zIndex:199 } ).hide();
		Mars.CardDeck.next.css( { zIndex:201 } );

		Mars.CardDeck.item.css( { webkitAnimationName:'reset' } );

		Mars.CardDeck.enabled = true;
	}
}
jQuery( document ).ready( Mars.CardDeck.setup );




// Careers Carousel
Mars.HistoryCarousel =
{
	parent:null,
	scroller:null,
	delta:20,
	wheelDelta:5,
	interval:30,
	delay:500,
	timer:null,
	direction:0,
	auto:false,

	setup: function()
	{
		var width, listItems;
	
		Mars.HistoryCarousel.parent = jQuery( '#CareersCarousel' );
	
		if ( Mars.HistoryCarousel.parent.length )
		{
			Mars.HistoryCarousel.scroller = Mars.HistoryCarousel.parent.find( '.careersCarouselContent' );
		
			// set correct width of scroller
			listItems = Mars.HistoryCarousel.scroller.find( 'li' );
			width = listItems.length * 330 + ( 20 * ( listItems.length - 1 ) );
			Mars.HistoryCarousel.scroller.find( 'ul' ).css( { width:width } );
		
			// bind scroll events
			Mars.HistoryCarousel.parent.find( '.careersCarouselButtonPrevious, .careersCarouselButtonNext' )
				.bind( 'mousedown', Mars.HistoryCarousel.preStartScroll )
				.bind( 'mouseup', Mars.HistoryCarousel.stopScroll )
				.bind( 'click', function(e) { e.preventDefault() } );
			Mars.HistoryCarousel.scroller
				.bind( 'mousewheel', Mars.HistoryCarousel.mouseScroll )
				.bind( 'DOMMouseScroll', Mars.HistoryCarousel.mouseScroll );
			
			// set up IE transparency
			if ( jQuery.browser.msie && jQuery.browser.version < 7 )
			{
				Mars.HistoryCarousel.parent.find( 'img' ).each( function()
				{
					if ( this.src.match( /\.png/ ) )
					{
						this.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + this.src + '")';
						this.src = 'http://www.mars.com/global/assets/images/banners/x.gif';
					}
				} );
			}
		}
	},
	
	preStartScroll: function(e)
	{
		var element = this;

		Mars.HistoryCarousel.auto = false;
		Mars.HistoryCarousel.direction = this.className.match( /previous/i ) ? -1 : 1;

		Mars.HistoryCarousel.scroller.stop();

		Mars.HistoryCarousel.timer = setTimeout( Mars.HistoryCarousel.startScroll, Mars.HistoryCarousel.delay );
	},
	
	startScroll: function()
	{
		var direction;

		Mars.HistoryCarousel.auto = true;
		Mars.HistoryCarousel.timer = setInterval( Mars.HistoryCarousel.scroll, Mars.HistoryCarousel.interval );
	},
	
	scroll: function()
	{
		Mars.HistoryCarousel.scroller[0].scrollLeft =  Mars.HistoryCarousel.scroller[0].scrollLeft + Mars.HistoryCarousel.direction * Mars.HistoryCarousel.delta;
	},
	
	stopScroll: function(e)
	{
		var scrollWidth;
	
		clearTimeout( Mars.HistoryCarousel.timer );
		
		// if autoscroll didn't start scroll by 1 item
		if ( !Mars.HistoryCarousel.auto )
		{
			scrollWidth = Math.floor( Mars.HistoryCarousel.scroller[0].scrollLeft / 350 ) * 350 + Mars.HistoryCarousel.direction * 700;
		
			Mars.HistoryCarousel.scroller.animate( { scrollLeft:scrollWidth }, 750, 'easeInOutQuad' );
		}
	},
	
	mouseScroll: function(e)
	{
		var delta = -e.wheelDelta / 3 || e.detail;

		Mars.HistoryCarousel.scroller[0].scrollLeft = Mars.HistoryCarousel.scroller[0].scrollLeft + delta * Mars.HistoryCarousel.wheelDelta;
		
		e.preventDefault();
	}
}
jQuery( document ).ready( Mars.HistoryCarousel.setup );


Mars.MiniVideoGallery =
{
	parent:null,

	setup: function()
	{
		var items;

		Mars.MiniVideoGallery.parent = jQuery( '#div_centercontents .careersGalleryHead' );
		
		if ( Mars.MiniVideoGallery.parent.length )
		{
			// set up rollovers
			items = Mars.MiniVideoGallery.parent.find( 'li' );
			
			items.each( function() { jQuery( this ).data( 'image', jQuery( this ).find( 'img' )[0].src ); } )
				.bind( 'mouseenter', function()
				{
					jQuery( this ).find( 'img' )[0].src = jQuery( this ).find( 'img' )[0].className.match( /rollover-([^\s]+)/ )[1];
				} )
				.bind( 'mouseleave', function()
				{
					jQuery( this ).find( 'img' )[0].src = jQuery( this ).data( 'image' );
				} );
		}
	}
}
jQuery( document ).ready( Mars.MiniVideoGallery.setup );


