// Princeton Lofts
// ---------------------------------
// Common JavaScript functions
// Copyright 2007


/* CLASSES
*************************************************************/
/* DISPLAY AREA
   Display Area Class
*/
function DisplayArea(id) {
	// Variables
	var displayid = id;
	var totalimages = 0;
	var position = 0;
	
	// Methods
	function hideAll() {
		totalimages = 0;
		$(displayid + ' li').each(function(i) {
			if (i) $(this).hide();
			totalimages++;
		});
		$(displayid + ' .prev').hide();
		$(displayid + ' ul').show();
	}
	function prevImage() {
		$(displayid + ' li:eq('+ (position--) +')').fadeOut(700, "easeInOutCubic");
		
		if (position == 0) {
			$(displayid + ' .prev').fadeOut(400);
		}
		if (position == (totalimages - 2)) {
			$(displayid + ' .next').fadeIn(400);
		}
	}
	function nextImage() {
		$(displayid + ' li:eq('+ (++position) +')').fadeIn(700, "easeInOutCubic");
		
		if ((position + 1) == totalimages) {
			$(displayid + ' .next').fadeOut(400);
		}
		if (position == 1) {
			$(displayid + ' .prev').fadeIn(400);
		}
	}
	
	// Binding
	this.hideAll = hideAll;
	this.prevImage = prevImage;
	this.nextImage = nextImage;
	
	// Events
	$(displayid + ' .prev').click(function(){ 
		prevImage();
		return false;
	});
	$(displayid + ' .next').click(function(){ 
		nextImage();
		return false;
	});
	
	// Setup
	hideAll();
}


/* FUNCTIONS
*************************************************************/
// Checks String is a valid email address
// Filter obtained from http://www.quirksmode.org/js/mailcheck.html
function validEmail(email) {
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(email)) return true;
	return false;
}
// Returns name of the document
function pageName() {
	var path = document.location.pathname;
	
	// Cut off start bit
	var pos = path.lastIndexOf("/");
	if (pos != -1) path = path.substr(pos + 1);
	
	// Cut off end bit
	pos = path.indexOf("?");
	if (pos != -1) path = path.substr(0, pos);
	pos = path.indexOf("#");
	if (pos != -1) path = path.substr(0, pos);
	
	return path;
}
// Field checking Contact form
function checkContactForm() {
	var err = false;
	var fname = $(".field-first input").val();
	var lname = $(".field-last input").val();
	var email = $(".field-email input").val();
	var subj = $(".field-subject option:selected").val();
	
	// Remove all first
	$("label em").each(function() { $(this).remove(); });
	
	// Highlight problem fields
	if (fname == "") {
		$(".field-first input").before('<em style="display:none;">Required</em>');
		$(".field-first em").fadeIn(400);
		err = true;
	}
	if (lname == "") {
		$(".field-last input").before('<em style="display:none;">Required</em>');
		$(".field-last em").fadeIn(400);
		err = true;
	}
	if (!validEmail(email)) {
		$(".field-email input").before('<em style="display:none;">Not a valid Address</em>');
		$(".field-email em").fadeIn(400);
		err = true;
	}
	if (subj == "") {
		$(".field-subject select").before('<em style="display:none;">Required</em>');
		$(".field-subject em").fadeIn(400);
		err = true;
	}
	
	// Submit Form
	if (!err) {
		$("form")[0].submit();
		document.forms["contactform"].submit();
	}
}


/* SETUP
*************************************************************/
$(document).ready(function(){
	
	switch(pageName()) {
		case "about.html":
		case "construction.html":
		case "partners.html":
		case "dna.html":
		case "veniceliving.html":
			var da = new DisplayArea('#display');
			break;
		case "contact.asp":
			$("input[name='f_submit']").click(function() {
				checkContactForm();
			});
			break;
		default:
			// blank
	}
	
});
