$(document).ready(function(){

   $("#global_nav a").mouseover(function() {
     $(this).parent().addClass("hover");
   });

   $("#global_nav a").mouseout(function() {
     $(this).parent().removeClass("hover");
   });

    $("#herobox2").cycle({
    	speed:  2500,
    	height: 240
    });

		$('#clouds2').css('left', getRand(1680));


    $(function() {
  		animateClouds('#clouds2',0,0);
    });

    $("#contactForm").submit(function(event){

       if(event.preventDefault){
          event.preventDefault();
       }else{
          event.returnValue = false;
       };

        ajaxProcessForm('contactForm', 'contact', '/includes/process_form.php', 'POST');

    });

    /*
  	$("input#submit").click(function (event)
    {
         event.preventDefault();
         ajaxProcessForm('contactForm', 'contact', '/includes/process_form.php', 'POST');
    });
		*/

});


function animateClouds(elementID, iTop, iLeft) {

  var newLeft = getRand(1680);
  //var newTop = getRand(40);
  var newTop = 0;
  var speed = 100000;

  //alert(newLeft);

  $(elementID).animate(
    {
  	  top: newTop+"px",
  	  left: newLeft+"px",
    },
  	speed,
  	function() {
  		return animateClouds(elementID, newTop, newLeft);
  	}
  );

}

function getRand(range) {
  var rangeFix = range+1;
  return Math.floor(Math.random()*rangeFix);
}

$.fn.serializeObject = function()
{
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

function ajaxProcessForm(form_id, form_name, form_action, form_method){

	var formData = $('#'+form_id).serializeObject();
	formData['form_url'] = form_name;
	var feedback = form_name+'_feedback';
	var ajaxresult = form_name+'_ajaxResult';

	$.ajax({
		url: form_action,
		type: form_method,
		dataType: 'json',
		data: formData,
		success: function(response) {
			if(!response.errors) {
				$('#'+feedback).hide();
				$('#'+ajaxresult).html('Thank you for filling out the form. A representative will contact you shortly.');
				$('#'+ajaxresult).css('display', 'block');
				//$('#'+form_id).css('display', 'none');
				$('#'+form_id+' input').each(function() {
				  $(this).val('');
				});
			}
			else {
				$('#'+feedback).html(response.errors).show();
			}
		}
	});

}



