
function get_slide_offset(num) {
	return (num * 38);
}

function slide() {

	clearInterval(animate_interval_id);

	$this = $(this);
	
	$this.blur();
	
		var num = $this.attr('id').split('-', 2)[1];
		slide_indv(num);

	return false;
}

function slide_indv(num) {
	
	var offset;
	
	num = parseInt(num);
	
	if ($('#slide-'+num).hasClass("open")) {

		$('.open').each(function(){
			
			var current_offset = $('#slide-'+num).css('left').replace('px', '');
		
			if ('#slide-'+num){
			
				$('#slide-'+num).animate({left: ((num * 38) - 38)}, 600);
				$('#slide-'+num).removeClass("open");	
				$('#slide-'+num).css("z-index:-1");
				num = (num + 1);
			}
		}); 
	
	}else{
		
		for (i=1;i<=6;i++) {
			if (num >= (i +1)){	
				
				var id = i
				
				var current_offset = $('#slide-'+id).css('left').replace('px', '');
				
				if (($('#slide-'+id).position().left) > -1){
					
					$('#slide-'+id).animate({left: -(695 - current_offset)}, 600);
					$('#slide-'+id).addClass("open");
				
				}
			}
		}
	}
}


var slide_open = 1;
var state = 0;
var animate_interval_id = 0;

function animate_slides(num) {

	if (slide_open == 1) {
		slide_open = 1;
		state = 0;
	}
	
	if (slide_open == 7) {
		slide_open = 7;
		state = 1;
	}
	
	if (state == 0){
		slide_open = (slide_open + 1);
	}

	if (state == 1){
		slide_open = (slide_open - 1); 
	}
	
	slide_indv(slide_open);

	
	
}

function redirect() {
	
		$this = $(this);
		
		var num = $this.parent().attr('id').split('-', 2)[1];
		
		if (num == 1){
			window.location.replace("/accommodation/");
		}else if(num == 2){
			window.location.replace("/shop/");
		}else if(num == 3){
			window.location.replace("/weddings/");
		}else if(num == 4){
			window.location.replace("/restaurants-and-bars/");
		}else if(num == 5){
			window.location.replace("/meetings-and-events/");
		}else if(num == 6){
			window.location.replace("/loch-fyne/");
		}else if(num == 7){
			window.location.replace("/water/");
		}
}



function isDate(dateStr) {

	var errors;
	
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?

	if (matchArray == null) {
		errors = ("Please enter date as either dd/mm/yyyy.");
		return errors;
	}

	day = matchArray[1];
	month = matchArray[3]; // p@rse date into variables
	year = matchArray[5];

	if (day < 1 || day > 31) {
		errors = ("Day must be between 1 and 31.");
		return errors;
	}
	
	if (month < 1 || month > 12) { // check month range
		errors = ("Month must be between 1 and 12.");
		return errors;
	}

	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		errors = ("Month "+month+" doesn`t have 31 days!")
		return errors;
	}

	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
			if (day > 29 || (day==29 && !isleap)) {
				errors =  ("February " + year + " doesn`t have " + day + " days!");
			return errors;
		}
	}
	return true; // date is valid
}

function checkemail(email){
	
	var errors;
	
	
	var filter=/^.+@.+\..{2,3}$/

	 if (filter.test(email))
	    return true
	 else {
		 errors = ("Please input a valid email address!")
		 return errors;
	} 
}

function validate(){
	
	var email = $('#email').val()
	
	var errorOutput;
	
	var emailbln = checkemail(email);
	
	if (emailbln == true){
		
		
		$("#form_error").hide();
		$('#contact_submit').removeAttr("disabled");
		
	}else{

		$('#contact_submit').attr("disabled", "true");
		
		if (emailbln == true){
			emailbln = "";
		}else{
			errorOutput = emailbln;
		}	
		
		$("#form_error").replaceWith("<div id='form_error'><p>" + errorOutput + "</p></div>");
	}
}

function imgswap() {
	
	$this = $(this);
	
	var tn_path = this;
	
	$('#main_image img').attr('src', tn_path);

	return false;
	
}

$('document').ready(function() {

	$('#slider').hide();
	$('#contact_submit').attr("disabled", "true");
	$('#date, #email').keyup(validate);
	
	if ($('#slides').length != 0) {
		
		$('#sc-1').click(slide);
		$('#sc-2').click(slide);
		$('#sc-3').click(slide);
		$('#sc-4').click(slide);
		$('#sc-5').click(slide);
		$('#sc-6').click(slide);
		$('#sc-7').click(slide);

		$('.sl').click(redirect);

		$('#slider').show();
		//animate_slides(0)

	}

	$("#form_error").hide();
	
	$('#thumbs a').click(imgswap);
	$('#thumbs a').hover(function(){$(this).css('cursor','pointer'); });
  
	$('#gallery_thumbs a').lightBox();
	$('#gallery_thumbs a').hover(function(){$(this).css('cursor','pointer'); });

	$('#berths_map a').lightBox();
	$('#berths_map a').hover(function(){$(this).css('cursor','pointer'); });
	
	$('.sl').hover(function(){$(this).css('cursor','pointer'); });

	$(document).pngFix(); 
});




