var rotateImages = new Array();
var rotateCounter = 0;
var rotateTotal = 0;
$(function(){

  //$('li.headlink').hover(
	//		function() { 
	//		 //$('ul', this).css('display', 'block'); 
	//		 $(this).children('ul.sub').css('display', 'block');
	//		 },
	//		function() { $('ul', this).css('display', 'none'); }
  //);
	//

	//$('li.headlink').hover(
	//		function() { $('ul', this).css('display', 'block'); },
	//		function() { $('ul', this).css('display', 'none'); }
	//);
	
	
	
	if($('#rotate').length > 0) {
	
		$('.rotateImages').each(function(){
			rotateImages[rotateCounter] = $(this).attr('src');
			rotateCounter++;
		});
		rotateTotal = rotateCounter;
		rotateCounter = 0;
		setInterval("rotate()", 7000);
	}
	
	
	// send the message! (assuming there is actual info in the boxes
	$("#send_mail").click(function(){
		// check the boxes

		$('#send_mail').val("Sending...").attr('disabled', true);
		
		var go = 1;
		var msg = "";
		if($("#name").val() == '' || $("#name").val() == 'Your Name')
		{
			// show them they need to put in a name
			//msg = "You must enter a name. <br /><br />";
			$('#name').css('background-color', '#D91A1A');
			go = 0;
		}
		else
			$('#name').css('background-color', 'white');
		
		if($("#email").val() == '' || $("#email").val() == 'Your Email' || validate($("#email").val()) == false)
		{
			// show them they need to put in an email addy
			//msg = msg + "You must enter a valid email address. <br /><br />";
			$('#email').css('background-color', '#D91A1A');
			go = 0;
		}
		else
			$('#email').css('background-color', 'white');
		
		if($("#message").val() == '' || $("#message").val() == 'Your message goes in here.' || $('#message').val() == 'User Account Request\n====================\n[type reason for request here]')
		{
			// user needs a message
			$('#message').css('background-color', '#D91A1A');
			
			//msg = msg + "You must enter some message text. <br /><br />";
			go = 0;
		}
		else
			$('#message').css('background-color', 'white');

		if($("#cp").val() == '' || $("#cp").val() != 10)
		{
			// user needs a message
			$('#cp').css('background-color', '#D91A1A');
			
			//msg = msg + "You must enter some message text. <br /><br />";
			go = 0;
		}
		else
			$('#cp').css('background-color', 'white');

		if(go == 1) // send email
		{	
			var postData = 
	  	{
	      email: $("#email").val(),
	      name: $("#name").val(),
	      message: $("#message").val()
	    };
			$.post('webservices/email.php',postData,function(xml){ 
			 	var status = $(xml).find('response').find('status').text();
	      
	      if (status == 'Success')
	      {
	      	$("#contact").html("<h2>Your message has been sent to us. Thank you and we will get back to you as soon as we can.</h2>");
	      	$("#frmMessages").html("");
	      }
	      else
	      {
	        $("#frmMessages").html("It appears that there was an error sending you email, please try again.");
	        $('#send_mail').val("Contact Us").attr('disabled', false);
	      }
			 });
		}
		else
		{
			$("#frmMessages").html("<br /><b>Please correct the fields highlighted in red and try again.</b><br /><br/>" + msg);
			$('#send_mail').val("Contact Us").attr('disabled', false);
		}
	});
	
	
	$(function() {
		$('#main_image').parent('p').css('min-height','484px');
    $(".carousel").jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev",
        visible: 5,
        auto: 4000,
        speed: 1500,
        beforeStart: function(a) { $('#main_image').fadeOut(1000); 
        },
        afterEnd: function(a) {
          
          var img = $(a[2]).children().children().attr('src');
              
          
          $('#main_image').attr('src', img);
          $('#main_image').fadeIn(1000);
        }
    });
	});
	
	$('.carousel a').lightBox({fixedNavigation:false});

	$('#login').click(function(){
		if($("#username").val() == '' || $("#password").val() == '')
		{
			// show them they need to put in an email addy
			var msg = "Please enter a username and password";
			$('#errmsg').html(msg);
			return;
		}
		var data = {
			username: $('#username').val(),
			password: $('#password').val()
		};
		
		$.post('webservices/login.php',data,function(res){
			if(res.status == 'success')
				location.reload(true);
			else 
				$('#errmsg').html(res.msg);
		}, "json"); 
	
	});
	
	
});

function rotate() {
	$('#rotate').fadeOut(700, function(){
		$('#rotate').attr('src', rotateImages[rotateCounter]);	
		$('#rotate').fadeIn(700);
	});

	rotateCounter++;
	if(rotateCounter >= rotateTotal)
		rotateCounter = 0;	
		
		
}


function validate(address) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(address) == false) {
      return false;
   }
   else
   	return true;
}
