// JavaScript Document
function RoofingManager()
{
 	this._init();
};

RoofingManager.prototype._divArray = new Array("#spray-foam", "#coating", "#tile", "#shingle");

RoofingManager.prototype._init = function()
{
	// Tabs
	$('#tabs').tabs();
	
	//Service Buttons
	/*$('#spray-foam-link').click(function() {
	  alert('Handler for .click() called.');
	});*/
	
	$('#portfolio-gallery a').lightBox();
	
	
	var _this = this;
	
	$('#spray-foam-link').click(function() {
	  _this.toggleServices("#spray-foam");
	});
	
	$('#coating-link').click(function() {
	  _this.toggleServices("#coating");
	});
	
	$('#tile-link').click(function() {
	  _this.toggleServices("#tile");
	});
	
	$('#shingle-link').click(function() {
	  _this.toggleServices("#shingle");
	});
	
	// prepare the form when the DOM is ready 
	$(document).ready(function() { 
		var estimateOptions = { 
			target:    '#estimate-response',   // target element(s) to be updated with server response 
			dataType:  'json', 
			beforeSubmit:  _this.validateEstimate,  // pre-submit callback 
			success:       _this.showEstimateResponse  // post-submit callback 
		}; 
		
		var conactOptions = { 
			target:    '#contact-response',   // target element(s) to be updated with server response 
			dataType:  'json', 
			beforeSubmit:  _this.validateContact,  // pre-submit callback 
			success:       _this.showContactResponse  // post-submit callback 
		};
	 
		// bind form using 'ajaxForm' 
		$('#estimateForm').ajaxForm(estimateOptions); 
		$('#contactForm').ajaxForm(conactOptions); 
		
		$('.tooltip2').tooltip();
		
	});
	
};


RoofingManager.prototype.validateContact = function(formData, jqForm, options) { 
    
	var form = jqForm[0]; 
	var valid = true;
	if (!form.customers_email.value) {
		valid=false;
		$("#customers_email").addClass("require-field");
	}
	if (!form.message.value) {
		valid=false;
		$("#message").addClass("require-field");
	}
	if(valid == false)
	{
		$("#warn-contact").fadeIn("slow");
		return false;
	}
 
    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue 
    return true; 
}

RoofingManager.prototype.validateEstimate = function(formData, jqForm, options) { 
    
	var inputFields = new Array("first_name", "last_name", "phone", "bldg_city", "bldg_state", "project_size", "property_type", "roof_state");
	var radioFields = new Array("property_type", "roof_state");
 	var form = jqForm[0]; 
	var valid = true;
	
	for (j=0;j<inputFields.length;j++)
	{
		if (!form[inputFields[j]].value) { 
			valid = false; 
			$("#"+inputFields[j]).addClass("require-field");
		}
		else {
			$("#"+inputFields[j]).removeClass("require-field");
		}
	}
	for (k=0;k<radioFields.length;k++)
	{
		if (form[radioFields[k]].value == "not_selected") { 
			valid = false; 
			$("#"+radioFields[k]).addClass("require-field");
		}
		else {
			$("#"+radioFields[k]).removeClass("require-field");
		}
	}
	
	if(valid == false)
	{
		$("#warn").fadeIn("slow");
		return false;
	}
    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue 
    return true; 
} 

RoofingManager.prototype.showEstimateResponse = function(responseText, statusText, xhr, $form)  
{ 
	 $("#estimate-form").fadeOut("fast");
	 $("#warn").fadeOut("fast");
	 $("#estimate-response").fadeIn("slow");
}

RoofingManager.prototype.showContactResponse = function(responseText, statusText, xhr, $form)  
{ 
	 $("#contact-form").fadeOut("fast");
	 $("#warn-contact").fadeOut("fast");
	 $("#contact-response").fadeIn("slow");
}

RoofingManager.prototype.toggleServices = function(service)  
{ 
	 for (i=0;i<this._divArray.length;i++)
	 {
		if(this._divArray[i] != service && $(this._divArray[i]).is(':visible'))
		{
			$(this._divArray[i]).fadeOut(5);
		}
	 }
	 if(!$(service).is(':visible'))
	 {
	 	$(service).fadeIn("slow");
	 }
}

