$(document).ready(function(){
	/*var zindex = 0;
	var alreadyactive = false;
	var count = 1;
	$("#home-rotate li").each(function(){
		if(count !== 1)
		{
			--zindex;
			var file = $(this).attr("id");
			$("#home-rotate #image").append("<img class=\"old\" src=\""+file+"\" style=\"z-index: "+zindex+"\" alt=\"\" />");
		}
		++count;
	});*/
	
	$("#home-rotate li").click(function(){
		stoprotate = true;
		
		if($(this).hasClass('show'))
		{
			return;
		}
		
		var file = $(this).attr("id");
		
		$("#home-rotate #image img").each(function(){
			if($(this).attr("src") == file)
			{
				if($(this).hasClass("active"))
				{
					return;
				}
				$("#home-rotate #image img.active").fadeOut().addClass("old").removeClass("active");
				$(this).addClass("active").removeClass("old");
				$(this).fadeIn();
			}
		});
		
		$("#home-rotate li.show").removeClass("show");
		$(this).addClass("show");
	});
	
	theRotator();
	
	$("form#contact").submit(function(ev){
		var errorcount = 0;
		$("input.text").each(function(){
			if($(this).hasClass("skip") == false)
			{
				var errortrue = 0;
				var currentvalue = $(this).val();
				if(currentvalue == "")
				{
					errorcount++;
					errortrue++;
				}
				if(errortrue > 0)
				{
					$(this).parent("fieldset").children("legend").addClass("error");
				}
			}
		});
		
		if(errorcount > 0)
		{
			ev.preventDefault();
		}
		
		// Lets submit!
		$(this).attr("action", "");
		$.post("http://greenview-estates.com/contactform.php", $(this).serialize(), function(data){
			if(data == "true")
			{
				$("#contact").before("<p class=\"contact-complete\">Thanks for your message! We will get back to you as soon as possible.</p>");
			}
			else
			{
				alert(data);
				$("#contact").before("<p>Sorry, we could not send your message. Try again.</p>");
			}
		});
		ev.preventDefault();
	});
	
	//Lightbox 
	$("a.lightbox").fancybox({
				'titlePosition'	: 'over'
			});
});

var zindex = 0;
var stoprotate = false;
var count = 1;

function theRotator()
{
	$("#home-rotate li").each(function(){
		if(count > 1)
		{
		--zindex;
		var file = $(this).attr("id");
		$("#home-rotate #image").append("<img class=\"old\" src=\""+file+"\" style=\"z-index: "+zindex+"; display: none;\" alt=\"\" />");
		}
		++count;
	});
	
	$("#home-rotate li:first").addClass("show");
	
	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('rotate()',5000);
}

function rotate() {
	if(stoprotate == false)
	{
	//Get the first image
	var current = ($('#home-rotate li.show')?  $('#home-rotate li.show') : $('#home-rotate li:first'));

	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('#home-rotate li:first') :current.next()) : $('#home-rotate li:first'));	
	
	var file = next.attr("id");
		
	$("#home-rotate #image img").each(function(){
		if($(this).attr("src") == file)
		{
			if($(this).hasClass("active"))
			{
				return;
			}
			$("#home-rotate #image img.active").fadeOut().addClass("old").removeClass("active");
			$(this).addClass("active").removeClass("old");
			$(this).fadeIn();
		}
	});
	
	//Set the fade in effect for the next image, the show class has higher z-index
	next.addClass('show');

	//Hide the current image
	current.removeClass('show');
	}
};
