// JavaScript Document

function fixSafariLabels() {
	//fix label tag support for Safari using jQuery
	//Sunny Walker, www.miraclesalad.com
	//Version: 20070405
	if ($.browser.safari && ($.browser.version < 500)) { //only fix labels for Safari
		$('label').click(function() { //add function to all label tags
			var tj = $('#'+$(this).attr('for')); //get the target specified in for="target"
			var tg = (tj.length == 0) ? $(this).children('input,select,textarea,button').get(0) : tj.get(0); //if no for="target", target first valid child element
			if (tg) { //proceed if we have a target DOM element to work with
				var t = tg.type.toLowerCase(); //get the target element's type
				if (t == 'radio') tg.checked = true; //browser handles turning off other options
				else if (t == 'checkbox') tg.checked = !tg.checked; //toggle the checked attribute if a radio or checkbox
				if ((t == 'radio' || t == 'checkbox') && tg.onclick) tg.onclick.call(tg); //trigger onclick event if any attached to radio/checkbox
				tg.focus(); //move focus to the target element
			}
		});
	}
} //fixSafariLabels()

$(fixSafariLabels); //run on document.ready()

/**
 * Clears the selected form elements.
 */
$.fn.clearFields = $.fn.clearInputs = function() {
	return this.each(function() {
		var t = this.type, tag = this.tagName.toLowerCase();
		if (t == 'text' || t == 'password' || tag == 'textarea')
			this.value = '';
		else if (t == 'checkbox' || t == 'radio')
			this.checked = false;
		else if (tag == 'select')
			this.selectedIndex = -1;
	});
};

// Set the carousel
$(document).ready(function(){ $("form")[0].reset(); });
$(".jcarousel-skin-pearson").removeClass("cf").jcarousel();
$("#regStep2").css({"height":"0px", "overflow":"hidden"});
$("#regBooks-USLink").addClass("default");
$("#regBooks-US").parent("div").parent("div").addClass("default");
$("#regStep3").hide().addClass("hide");
$("#eBookLinkTxt").html('<a href="#eBookDef" id="eBookLink">eBook</a>');
$("#eBookDef").hide().addClass("hide");

$("#registerForm input").addClass("radio");
$("#regSubmit").attr({disabled: "disabled"});

// figure out what is happening in step 1
$("#regStep1 label").click(function() {
	$("#regStep2 input").clearFields();
	$("#regStep3 input").clearFields();
	$("#regStep2 label").removeClass("selected");
	$("#regStep1 label").removeClass("selected");
	$(this).addClass("selected");
	// if "yes" is selected
	if ($("#accessY").hasClass("selected")) {
		$("#regSubmit").attr({disabled: ""});
		$("#regStep2").css({"height":"0px", "overflow":"hidden"});
		$("#regStep3").hide();
	}
	// if "no" is selected
	if ($("#accessN").hasClass("selected")) {
		$("#regStep2").css({"height":"1%", "overflow":"visible"});
		$("#regSubmit").attr({disabled: "disabled"});
		if ($(".jcarousel-skin-pearson label").hasClass("selected")) {
			$("#regStep3").show();
			$("#regSubmit").attr({disabled: ""});
		};
	};
});

// highlight the clicked element in step 2 and determine if step 3 is necessary
$("#regStep2 label span").click(function() {
	$("#regStep2 label").removeClass("selected");
	$(this).parent().addClass("selected");
	$("p#youSelected").remove();
	$("<p id=\"youSelected\">You selected:<br /><strong id=\"selectedTitle\"></strong></p>").appendTo("#regStep2");
	$("#selectedTitle").html($(this).clone());
	if (!($.browser.safari && ($.browser.version < 500)) && $("#selectedTitle .author").html()) $("#selectedTitle br").replaceWith(", ");
	if ( $(this).parent().hasClass("ebookN") ) {
		if ($("#regStep3 label").hasClass("selected")) {
			$("#regStep3 label").removeClass("selected");
		}
		$("#eBookRadios input").clearFields();
		$("#eBookRadios").hide();
		$("#eBookTxt").hide();
		$("#eBookTxtNo").show();
		if ( $("#regStep3").hasClass("hide") ) {
			// do nothing for now
		}
		$("#regSubmit").attr({disabled: ""});
	} else if ($(this).parent().hasClass("ebookY")) {
		$("#regStep3").show().addClass("show");
		$("#regStep3").show().removeClass("hide");
		$("#eBookRadios").show();
		$("#eBookTxt").show();
		$("#eBookTxtNo").hide();
		if ($("#regStep3 label").hasClass("selected")) {
			$("#regSubmit").attr({disabled: ""});
		} else {
			$("#regSubmit").attr({disabled: "disabled"});
		}
	}
	if ($("#accessY").hasClass("selected")) { $("#regSubmit").attr({disabled: ""}); }
});

$("#regStep3 label").click(function() {
	$(this).addClass("selected");
	$("#regSubmit").attr({disabled: ""});
});

// IE 6 is the only browser that needs this extra class since it does not understand how to :hover on labels
if ( ($.browser.msie) && ($.browser.version == 6) ) {
	$(".jcarousel-skin-pearson label").hover(
		function () {
			$(this).addClass("hover");
		},
		function () {
			$(this).removeClass("hover");
		}
	);
};

$("#regBooks-USLink").click(function() {
	$("#regBooks-Intl").parent("div").parent("div").removeClass("default");
	$("#regBooks-IntlLink").removeClass("default");
	$("#regBooks-US").parent("div").parent("div").addClass("default");
	$("#regBooks-USLink").addClass("default");
	return false;
});
$("#regBooks-IntlLink").click(function() {
	$("#regBooks-US").parent("div").parent("div").removeClass("default");
	$("#regBooks-USLink").removeClass("default");
	$("#regBooks-Intl").parent("div").parent("div").addClass("default");
	$("#regBooks-IntlLink").addClass("default");
	return false;
});

$("#eBookLink").click(function() {
	$("#eBookDef").modal();
	return false;
});
