// 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();
$("#regStepSelectBook").css({"height":"0px", "overflow":"hidden"});
$("#regBooks-USLink").addClass("default");
$("#regBooks-US").parent("div").parent("div").addClass("default");
$("#regStepVirtualLab").hide().addClass("hide");
$("#regStepEBook").hide().addClass("hide");
$("#eBookLinkTxt").html('<a href="#eBookDef" id="eBookLink">eBook</a>');
$("#eBookDef").hide().addClass("hide");

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

// does student have access or not?
$("#regStepAccessCode label").click(function() {
	if (! $(this).hasClass("selected")) {
		resetRegStepAccessCode();
		$(this).addClass("selected");
		// if "yes" is selected
		if ($("#accessY").hasClass("selected")) {
			hideRegStepSelectBook();
			$("#regSubmit").attr({disabled: ""});
		}
		// if "no" is selected
		if ($("#accessN").hasClass("selected")) {
			showRegStepSelectBook();
		};
	}
});

// highlight the clicked element in step 2 and display step 3 (whether virtual labs or etext)
$("#regStepSelectBook label span").click(function() {
	if (! $(this).parent().hasClass("selected")) {
		resetRegStepSelectBook();
		$(this).parent().addClass("selected");
		$("<p id=\"youSelected\">You selected:<br /><strong id=\"selectedTitle\"></strong></p>").appendTo("#regStepSelectBook");
		$("#selectedTitle").html($(this).clone());
		if (!($.browser.safari && ($.browser.version < 500)) && $("#selectedTitle .author").html()) $("#selectedTitle br").replaceWith(", ");
	
		if ($(this).parent().hasClass("virtualLabY")) {
			showRegStepVirtualLab();
			
			if ( $(this).parent().hasClass("eBookN") ) {
				$("#regStepVirtualLab").addClass("eBookN");
			} else if ($(this).parent().hasClass("eBookY")) {
				$("#regStepVirtualLab").addClass("eBookY");
			}
		}
		else if ($(this).parent().hasClass("virtualLabN")) {
			showRegStepEBook($(this).parent());
		}
	}
});

// choose to purchase virtual lab or not
$("#regStepVirtualLab label").click(function(event) {
	if (! $(this).hasClass("selected")) {
		$("#regStepVirtualLab label").removeClass("selected");
		$(this).addClass("selected");
		if ($("#virtualLabY").hasClass("selected")) {
			hideRegStepEBook();
			$("#regSubmit").attr({disabled: ""});
		} else if ($("#virtualLabN").hasClass("selected")) {        
			showRegStepEBook($("#regStepVirtualLab"));
		}
	}
});

// choose to purchase eText or not
$("#regStepEBook label").click(function() {
	$("#regStepEBook label").removeClass("selected");
	$(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() {
    if (! $("#regBooks-USLink").hasClass("default")) {
    	$("#regBooks-Intl").parent("div").parent("div").removeClass("default");
    	$("#regBooks-IntlLink").removeClass("default");
    	$("#regBooks-US").parent("div").parent("div").addClass("default");
    	$("#regBooks-USLink").addClass("default");
    	resetRegStepSelectBook();
    }
	return false;
});

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

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

function resetRegStepAccessCode() {
    $("#regStepAccessCode label").removeClass("selected");
    resetRegStepSelectBook();
    hideRegStepSelectBook();
}

function resetRegStepSelectBook() {
    $("#regStepSelectBook input").clearFields();
    $("#regStepSelectBook label").removeClass("selected");
    $("p#youSelected").remove();
    resetRegStepVirtualLab();
    hideRegStepVirtualLab();
}

function resetRegStepVirtualLab() {
    $("#regStepVirtualLab input").clearFields();
    $("#regStepVirtualLab label").removeClass("selected");
    if ($("#regStepVirtualLab").hasClass("eBookN")) $("#regStepVirtualLab").removeClass("eBookN");
	if ($("#regStepVirtualLab").hasClass("eBookY")) $("#regStepVirtualLab").removeClass("eBookY");
    resetRegStepEBook();
    hideRegStepEBook();
}

function resetRegStepEBook() {
    $("#regStepEBook input").clearFields();
	$("#regStepEBook label").removeClass("selected");
}

function hideRegStepSelectBook() {
    $("#regStepSelectBook").css({"height":"0px", "overflow":"hidden"});
    hideRegStepVirtualLab();
}

function hideRegStepVirtualLab() {
    $("#regStepVirtualLab").hide().addClass("hide");
    $("#regStepVirtualLab").removeClass("show");
    hideRegStepEBook();
}

function hideRegStepEBook() {
    $("#regStepEBook").hide().addClass("hide");
    $("#regStepEBook").removeClass("show");
}

function showRegStepSelectBook() {
    $("#regSubmit").attr({disabled: "disabled"});
    $("#regStepSelectBook").css({"height":"1%", "overflow":"visible"});
}

function showRegStepVirtualLab() {
    $("#regSubmit").attr({disabled: "disabled"});
    $("#regStepVirtualLab").show().addClass("show");
    $("#regStepVirtualLab").removeClass("hide");
    $("#virtualLabRadios").show();
    $("#virtualLabTxt").show();
}

function showRegStepEBook(htmlObj) {
    if ($(htmlObj).hasClass("virtualLabY") || $(htmlObj).attr('id') == 'regStepVirtualLab') {
        $("#regStepEBook h2").html("Step 4: eText Options");
    }
    else if ($(htmlObj).hasClass("virtualLabN")) {
        $("#regStepEBook h2").html("Step 3: eText Options");
    }
    $("#regSubmit").attr({disabled: "disabled"});
    $("#regStepEBook").show().addClass("show");
    $("#regStepEBook").removeClass("hide");
    if ($(htmlObj).hasClass("eBookY")) {
	    $("#eBookTxtNo").hide();
	    $("#eBookTxt").show();
	    $("#eBookRadios").show();
	} else if ($(htmlObj).hasClass("eBookN")) {
	    $("#eBookRadios").hide();
		$("#eBookTxt").hide();
		$("#eBookTxtNo").show();
		if ( $("#regStepEBook").hasClass("hide") ) {
			// do nothing for now
		}
		$("#regSubmit").attr({disabled: ""});
    }
}

