var sEmailPattern = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)$/i;

$(document).ready(function(){
	// Main slide show
	$("#main_slide").cycle({fx:    'fade',timeout:  5000 });
	// Inputs with place holders
	$('.placeholder').each(function(index, el){

		var el = $(el);
		el.blur(placeholderInputBlur);
		el.focus(placeholderInputFocus);
		el.val(el.attr('placeholder'));

		el.parents('form:not(.submit-keep-placeholders)').bind('submit', placeholderFormSubmit);
	});
	
	// Google maps
	if ($('.google-maps').length) {
		loadGoogleMapsScript('loadMap');
	}
});

// Placeholders

function placeholderInputFocus(event){

   var el = $(event.target);

   if (el.hasClass('placeholder')){
      el.val('');
      el.removeClass('placeholder');
   }
}

function placeholderInputBlur(event){

   var el = $(event.target);

   if (!el.hasClass('placeholder') && el.val().replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, '') == ''){
      el.val(el.attr('placeholder'));
      el.addClass('placeholder');
   }
}

function placeholderFormSubmit(event){

   var form = $(event.target);
   
   form.find('input.placeholder').each(function(index, el){
      placeholderInputFocus({'target': $(el)});
   });

   return true;
}

function placeholderFormSubmitCancel(form){

   var form = $(form);
      
   form.find('input.placeholder').each(function(index, el){
      placeholderInputBlur({'target': $(el)});
   });

   return true;
}

// Map

function loadMap() {
	
	$('.google-maps').each(function(index, el){
		
		var container = $(el);
		var latLng = new google.maps.LatLng(container.attr('data-lat'), container.attr('data-lng'));

		var mapOptions = {
			backgroundColor: container.css('background-color'),
			zoom: parseInt(container.attr('data-zoom')),
			center: latLng,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			scrollwheel: false			
		};
	
		var map = new google.maps.Map(el, mapOptions);	
		
		var marker = new google.maps.Marker({
			position: latLng,
			map: map
		});
		
		var infowindow = new google.maps.InfoWindow({
			content: container.attr('data-address')
		});
		
		google.maps.event.addListener(marker, 'click', function() {
			infowindow.open(map, marker);
		});		
	});
}

function loadGoogleMapsScript(callback) {
	var script = document.createElement("script");
	script.type = "text/javascript"; 	
	script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=" + callback;
	document.body.appendChild(script);
}

// Slide show

var eSlideshow;
var eSlideshowImage;
var eSlideshowButtonPrevious;
var eSlideshowButtonNext;
var eSlideshowCounter;

function slideshowInit() {
	 	
	eSlideshow = $('#slideshow');
	
	if (!eSlideshow) {
		return false;
	}
	
	eSlideshowImage = eSlideshow.find('.image > img');	
	eSlideshowCounter = eSlideshow.find('.tip > strong');
	
	eSlideshowButtonPrevious = eSlideshow.find('.image > .previous');
	eSlideshowButtonNext = eSlideshow.find('.image > .next');
	
	eSlideshowButtonPrevious.click(function(){
		slideshowPrevious();
		return false;
	});
	
	eSlideshowButtonNext.click(function(){
		slideshowNext();
		return false;
	});
   	
	for (var j = 0; j < aSlides.length; j++) {
		$.preloadImage(aSlides[j].image);
	}
}

function slideshowGetIndex(i) {
	
	return (i + aSlides.length) % aSlides.length;
}

function slideshowNext() {
	slideshowShow(iSlideCurrent+1);
}

function slideshowPrevious() {
	slideshowShow(iSlideCurrent-1);
}

function slideshowShow(i) {
	
	i = slideshowGetIndex(i);
	
	if (iSlideCurrent == i) {
		return;
	}
	
	slideshowSetContent(i);
	iSlideCurrent = i;
}

function slideshowSetContent(i) {
	
	i = slideshowGetIndex(i);
	eSlideshowImage.attr('src', aSlides[i].image);
	eSlideshowCounter.html(i+1);
}

// Image preload

jQuery.preloadImage = function(src) {
	jQuery("<img>").attr("src", src);
}

jQuery.preloadImages = function() {
	jQuery.each (arguments,function (e) {
		jQuery("<img>").attr("src", this);
	});
}
