var current_page = 0;
var page_count = 0;
$(document).ready(function() {
	
	// header log in form	 
	$("#nav_user").toggle(
		function(){
			openLogin()
		},
		function(){
			closeLogin()
		}
	);
	
	// recipe paganiation
	if ($("#paganiate_recipe").innerhtml = '&nbsp;') {
		paginateRecipes();
	}
  
	
	// promo winners : /promotions/winners/
	$('.content_toggle_container').hide();
	
	$('.content_toggle').toggle(
	    function(){
	        contentToggleShow(this);
	    },
	    function(){
	        contentToggleHide(this);
	    }
	);
	
	
  	// where to buy accordion
	$('#accordion h3').click(function() { 
	
		$(this).next().toggle();
		
		return false;
		
	});
	$('#accordion h3').next().hide();
		

	// homepage tabs	
	$("#hSlide ul.tabs").tabs("div.panes > div", {
			effect: 'MY_horizontal',
			width:624,
			rotate: true,
			initialIndex:4
		
	}).slideshow({
		autoplay:true,
		interval:6000,
		clickable:false
	});
	
	$("#hSlide .pane").css('width','0px');
	
	
	
	// Perfect Pairings tabs
	var tabs = ['pizza','baking','desserts'];
	
	if( $("#pairingsSlides").attr('id')!=undefined ){
	
		$('.row div:last-child').addClass("last");

		$("#pairingsSlidesTabs").tabs("#pairingsSlides > .pane").history();

		$('.forward').click(function(){return false;})
		$('.backward').click(function(){return false;})
		
		$("#pairingsSlides div ul.tabs").tabs("div.panes > div", {
				effect: 'fade',
				width:735,
				rotate: false,
				onClick: function(event, tabIndex) {
					
					var parentId = this.getCurrentPane().parent().parent().attr('id');
					
					//console.log(this.getCurrentPane().width('id'));
				
					$('#'+parentId+' > form .pagination-current').val(this.getIndex()+1);
					$('#'+parentId+' > form .pagination-total').text(this.getTabs().size());
					
					//console.log(this.getIndex() +":"+ this.getTabs().size());
					
					$('#'+parentId+' > form .cta-pagination-next').show();
					$('#'+parentId+' > form .cta-pagination-prev').show();
						
					if(this.getIndex() <= 0)
					{
						$('#'+parentId+' > form .cta-pagination-prev').hide();				
					}
					else if(this.getIndex()+1 == this.getTabs().size())		
					{
						$('#'+parentId+' > form .cta-pagination-next').hide();
					}
					
					return false;
					
				}
		}).slideshow({
			autoplay:false,
			clickable:false
		});
	
		//$("#pairingsSlides .panes .pane").css('width','735px');

		$.each(tabs, function(index, value){

			var section = "#"+value;
			
			$(section+' form .pagination-current').autotab({format: 'numeric' });
	
			$(section+' form').submit(function(){

				var totalPanes = parseInt($('.pagination-total', this).text());
				var targetIndex = parseInt($('.pagination-current', this).val() - 1);
				
				var parentId= $(this).parent().attr("id");
				
				if(targetIndex <=0)
				{
					$( '#'+parentId + " ul.tabs").tabs().click(0);					
				}
				else if(targetIndex < totalPanes)		
				{
					$( '#'+parentId + " ul.tabs").tabs().click(targetIndex);
				}
				else
				{
					$( '#'+parentId + " ul.tabs").tabs().click(totalPanes-1);
					$('#'+parentId+' > form .pagination-current').val(totalPanes);
				}
				
				return false;
			});

		});

	}
	$('#drinkPairing_tab1').click(function(e){
		$('.drinkPairing_pane1').show();
		$('.drinkPairing_pane2').hide();
	});
	$('#drinkPairing_tab2').click(function(e){
		$('.drinkPairing_pane2').show();
		$('.drinkPairing_pane1').hide();
	});
	
	// touts bigger links	
	$('.tout, .landing-tout, .product-tout').biggerlink({
		clickableclass:'tout-hot',
		hoverclass:'tout-hover'
	});

	//light box
	$("a.lightbox").fancybox({
		'overlayColor': '#333321',
		'overlayOpacity':.6,
		'autoScale': true,
		'centerOnScroll': true,
		'autoScale':true,
		'titleShow':false
	});
	
	$('a#callShare').fancybox({
		'height': 500,
		'width': 500,
		'autoScale':true
	});
	
	$('a#shareRecipe').fancybox({
		'height': 400,
		'width': 855,
		'autoScale':true
	});
	
	$('a#login-now').fancybox({
		'height': 430,
		'width': 440,
		'autoScale':true
	});
	if(!jQuery.browser.msie && jQuery.browser.version!='6.0'){
		$('a.enter-now-baking').fancybox({
			'height': 570,
			'width': 870,
			'autoScale':true,
			'titleShow':false
		});
	}else{
		
		$('a.enter-now-baking').fancybox({
			'height': 650,
			'width': 950,
			'autoScale':true,
			'titleShow':false
		});
		
	}
	        
	$("a.contest_lightbox").fancybox({
		'overlayColor': '#333321',
		'overlayOpacity':.6,
		'showCloseButton': false,
		'centerOnScroll': true,
		'autoScale': true,
		'height': 457,
		'width': 688,
		'autoScale':true
	});
	
	$("#video_popup_tout").click(function(){
		$("#popup_trig").trigger('click');						 
	});
	
	
	//autotab phone numbers
	$('#user_postal1').autotab({ target: 'user_postal2'});
	$('#user_postal2').autotab({	previous: 'user_phone1', target: 'user_phone1'});
	$('#user_phone1').autotab({ target: 'user_phone2', format: 'numeric', previous: 'user_postal2'  });
	$('#user_phone2').autotab({ target: 'user_phone3', format: 'numeric', previous: 'user_phone1' });
	$('#user_phone3').autotab({format: 'numeric', previous: 'user_phone2' });	
	
	$('#user_smsphone1').autotab({ target: 'user_smsphone2', format: 'numeric'  });
	$('#user_smsphone2').autotab({ target: 'user_smsphone3', format: 'numeric', previous: 'user_smsphone1' });
	$('#user_smsphone3').autotab({format: 'numeric', previous: 'user_smsphone2' });
	
	$('#why_required').toggle(
		function(){
			$('#why_required_popup').show();
		},
		function(){
			$('#why_required_popup').hide();
		}
	);
	$('#why_required_popup').click(function(){
		$(this).hide()
	});
	
	
	
	// Cufon font replacement

	Cufon('.cta, .fancy span', {fontFamily: "Wrexham Script", hover: true});
	
	if(!jQuery.browser.msie && jQuery.browser.version!='6.0'){
		Cufon('h2, .landing-tout h3, .product-tout h3, .everydayTout h3, .cheltenham', {fontFamily: "ITC Cheltenham"});
	}

	
	if($('.recipeContainer').length > 0 ){
		$('#current_page').val(window.location.hash.slice(-1));
		gotopage();
		$('.recipeContainer a').click(function(){
			window.location = $(this).attr('href') + '#' + window.location.hash.slice(-1);
			return false;
		});
	}
	
	if($('#module_twelveMinuteSides_action_show, #module_perfectDessertRecipes_action_show').length > 0){
		$('#recipeButton a').click(function(){
			window.location = $(this).attr('href') + '#' + window.location.hash.slice(-1);
			return false;
		});
	}

});


// custom tab transition effect -  removes cufon items for ie 

var w=0;

$.tools.tabs.addEffect("MY_horizontal",  function(i, done) {
	
	// store original width of a pane into memory
	//console.log(this.getPanes().eq(0).width());

	
	if (!w) {w =  this.getPanes().eq(0).width(); }
	
	if(jQuery.browser.msie)
		$(".panes .cta, .panes .fancy").hide(); 
		
	// set current pane's width to zero
	this.getCurrentPane().animate({width: 0}, function() { $(this).hide(); });
	
	// grow opened pane to it's original width
	this.getPanes().eq(i).animate({width: w}, function() { 
		$(this).show();
		if(jQuery.browser.msie)
			$(".panes .cta, .panes .fancy").fadeIn('slow');
		done.call();
	});
	
});





function closeFancyBox() {
	$.fn.fancybox.close();
}


// header login callback fucntions

function openLogin(){
	$('#sub_nav_loggin_form').slideDown('slow',function(){
		if(jQuery.browser.msie)
			$("#sub_nav_remember").show();		
	});
	$("#nav_user").addClass('active');
}

function closeLogin(){
	$('#sub_nav_loggin_form').slideUp('slow');
	$("#nav_user").removeClass('active');

	if(jQuery.browser.msie)
		$("#sub_nav_remember").hide();
}

function contentToggleHide(_this){
     $('.content_toggle_container', _this).slideUp('slow');
	   $(_this).removeClass('ct_active');
}

function contentToggleShow(_this){
    $('.content_toggle_container', _this).slideDown('slow');
    $(_this).addClass('ct_active');  	     
}




// 

function paginateRecipes() {
	setPageButton(1);
	page_count = getTotalPages();
	setPageTotal(page_count);
	$("div.recipeContainer").each( function(index) {
		if (index > 0) {
			$(this).hide();
		}
	});
}

function setPageButton(current_page) {
	$('#current_page').val(current_page);
	//$('#current_page').css("color", "#fba818");
}

function setPageTotal(page_count) {
	$('#page_count').text(page_count);
}

function next_page() {
	if (current_page >= page_count - 1 ) { 
		return;
	} else {
		current_page += 1;
		showPage(current_page,'right');
	}
}

function previous_page() {
	if (current_page  <= 0 ) { 
		return; 
	} else {
		current_page -= 1;
		showPage(current_page, 'left');
	}
}

function getTotalPages() {
	return $("div.recipeContainer").length;
}


function showPage(page, dir) {
	if (!dir) {
		dir = 'left';
	}
	//dir = 'right';
	$("div.recipeContainer").each( function(index) {
		if (index == page) {
			if (dir == 'right') {	
				var lastWidth = '-' + $(this).css('width').replace('px','') + 'px';
				$(this).css({
					'position': 'absolute',
					'left' : lastWidth,
					'display': 'block',
					'z-index': 100
				}).animate({ 'left' : 0 }, 500, function(){
					$(this).css('z-index', 0).siblings('.recipeContainer').hide();
					$(this).show();
				});
			} 
			if (dir == 'left') {
				var lastWidth = $(this).css('width').replace('px','') + 'px';
				$(this).css({
                                        'position': 'absolute',
                                        'left': lastWidth,
                                        'display': 'block',
                                        'z-index': 100
                                }).animate({ 'left' : 0 }, 500, function(){
                                        $(this).css('z-index', 0).siblings('.recipeContainer').hide();
                                        $(this).show();
                                  });
			}
			
		}
	  });
	$('#current_page').val(current_page + 1);
	window.location.hash = current_page+1;
	displayPagControls();
}
function gotopage() {
	var val = $('#current_page').val();
	if ( (val > 0)  && (val <= page_count)) { 
		current_page = val - 1;
		showPage(current_page, 'left');
	} else {
		$('#current_page').val(current_page + 1);
	}
	displayPagControls();
}

function displayPagControls(){
	
	
	var val = $('#current_page').val();
	
	$('.ControlBoxLeft a').show();
	$('.ControlBoxRight a').show();
	
	if (val <= 1){
		
		$('.ControlBoxLeft a').hide();
		
	} else if (val == page_count) {
		 
		$('.ControlBoxRight a').hide();
		
	}
} 


