	/* 
	### Main Javascript Functions 
	### Created On
	### Developed By Will Ayers -= dev.willayers@gmail.com
*/

/* 
	## Widget Initiation function with Google Analytics Integration
*/	

document.initiated = false;	
function initiate(){
	if(!document.initiated){
		document.initiated = true;
		pageTracker._trackPageview('/actions/reserve-initiated');
	}
}


$(document).ready(function() {
	
	/* 
		## Show only selected sizes for selected location
	*/
	
	//$('#wr_location').change(function() {});
	
	/* 
		## Show the correct location when clicked
	*/
	
	$('.link-location').click(function() {
		var rel = $(this).attr('rel');
		$('#wr_location').attr('value', rel);
		initiate();
		return false;
	});
	
	/* 
		## Set the Initiated var
	*/
	
	$('#wr_location').click(function() {
		initiate();
	});
	$('#wr_size').click(function() {
		initiate();
	});
	$('#wr_name').focus(function() {
		initiate();
	});
	$('#wr_phone').focus(function() {
		initiate();
	});
	$('#wr_email').focus(function() {
		initiate();
	});	

	
	/* 
		## Show the correct location when clicked
	*/
	
	$('.link-location-name').click(function() {
		var rel = $(this).attr('rel');
		$('#wr_location').attr('value', rel);
	});
	
	/* 
		## Before submitting the form
	*/
	
	$('#wr_form_submit').submit(function() {
		var base_uri = $(".base_url").attr('href');
		var error = false;
		var msg = 'To complete the form, please check on these errors :' + "\n\n";
		
		var location = $('#wr_location').attr('value');
		
		var size = $('#wr_size').attr('value');
		
		var name = $('#wr_name').attr('value');
		var phone = $('#wr_phone').attr('value');
		var email = $('#wr_email').attr('value');
		
		if(location == 'null') {
			error = true;
			msg += "You must select a location\n";
		}
		if(size == 'null') {
			error = true;
			msg += "You must select a size\n";
		}
		if(name == '') {
			error = true;
			msg += "Your name is a required field\n";
		}
		if(phone == '' && email == '') {
			error = true;
			msg += "Either your phone number or email field must be filled out\n";
		}
		
		if(error) {
			alert(msg);
		} else {
			/* ajax send mail */
			var postdata = 'location=' + location + '&size=' + size + '&name=' + name + '&phone=' + phone + '&email=' + email;
			
			$.ajax({
			   type: "POST",
			   url: "ajax/widget",
			   data: postdata,
			   success: function(msg){					 
			     //alert('Thank you for submitting your request. If you provided an email address, you should receive an email soon. If not, we will contact you by phone.');				
			   }
			 });
			$.facebox({ image: '/assets/img/thank-you.png' })
			pageTracker._trackPageview('/actions/reserve-finished');
			$('#wr_location').attr('value', 'null');
			$('#wr_size').attr('value', 'null');
			$('#wr_name').attr('value', '');
			$('#wr_phone').attr('value', '');
			$('#wr_email').attr('value', '');
		}
		return false;
	});
	
	$('.quotes').innerfade({
		speed: 'slow',
		timeout: 12000,
		type: 'random',
		containerheight: '300px'
	});
	
	$('.photo_rotate').innerfade({
		speed: 'slow', timeout: 4000, type: 'sequence', containerheight: '252px'
	});
});