$.fn.hoverClass = function(c) {
	return this.each(function(){
		$(this).hover(function() { 
			$(this).addClass(c);
		},
		function() { 
			$(this).removeClass(c);
		});
	});
};

$(document).ready(function() {	
	/* *** Sub Navigation *** */
	
	$("#top-nav li").hover(function(){ 
		$("ul", this).fadeIn("slow");
	}, function() {});
	
	if (document.all) {
		$("#top-nav li").hoverClass ("sfHover");
	};
	
	
	
	$('.thclick').click(function() {
		$('#ea-main-thumbs a').each(function() {
			$(this).attr('class', 'thclick noncurrent');
		});
		
		rel = $(this).attr('rel');
		
		if(rel != document.current_image) {
			$('#ea-main-photo #' + document.current_image).hide();
			$('#ea-main-photo #' + rel).fadeIn('slow');
		
			document.current_image = rel;
		}
		
		current = '#p_t_' + document.current_image;
		
		$(current).attr('class', 'thclick');
		
		return false;
	});
	
	$('#contactForm').submit(function() {
		var error = false;
		var msg = '<ul class="alert">';
		var rooms = '';
		
		var name = $('#name').attr('value');
		var email = $('#email').attr('value');
		var phone = $('#phone').attr('value');
		var desc = $('#desc').attr('value');

		if(name == '') {
			error = true;
			msg += '<li>Name is a required field</li>';
		}
		if(email == '') {
			error = true;
			msg += '<li>Email is a required field</li>';
		}
		if(phone == '') {
			error = true;
			msg += '<li>Phone is a required field</li>';
		}
		
		msg += '</ul>';
		
		if(error) {
			$.facebox(msg);
			return false;
		} else {
			
			var postdata = "name=" + name + "&";
			postdata += "email=" + email + "&";
			postdata += "phone=" + phone + "&";
			postdata += "desc=" + desc;
			
			$.facebox('<h2>Proccessing Request ... &nbsp; &nbsp; <img id="facebox-img" src="/assets/js/facebox/loading.gif" /></h2><p>This might take up to a minute. Do not close this box until the process is complete. Thanks.</p>');
			
			$.ajax({
			   type: "POST",
			   url: "app/ajax/sendmail.php",
			   data: postdata,
			   success: function(msg){
				 $('#facebox h2').html('Your information has been submitted!');
				 $('#facebox p').html('Thank you for taking the time to fill out this form.');
				 $('#facebox #facebox-img').remove();
			   }
			 });
			
			$('#name').attr('value', '');
			$('#email').attr('value', '');
			$('#phone').attr('value', '');
			$('#desc').attr('value', '');

			
			//pageTracker._trackPageview('/cta/contact-residential');
			
			return false;
		}
		
		
	});
	
	$('.media-flash').media({width: 774, height: 400, bgColor: 'transparent', params: {wmode:'transparent'}});
	
});