$(window).load(function() {
  initFadeDrop();
  // init fade drop
  function initFadeDrop(){
  	var dur = 300;
  	var activeClass = "hover"
  	jQuery('#nav > li').each(function(){
  		var hold = jQuery(this);
  		var list = hold.find('>div.drop');
  		var link = hold.find('>em');
  		var drop = list.add(link);
  		if(jQuery.browser.msie && jQuery.browser.version < 9){
  			drop.hide();
  			hold.mouseenter(function(){
  				hold.addClass(activeClass);
  				drop.show();
  			}).mouseleave(function(){
  				drop.hide();
  				hold.removeClass(activeClass);
  			});
  		} else {
  			drop.css({opacity: 0}).hide();
  			hold.mouseenter(function(){
  				hold.addClass(activeClass);
  				drop.show().stop().animate({opacity:1},{duration: dur, queue: false})
  			}).mouseleave(function(){
  				drop.show().stop().animate({opacity:0},{duration: dur, queue: false,complete:function(){
  					drop.hide();
  				  hold.removeClass(activeClass);
  				}})
  			});
  		}
  	}); 	
  }    
  
  $('#slider').nivoSlider({
      effect:'slideInRight',
      pauseTime:7000,
      startSlide:0,
      directionNav:false,
      directionNavHide:false,
      captionOpacity: 1,
      controlNav:true,
      keyboardNav:true,
      pauseOnHover:false
  });
  
  $("#nav .drop").css({display: "none"}); // Opera Fix
  $("#nav li").hover(function(){
  $(this).find('ul:first').css({visibility: "visible",display: "none"}).show(); },function(){ $(this).find('ul:first').css({visibility: "hidden"}); });
  clearInputs();
  submitContactForm();
    
});

function submitContactForm() {
  $("input#submit").click(function() {
    $('form#contact-form').bind('submit', function(event){
  	  var hasError = false;
  	  var errorCheck = false;
  	  var errorMsg = '';
  	  var counter = 0;
  		$('li').removeClass('error');
  		$('li').removeClass('error-alter');
      if (errorCheck == false) {
        errorMsg = '';    
        $('.requiredField').each(function() {
          counter++;  
    			if(jQuery.trim($(this).val()) == '' || $(this).val() == 'Last Name' || $(this).val() == 'First Name' || $(this).val() == 'Apt'  || $(this).val() == 'City' || $(this).val() == 'Country' || $(this).val() == 'State' || $(this).val() == 'Zipcode' || $(this).val() == 'Phone Number' || $(this).val() == 'Email') {
            $(this).closest('li').addClass('error');
    				hasError = true;    		
          } else if($(this).hasClass('email')) {
    				var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    				if(!emailReg.test(jQuery.trim($(this).val())) || $(this).val() == 'Email') {
    					$(this).closest('li').addClass('error');  					
    					hasError = true;
    				}
    			}
    		});
  		} 
      //console.log(hasError);
      if (hasError == false) {     
        var dataString = '';
        $('.requiredField').each(function() {
          dataString += $(this).attr('name') + '=' + $(this).val() + '&'    
      	});
      	//console.log(dataString);
        $.ajax({
          type: "POST",
          url: "js/sendmail.php",
          data: dataString,
          success: function() {
            $('#confirm').text('Request Sent');  
          }
        });      
      } 
      return false;	
  	});

  });
}


// clear inputs
function clearInputs(){
	jQuery('input:text, input:password, textarea').each(function(){
		var _el = jQuery(this);
		var _val = _el.val();
		_el.bind('focus', function(){
			if(this.value == _val) this.value = '';
		}).bind('blur', function(){
			if(this.value == '') this.value = _val;
		});
	});
};

