$(document).ready(function(){
	
	//SIGNUP VOUCHER FORM
	
	$('#offer_form button').click(function(){
		email = $('#tulrg-tulrg').attr('value');
		
		if(email == ''){
			alert('Please enter your name and email address');
			return false;
		}else{
			window.open ('/voucher.php?email='+email,'Voucher');
		}
	});
	
	
	//GMAPS
	
	if($('body').attr('id') == 'location'){
		initialize();
	}

	//MENU CATEGORIES
	
	$('#menu_categories li:odd').addClass('odd');
	
	$('#menu_categories li a').click(function(){
		$(this).addClass('on').parent('li').siblings('li').children('a').removeClass('on');
		newContent = $(this).attr('href');
		$(newContent).show().siblings('div.category').hide().removeClass('active');
		
		$('.drop_down').css({overflow:'hidden'});
		$('.drop_down a.top').text($(this).text());
		return false;
	});
	
	//ON LOAD
	
	if($('body').attr('id') == 'menus'){
		url = window.location.href;
		cat = url.substr(url.indexOf('#'));
		$('#menu_categories li a[href='+cat+']').click();
	}
	
	$('#specials dl:odd').addClass('odd');
	
	
	//MENU ICONS
	
	$('dl.vegi dt').append('<span class="vegi"> Vegetarian</span>');
	$('dl.nuts dt').append('<span class="nuts"> Contains nuts</span>');
	
	
	//EVENTS PAGE
	
	if($('.event').size() > 4){
		$('.event').removeClass('open').children('.content').hide();
		$('.event:first .content').show().parent().addClass('open');
		$('.event h2').click(function(){
			if($(this).parent('.event').hasClass('open') != true){
				//CLOSE
				$(this).next('.content').show();
				$('.open .content').hide().parent().removeClass('open');
				$(this).parent().addClass('open');
			}else{
				//OPEN
				$(this).next('.content').hide();
				$(this).parent().removeClass('open');
			}
			return false;
		});
	}
	
	//HOMEPAGE SPECIALS MENU

	xPos = 0;
	sp = 1;
	spTotal = $('#sp_slider div').size();
	moose = true;
	mooseAuto = true;
	spDelay = 5000;
	
	$('#sp_nav a').click(function(){
		c = $(this).text();
		if(moose==true){
			xPos = '-' + (207 * (c-1));
			sp = c;
			slideTo(xPos);
			
			$(this).parent('li').addClass('on').siblings('li').removeClass('on');
		}
		return false;
	});
	
	$('#specials').hover(function(){
		mooseAuto = false;
		$.clearTimer(slideTimer);
	},function(){
		mooseAuto = true;
		autoMagical();
	});
	
	var slideTimer = {};
	
	function autoMagical(){
		slideTimer = $.timer(spDelay,function(){
			if(mooseAuto == true){
				if(sp == spTotal){
					$('#sp_nav li a:first').click();
				}else{
					$('#sp_nav li.on').next('li').children('a').click();
				}
			}
			autoMagical();
		});
	}
	
	autoMagical();
	
	function slideTo(newX){
		moose = false;
		$('#sp_slider').stop([]).animate({left:newX},414,'easeInOutExpo',function(){
			moose = true;
		})
	}
	
	
	//BACK TO TOP LINK
	
	$('#footer .first a').click(function(){
		$('html, body').animate({
			scrollTop: $("#scrollToHere").offset().top
		}, 400, 'easeInOutExpo');
		return false;
	});
	
	//SIGNUP BOX
	
	$('#signup .su_1 a').click(function(){
		$(this).parent().fadeOut(function(){
			$(this).hide().next('.su_2').fadeIn();
		});
		return false;
	});
	
	$('#signup input').each(function(){
		$(this).attr('value',$(this).attr('title'));
	});
	
	$('#signup input').focus(function(){
		if($(this).attr('value') == $(this).attr('title')){
			$(this).attr('value','');
		}
	});
	
	$('#signup input').blur(function(){
		if($(this).attr('value') == ''){
			$(this).attr('value',$(this).attr('title'));
		}
	});

});


//EASING

jQuery.easing['jswing'] = jQuery.easing['swing'];

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;
	}
});


//CUFON

Cufon.replace('#specials h3');
Cufon.replace('.cufon');
Cufon.replace('a.button', {
	hover: true
});


//TIMER

/*
 * 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;
};


//GMAPS

var gmarkers = [];
var htmls = [];
var to_htmls = [];
var from_htmls = [];
var i=0;

// A function to create the marker and set up the event window

function createMarker(point,name,html) {
	var marker = new GMarker(point);

	// The info window version with the "to here" form open
	
	to_htmls[i] = html + '<p class="directions">Directions: <strong class="selected">To here<\/strong> - <a href="javascript:fromhere(' + i + ')">From here<\/a><\/p>' +
	'<p class="form"><strong>Start address:<\/strong><form action="http://maps.google.com/maps" method="get" target="_blank">' +
	'<input type="text" name="saddr" id="saddr" value="" /><br>' +
	'<button type="SUBMIT">Get directions<\/button>' +
	'<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() + '"/><\/p>';
	
	// The info window version with the "to here" form open
	
	from_htmls[i] = html + '<p class="directions">Directions: <a href="javascript:tohere(' + i + ')">To here<\/a> - <strong class="selected">From here<\/strong><\/p>' +
	'<p class="form"><strong>End address:<\/strong><form action="http://maps.google.com/maps" method="get"" target="_blank">' +
	'<input type="text" name="daddr" id="daddr" value="" /><br>' +
	'<input value="Get Directions" TYPE="SUBMIT">' +
	'<input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() + '"/><\/p>';
	
	// The inactive version of the direction info
	
	html = html + '<p class="directions">Directions: <a href="javascript:tohere('+i+')">To here<\/a> - <a href="javascript:fromhere('+i+')">From here<\/a><\/p>';

	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
	});
	//marker.openInfoWindowHtml(html);
	
	gmarkers[i] = marker;
	htmls[i] = html;
	i++;
	return marker;
}

// functions that open the directions forms

function tohere(i) {
	gmarkers[i].openInfoWindowHtml(to_htmls[i]);
}
function fromhere(i) {
	gmarkers[i].openInfoWindowHtml(from_htmls[i]);
}

function initialize() {

	// Display the map, with some controls and set the initial location
	
	var map = new GMap2(document.getElementById("google_map_container"));
	map.setMapType(G_SATELLITE_MAP);
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
	map.removeMapType(G_HYBRID_MAP);
	map.setCenter(new GLatLng( 51.649533, 0.056831), 17);

	//CUSTOM MARKER
	var myIcon = new GIcon(G_DEFAULT_ICON);
	myIcon.image = '/images/gmap/image.png';
	myIcon.printImage = '/images/gmap/printImage.gif';
	myIcon.mozPrintImage = '/images/gmap/mozPrintImage.gif';
	myIcon.iconSize = new GSize(33,33);
	myIcon.shadow = '/images/gmap/shadow.png';
	myIcon.transparent = '/images/gmap/transparent.png';
	myIcon.shadowSize = new GSize(50,33);
	myIcon.printShadow = '/images/gmap/printShadow.gif';
	myIcon.iconAnchor = new GPoint(17,33);
	myIcon.infoWindowAnchor = new GPoint(17,0);
	myIcon.imageMap = [23,0,24,1,25,2,25,3,26,4,27,5,27,6,28,7,29,8,29,9,30,10,31,11,31,12,32,13,32,14,32,15,31,16,30,17,30,18,30,19,30,20,30,21,30,22,30,23,30,24,29,25,28,26,26,27,25,28,23,29,21,30,20,31,18,32,16,32,14,31,13,30,11,29,9,28,7,27,6,26,4,25,3,24,2,23,2,22,2,21,2,20,2,19,2,18,2,17,2,16,1,15,1,14,0,13,0,12,0,11,1,10,2,9,4,8,5,7,7,6,9,5,10,4,12,3,14,2,17,1,20,0];
	
	markerOptions = { icon:myIcon };

	// Set up three markers with info windows 
	
	var point = new GLatLng( 51.649533, 0.056831);
	var marker = createMarker(point,'The Olive Tree','<strong class="title">The Olive Tree<\/strong>');
	map.addOverlay(marker);
}