var started = false;


function startSlideShow() {

	var $slideshow = $('#slideshow, #portrait-slideshow'); //.css('opacity', 0);
	var controls = $('#controls'); //.css('opacity', 0.4);
	var imageCount = $slideshow.find('li').length;
	var i = 0;



		if (started) return false;
		started = true;

				$slideshow.fadeIn('slow').animate({ opacity: 1 }, 'slow', function() {
				//	$('#controls').animate({ opacity: 1 }, 'slow');
					$slideshow.cycle({ 
						delay: -3000, 
						timeout: 5000, 
						fx: 'fade', 
						prev: '#prev', 
						next: '#next', 
						pause: true, 
						pauseOnPagerHover: true,
//						requeueOnImageNotLoaded: true,
						before: function(currSlideElement, nextSlideElement) {
							
							var curr = $(nextSlideElement).attr('id');
							curr = curr.substring(6, curr.length);
							$('p', controls).text(curr + ' / ' + imageCount);
						},
						after: function(currSlideElement, nextSlideElement) {

							var next = $(nextSlideElement).next();
							
							next = $('#' + next.attr('id'));
							if (next.find('img').length == 0) {
								var img = $('<img />').attr('src', next.attr('imgsrc'));
								if (next.attr('imgstyle') != undefined) img.attr('style', next.attr('imgstyle'));
								next.append( img );
							}
						}
					});
				});
	}



$(document).ready(function() {

	
	setTimeout(startSlideShow, 5000);


	if ($('#contact-page').length > 0) {

		var form = $('#contact-page form');

		jQuery.validator.addMethod('contact', function(value, element) {
	
			result = ( ($('.email', form).val().length > 0) || ($('.phone', form).val().length > 0) );
	
			if (result) $('#contacterror', form).hide('fast');
			else $('#contacterror', form).show('fast');
			
			return result;
	
		});
	
		form.validate({ 
				errorClass: 'error', 
				errorElement: 'p',
				messages: {
					'fields[name]': { required: 'Please enter your name' },
					'fields[email]': { invalid: 'Please enter a valid email address',
										contact: '' },
					'fields[phone-number]': { contact: ''},
					'fields[where-did-you-hear-about-us]': { required: 'Please state how you heard about us' },
					'fields[enquiry]': { required: 'Please leave a message' }
				}
	
		});
		
		$('#date', form).datepicker({dateFormat: 'dd/mm/yy'});
	} 
	
	
	$('.facebook', '.share').click(function(e) {
		e.preventDefault;
		
		window.open($(this).attr('href'), '', 'left=20,top=20,width=800,height=500,toolbar=1,resizable=0');
		return false;
	});
	
	$("a[href^='http']").attr('target','_blank');
});
