//------------------------------------
//	HOME.JS
//	Author: 	Engage Interactive
//	Requires:	jquery
//				corporate.js
//------------------------------------


Cufon.replace('article h1, html:not(.ie7,.ie6,.ie8) article p, article a',{ 
	textShadow:	'1px 1px #000000',
	hover:		{
		color:	'#FFF'
	}
});


$(function(){
//BEGIN jQuery

	// Ban the moose. BAD MOOSE!
	var mooseBan = false;
	
	var videoOpen = false;
	
	// Video player
	$('a[href=#video]').click(function(){
		if( videoOpen == false ){
			
			videoOpen = true;
			$('#video').show().css({opacity:0}).animate({opacity:1},300,function(){
				
				setTimeout(function() {
					$('#player_holder').html('<iframe src="http://player.vimeo.com/video/23202625?title=0&amp;byline=0&amp;portrait=0&amp;color=512698&amp;bgcolor=000000&amp;autoplay=1" bgcolor="000000" width="640" height="360" frameborder="0"></iframe>');
				}, 150);
				
			});
		}
		$('html, body').animate({scrollTop:0},600,'easeInOutExpo');
		return false;
	});
	
	$('#video .close').click(function(){
		$('#video').fadeOut(300,function(){
			
			$('#player_holder iframe').remove();	
			videoOpen = false;
			
		});
		return false;
	});
	
	hpslider = function(){
		
		// Variables
		$sc = $('#slider');									// Container
		$s = $('#slide');									// Sliding div
		$n = $('#slide_nav ul');							// Logos container
		var total = $n.find('li').size() - 1;				// Total
		var logos = new Array();							// Array to store logos
		var c = 0;											// Current slide
		var cnav = 0;										// Middle logo
		var h = $sc.find('li:first').outerHeight();			// Logo height
		var s = 600;										// Speed of animation
		var e = 'easeInOutExpo';							// Easing to use
		var r = Math.floor( Math.random() * 3 );			// Random intial logo
		var d = 6;											// Delay between slides in seconds
		
		// Save backgrounds in data
		$s.find('article').each(function(){
			$(this).data('image',$(this).attr('title')).removeAttr('title');
		});
		
		// Store logos in array and then show first three
		setUpNav = function(){
			for ( i = 0; i <= total; i++ ){
				
				// Randomise the order of the first 3
				if( i <= 2 ){
					logos[i] = $n.find('li:eq(' + r + ')').html();
					r++;
					if( r > 2 ) r = 0;
				}else{
					logos[i] = $n.find('li:eq(' + i + ')').html();
				}
				
				// Once we've got them all...
				if( i == total ){
					
					// Add in the three visible nav items and set on state
					$n.html('<li>' + logos[0] + '</li><li>' + logos[1] + '</li><li>' + logos[2] + '</li>').find('li:eq(1)').children('a').addClass('on');
					
					// Remove any extras
					$n.find('li:lt(3)').show().siblings(':not(:visible)').remove();

					var href = cleanUrl($n.find('li:eq(1) a').attr('href'));
					var $active = $(href);

					$active.addClass('active').loadImg($active.data('image'),function(){
						var src = 'url(' + $(this).attr('src') + ')';
						$s.find('.active').css({backgroundImage:src});
						$sc.find('.loading').hide();
					});
				}
			}
		}
		
		setUpNav();
		
		// Nav click
		$('#slide_nav a').live('click',function(){

			$a = $(this);

			if( mooseBan == false && !$a.hasClass('on') ){
				
				mooseBan = true;
				
				if( $a.hasClass('scroll') ){
					
					navSlide($a.attr('class').replace('scroll scroll_',''),true);
					
				}else{

					if( $a.parent('li').index() == 0 ){
						navSlide('up');
					}else if( $a.parent('li').index() == 2 ){
						navSlide('down');
					}else{
						navSlide('stay');
					}
					
					$a.addClass('on').parent('li').siblings('li').children('a').removeClass('on');
					
				}
			
			}
			
			return false;
		});
		
		navSlide = function(dir,page){
			
			cleanUp = function(){
				
				if( dir == 'up' ){
					$n.find('li:gt(2)').remove();
				}else if( dir == 'down' ){
					if( page == true ){
						$n.find('li:lt(3)').remove();
					}else{
						$n.find('li:first').remove();
					}
				}
				
				$n.css({top:8});
				
				if( page == true ){
					unBan();
				}
				
			}
			
			setCurrent = function(){
				$n.find('li a[href=#' + $sc.find('.active').attr('id') + ']').addClass('on');
			}

			if( dir == 'up' ){

				if( page != true ) goTo($n.find('li:eq(0)').children('a').attr('href'));

				cnav--;

				if( cnav < 0 ){
					cnav = total;
				}
				
				if( page != true ){
					
					$('<li/>',{
						html:	logos[cnav]
					}).prependTo($n).show();
					
					$n.css({top:-126}).animate({top:8},s,e,cleanUp);
					
				}else{
				
					for( i = 0; i <= 2; i++ ){
						
						$('<li/>',{
							html:	logos[cnav]
						}).prependTo($n).show();
						
						if( i == 2 ){
							setCurrent();
							$n.css({top:-394}).animate({top:8},s,e,cleanUp);
						}else{
							cnav--;
						
							if( cnav < 0 ){
								cnav = total;
							}
						}
						
					}
				
				}
				
			}else if( dir == 'down' ){

				if( page != true ) goTo($n.find('li:eq(2)').children('a').attr('href'));
				
				cnav++;
				
				if( cnav > total ){
					cnav = 0;
				}
				
				if( page != true ){
				
					next = cnav + 2;
					
					if( next > total ){
						next = ( next - total ) - 1;
					}
					
					$('<li/>',{
						html:	logos[next]
					}).appendTo($n).show();
					
					$n.animate({top:-126},s,e,cleanUp);
					
				}else{
					
					for( i = 0; i <= 2; i++ ){
						
						next = cnav + 2;
						
						if( next > total ){
							next = ( next - total ) - 1;
						}
						
						$('<li/>',{
							html:	logos[next]
						}).appendTo($n).show();
						
						if( i == 2 ){
							setCurrent();
							$n.animate({top:-394},s,e,cleanUp);
						}else{
							cnav++;
							
							if( cnav > total ){
								cnav = 0;
							}
						}
					}
				}
				
			}else{
				goTo($n.find('li:eq(1)').children('a').attr('href'));
				cleanUp();
			}
			
		}
		
		goTo = function(id){

			id = cleanUrl(id);

			loaded = false;
			url = $s.find(id).data('image');

			setTimeout("if( loaded == false ) $sc.find('.loading').fadeIn();",500);
			
			$sc.find('.active' ).css({display:'block', left: 0}).removeClass('active');
			
			$(id).addClass('active new').css({backgroundImage: 'url(' + url + ')'});
			
			$s.loadImg(url,function(){
				
				loaded = true;
				
				$sc.find('.loading').fadeOut();
				
				$s.animate({left:-710},1000,e,function(){
					$(id).removeClass('new').siblings().removeAttr('style');
					$s.css({left:5});
					unBan();
				});
			});

		}
		
		$sc.mouseenter(function(){
			$.clearTimer(sliderTimer);
		});
		$sc.mouseleave(function(){
			autoMagical();
		});
		
		var sliderTimer = {};
		
		function autoMagical(){
			sliderTimer = $.timer(( d * 1000 ),function(){
				$.clearTimer(sliderTimer);
				$n.find('li:eq(2) a').click();
				autoMagical();
			});
		}
		
		autoMagical();
		
	}
	
	hpslider();
	
	// Deban the mouse
	unBan = function(){
		mooseBan = false;
	}

	// Clean up url
	function cleanUrl(url){
		return url.substr(url.indexOf('#'));
	}

	
	////////////////////////////
	// FIND MY NEAREST
	
	var $r = $('#results');
	
	var nearestMsg = function(html) {
		
		// Hide intro paragraph
		$('#location_finder > p').animate({height:0, paddingBottom:0},600,'easeInOutExpo',function(){
	
			$r.animate({height:0},600,'easeInOutExpo',function(){
	
				$r.html(html).show();
				var h = $r.find('ul').height();
				if(h == null) h = $r.children('div').height();
				$r.addClass('active').animate({height:h},600,'easeInOutExpo',function(){
					$('#location_finder .loading').fadeOut(function(){
						$(this).remove();
					});
				});	
				
			});
			
		});
		
	};
	
	$('#location_finder form').submit(function(e){
		
		// Prevent submit
		e.preventDefault();
		
		// Add loading spinner
		$('<span/>',{
			'class':'loading'
		}).appendTo(this).hide().fadeIn();
		
		// Get search term
		var searchTerm = $(this).find('input').val();
		
		if( searchTerm == '' ){
			nearestMsg('<div class="error"><h4>Please enter a search term</h4><p>Why not try searching using one of the following:</p><p class="examples">&raquo; A full UK postcode<br />&raquo; A UK city, town or village<br />&raquo; A UK landmark (e.g. Big Ben)</p></div>');
			return false;
		}
		
		$r.removeClass('active').animate({height:0},600,'easeInOutExpo');
		
		// Do the search
		findmynearest(searchTerm, {
			dataSource: 'locations/alllocationsJsonList',
			searchTotal: 4,
			onLondon: function() {
				nearestMsg('<div class="error"><h4>Please try and refine your search</h4><p>We have many restaurants in London so if you could try and be a bit more specific that would really help.</p><p>For example, try entering a London suburb such as &lsquo;Islington&rsquo;, a postcode or even a popular landmark.</p></div>');
			},
			onNoResult: function() {
				nearestMsg('<div class="error"><h4>Sorry&hellip;</h4><p>We couldn&rsquo;t find any matches. Why not try searching using one of the following:</p><p class="examples">&raquo; A full UK postcode<br />&raquo; A UK city, town or village<br />&raquo; A UK landmark (e.g. Big Ben)</p></div>');
			},
			onResult: function(locations) {
				var htmlResults = '<ul>';
				for(var key in locations) {
					var location = locations[key];
					var aMap = ['', 'Bella Italia', 'Belgo', 'Caf&eacute; Rouge', 'Strada'];
					var aMapUrl = ['', 'www.bellaitalia.co.uk', 'www.belgo-restaurants.co.uk', 'www.caferouge.co.uk', 'www.strada.co.uk'];
					var url = 'http://' + aMapUrl[location.brand] + '/location/' + location.slug;
					if(location.brand == 2) url = 'http://www.belgo-restaurants.co.uk/locations';
					htmlResults += '<li><a href="' + url + '" title="View ' + aMap[location.brand] + ' - ' + location.name + '"><strong>' + aMap[location.brand] + '</strong> ' + location.name + ' <em>' + location.miles + 'mi</em></a></li>';
				}
				htmlResults += '</ul>';
				nearestMsg(htmlResults);
			}			  
		});
		
	});
	
	$('#location_finder form input').focus(function(){
		if( !$r.hasClass('active') ) return false;
		$r.removeClass('active').animate({height:0},600,'easeInOutExpo',function(){
			//$r.empty();
		});
	});
	
//END jQuery
});


(function($){
	$.fn.loadImg = function(src,callback){
		var img = new Image();
		$(img).load(function(){
			$(this).hide();
			callback.call(this);
		}).attr('src', src);
	};
})(jQuery);
