﻿var currentHash = location.hash;
$(function() {
	var defaultSize = $('#firefoxPadder').height();
	$('#requestForm').bind("submit",submitMessage);
	$('#rightGalleryArrow').bind("click",galleryRight);
	$('#leftGalleryArrow').bind("click",galleryLeft);
	$('.horizontalThumb').bind("click", showLargeVersion);
	setInterval(checkPageChange, 300);
});

function checkPageChange() {
	if(location.hash != "" && location.hash != "undefined" && location.hash != null) {
		if(currentHash != location.hash) {
			var hashBreakup = location.hash.split('_');
			var mainImageDirectory = hashBreakup[1];
			var mainImageNum = hashBreakup[2];
			var fullImagePath = "images/homes/"+mainImageDirectory+"/full/full_"+mainImageNum+".jpg";
			$('#slideshowFull').html('<img alt="Blue Heron Home" src="'+fullImagePath+'" />');
			currentHash = location.hash;
		}
	}
}

function submitMessage(event) {
	if($('.ajaxFinished').length > 0) { $('.ajaxFinished').remove(); }
	if($('.error').length > 0) { $('.error').remove(); }
	if($('#ajaxLoader').length > 0) { $('#ajaxLoader').remove(); }
	var queryString = $(this).serialize();
	var actionPage = $(this).attr("action");
	var fullRequest = actionPage + "?" + queryString;
	var ajaxLoader = '<img src="images/ajax-loader.gif" alt="Sending Message" id="ajaxLoader" title="Sending Message" align="left" />';
	$('.sendMessage').before(ajaxLoader);
	$.getJSON(fullRequest,function(json) {
		if(json.outcome == "success") {
			$('#ajaxLoader').remove();
			$('#firefoxPadder').slideUp("slow");
			$('#contactForm').slideUp("slow",function() {
				var messageSent = '<p class="ajaxFinished" style="display:none;">Your message has been sent! Thank you for your interest!</p>';
				$('#contactForm').before(messageSent);
				$('.ajaxFinished').fadeIn("slow", function() {
					setTimeout(function() { 
						$('#address, #subject, #message').val("");
						$('.ajaxFinished').fadeOut("slow");
						$('#contactForm').slideDown("slow");
						$('#firefoxPadder').slideDown("slow", function() {
							$('#firefoxPadder').height(defaultSize);
						});
					}, 4000);
				});
			});	
		} else {
			$('#ajaxLoader').remove();
			if(json.address == 'fail') { 
				$('#address').before('<p class="error">You must provide your correct return email address so that we can respond!</p>'); 
				$('#firefoxPadder').height($('#firefoxPadder').height() + 23);
			}
			if(json.subject == 'fail') { 
				$('#subject').before('<p class="error">Provide a subject for this message!</p>'); 
				$('#firefoxPadder').height($('#firefoxPadder').height() + 23);
			}
			if(json.message == 'fail') { 
				$('#message').before('<p class="error">You need to let us know what you are inquiring about otherwise we can\'t help!</p>'); 
				$('#firefoxPadder').height($('#firefoxPadder').height() + 23); 
			}
			if(json.address == 'pass' && json.subject == 'pass' && json.message == 'pass') {
				$('#contactForm').before('<p class="error">There was an error sending your message! Make sure <em>your</em> email address is correct...</p>');
			}
		}
	});
	return false;
}


function galleryRight(event) {
	var currentThumb = $('#currentThumb').val();
	var totalThumbs = $('.horizontalThumb').length;
	var leftness = -(134*currentThumb)+"px";
	if((parseInt(currentThumb) + 4) < totalThumbs) {
		$('#thumb'+currentThumb).animate({marginLeft:leftness},250);
		currentThumb++;
		$('#currentThumb').val(currentThumb);
	}
	return false;
}

function galleryLeft(event) {
	var currentThumb = $('#currentThumb').val();
	var totalThumbs = $('.horizontalThumb').length;
	currentThumb--;
	var leftness = -(134*(currentThumb-1))+"px";
	$('#thumb'+currentThumb).animate({marginLeft:0},250);
	$('#currentThumb').val(currentThumb);
	return false;
}

function showLargeVersion(event) {
	var thumbHrefSplit = $(this).attr("href").split("_");
	var picDir = thumbHrefSplit[1];
	var picNum = thumbHrefSplit[2];
	var largePicString = '<img alt="Blue Heron Home" src="images/homes/'+picDir+'/full/full_'+picNum+'.jpg"/>';
	$('#slideshowFull').html(largePicString);
}