jQuery(function () {
	$('body').addClass('jsEnabled'); // for js-enabled CSS
	$('#card-container').cardShow();
});

(function ($) {
	$.fn.cardShow = function () {
		//if ($.browser.msie && $.browser.version < 7) return;
		
		var distance = 10; // distance in px to animate on hover
		var timeAnimate = 100; // time in ms to animate
		
		// Array to store position attributes
		var cards = new Array();
		
		// add class to front card		
		$('#card-container .card:first-child').addClass('front-card');
		
		// arguments to use for hoverIntent		
		var hoverArgs = {
			interval: 50,
			timeout : 100,
			sensitivity : 7,
			over : bumpUp,
			out : bumpDown
		};
		
		// int to loop through cards
		var i=1;
		
		$('.card').each(function () {
			var $thisCard = $(this);
			
			// set the headers and hide content not on front-card
			if ( i == 1 ) { 
				// front-card
				$thisCard.find('h2').addClass('card-header-c');
			} else if ( i == 2 || i == 4 ) { 
				// cards on the left
				$thisCard.find('h2').addClass('card-header-l');
				$thisCard.find('.fc-cover').stop().hide();
				$thisCard.find('.card-content').stop().hide();
			} else if ( i == 3 || i == 5 ) { 
				// cards on the right
				$thisCard.find('h2').addClass('card-header-r');
				$thisCard.find('.fc-cover').stop().hide();
				$thisCard.find('.card-content').stop().hide();
			}
			
			// Store position attributes for swapping
			cards["card-"+i]=new Object();
			cards["card-"+i].pos={
				top:$thisCard.position().top,
				left:$thisCard.position().left
			};
			cards["card-"+i].grown={
				top:$thisCard.position().top-distance,
				left:$thisCard.position().left-distance
			};
			cards["card-"+i].zIndex=$thisCard.css('zIndex');
			i++;
		});
		
		// animations for hoverIntent		
		function bumpUp(){ 
			if(!$(this).hasClass('front-card')){
				$(this).dequeue().animate({top:cards[$(this).attr("id")].grown.top},timeAnimate);
			}
		}
		function bumpDown(){ 
			if(!$(this).hasClass('front-card')){
				$(this).dequeue().animate({top:cards[$(this).attr("id")].pos.top},timeAnimate);
			}
		}

		// animations for card click event
		function cardDance(event){
			// set some vars
			var swapTime = 500;
			var fadeTime = 250;
			var textFadeTime = 500;
			var swapOpacity = 0.7;
			
			// temporarily disable hoverIntent functions
			$('.card').unbind('mouseover mouseout').dequeue();
			
			if(!$(this).hasClass('front-card')){
				var $newFrontCard = $(this);
				var newFrontCardAtts = cards[$newFrontCard.attr('id')];
				var $currentFrontCard = $newFrontCard.siblings('.front-card');
				var currentFrontCardAtts = cards[$currentFrontCard.attr('id')];
				var newHdrClass=$newFrontCard.find('h2').attr('class');
				var currentFrontHdrClass=$currentFrontCard.find('h2').attr('class');
				
				// swap classes
				$currentFrontCard.removeClass('front-card');
				$newFrontCard.addClass('front-card');
				$currentFrontCard.find('h2').removeClass().addClass(newHdrClass);
				$newFrontCard.find('h2').removeClass().addClass(currentFrontHdrClass);
				
				// hide the content
				$('.card-content').stop().hide();
				$('.fc-cover').stop().hide();
				
				// switch the attributes
				cards[$newFrontCard.attr('id')]=currentFrontCardAtts;
				cards[$currentFrontCard.attr('id')]=newFrontCardAtts;
				
				// change opacity before animating
				if ( jQuery.support.opacity ) {
					$newFrontCard.css('opacity',swapOpacity);
					$currentFrontCard.css('opacity',swapOpacity);
				}
				
				// animation for new front card
				$newFrontCard.css('zIndex',cards[$newFrontCard.attr('id')].zIndex).animate(cards[$newFrontCard.attr('id')].pos,swapTime,function(){
					// fadein and show content
					if ( jQuery.support.opacity ) {
						$('.front-card').children('.fc-cover').show();
						$newFrontCard.animate({opacity:1},fadeTime,function() {
							$('.front-card').children('.card-content').show();
						});
					// otherwise just show	
					} else {
						$('.front-card').children('.fc-cover').show();
						$('.front-card').children('.card-content').show();
					}
				});
				
				// animation for old front card
				$currentFrontCard.css('zIndex',cards[$currentFrontCard.attr("id")].zIndex).animate(cards[$currentFrontCard.attr('id')].pos,swapTime,function(){
					//fadein
					if ( jQuery.support.opacity ) {
						$(this).animate({opacity:1},fadeTime);
					}
					// re-enable hoverIntent for all cards
					$('.card').hoverIntent(hoverArgs);
				});
			}
		}
		
		// initialize cards
		function initCards() {
			$('.card').hoverIntent(hoverArgs);
			$('.card').click(cardDance);
		}
	//	$('#home #content-wrapper').css('background','none');
	
		// initialize layout
		function initLayout() {
			var hideDelay = 1500;
			var fadeOut = 500;
			var fadeIn = 500;
			var animeTime = 750;
			
			// move all cards behind front card and hide everything
			$('.card').animate(cards[$('.front-card').attr("id")].pos,0,function(){
				if ( jQuery.support.opacity ) {
					$('#card-container').animate({opacity:0},0);
				} else {
					// IE fix so it doesn't collapse
					var tempHeight = $('#card-container').css('height');										
					$('#content-wrapper').css('height', tempHeight);
					$('#card-container').css('display', 'none');
				}
				$('#card-container').css('left','0');
			});
			
			hideDelayTimer = setTimeout(function () {
				if ( jQuery.support.opacity ) {
					$('#content-wrapper #loader').animate({opacity:0},fadeOut, function() {
						$('#card-container').animate({opacity:1}, fadeIn, function(){
							$('.card').each(function(){
								$(this).animate(cards[$(this).attr("id")].pos,animeTime,function(){
									initCards();
								})
							})
						})
					})
				} else {
					$('#content-wrapper #loader').css('display', 'none');
					$('#card-container').css('display','block');
					$('#content-wrapper').css('height', 'auto');
					$('.card').each(function(){
						$(this).animate(cards[$(this).attr("id")].pos,animeTime,function(){
							initCards();
						})
					})
				}			
			}, hideDelay);
			
						
			
			// fadein then animate to start positions
//			if ( jQuery.support.opacity ) {
//				$('#card-container:hidden').fadeIn(fadeIn,function(){
//					$('.card').each(function(){
//						$(this).animate(cards[$(this).attr("id")].pos,animeTime,function(){
//							initCards();
//						});
//					});
//				});

			// otherwise, just show then animate
		}
		
		initLayout();
	}
})(jQuery);

