/* Author: 

*/

function sendMessage(dataString){
	//alert(dataString);
	$.ajax({
      type: "POST",
      url: "/bin/process.php",
      data: dataString,
      success: function() {
        $('#contactForm').html("");
        $('#signupMessage').html("<p class='subhead'>Thank you for your submission. We will respond to you shortly!</p>")
        .hide()
        .fadeIn(2500, function(){
        	$('#signDiv').delay(2000).slideToggle("slow");
        	});
	    return false;
        }
     });
}

$().ready(function() {

	//$('.node').hide();
	
    $.history.init(function(hash){
    	//$('.node').hide();
        if(hash == "") {
            $('#nodes div:first-child').show();
            //alert('hello');
        } else {
            $('#'+hash).show();
            //alert(hash);
        }
    },
    { unescape: ",/" });
	
     $('#nodeNav a').click(function(e) {
             var url = $(this).attr('href');
             url = url.replace(/^.*#/, '');
             $.history.load(url);
             return false;
         });
	
	jQuery.validator.messages.required = "";

		$('#weather').weatherfeed(['USGA0028']);

		// nav startup
		// remove link background images since we're re-doing the hover interaction below 
		// (doing it this way retains the CSS default hover states for non-javascript-enabled browsers)
		// we also want to only remove the image on non-selected nav items, so this is a bit more complicated
		$(".nav").children("li").each(function() {
			var current = "nav current-" + ($(this).attr("class"));
			var parentClass = $(".nav").attr("class");
			if (parentClass != current) {
				$(this).children("a").css({backgroundImage:"none"});
			}
		});	


		// create events for each nav item
		attachNavEvents(".nav", "navLawn");
		attachNavEvents(".nav", "navTree");
		attachNavEvents(".nav", "navEstimate");
		attachNavEvents(".nav", "navOther");
		attachNavEvents(".nav", "navFaqs");
		attachNavEvents(".nav", "navContact");

		function attachNavEvents(parent, myClass) {
			$(parent + " ." + myClass).mouseover(function() {
				$(this).append('<div class="nav-' + myClass + '"></div>');
				$("div.nav-" + myClass).css({display:"none"}).fadeIn(200);
			}).mouseout(function() {
				$("div.nav-" + myClass).fadeOut(200, function() {
					$(this).remove();
				});
			}).mousedown(function() {
				$("div.nav-" + myClass).attr("class", "nav-" + myClass + "-click");
			}).mouseup(function() {
				$("div.nav-" + myClass + "-click").attr("class", "nav-" + myClass);
			});
		}


		$("#closeClick").click(function () {
		  $("#signDiv").slideToggle("slow");
		  return false;
		});
		
		$("#free-estimate").click(function () {
		  $("#signDiv").slideToggle("slow");
		  return false;
		});


		$("a.rollover img").hover(
		 function() {
			this.src = this.src.replace("-off","-on");
		 },
		 function(){
			this.src = this.src.replace("-on","-off");
		 }
		);

		//apply rounded corners
		$('.rounded').corners();
	
		
	$("#splashSet a").hover(function(){
		$(this).children("span").fadeOut();
	}, function(){
		//$(this).children("span").fadeIn();
	})	
	
	if(page==""){
		var t=$("#mainSplashImg");
		var l=$("#mainSplash");
	    var src1= t.attr('src'); // initial src
		var src2 = "/img/splash"+src1.substring(src1.lastIndexOf('/'), src1.lastIndexOf('.'));
	}
	
	$("#tabSet a").hover(function(){
		currentId = $(this).attr('id');
		$(this).children("span").fadeOut();
		var newSrc = src2 + '-' + currentId + '.' + /[^.]+$/.exec(src1);
    	t.attr('src', newSrc);
    	l.children("span").fadeOut();
	}, function(){
		$(this).children("span").fadeIn();
		t.attr('src', src1);
		//l.children("span").fadeIn();
	})	
	
	
		
	$("#printButton").click(function () { 
		$('#interior').jqprint();  
    });

	$(".pseudoClick").hover(function () {
		$(this).addClass("hover");
    }, function () {
		$(this).removeClass("hover");
    });

	var originalFontSize = $('html').css('font-size');
	$(".resetFont").click(function(){
		$('html').css('font-size', originalFontSize);
	});
	  
	// Increase Font Size
	$(".increaseFont").click(function(){
		var currentFontSize = $('html').css('font-size');
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		var newFontSize = currentFontSizeNum*1.2;
		$('html').css('font-size', newFontSize);
			return false;
	});
	  
	// Decrease Font Size
	$(".decreaseFont").click(function(){
		var currentFontSize = $('html').css('font-size');
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		var newFontSize = currentFontSizeNum*0.8;
		$('html').css('font-size', newFontSize);
	    	return false;
	});
	
	var click = false;
	
	$("#toggleFont").click(function(){
		var currentFontSize = $('html').css('font-size');
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		//alert(currentFontSizeNum);
		if(click==true) {
			var newFontSize = currentFontSizeNum-2;
			click=false;
		} else {
			var newFontSize = currentFontSizeNum+2;
			click=true;
		}
		$('html').css('font-size', newFontSize);
	    	return false;
	});



	/* Slideshow */
	
	$.fn.cycle.defaults = { 
    timeout:       4000,  // milliseconds between slide transitions (0 to disable auto advance) 
    speed:         1000,  // speed of the transition (any valid fx speed value) 
    next:          null,  // id of element to use as click trigger for next slide 
    prev:          null,  // id of element to use as click trigger for previous slide 
    before:        null,  // transition callback (scope set to element to be shown) 
    after:         null,  // transition callback (scope set to element that was shown) 
    height:       'auto', // container height 
    sync:          1,     // true if in/out transitions should occur simultaneously 
    fit:           0,     // force slides to fit container 
    pause:         0,     // true to enable "pause on hover" 
    delay:         0,     // additional delay (in ms) for first transition (hint: can be negative) 
    slideExpr:     null   // expression for selecting slides (if something other than all children is required) 
	}; 


	$('#slides').cycle()



	// validate signup form on keyup and submit
	$("#turfForm").validate({
		invalidHandler: function(e, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				var message = errors == 1
					? 'You missed 1 field. It has been highlighted below'
					: 'You missed ' + errors + ' fields.  They have been highlighted below';
				$("div.error span").html(message);
				$("div.error").show();
			} else {
				$("div.error").hide();
			}
		},
		onkeyup: false,
		submitHandler: function() {
			$("div.error").hide();
			//alert("submit! use link below to go to the other step");
			
			var first_name = $("input#first_name").val();
			var last_name = $("input#last_name").val();
			var address = $("input#address").val();
			var city = $("input#city").val();
			var state = $("#state").val();
			var zip = $("input#zip").val();
			var phone = $("input#phone").val();
			var email = $("input#email").val();
			
			var radioCall = $('input:radio[name=radioCall]:checked').val(); //$(".radioCall").val();
			
			var checkPets = 'No';//$("#checkbox_1").val();
			var checkGate = 'No';//$("#checkbox_2").val();
			
			
			if($("#checkbox_1").attr('checked')){
				checkPets = 'Yes';
			}
			
			if($("#checkbox_2").attr('checked')){
				checkGate = 'Yes';
			}
			
			
			var servTurf = $("#checkbox_3").val();
			var servPlants = $("#checkbox_4").val();
			var servAeration = $("#checkbox_5").val();
			var servOverseeding = $("#checkbox_6").val();
			var servDisease = $("#checkbox_7").val();
			var servSoil = $("#checkbox_8").val();
			
			var message = $("#message").val();
			
			var referal = $("#referal").val();	
			
			var dataString = 'form=true&first='+ first_name + '&last='+ last_name + '&address=' + address + '&city=' + city + '&state=' + state + '&zip=' + zip + '&phone=' + phone + '&email=' + email + '&radioCall=' + radioCall + '&checkPets=' + checkPets + '&checkGate=' + checkGate + '&servTurf=' + servTurf + '&servPlants=' + servPlants + '&servAeration=' + servAeration + '&servOverseeding=' + servOverseeding + '&servDisease=' + servDisease + '&servSoil=' + servSoil + '&message=' + message + '&referal=' + referal;
			sendMessage(dataString);
		},
		rules: {
			first_name: "required",
			last_name: "required",
			address: "required",
			city: "required",
			state: "required",
			zip: "required",
			phone: "required",
			email: {
				required: true,
				email: true
			}
		}
	});



});
















