/* Author: 

                  ___________
          (__)    /           \         
          (oo)   ( MOOoooooo... 
   /-------\/  --'\___________/        
  / |     ||
 *  ||----||             
    ^^    ^^ 

*/

/*region*/
if ( $('.launchoverlay').length  > 0 ) { 
	$('#launchoverlay').colorbox({width:"50%", inline:true, href:"#land_overlay"});
}

var dir = $('body').attr('dir'); //text direction
var base = $('base').attr('href');

$('#main').not('.ie6 #main, .ie7 #main, .ie8 #main').hide().fadeIn(1000);

if ( $('#region').length  > 0 ) { 

	$('#region ul').addClass('select');
	$('#region ul li').addClass('hide');
	
	//Build active
	var s  = $('#region ul').find('.selected').html();
	var l  = $('#region ul').find('.selected').attr('lang');
	var c  = $('#region ul').find('.selected').attr('class');
	
	$('#region ul').prepend('<li class="first '+c+'" lang="'+l+'">'+s+'</li>');
	$('#region ul .first').removeClass('selected hide');
	
	//build toggle
	$('#region ul li a').click(function(e){
		if ( $(this).parent().hasClass('first') ) {
			$(this).parent().siblings('li').not('.first').toggleClass('hide');
			$("#region ul").toggleClass('openend');
			return false;
		}
	});
	
	//close that fang!
	$(document.body).click(function () {
			if( $("#region .openend").length > 0 )  {
					$('#region li').not('.first').addClass('hide');
					$("#region ul").toggleClass('openend');
			}
	});	
	
}


if ( $('.carousel').length  > 0 ) { 
	$('.carousel').removeClass('clearfix');
	/*carousel activation*/
	
	var getstart =  $(".carousel ul li.selected").index('.carousel ul li');
	
	if ($('.carousel ul li').hasClass('selected') == false ) {
		$('.carousel ul li:first').addClass('selected');
	}	
	
	$(".carousel ul").jcarousel({
			circular: false,
			scroll: 1,
			visible:4,
			start: getstart,
			horizontalDirection: dir 
	});
	
	
	$(".carousel ul li a").each(function(){
				
				var href = $(this).attr('href');
				$(this).attr('rel', href+'?method=ajax');
				$(this).click(function(){
						
									$(this).append('<span class="loading">loading</span>');				   
	
						
					$(".carousel ul .selected").removeClass('selected');
					$(this).parents('li').addClass('selected');
									   
						ajax = $(this).attr('rel');
						//make the call
						$.get(ajax, function(data, response) {
						 	
						if (response == "success") {
							
							 // 	var productInfo =  eval("(" + data + ")"); //parse the data
								
								var productInfo =  jQuery.parseJSON(data); 
								
								//build variables
								var a_title			=   productInfo['title']
								var urltoken		=   productInfo['token']
								var a_img			=	productInfo['image']
								var a_desc			=	productInfo['longtexts_1']
								var a_added			=	productInfo['longtexts_2']
								var a_energy		=	productInfo['varchars_2']
								var a_protein		=	productInfo['varchars_3']
								var a_carbs			=	productInfo['varchars_4']
								var a_fat			=	productInfo['varchars_5']
								var a_fibre			=	productInfo['varchars_6']
								var a_sodium		=	productInfo['varchars_7']	
	
								
												
								$('.ingredients ul li').show();
								
								//replace content
								
								
								var img = new Image();
								
								img.onload = function(){

									if ($('.ie7').length > 0 || $('.ie8').length > 0) {
										$('#a_img').css('background-color' , '#d1d2d4');
									}

									$('#a_img').stop().animate({opacity :0},400,function() {
											
											

											$('#a_img').attr({ src: a_img}).animate({opacity :1},400, function() {
												if ($('.ie7').length > 0 || $('.ie8').length > 0) {
													$(this).css('background-color' , 'transparent');
													this.style.removeAttribute('filter');
												}											    
											  });
											
											$('.loading').stop().fadeTo(400, 0 , function(){ 
												$(this).remove();  
											});
									});
								};
								img.src = a_img;

								$('#a_title').hide().html(a_title).fadeIn(1000);
								$('#a_desc').hide().html(a_desc).fadeIn(1000);
								$('#a_added').hide().html(a_added).fadeIn(1000);
								
								//check and build						
								if( a_energy != "" ) { 							
									$('#a_energy').hide().text(a_energy).fadeIn(1000);
								} else { 
									$('#a_energy').parent().hide();
								}
								
								if( a_protein != "" ) { 	
									$('#a_protein').hide().html(a_protein).fadeIn(1000);
								} else { 
									$('#a_protein').parent().hide();
								}
								
								if( a_carbs != "" ) { 	
									$('#a_carbs').hide().html(a_carbs).fadeIn(1000);
								} else { 
									$('#a_carbs').parent().hide();
								}
								
								if( a_fat != "" ) { 	
									$('#a_fat').hide().html(a_fat).fadeIn(1000);
								} else { 
									$('#a_fat').parent().hide();
								}
								
								if( a_fibre != "" ) { 	
									$('#a_fibre').hide().html(a_fibre).fadeIn(1000);
								} else { 
									$('#a_fibre').parent().hide();
								}
								
								if( a_sodium != "" ) { 	
									$('#a_sodium').hide().html(a_sodium).fadeIn(1000);	
								} else { 
									$('#a_sodium').parent().hide();
								}
								
								
								
								
							
								//Stores 
								
								$('#a_avail ul').empty();
								
								if(productInfo.stores =='') { 
									$('#a_avail').hide();
								} else { 
									$.each(productInfo.stores, function(index, element) {
										var src  =  this['files_1_uri']
										var title  =  this['title']
										$('#a_avail ul').append('<li><img  src="uploads/'+src+'" alt="'+title+'" /></li>');
									});
									$('#a_avail').show();
								}
							
							
							 }
							
							//build URL
							window.location.hash = urltoken
							//Google Analytics
									_gaq.push(['_trackPageview', '/our-products/'+urltoken+'/']);
							
						
						});
					
						return false;								   
				});
	 });

}


/*video*/
if ( $('#video').length  > 0 ) { 
	
	// COMMENTED OUT BY MM
	/*$(".adverts ul li a").each(function(){
				
			var href = $(this).attr('href');
			$(this).attr('rel', href+'?method=ajax');
			
			$(this).click(function(){
								   
					
				$(".adverts ul .selected").removeClass('selected');
				$(this).parents('li').addClass('selected');
								   
					ajax = $(this).attr('rel');
					//make the call
					$.get(ajax, function(data, response) {
						if (response == "success") {
							var adinfo = jQuery.parseJSON(data); //parse the data
							//build variables
							var a_title			=   adinfo['title']
							var urltoken		=   adinfo['token']
							var a_img			=	adinfo['image']
							var a_desc			=	adinfo['longtexts_1']
							
							var a_yt_id			=	adinfo['varchars_1']
							
							var medium_img		=	adinfo['medium_img']	
							var large_img		=	adinfo['large_img']	
							
	
							//replace content
							$('#a_title').hide().html(a_title).fadeIn(1000);
							$('#a_desc').hide().html(a_desc).fadeIn(1000);
							
							
							//if YT
							if ( a_yt_id.length > 0 )	{				
								//empty out the player and re-build
								$('#a_video').show().empty().append('<div id="video"></div>');
								$('#poster').remove();
								//set the vars and fire the function
								var youtube_id = a_yt_id;
								swfGo(youtube_id);
								
							} else  {
								//if IMG
								$('#a_video').empty().parent().append('<a id="poster" href="'+ large_img +'" style="display : none"><img src="'+ medium_img +'" alt="'+ a_title +'" /></a>');
								$('#poster').fadeIn(1000).colorbox();
	
							}
							
							_gaq.push(['_trackPageview', '/our-advertising/'+urltoken+'/']); // GA

						}// response
						window.location.hash = urltoken

					});

					return false;								   
			});
	 });*/
	
	
	var youtube_id = $('#youtube_id').val();
	
	var swfGo =	function(youtube_id) { 
		
		var flashvars = {};
		
		flashvars.youtube_code = youtube_id;
			
		var params = {};
		params.menu = "false";
		params.bgcolor = "000000";
		params.wmode = "transparent";
		params.allowFullScreen = "true";
		
		
		var attributes = {};
		attributes.id = "video_flash";
		attributes.name = "video_flash";
		
		swfobject.embedSWF("assets/swf/video_player_yt.swf", "video", "290", "240", "8.0.0", false, flashvars, params, attributes);

	}
	swfGo(youtube_id);
}

if ( $('.launchoverlay').length  > 0 ) { 

	$('#poster').colorbox()
}


/*tabs*/
if ( $('.tabs').length  > 0 ) { 
	
	var tabContainers = $('div.tabs .tab_content');
	
	if ($.browser.msie && $.browser.version.substr(0,1)) {
		tabContainers.hide().filter(':first').show();
	} else {
		tabContainers.hide().filter(':first').fadeTo(400,1);
	}
	
	$('div.tabs ul.t_nav a').click(function () {
		tabContainers.hide();
	if ($.browser.msie && $.browser.version.substr(0,1)) {
		tabContainers.filter(this.hash).show();
	} else {
		tabContainers.filter(this.hash).fadeTo(400,1);
	}
		//tabContainers.filter(this.hash).fadeTo(400,1);
		$('div.tabs ul.t_nav a').removeClass('selected');
		$(this).addClass('selected');
		return false;
	}).filter(':first').click();
	
}


if ( $('#takeover').length  > 0 ) { 

	 $('.overlay').click(function(){ 
				$('#takeover').fadeTo(800, 0, function(){ $('#takeover').remove()  });
	 });

}

/*recipes*/
$(document).ready(function(){

	// Fix figcaption position in banner
	$('figure.banner figcaption').each(function(){
		padding = (255-$(this).outerHeight())/2;
		$(this).css({
			'top' : padding+"px",
			'bottom' : padding+"px"
		});
	});
	
	// Nice input focus
	$('input.focus').live('mouseenter', function(){
		if($(this).attr('value') == $(this).attr('placeholder')) $(this).val('');
		$(this).focus();
	});
	$('input.focus').live('mouseleave', function(){
		if($(this).attr('value') == '') {
			$(this).val($(this).attr('placeholder'));
			$(this).blur();
		}
	});
	$('textarea.focus').live('mouseenter', function(){
		if($(this).html() == $(this).attr('placeholder')) $(this).html('');
		$(this).focus();
	});
	
	// Display form
	$('#new-recipe').click(function(){
		$('#overlay').load('http://192.168.1.156:8888/lurpak-recipes/submit.html article.submit-recipe', function() {
  			$(this).fadeIn(300);
		});
		return false;
	});
	$('#overlay').click(function(e){
        if (e.target === this) $('#overlay').fadeOut(300);
        return false;
    });
	        
});

