$(document).ready(function() {
	//mailing list
	$("#mailing_list .text").focus(function() {
		var value = $(this).val()
		if (value == "Email Address") {
			$(this).val("");
		}
	});
	$("#mailing_list .text").blur(function() {
		var value = $(this).val()
		if (value == "") {
			$(this).val("Email Address");
		}
	});
	
	//signs
	$("#home a[rel='signs']").colorbox({maxWidth:960, maxHeight:600, scalePhotos: true});
	$("#signs a[rel='signs']").colorbox({maxWidth:960, maxHeight:600, scalePhotos: true});
	
	//photos
	$("#gallery a[rel='gallery']").colorbox({maxWidth:960, maxHeight:600, scalePhotos: true});
	
	var photos_pages = $("#photos .gallery_page").length;
	var photos_current_page = 1;
	
	if (photos_pages > 1) {		
		$('#gallery_wrapper').css("width", (photos_pages * 864));
		$("#gallery_status").text("Page 1 of " + photos_pages);
		
		$('#gallery_next').click(function() {
			nextPhotoPage();
		});
		$('#gallery_last').click(function() {
			prevPhotoPage();
		});
	}
	
	function updatePhotoPage() {
		$("#gallery_status").text("Page " + photos_current_page + " of " + photos_pages);
	}
	
	function nextPhotoPage() {
		var next_page = photos_current_page + 1;
		var adjusted_margin = -((next_page - 1) * 864);
		if (next_page <= photos_pages) {
			$('#gallery_wrapper').animate({marginLeft: adjusted_margin}, 'normal');
			photos_current_page = next_page;
			updatePhotoPage();
		}
	}
	function prevPhotoPage() {
		var prev_page = photos_current_page - 1;
		var adjusted_margin = -((prev_page - 1) * 864);
		if (prev_page > 0) {
			$('#gallery_wrapper').animate({marginLeft: adjusted_margin}, 'normal');
			photos_current_page = prev_page;
			updatePhotoPage();
		}
	}
	
	//videos	
	var videos_pages = $("#videos .gallery_page").length;
	var videos_current_page = 1;
	
	if (videos_pages > 1) {		
		$('#video_gallery_wrapper').css("width", (videos_pages * 465));
		$("#video_gallery_status").text("Page 1 of " + videos_pages);
		
		$('#video_gallery_next').click(function() {
			nextVideoPage();
		});
		$('#video_gallery_last').click(function() {
			prevVideoPage();
		});
	}
	
	function updateVideoPage() {
		$("#video_gallery_status").text("Page " + videos_current_page + " of " + videos_pages);
	}
	
	function nextVideoPage() {
		var next_page = videos_current_page + 1;
		var adjusted_margin = -((next_page - 1) * 465);
		if (next_page <= videos_pages) {
			$('#video_gallery_wrapper').animate({marginLeft: adjusted_margin}, 'normal');
			videos_current_page = next_page;
			updateVideoPage();
		}
	}
	function prevVideoPage() {
		var prev_page = videos_current_page - 1;
		var adjusted_margin = -((prev_page - 1) * 465);
		if (prev_page > 0) {
			$('#video_gallery_wrapper').animate({marginLeft: adjusted_margin}, 'normal');
			videos_current_page = prev_page;
			updateVideoPage();
		}
	}
	
	//bookings
	$("#bookings_submit").click(function() {
		submitBookings();
	});
	function submitBookings() {		
		var name = $("#bookings_name").val();
		var email = $("#bookings_email").val();
		var message = $("#bookings_message").val();
		
		//create data query string
		var q = "action=bookings&name="+name+"&email="+email+"&message="+message;
		
		//send bookings to database
		$.ajax({type: "POST", url: "/includes/helpers/jquery.bookings.php", data: q, dataType: "json",
			success: function (data, textStatus) {
				if (data.errors) {					
					//display errors
					for (i = 0; i < data.errors.length; i++) {
						var ref = data.errors[i].input + "_error";
						if ($("#"+ref)) {
							//$("#"+ref).remove();
							$("#"+data.errors[i].input).removeClass("highlight");
						}
						if (data.errors[i].display.length > 0) {						
							//$("#"+data.errors[i].input).after('<div id="'+ref+'" class="error">' + data.errors[i].display + '</div>');
							$("#"+data.errors[i].input).addClass("highlight");
							//$("#"+ref).hide();
							//$("#"+ref).slideDown("fast");		
						}
					}
					
					//stop
					return false;
				} else {			
					//clear errors, show confirmation
					//$("#bookings_form div.error").remove();	
					$("#bookings_form input").removeClass("highlight");
					$("#bookings_form textarea").removeClass("highlight");
					$("#bookings_form form").html('<p class="success">Your bookings request is on its way to Greg &#8220;Batman&#8221; Davis &mdash; we&#8217;ll get back to you soon.</p>');				
				}
			}
		});
	}
	
	//contact
	$("#contact_submit").click(function() {
		submitContact();
	});
	function submitContact() {		
		var name = $("#contact_name").val();
		var email = $("#contact_email").val();
		var message = $("#contact_message").val();
		
		//create data query string
		var q = "action=contact&name="+name+"&email="+email+"&message="+message;
		
		//send contact to database
		$.ajax({type: "POST", url: "/includes/helpers/jquery.contact.php", data: q, dataType: "json",
			success: function (data, textStatus) {
				if (data.errors) {					
					//display errors
					for (i = 0; i < data.errors.length; i++) {
						var ref = data.errors[i].input + "_error";
						if ($("#"+ref)) {
							//$("#"+ref).remove();
							$("#"+data.errors[i].input).removeClass("highlight");
						}
						if (data.errors[i].display.length > 0) {						
							//$("#"+data.errors[i].input).after('<div id="'+ref+'" class="error">' + data.errors[i].display + '</div>');
							$("#"+data.errors[i].input).addClass("highlight");
							//$("#"+ref).hide();
							//$("#"+ref).slideDown("fast");		
						}
					}
					
					//stop
					return false;
				} else {			
					//clear errors, show confirmation
					//$("#contact_form div.error").remove();	
					$("#contact_form input").removeClass("highlight");
					$("#contact_form textarea").removeClass("highlight");
					$("#contact_form form").html('<p class="success">Your message is on its way to Greg &#8220;Batman&#8221; Davis &mdash; we&#8217;ll get back to you soon.</p>');				
				}
			}
		});
	}
});

function isIE() {
  return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}
