$(document).ready(function() {

/***********************************************
***  COMPACT FORM (LABEL OVER INPUT)  **********
************************************************/
	
	$('form.compact label').each(function() {
			var t = $(this);
			if($('#' + t.attr('for')).val()) {
				t.hide();
			}
			else {
				t.css({
						opacity: 0.8
					});
			}
		});
	$('form.compact .txtInput').focus(function(){
			$('label[for=' + $(this).attr('id') + ']').stop().animate({
					opacity: .4
				}, 200);
		});
	$('form.compact .txtInput').keypress(function(){
			$('label[for=' + $(this).attr('id') + ']').hide();
		});
	$('form.compact .txtInput').blur(function(){
			if($(this).val().length == 0)
				$('label[for=' + $(this).attr('id') + ']').show();
				$('label[for=' + $(this).attr('id') + ']').stop().animate({
					opacity: .8
				}, 200);
		});
	$('form.compact').removeClass('compact');


/***********************************************
***  ANIMATED SCROLLING   **********************
************************************************/

	$('.topLink[href^="#"]').click(function(){
											
			var id = $(this).attr('href');
			if(id.length === 1) return;
			var ele = $(id);
			$(this).addClass('clicked');
			if(ele.length) {
				var destination = ele.offset().top;
				$('html:not(:animated), body:not(:animated)').animate({
						scrollTop: destination
					}, 300, 'swing');
			};
			return false;
		});
		

/***********************************************
***  EXTERNAL LINKS  ***************************
************************************************/

	$('a[rel=external]').each(function() {
		$(this).addClass('external');
		$(this).attr({
			title: 'Open this page in a new window',
			target: '_blank'
		});
	});
	
/***********************************************
***  INITIALIZE TABS  **************************
************************************************/

	$('.tabMenu li').geeTabs({tabbedContentSelector: '.tabContent > li'});

/***********************************************
***  INITIALIZE FANCYBOX  **************************
************************************************/

	$('.productDetailContainer .images ul a').fancybox({
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'speedIn'		: 200, 
		'speedOut'		: 200,
		'cyclic'		: true,
		'overlayColor'		: '#000',
		'overlayOpacity'	: .2
	});
	
/***********************************************
***  INITIALIZE FANCYBOX  **************************
************************************************/

	$('.revealBtn').click(function(e) {
		e.preventDefault();
		$('#revealTest').reveal();
	});

	$('.reveal-modal').each(function() {
			var $this = $(this);

			var currentIndex = 0;
			var maxIndex = $this.find('.messageContent > li').length -1;
			

			var pageIndicator = $this.find('#currentNum');

			function showPage(i) {
				$this.find('.messageContent > li').hide().eq(currentIndex).show();
				pageIndicator.html(currentIndex +1);
			}
			if (maxIndex == 0 ) {
				$this.find('a.btnNext').addClass('btnDisabled');
			}
			if (maxIndex > 0 ) {
				$this.find('a.btnPrev').click(function(e) {
						e.preventDefault();
	
						var btn = $(this);
						if(! btn.is('.btnDisabled')) {
							showPage(currentIndex--);
							if(currentIndex < maxIndex) {
								$this.find('.btnDisabled').removeClass('btnDisabled');
							}
							if(currentIndex === 0) {
								btn.addClass('btnDisabled');
							}
						}
					});
				$this.find('a.btnNext').click(function(e) {
						e.preventDefault();
	
						var btn = $(this);
						if(! btn.is('.btnDisabled')) {
							showPage(currentIndex++);
							if(currentIndex > 0) {
								$this.find('.btnDisabled').removeClass('btnDisabled');
							}
							if(currentIndex === maxIndex) {
								btn.addClass('btnDisabled');
							}
						}
					});
			}
		});
	
/***********************************************
***  EQUAL HEIGHTS  ****************************
************************************************/

	function equalHeight(group) {
		tallest = 0;
		group.each(function() {
			thisHeight = $(this).height();
			if(thisHeight > tallest) {
			    tallest = thisHeight;
			}
		});
		if ($.browser.msie && $.browser.version.substring(0,1) === '6'){
			group.css('height',tallest);
		}
		else {
			group.css('min-height',tallest);
		}
	}
	//$(document).ready(function() {
	    equalHeight($('.homeColsSplitTwo .colContent'));
	    equalHeight($('.productListingCol'));
	    //equalHeight($('.flyout .flyoutCol'));
	//});


/***********************************************
***  FADE IN MEGA FLYOUT  **********************
************************************************/

	$('.flyout').hide();
	$('#menuMain ul > li').hover(function(){
			$(this).find('.flyout').stop(true, true).css('visibility','visible').fadeIn(200);
		},
		function(){
				$(this).find('.flyout').stop(true, true).fadeOut(0, function() { $(this).css('visibility','hidden') });
			});
			
	$(window).unload(function(){
			$('.flyout').stop(true, true);
		});

/***********************************************
***  DATEPICKER  *******************************
************************************************/

	$(function() {
		$(".dateField").datepicker();
	});

/***********************************************
***  PROGRESSIVE ENHANCMENT  *******************
************************************************/

	// Set .first / .last class
	$('.productList > li:nth-child(3n-2), .searchBar .inputContainer:first').addClass('f');
	$('.productList > li:nth-child(3n), .searchBar .inputContainer:last').addClass('l');
	$('.productDetailContainer .images li:nth-child(2n-1)').addClass('f');
	
	$('#menuMain .flyout').each(function() {
		$(this).find('.flyoutCol:first').addClass('f');
	});
	$('.newsEventsList').each(function() {
		$(this).find('li:last').addClass('l');
	});
	
	// Set .dark class
	$('#shoppingCart tr:nth-child(2n)').addClass('dark');
	$('.productDetailContainer dl dt:nth-child(2n)').addClass('dark');
	$('.productDetailContainer dl dd:nth-child(2n-1)').addClass('dark');
		
	// Set first row
	var fRow = $('.productList > li');
	for(var i=0; i<Math.min(3, fRow.length); i++) {
		fRow.eq(i).addClass('firstRow');
	}

	
	// Zebra striping
	$('.checkoutTable tr:nth-child(2n), .contentTable tr:nth-child(2n)').addClass('dark');
	
	// Add first columns
	$('.checkoutTable td:nth-child(1), .contentTable td:nth-child(1)').addClass('f');
	$('.checkoutTable th:nth-child(1), .contentTable th:nth-child(1)').addClass('f');
	
	// Traversing for hover effects
	
	$(".priceTagCTA .cta, .priceTagCTA .moreBtn a").live({
        mouseenter:
           function()
           {
			$(this).parents(".priceTagCTA").addClass('hover');
           },
        mouseleave:
           function()
           {
			$(this).parents(".priceTagCTA").removeClass('hover');
           }
       }
    );
	
	
	$(".priceTagCTA .checkoutBtn, .priceTagCTA .moreBtn a").live({
        mouseenter:
           function()
           {
			$(this).parents(".priceTagCTA").addClass('checkoutHover');
           },
        mouseleave:
           function()
           {
			$(this).parents(".priceTagCTA").removeClass('checkoutHover');
           }
       }
    );


				
	//$('.priceTagCTA .checkoutBtn').hover(function(){
//			$(this).parents(".priceTagCTA").addClass('checkoutHover');
//		},
//			function(){
//					$(this).parents(".priceTagCTA").removeClass('checkoutHover');
//				});

/***********************************************
***  ANIMATED SCROLLING   **********************
************************************************/

	$('.msgBox ol a[href^="#"]').click(function(){
			
			var id = $(this).attr('href');
			if(id.length === 1) return;
			var ele = $(id);
			$(this).addClass('clicked');
			if(ele.length) {
				var destination = ele.offset().top;
				$('html:not(:animated), body:not(:animated)').animate({
						scrollTop: destination -20
					}, 700, 'swing');
			};
			return false;
		});
			
/***********************************************
***  TOOLTIPS  *********************************
************************************************/

	$(".tooltipTrigger").hover(function(){
		if($(this).parent().find(".tooltipContent").is(':animated'))
			return;
		$(this).parent().find(".tooltipContent").stop().addClass('open').css({ bottom: '22px' }).animate({
						bottom: 32,
						opacity: 1
					}, 150);
	},
	function(){
			
			$(this).parent().find(".tooltipContent").stop().css({ bottom: '32px' }).animate({
						bottom: 42,
						opacity: 0
					}, 150, function(){
							$(this).removeClass('open');
					});
		});
				
});

/***********************************************
***  FORM  AUTHORISATION ***********************
************************************************/
$(function() {
	$('#formContactus').prepend('<input type="hidden" name="formAuth" value="001" />');
});




