//------------------------------------
//	CHEZ.JS
//	Author: 	Engage Interactive
//	Requires:	jquery 1.4.2
//				Cufon
//				Trade Gothic font
//	Version:	0.1
//------------------------------------

////////////////////////////
// CUFON

Cufon.replace('#content h2, a.more, a.more_blue, form button, h3:not(hgroup h3), footer h4, .private_dining h4');


////////////////////////////
// BEGIN JQUERY

$(function(){

	$('.external').attr('target','_blank');

	// Ban the moose. BAD MOOSE!
	var mooseBan = false;
	
	// Add on state to nav as soon as clicked. Makes it look nicer.
	$('header nav ul li a').click(function(){
		$(this).addClass('on').parent('li').siblings('li').children('a').removeClass('on');
	});
	
	// Footer column autoheight
	$('footer .dark').css({height:$('footer').innerHeight() - 16});
	
	function slider(){
		$('#promo ul li a').click(function(){
			var dir = $(this).parent().attr('class');			
		});
	}
	
	slider();
	
	// Locations select
	$('#book_location').change(function(){
		location.href = $(this).val();								
	});
	
	
	////////////////////////////
	// FIND MY NEAREST
	
	var $nearestResults = $('#results');
	
	var nearestMsg = function(html) {
		
		$nearestResults.animate({height:0},600,'easeInOutExpo',function(){
			$nearestResults.removeAttr('style').hide();
				
			$nearestResults.html(html).show();
			var h = $nearestResults.find('ul').height();
			if(h == null) h = $nearestResults.children('div').height();
			$nearestResults.animate({height:h},800,'easeInOutExpo',function(){
				$('#nearest .loading').remove();
			});	
		});
		
	};
	
	$('#nearest form').submit(function(e){
		e.preventDefault();
		
		var searchTerm = $(this).find('input').val();
		
		if(!googleMapsEnabled) return false;
		
		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;
		}
		
		$('<span/>',{
			'class':'loading'
		}).appendTo(this);
			
		findmynearest(searchTerm, {
			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];
					htmlResults += '<li><a href="/location/' + location.slug + '">' + location.name + ' <span>' + location.miles + 'mi</span></a></li>';
				}
				htmlResults += '</ul>';
				nearestMsg(htmlResults);
			}			  
		});
	});
	
	// Hide results
	$('#nearest form input').focus(function(){
		if(!$nearestResults.length) return false;
		$nearestResults.animate({height:0},600,'easeInOutExpo',function(){
			$(this).removeAttr('style').hide().html('');
		});
	});

});

jQuery.extend( jQuery.easing,{
	easeInOutExpo: function (x, t, b, c, d){
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	}
});

/*
 * jQuery Timer Plugin
 * http://www.evanbot.com/article/jquery-timer-plugin/23
 *
 * @version      1.0
 * @copyright    2009 Evan Byrne (http://www.evanbot.com)
 */

jQuery.timer = function(time,func,callback){
	var a = {timer:setTimeout(func,time),callback:null}
	if(typeof(callback) == 'function'){a.callback = callback;}
	return a;
};

jQuery.clearTimer = function(a){
	clearTimeout(a.timer);
	if(typeof(a.callback) == 'function'){a.callback();};
	return this;
};
