removeSelected = function(lastTab) {
	$(lastTab).firstChild.removeClassName('selected');
	if ($("tab1Content")) {
		$('tab1Content').show();
	}
};
function selectTab(obj) {
	var linkElements = $$("ul#contentTabsNav li a");
	$A(linkElements).each(tabsReInitialize);
	function tabsReInitialize(element) {
		if (element == obj) {
			if (!element.hasClassName("selected")) obj.addClassName("selected");
			if ($(element.rel)) $(element.rel).show();
		} else {
			if (element.hasClassName("selected")) element.removeClassName("selected");
			if ($(element.rel)) $(element.rel).hide();
		}
	}
}

/* Begin: Home Page */
function subscribePopupOnload() {
	var subscribePopup = new Popup('keepMeInTheLoop', 'keepMeInTheLoopLnk', {"modal": "true", "effect": "fade", "duration": 0.05, "opacity": 0.7, "show_delay": 0.05, "hide_delay": 0.05});
	var subscribePopup = new Popup('keepMeInTheLoop', 'keepMeInTheLoopLnk2', {"modal": "true", "effect": "fade", "duration": 0.05, "opacity": 0.7, "show_delay": 0.05, "hide_delay": 0.05});
}

function homePageNotifyMe() {
	var url = "./notify_me.html";
	var form_serialized = $("keepMeInTheLoopForm").serialize();
	var AjaxRequest = new Ajax.Request(
		url,
		{
			method: 'post',
			parameters: form_serialized,
			onCreate: notify_loading,
			onComplete: notify_complete,
			onSuccess: notify_success,
			onFailure: null
		}
	);

	function notify_success(originalRequest) {
		var response_result = eval('(' + originalRequest.responseText + ')');
		switch (response_result.result){
			case "success":
				$("keepMeInTheLoopSuccess").update("Your request to be kept in the loop was successful. You'll be hearing from us.");
				if ($("keepMeInTheLoopSuccess").hasClassName("invisible")) $("keepMeInTheLoopSuccess").removeClassName("invisible");
				if (!$("keepMeInTheLoopError").hasClassName("invisible")) $("keepMeInTheLoopError").addClassName("invisible");
			break;
			case "error":
			default:
				switch (response_result.type){
					case "email_is_invalid": $("keepMeInTheLoopError").update("E-mail is invalid"); break;
					case "already_added": $("keepMeInTheLoopError").update("Your E-mail is already added"); break;
					case "sending_error": $("keepMeInTheLoopError").update("An Error occurred. Try again latter"); break;
					default: $("keepMeInTheLoopError").update("Unknown Error"); break;
				}
				if (!$("keepMeInTheLoopSuccess").hasClassName("invisible")) $("keepMeInTheLoopSuccess").addClassName("invisible");
				if ($("keepMeInTheLoopError").hasClassName("invisible")) $("keepMeInTheLoopError").removeClassName("invisible");
			break;
		}
	}

	function notify_loading() {
		if (!$("keepMeInTheLoopSuccess").hasClassName("invisible")) $("keepMeInTheLoopSuccess").addClassName("invisible");
		if (!$("keepMeInTheLoopError").hasClassName("invisible")) $("keepMeInTheLoopError").addClassName("invisible");
		if ($("keepMeInTheLoopLoading").hasClassName("invisible")) $("keepMeInTheLoopLoading").removeClassName("invisible");
	}

	function notify_complete() {
		if (!$("keepMeInTheLoopLoading").hasClassName("invisible")) $("keepMeInTheLoopLoading").addClassName("invisible");
	}
}
function homePageTabsOnLoadEvnt() {
	var linkElements = $$("ul#leftMenuNav li a"); //Get All elements
	$A(linkElements).each(tabsInitialize); //initialize the action on every link
	function tabsInitialize(element) {
		if ($(element.rel)) { //If Child element exists
			if (!element.hasClassName("selected") && !$(element.rel).hasClassName("selected")) { //None of the elements has been selected.
				$(element.rel).hide();
			}
		}
		function onMouseOverEvnt(evnt) {
			var linkElements = $$("ul#leftMenuNav li a");
			$A(linkElements).each(function (linkElement) {
				if ($(linkElement.rel)) $(linkElement.rel).hide(); //hide all child content
				if ($(linkElement).hasClassName("selected")) $(linkElement).removeClassName("selected");
			});
			if ($(element.rel))
			$(element.rel).show(); //show current content
			if (!$(element).hasClassName("selected")) $(element).addClassName("selected");
		}
		function selectDefaultTab(element) {
			element.addClassName("selected");
			$(element.rel).show();
		}
		element.observe("mouseover", onMouseOverEvnt);
	}
}
/* Begin: Images Preload & Rotate */
function homePageRotateBackgrounds() {
	var BASize = backgrounds.size();
	if (startPhotoIndex == BASize) startPhotoIndex = 0;
	if ($("slideshowBackground")) {
		var menuContainer = $("slideshowBackground");
		menuContainer.setStyle(
			{"backgroundImage": "url(" + backgrounds[startPhotoIndex] + ")"}
		);
		homePageFadeIn('slideshowBackground'); //fade in: 0 sec
		window.setTimeout("homePageFadeOut('slideshowBackground');", 9000); // 9 sec abs visible
		window.setTimeout("homePageRotateBackgrounds(" + startPhotoIndex + ")", 10000); //1 second fade out
	}
	startPhotoIndex++;
}
function homePageFadeIn(objID) {
	var appearEffect = new Effect.Appear(objID, {duration: 1.0, from:0.1, to:1.0});
}
function homePageFadeOut(objID) {
	var fadeEffect = new Effect.Fade(objID, {duration: 1.0, from:0.99, to:0.1});
}
function homePageImagesPreload() {
	$A(backgrounds).each(function(image_src) {
		var preloadedImage = new Image(959, 571);
		preloadedImage.src = image_src;
	});
}
/* End: Images Preload & Rotate */
/* End: Home Page */

function linksImgsFit(containerClassName) {
	var maxLiHeight = 0;
	var maxLiWidth = 0;

	var liElements = $$(containerClassName);
	function getMaxWidthHeight(element) {
		maxLiHeight = maxLiHeight < element.clientHeight ? element.clientHeight : maxLiHeight;
		maxLiWidth = maxLiWidth < element.clientWidth ? element.clientWidth : maxLiWidth;
	}
	function setMaxHeight(element) { element.setStyle({"height": maxLiHeight + "px"}); }
	liElements.each(getMaxWidthHeight);
	liElements.each(setMaxHeight);

	function setLinkStyle(element) {
		$(element.parentNode).setStyle({
			position: "absolute",
			left: Math.floor((maxLiWidth-element.clientWidth)/2) + "px",
			top: Math.floor((maxLiHeight-element.clientHeight)/2) + "px"
		});
	}
	var imgLinkElements = $$(containerClassName + " a img");
	imgLinkElements.each(setLinkStyle);
}

function entry_form_validate() { //Entry Form Validation
	var url = "./entry_form_validate.html";
	var form_serialized = $("entryForm").serialize();
	var AjaxRequest = new Ajax.Request(
		url,
		{
			method: 'post',
			parameters: form_serialized,
			onCreate: entry_form_validate_loading,
			onComplete: entry_form_validate_loaded,
			onSuccess: entry_form_validate_success,
			onFailure: null
		}
	);
	function entry_form_validate_loading() {
		if ($("requestStatus").hasClassName("invisible")) $("requestStatus").removeClassName("invisible");
		if ($("requestStatus").hasClassName("error")) $("requestStatus").removeClassName("error");
		if (!$("requestStatus").hasClassName("loading")) $("requestStatus").addClassName("loading");
		$("requestStatus").update("Processing your request. Please wait...");
	}
	function entry_form_validate_loaded() {
		if ($("requestStatus").hasClassName("loading")) $("requestStatus").removeClassName("loading");
	}
	function entry_form_validate_success(originalRequest) {
		var response_result = eval('(' + originalRequest.responseText + ')');
		switch (response_result.type) {
			case "success":
				$("itemNumber").value = response_result.item_id;
				$("amount").value = response_result.price;
				$("currencyCode").value = response_result.currency;
				$("requestStatus").update("Redirecting to PayPal. Please wait...");
				$("entryForm").submit();
				break;
			case "error":
				var human_fields_names = "";
				if (!$("requestStatus").hasClassName("error")) $("requestStatus").addClassName("error");
				if (response_result.code == "fields_are_invalid") {
					for (var i = 0; i < response_result.fields.length; i++) {
						switch (response_result.fields[i]) {
							case "first_name": human_fields_names += "First Name, "; break;
							case "last_name": human_fields_names += "Last Name, "; break;
							case "email": human_fields_names += "E-Mail, "; break;
							case "confirm_email": human_fields_names += "E-Mail Verification, "; break;
							case "phone_number": human_fields_names += "Phone Number, "; break;
							case "os0": human_fields_names += "Blurb Book Title, "; break;
							case "os1": human_fields_names += "Blurb Order ID, "; break;
							case "os2": human_fields_names += "Award Category, "; break;
						}
					}
					if (response_result.fields.length > 1) {
						$("requestStatus").update("Whoops! Please correct the following fields above: " + human_fields_names.substr(0, human_fields_names.length - 2));
					} else {
						$("requestStatus").update("Whoops! Please correct the following field above: " + human_fields_names.substr(0, human_fields_names.length - 2));
					}
				}
				break;
		}
	}
}
/* update Book Carousel */
updateChooser = function(selected) {
    items = $('chooser').childElements();
    for (i = 0; i < items.length; i++) {
        items[i].removeClassName('selected')
    }
    $(selected).addClassName('selected');
}
/* NEW image rotater */
// this array consists of the id attributes of the divs we wish to alternate between
var divs_to_fade = new Array('box-1', 'box-2', 'box-3','box-4', 'box-5', 'box-6');

// the starting index in the above array.  It should be set to the value of the div which doesn't have the CSS Display property set to "none"
var i = 0;

// the number of milliseconds between swaps.  Default is five seconds.
var wait = 6000;

// the function that performs the fade
function swapFade() {
	Effect.Fade(divs_to_fade[i], { duration:1, from:1.0, to:0.0 });
	i++;
	if (i % divs_to_fade.length == 0 ) i = 0;
	Effect.Appear(divs_to_fade[i], { duration:1, from:0.0, to:1.0 });
}

// the onload event handler that starts the fading.
function startPage() {
	setInterval('swapFade()',wait);
}
