var imgView=0;
var FadeSpeed=500;
var TimeOut=5000;
var c;
var l;
var images;
var ff;
var eml;
var pstcd;
var pc;

$(document).ready(function(){	
								
	
	$(window).bind('load',function(){
		$('#images img').each(function(){
			thissrc = $(this).attr('src');
			$('#thumbs').append("<span><img src='" + thissrc + "'/></span>");
		});
		
		eml="email address";
		ff=$('#f1');
		$('#f1').focus(function(){
			if(ff.val()==eml||ff.val()=="")ff.val("")
		}).blur(function(){
			if(ff.val()==eml||ff.val()=="")ff.val(eml);
		});
		
		pstcd="postcode";
		pc=$('#pc');
		$('#pc').focus(function(){
			if(pc.val()==pstcd||pc.val()=="")pc.val("")
		}).blur(function(){
			if(pc.val()==pstcd||pc.val()=="")pc.val(pstcd);
		});

		
		//images (inside doc.ready)	   
		images = $('#images img');
		if(images.length>0){
			c=0; l=images.length-1;
			images.css({opacity:0},{queue:false}).eq(c).css({opacity:1},{queue:false});
			$().everyTime(5000, function(){
				if(c==l) c=0; else c++; 
				images.animate({opacity:0},{queue:false}).eq(c).animate({opacity:1},{queue:false});
			});
		}
		

		//thumb function
		$('#thumbs span').mouseover(function(){
			
			thisind = $('#thumbs span').index(this);
			$('#images img').animate({opacity:0},{queue:false}).eq(thisind).animate({opacity:1},{queue:false});
		});
		
	});
	
	$('#frm_baf').submit(function(){
		emdaddr=$('#f1').val();
		if(CheckEmail(emdaddr))
		{
			$.post("media/scripts/php/baf.php",{emailadd:emdaddr,innname:'The Noble Vine'},function(data){
				$('#bafreturn').AnimateMessage(data);
				$('#frm_baf .txtbox').val('');
			});
		}
		else
		{$('#bafreturn').AnimateMessage("Please enter a valid email address.");}
		return false;							  
	});
	
	$('#frm_meetings').submit(function(){
		var frmAr=new Array();
		var res1=true;
		
		$('#frm_meetings .txtbox').each(function(){
			thisVal=$(this).val();
			if(thisVal!="")
			{frmAr.push(thisVal);}
			else{res1=false;}									 
		});
		if(res1)
		{
			if(CheckEmail(frmAr[1]))
			{
				$.post("media/scripts/php/sndmeeting.php",{
					   f1:frmAr[0],f2:frmAr[1],f3:frmAr[2],f4:frmAr[3],f5:frmAr[4],f6:frmAr[5]},function(data){
					$('#frm_return').AnimateMessage(data);
					$('#frm_meetings .txtbox').val('');
				});
			}
			else
			{$('#frm_return').AnimateMessage("Please enter a valid email address.");}
		}
		else{$('#frm_return').AnimateMessage("Please ensure all fields are filled in correctly.");}
		return false;
	});
	
	$('#frm_directions').submit(function(){
		startfrom=$('#frm_directions .txtbox').val();
		window.open("http://maps.google.co.uk/maps?f=d&saddr="+startfrom+"&daddr=KT12+1DG");
		return false;							  
	});
	
	$('a').click(function(){
		thislink=$(this).attr('href');
		if(thislink=="" || thislink=="#"){return false;}
		else if($(this).attr('rel')=="nw"){window.open(thislink);}
		else{window.location=thislink;}
		return false;
	});

});

$(function(){
	$.fn.AnimateMessage = function(returnmessage){
		$(this).text(returnmessage).fadeIn(FadeSpeed,function(){
			$(this).animate({opacity:1},2000,function(){$(this).fadeOut(FadeSpeed);});
		});
	};
});

function CheckEmail(inputemail) {
	AtPos = inputemail.indexOf("@");
	StopPos = inputemail.lastIndexOf(".");

	if (AtPos == -1 || StopPos == -1)
	{
		return false;
	}
	else
	{
		return true;	
	}

}
