// ============================================== //

		// PAGE SIZE FUNCTIONS //

// ============================================== //

_getPageSize = function() {var xScroll, yScroll;if (window.innerHeight && window.scrollMaxY) {xScroll = window.innerWidth + window.scrollMaxX;yScroll = window.innerHeight + window.scrollMaxY;} else if (document.body.scrollHeight > document.body.offsetHeight){xScroll = document.body.scrollWidth;yScroll = document.body.scrollHeight;} else {xScroll = document.body.offsetWidth;yScroll = document.body.offsetHeight;};var windowWidth, windowHeight;if (self.innerHeight) {if(document.documentElement.clientWidth){windowWidth = document.documentElement.clientWidth;} else {windowWidth = self.innerWidth;};windowHeight = self.innerHeight;} else if (document.documentElement && document.documentElement.clientHeight) {windowWidth = document.documentElement.clientWidth;windowHeight = document.documentElement.clientHeight;} else if (document.body) {windowWidth = document.body.clientWidth;windowHeight = document.body.clientHeight;};if(yScroll < windowHeight){pageHeight = windowHeight;} else { pageHeight = yScroll;};if(xScroll < windowWidth){	pageWidth = xScroll;		} else {pageWidth = windowWidth;};arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);return arrayPageSize;};

_getPageScroll = function() {var xScroll, yScroll;if (self.pageYOffset) {yScroll = self.pageYOffset;xScroll = self.pageXOffset;} else if (document.documentElement && document.documentElement.scrollTop) {yScroll = document.documentElement.scrollTop;xScroll = document.documentElement.scrollLeft;} else if (document.body) {yScroll = document.body.scrollTop;xScroll = document.body.scrollLeft;};arrayPageScroll = new Array(xScroll,yScroll);return arrayPageScroll;};
		
		
// ============================================== //

		// TIMER FUNCTIONS //

// ============================================== //

jQuery.fn.extend({everyTime:function(interval,label,fn,times,belay){return this.each(function(){jQuery.timer.add(this,interval,label,fn,times,belay)})},oneTime:function(interval,label,fn){return this.each(function(){jQuery.timer.add(this,interval,label,fn,1)})},stopTime:function(label,fn){return this.each(function(){jQuery.timer.remove(this,label,fn)})}});jQuery.extend({timer:{guid:1,global:{},regex:/^([0-9]+)\s*(.*s)?$/,powers:{'ms':1,'cs':10,'ds':100,'s':1000,'das':10000,'hs':100000,'ks':1000000},timeParse:function(value){if(value==undefined||value==null)return null;var result=this.regex.exec(jQuery.trim(value.toString()));if(result[2]){var num=parseInt(result[1],10);var mult=this.powers[result[2]]||1;return num*mult}else{return value}},add:function(element,interval,label,fn,times,belay){var counter=0;if(jQuery.isFunction(label)){if(!times)times=fn;fn=label;label=interval}interval=jQuery.timer.timeParse(interval);if(typeof interval!='number'||isNaN(interval)||interval<=0)return;if(times&&times.constructor!=Number){belay=!!times;times=0}times=times||0;belay=belay||false;if(!element.$timers)element.$timers={};if(!element.$timers[label])element.$timers[label]={};fn.$timerID=fn.$timerID||this.guid++;var handler=function(){if(belay&&this.inProgress)return;this.inProgress=true;if((++counter>times&&times!==0)||fn.call(element,counter)===false)jQuery.timer.remove(element,label,fn);this.inProgress=false};handler.$timerID=fn.$timerID;if(!element.$timers[label][fn.$timerID])element.$timers[label][fn.$timerID]=window.setInterval(handler,interval);if(!this.global[label])this.global[label]=[];this.global[label].push(element)},remove:function(element,label,fn){var timers=element.$timers,ret;if(timers){if(!label){for(label in timers)this.remove(element,label,fn)}else if(timers[label]){if(fn){if(fn.$timerID){window.clearInterval(timers[label][fn.$timerID]);delete timers[label][fn.$timerID]}}else{for(var fn in timers[label]){window.clearInterval(timers[label][fn]);delete timers[label][fn]}}for(ret in timers[label])break;if(!ret){ret=null;delete timers[label]}}for(ret in timers)break;if(!ret)element.$timers=null}}}});if(jQuery.browser.msie)jQuery(window).one("unload",function(){var global=jQuery.timer.global;for(var label in global){var els=global[label],i=els.length;while(--i)jQuery.timer.remove(els[i],label)}});

// ============================================== //

		// HOME PAGE ROLLOVERS //

// ============================================== //

homeWave = function(whichNum){
	var target = $('#home_grid').find('li').find('a').eq(whichNum);
	target.animate({ opacity: 1 }, 600, function(){
		$(this).animate({ opacity: 0 }, 600);
	});
};

setHomeWave = function(whichNum){
	homeWave(whichNum);
	whichNum++;
	if(whichNum==2){
		homeWave(whichNum);
		whichNum++;
	};
	if(whichNum==6){
		whichNum=0;
	};
	//
	$('#home_grid').stopTime().oneTime(500, function() {
		setHomeWave(whichNum);
	});
};

setHomeImages = function(){
	$('#home_grid').find('li').find('a').each(function() {
		var target = $(this);
		//
		target.animate({ opacity: 0 }, 0.001, function(){
			target.css('display', 'block');
		});
		//
		target.mouseenter(function(){
			$('#home_grid').stopTime();
			$('#home_grid').find('li').find('a').each(function() {
				$(this).stop().animate({ opacity: 0 }, 0.001);
			});
			target.animate({ opacity: 1 }, 200)	;
		}).mouseleave(function(){
			target.animate({ opacity: 0 }, 400, function(){
				$('#home_grid').stopTime().oneTime(800, function() {
					setHomeWave(0);
				});
			});	
		}).click(function(){
			setImageViewer(this);
			return false;
		});
	});
	setHomeWave(0);
};


// ============================================== //

		// IMAGE VIEWER FUNCTIONS //

// ============================================== //
	
addEnlargeLinks = function(){
	$('#image_block').find('a').each(function() {
		addEnlargeLink($(this));
	});
};

addEnlargeLink = function(whichLInk){
	whichLInk.click(function() {
		setImageViewer(this);
		return false;						
	});
};

setImageViewer = function(target){
	var arrPageSizes = _getPageSize();
	var imgPath = target.getAttribute('href');
	var contentHeight = $('#content').height();
	var contentwidth = arrPageSizes[0]-180;
	//
	$('body').append('<div id="jquery-overlay"><\/div><div id="enlarge_image_item"><a href="#" title="Close Window" class="close_window"></a><\/div>');
	//
	$('#jquery-overlay').css({
		opacity:	10,
		display:	'block',
		width:		contentwidth,
		height:		contentHeight		
	}).animate({opacity: 1}, 250, function(){
		var htmlVar = '<img src="'+imgPath+'" style="display:none;" \/>';
		$('#enlarge_image_item').prepend(htmlVar).find('img').animate({opacity: 0.001}, 0).show();
		//
		var img = new Image();
		$(img).load(function () {
			$('#enlarge_image_item img').animate({opacity: 1}, 250, function(){
				$('#enlarge_image_item').css({background:	'none'});															 
			});
		}).attr('src', imgPath);
	});
	//
	$(window).resize(function() {
		resizeOverlay();
	});
	//
	$('#jquery-overlay, #enlarge_image_item, a.close_window').click(function() {
		$('#enlarge_image_item').remove();
		$('#jquery-overlay').fadeOut(function() { $('#jquery-overlay').remove(); })
		// Show some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
		$('embed, object, select').css({ 'visibility' : 'visible' });
		return false;	
	});
};
	
resizeOverlay = function(){
	var arrPageSizes = _getPageSize();
	var contentwidth = arrPageSizes[0]-180;
	$('#jquery-overlay').css({width: contentwidth});
};

// ============================================== //

		// LOCATION PAGE ROLLOVERS //

// ============================================== //

setLocationRollovers = function(){
	$('a.location_link').mouseenter(function(){
		$(this).parent().addClass('icon_grey');	 
	}).mouseleave(function(){
		$(this).parent().removeClass('icon_grey');
	});
	//
	if($('a.location_enlarge').length){
		$('a.location_enlarge').each(function() {
			$(this).click(function() {
				setImageViewer(this);
				return false;						
			});
		});
	};
		
};

// ============================================== //

		// CONTACT FORM //

// ============================================== //


echeck = function(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1){
		return false;
	};
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		return false;
	};
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false;
	};
	if (str.indexOf(at,(lat+1))!=-1){
		return false;
	};
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false;
	};
	if (str.indexOf(dot,(lat+2))==-1){
		return false;
	};
	if (str.indexOf(" ")!=-1){
		return false;
	};
	return true;				
};

setContactForm = function(){
	$('#contact_form .form_error').hide();
	$('#contact_form .submit_form').click(function() {  
		// validate and process form here  
		$('.form_error').hide(); 
		//
		var name = $('input#name').val();  
		if (name == "") {  
			$('label#name_error').show();  
			$('input#name').focus();   
		};
		//
		var email = $('input#email').val();  
		if (email == "") {  
			$('label#email_error').text('This field is required.');
			$('label#email_error').show();  
			$('input#email').focus();  
		} ;
		//
		if (name == "" || email == "") { 
			return false;
		};
		//
		if(echeck(email)==false){
			$("label#email_error").text("This address is invalid.");
			$("label#email_error").show();  
			$("input#email").focus();
			return false;
		};
		//
		var surname = $("input#surname").val();
		var address = $("textarea#address").val();
		var city = $("input#city").val();
		var postcode = $("input#postcode").val();
		var country = $("input#country").val();
		var tel = $("input#tel").val();
		var comments = $("textarea#comments").val();
		//
		var dataString = '&name='+ name + '&surname='+ surname + '&address='+ address + '&city='+ city + '&postcode='+ postcode + '&country='+ country + '&tel='+ tel + '&email='+ email + '&comments='+ comments;  
		//
		$.ajax({  
			type: "POST",  
			url: "includes/php/process.php",  
			data: dataString,
			error: function(XMLHttpRequest, textStatus, errorThrown){ 
				alert(errorThrown); 
			},
			success: function() {
				$('#contact_form').html("<div id='thanks_message'></div>");  
				$('#thanks_message').html("<h3>Thank you for your enquiry</h3>").append("<p>We will be in touch shortly.</p>");
			}
		});
		//
		return false; 
	});
};
	
// ============================================== //

		// INITIALISE //

// ============================================== //

$(document).ready(function(){
	//
	if($('#home_grid').length){
		setHomeImages();
	};
	//
	if($('#thumb_block').length){
		addEnlargeLinks();
	};
	//
	if($('div.col_layout').length){
		setLocationRollovers();
	};
	//
	if($('#contact_form').length){
		setContactForm();
	};
	
});
//
$(window).load(function () {
});
