// *****************************************************************************
// Common Class
// *****************************************************************************

/**
 * @class Common
 * @constructor
 * @description Provides common client side behavior for pcthandbook.com.
 */

function Common() {

	//
	// Element handles
	//
	
	this.subHeaderRight = null;
	this.activeImage = null; 
	this.hiddenImage = null;

}; // end Common()



// *****************************************************************************
// Event Handling methods
// *****************************************************************************

/**
 * @description Initialization method run on document ready
 */

Common.prototype.init = function() {

	//
	// Get element handles
	//
	
	this.subHeaderRight = $("#subHeaderRight");
	this.activeImage = $(this.subHeaderRight).find("img.activeImage");;
	this.hiddenImage = $(this.subHeaderRight).find("img.hiddenImage");;

	//
	// Preload rotating images and then start the rotation
	//

	this.preloadImages();

}; // end Common.init()


/** 
 * @description On focus remove error and default class and values
 */

Common.prototype.input_focus = function(e) {
	var eInput = $(e.target);
	
	// If the element has it's default value reset value and remove class
	if (eInput.val() == eInput.attr("default")) {
		eInput.val("");
		eInput.removeClass("default");
	}

	// If the element has the error class reset value and remove class
	if (eInput.hasClass("error")) {
		eInput.val("");
		eInput.removeClass("error");
		eInput.removeClass("default");
	}
};


/**
 * @desecription On blur reset default if no value
 */

Common.prototype.input_blur = function(e) {
	var eInput = $(e.target);
	var value = eInput.val();

	if ((! value) || value.match(/^\s+$/)) {
		eInput.val(eInput.attr("default"));
		eInput.addClass("default");
	}
};



// *****************************************************************************
// UI Methods
// *****************************************************************************

/**
 * @description Rotate the images displayed in the sub header, fading the active
 * image out into the hidden one; and then swapping the active and hidden images
 * in preparation for the next iteration.
 */

Common.prototype.rotateImages = function() { 

	//
	// Get the next image index
	//

	gImageArrayIndex = gImageArrayIndex + 1;

	if (gImageArrayIndex >= gImageArray.length) {
		gImageArrayIndex = 0;
	}

	//
	// Load the next image and set up the onload event handler
	//

	$(this.hiddenImage).attr("src", gImageArray[gImageArrayIndex]);

	//
	// Fade out the active image and then:
	//
	// 1) Then swap the active and hidden images, making the hidden image the new active image.
	// 2) Get fresh handles to the active and hidden images for the next iteration.
	// 3) Ensure both images are dispayed in preparation for the next iteration.
	//
	
	var that = this;
	
	$(this.activeImage).fadeOut(
		1000,
		function() {
			// The active image is has been set to display none by the fade out so we 
			// can make it the hidden image with out accidentally showing it again!
			
			$(that.activeImage).removeClass("activeImage").addClass("hiddenImage");
			$(that.hiddenImage).removeClass("hiddenImage").addClass("activeImage");

			that.activeImage = $(that.subHeaderRight).find("img.activeImage");;
			that.hiddenImage = $(that.subHeaderRight).find("img.hiddenImage");;

			$(that.activeImage).css("display", "block");
			$(that.hiddenImage).css("display", "block");
		}
	);

}; // end Common.rotateImages()



// *****************************************************************************
// Data Methods
// *****************************************************************************

/** 
 * @description Validate the form, returning false if invalid.
 */

Common.prototype.valid = function(editorForm, method) {
	var bRetVal = true;
	var formValues = editorForm.find(".formValue");

	if (method != "delete") {
		for (var i = 0; i < formValues.length; i++) {
			var validateAttr = $(formValues[i]).attr("validate");

			if (! validateAttr) {
				continue;
			}

			var text = (validateAttr.indexOf("text") != -1 ? true : false);
			var phoneNumber = (validateAttr.indexOf("phoneNumber") != -1 ? true : false);
			var numeric = (validateAttr.indexOf("number") != -1 ? true : false);
			var nullable = (validateAttr.indexOf("notnull") != -1 ? false : true);
			var alphanumeric = (validateAttr.indexOf("alphanumeric") != -1 ? true : false);

			if (nullable == false) {
				var value = $(formValues[i]).val();
				var defaultValue = $(formValues[i]).attr("default");

				if (text == true) {
					// Text entries cannot be empty and may be required to contain alphanumeric contents only
					if (! value || 
						value.match(/^\s+$/) || 
						$(formValues[i]).hasClass("error") || 
						value == defaultValue || 
						(alphanumeric == true && value.match(/^[0-9a-zA-Z\s]+$/) == null)) 
					{
						$(formValues[i]).val(defaultValue);
						$(formValues[i]).addClass("error");
						bRetVal = false;
						break;
					}
				}
				else if (phoneNumber == true) {
					var stripped = String(value).replace(/[\(\)\.\-\ ]/g, '');

					stripped = String(stripped).replace(/ext/, '');
					stripped = String(stripped).replace(/x/, '');

					if (! stripped || isNaN(Number(stripped))) {
						$(formValues[i]).val(defaultValue);
						$(formValues[i]).addClass("error");
						bRetVal = false
						break;
					}
				}
				else if (numeric == true) {
					// Number entries cannot be empty and must be a valid number
					if (! value || 
						value.match(/^\s+$/) || 
						isNaN(Number(value)) == true || 
						$(formValues[i]).hasClass("error") || 
						value == defaultValue) 
					{
						$(formValues[i]).val(defaultValue);
						$(formValues[i]).addClass("error");
						bRetVal = false;
						break;
					}
				}
			}
		}
	}

	return bRetVal;

}; // end Common.valid()


/**
 * @description Preload the images in prepartion to start roating images
 * in the sub header, and once all are loaded rotate the image and then
 * continue rotating them every 10 seconds.  
 */

Common.prototype.preloadImages = function() {

	var body = $("body");
	var that = this;
	var index = 0;

	var fCallback = function() {
		index++;

		if (index < gImageArray.length) {
			$("<img>")
				.load(fCallback)
				.attr("style", "display: none;")
				.attr("src", gImageArray[index]);
		}
		else {
			that.rotateImages();	
			window.setInterval(function() { that.rotateImages(); }, 10000);
		}
	};

	//
	// Note that the load handler is set before specifying the image element's src attribute
	// because if the image is being loaded from cache, IE (and Opera too) will load the image,
	// instantly. In fact, it will have finished loading the image before the load event 
	// handler is even wired up, which means, it won't fire!
	// 	
	// See: http://blog.stchur.com/2008/02/26/ie-quirk-with-onload-event-for-img-elements/
	//

	$('<img>')
		.load(fCallback)
		.attr("style", "display: none;")
		.attr("src", gImageArray[index]);

}; // end Common.preloadImages()



// *****************************************************************************
// Global Instance
// *****************************************************************************

var oCommon = new Common();

$(document).ready(function() {
	oCommon.init();
});

