/*---[ Details ]---------------------------------------
Global Javascript Scripts
Author: Lee Powell
Contact: lee.powell@soup.co.uk
-------------------------------------------------------*/

/*-----------------------------------------------------
jQuery
-------------------------------------------------------*/
(function($){

/*
 * Add class to html element for styling hook
 * i.e. 'h1 { color: red; }, .js-active h1 { color: blue; }' will display accordingly if javascript is active
 */
$('html').addClass('js-active');


Cufon.replace('.cufon');

/*
 * Shadowbox
 */
if( typeof Shadowbox != 'undefined' )
{
	Shadowbox.init({
		skipSetup: true,
		players: ["html"]
	});
}

/*
 * jQuery document load...
 */
jQuery(function()
{
	
	var content = $('#content');
	
	if( content.hasClass('product') ) productPageInit();
	if( content.hasClass('range') ) rangePageInit();
	if( content.hasClass('recipe') ) recipePageInit();
	if( content.hasClass('recipes-home') ) recipeHomePageInit();
	if( content.hasClass('faqs') ) faqsPageInit();
	if( content.hasClass('wholegrain') ) wholegrainPageInit();
	if( content.hasClass('confessions') ) confessionsPageInit();
	
	
	$('.print').live('click', 
		function(e)
		{
			window.print();
			e.preventDefault();
		}
	);
	
	function productPageInit()
	{
		var carousel = $('#product-carousel');
		
		var carouselInner = $('#product-carousel-inner');
		
		var carouselItems = $('li', carouselInner);
		
		/* Find the index number of the 'current' carousel item */
		var carouselFocus = carouselItems.index( $('li.current') );
								
		if( carouselItems.length > 3 )
		{	
			carousel.addClass('carousel-active');
		
			/* Previous button */
			var btnPrev = $('<div></div>');
			
			btnPrev.addClass('prev');
			
			btnPrev.text('Previous');
			
			
			/* Next button */
			var btnNext = $('<div></div>');
			
			btnNext.addClass('next');
			
			btnNext.text('Next');

			
			/* Add Previous and Next buttons */			
			carousel.append(btnPrev);
			carousel.append(btnNext);
			
			carouselInner.jCarouselLite({
				btnNext: btnNext,
        		btnPrev: btnPrev,
        		circular: true,
        		start: carouselFocus
			});		
		}
		
		/* Product faqs */
		var faqs = $('#product-faqs dl');
				
		$('dd, dd div', faqs).hide();
		
		$('dt', faqs).click(
			function()
			{
				handleFaqs( $(this) );
			}
		);
		
		/* Shadowbox */
		$('a.gda').bind(
			'click keypress',
			
			function()
			{
				$.get(
					'/media/site/includes/inc.gda.php',
					
					{ product: 'true' },
					
					function( data ){
						Shadowbox.open({
							content: data,
							player:  'html',
							width:	 660,
							height:  530,
							options: {
								overlayColor: '#FFF'
							}
						});
					}
				);
				
				return false;
			}	
		);

	}
	
	function rangePageInit()
	{
		var prodRange = $('#product-range');
		
		$('ul li', prodRange).each(
			function()
			{
				/* Set some scope */
				var $this = $(this);
								
				/* This tooltip */
				var tooltip = $('span.tooltip', $this);
				
				/* All tooltips */
				var tooltips = $('span.tooltip');
				
				/* Add handler to overlay everything, this stops mouse flickering effect */
				var handler = $('<div></div>');
				
				handler.addClass('handler');
				
				$this.append(handler);
				
				handler.width( $this.width() );
				
				handler.height( $this.height() );
				
				handler.css({ 'background': 'url("/media/site/images/front-end/misc/ie_fixes/spacer.gif")' });
								
				/* remove hide class, and let jquery handle the show / hide from here on */
				tooltip.hide();
				
				tooltip.removeClass('js-hide');
				
				/* mouseover event for anchor to show tooltip, we add hover class to generate fake hover effect */
				$(handler).mouseover(
					function()
					{
						tooltips.hide();
						
						/* MSIE keeps having strange issues with pngs and fading them in, so we'll just show it instead */
						if( $.browser.msie )
						{
							tooltip.show();
						}
						else
						{
							tooltip.fadeIn('1');
						}
						
						$this.addClass('hover');
					}
				);
				
				/* mouseout event for anchor to hide tooltips and remove hover class */
				$(handler).mouseout(
					function()
					{
						tooltip.hide();
						
						$this.removeClass('hover');
					}
				);
				
				$(handler).click(
					function()
					{
						window.location = $('a', $this).attr('href');
					}
				);
			}
		);
	}
	
	
	function recipePageInit()
	{
		// Add print button
		var pageControls = $('ul#page-controls');
		
		if( pageControls.length ){
			
			var targets = $('li', pageControls);
			
			$(targets[0]).after('<li><a href="#" class="print imr">Print recipe</a></li>');
			
			var targets = $('li', pageControls);
		}
		
		// Add email button and handler
		var share = $('#recipe-share ul');
		
		if( share.length ){
			var target = $('#email-friend-wrapper');
			
			if( target.hasClass('js-hide') ){
				target.hide().removeClass('js-hide');
			}
			
			$(share[0]).append('<li class="em"><a href="#tell-a-friend" title="Share this recipe through Email">Email</a></li>');
			
			$('.em a', share).click(
				function(e){
					
					if( target.is(':visible') ){
						target.slideUp('fast');
					}
					else {
						target.slideDown('fast');
					}
										
					e.preventDefault();				
				}			
			);
		}
	}
	
	function recipeHomePageInit()
	{
		// Thorpe park promo
		var promoDefault = 'Enter your email address to...';
		
		var promoInput = $('#tp-promo-email');
		
		promoInput
		
		.focus(
			function(){
				
				if( $(this).val() == promoDefault ){
					$(this).val('');
				}
			}
		)
		
		.blur(
			function(){
				
				if( $.trim($(this).val()) == '' ){
					$(this).val(promoDefault);
				}
			}
		);
		
		// 500 max answer
		var max = 500;
		
		var textArea = $('#ya-answer');
		
		textArea.keyup(
			function(e){
				var val = textArea.val();
				var lng = val.length;
				
				if( lng > max ){
					textArea.val( val.substr(0, max) );
					textArea.scrollTop('400'); // keeps the focus at the bottom of the textarea
				}
			}
		);

	}

	
	function faqsPageInit()
	{
		var faqs = $('#faqs');
		
		$('dd, dd div', faqs).hide();
		
		$('dt', faqs).click(
			function()
			{
				handleFaqs( $(this) );
			}
		);
	}
	
	function wholegrainPageInit()
	{	
		if( !$('#content').hasClass('campaign') )
		{
			/* Whole grain section */
			$('div.profile').hide().removeClass('js-hide');
		
			/* Shadowbox */
			$('a.profile').bind(
				'click keypress',
				
				function()
				{
					displayProfileOverlay( $(this).attr('href') );				
					return false;
				}	
			);
			
			/* Embed youtube video */
			var videoId = 'fQy0L0jB_jI';
			
			var params = { allowScriptAccess: "always" };
    		var attrs = { id: "youtube" };

			swfobject.embedSWF("http://www.youtube.com/v/"+videoId+"&f=videos&app=youtube_gdata&enablejsapi=1&playerapiid=ytplayer&rel=0", "youtube", "535", "301", "8", "/media/site/swf/expressInstall.swf", null, params, attrs);
		}			
	}
	
	function confessionsPageInit()
	{
		$('#confessions-cam li a').click(
			function()
			{
				var url = $(this).attr('href');
				
				url = url.split('=');
				
				v(url[1]);
				
				return false;
			}
		);
		
		$('#msgTxt')
			.focus(
				function()
				{
					tb(true);
				}
			)
			.blur(
				function()		
				{
					tb(false);
				}
			);
			
	}
	
});

function handleFaqs( $this ){
	var dd = $this.next('dd');
	var div = $('div', dd);
	
	if( dd.is(':visible') )
	{
		div.slideUp(
			'fast', 
			function()
			{
				dd.hide();
			}
		);
	}
	else
	{
		dd.show();
		div.slideDown('fast');
	}
	
	$this.toggleClass('open');
}

})(jQuery);
/* END jQUERY */


function displayProfileOverlay( target )
{	
	$('#sb-container').addClass('wholegrain');
			
	var target = $(target);
	var html = target.html();
					
	Shadowbox.open({
		content: '<div id="' + target.attr('id') + '" class="profile overlay">' + html + '</div>',
		player:  'html',
		width:	 660,
		height:  530,
		options: {
			overlayColor: '#FFF',
			onFinish: function(){
				var heading = $('#sb-content h2');
				heading.removeClass('js-hide');
				Cufon.replace( heading );
				Cufon.refresh();
			}
		}
	});
}