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

RoofingManager.prototype._divArray = new Array("#home", "#aboutus", "#products", "#haildamage", "#roofmaintenance", "#gogreen", "#portfolio", "#contactinfo", "#getestimate", "#spray-foam", "#coating", "#tile-roofing", "#shingle-roofing");

RoofingManager.prototype._init = function()
{	

	var _this = this;
	for (i=0;i<this._divArray.length;i++)
	 {
		$(this._divArray[i]).click(function() {
			_this.toggleServices(this);
		});
	 }
	
	$('#portfolio-gallery a').lightBox();
	
	// 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();
		
		$("ul.subnav").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)

		$("ul.topnav li span").click(function() { //When trigger is clicked...
	
			//Following events are applied to the subnav itself (moving subnav up and down)
			$(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click
	
			$(this).parent().hover(function() {}, function(){
				$(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
			});
	
			//Following events are applied to the trigger (Hover events for the trigger)
			}).hover(function() {
				$(this).addClass("subhover"); //On hover over, add class "subhover"
			}, function(){	//On Hover Out
				$(this).removeClass("subhover"); //On hover out, remove class "subhover"
		});
		$("ul.topnav li").click(function() { //When trigger is clicked...
	
			//Following events are applied to the subnav itself (moving subnav up and down)
			$(this).find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click
	
			$(this).hover(function() {}, function(){
				$(this).find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
			});
	
			//Following events are applied to the trigger (Hover events for the trigger)
			}).hover(function() {
				$(this).addClass("subhover"); //On hover over, add class "subhover"
			}, function(){	//On Hover Out
				$(this).removeClass("subhover"); //On hover out, remove class "subhover"
		});
		var ajax_load = "<div id='loading'><img src='images/ajax-loader.gif' alt='loading...' /></div>";
		$("#HomePage").html(ajax_load).load("home.php #home-slideshow", function () {
			_this.sliderHome();
		});
		
    });
	
};

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(clickedService)  
{ 
	var service = "#"+$(clickedService).attr('id')+"-content";	
	
	 for (i=0;i<this._divArray.length;i++)
	 {
		var currentVal = this._divArray[i]+"-content";
		if(currentVal != service && $(currentVal).is(':visible'))
		{
			$(currentVal).fadeOut(5);
		}
	 }
	 if(!$(service).is(':visible'))
	 {
	 	$(service).fadeIn("slow");
	 }
 
}
RoofingManager.prototype.sliderHome = function()
{
	$("#slider-slideshow").easySlider({
		  	controlsShow: false,
			auto: true,
			continuous: true 
		});
}
