$(document).ready(function() {

	/*
	 * Help Page
	 */
	if($(".help-header")){
		$(".help-header:first").css("backgroundImage","url(/images/myaccount/grayminus.gif)").addClass("open");
		$(".help-header").click(function(){
			if($(this).hasClass("open") == false){
				$(".help-header").css("backgroundImage","url(/images/myaccount/grayplus.gif)");
				$(".help-header").removeClass("open");
				$(".help-area").slideUp("slow");

				$(this).addClass("open");
				$(this).css("backgroundImage","url(/images/myaccount/grayminus.gif)");
				$(this).next().slideDown("slow");
			}
			return false;
		});
	}
	
	/*
	 * Order Status Sliders
	 */
	try
	{
		if($(".status-header")){
			$("#recent-table tr td a, #all-table tr td a").attr("href", function(){
				url = $.getURLParam("loc");
				return this+"&loc="+url;
			});
			$(".status-header").click(function(){
				var sec = new Array;
				var urlString = '';
				if($(this).hasClass("open") == false){
					$(this).addClass("open");
					$(this).css("backgroundImage","url(/images/myaccount/grayminus.gif)");
					$(this).next().slideDown("slow");
				} else {
					$(this).removeClass("open");
					$(this).css("backgroundImage","url(/images/myaccount/grayplus.gif)");
					$(this).next().slideUp("slow");
				}
				$(".status-header").each(function(){
						if($(this).hasClass("open")){ sec.push($(this).next().attr("id")); }
				});
				$("#recent-table tr td a, #all-table tr td a").attr("href", function(){
					url = this;
					url = url.toString()
					urlString = url.split("&loc");
					urlString[0] = urlString[0]+"&loc="+sec;
					return urlString[0];
				});
				return false;
			});
		}
	} catch(e) {  }

	try 
	{
		if($("#recent") || $("#back") || $("#all") || $("#returns")){
			var loc = $.getURLParam("loc");
			loc = loc.split(",");
			$.each(loc, function() {
				$("#"+this).prev().css("backgroundImage","url(/images/myaccount/grayminus.gif)");
				$("#"+this).css({ 'display' : 'block' }).prev().addClass("open");
			});
		}
	} catch(e) {  }
	
	try
	{
		if($(".detail-header"))
		{
			$("#back-button").attr("href", function(){
				return this+"&loc="+$.getURLParam("loc");
			});
			$(".detail-header").addClass("open");
			$(".detail-header").click(function(){
				if($(this).hasClass("open") == false){
					$(this).addClass("open");
					$(this).css("backgroundImage","url(/images/myaccount/arrowopen.gif)");
					$(this).next().slideDown("slow");
				} else {
					$(this).removeClass("open");
					$(this).css("backgroundImage","url(/images/myaccount/arrowclose.gif)");
					$(this).next().slideUp("slow");
				}
				return false;
			});
		}
	} catch(e) {  }

	/*
	 * EZ Order add 5 more input lines.
	 */
	if($("#addAdditional")){
		$("#addAdditional").click(function(){
			var insertCount = 5;
			var rowCount = $("table tr").length;
			var prev = "";
			var insert = "";
			rowCount = rowCount - 1; //remove header row and leave footer row so we are above
			
			for(var i=0; i<insertCount; i++){
				
				prev = $("table tr:last").prev();
				prev.after("<tr><td><input class='medium' type='text' name='ISBN"+ rowCount +"' maxlength='25'/></td><td><input class='small' type='text' name='qty"+ rowCount +"' maxlength='5'/></td></tr>").fadeIn("slow");
				rowCount = rowCount + 1;
			}
			
			return false;
		});
	}

	/*
	 * Price & Availability check all items to add to cart
	 */
	if($("#addAll")){
		var tog = false;
		$("#addAll").click(function(){
			if(tog == false){
				$(this).attr("checked","checked");
				$("tr td :checkbox").attr("checked","checked");
				tog = true;
			}
			else {
				$(this).attr("checked","");
				$("tr td :checkbox").attr("checked","");
				tog = false;
			}
		});
	}

	/*
	 * Code added for Omniture Custom Link tracking -- Updated 10/20/09
	 */
	if ($('.track,.trackex')){
		$(".track,.trackex").click(function() {
			
			//If title value exists
			if ($(this).attr('title') != ""){
				//Grab title attribute
				var cus_link_name = $(this).attr('title');
				
				//Omniture code
				var s = s_gi('cengageordercenterdev');
				s.linkTrackVars = 'None';
				s.linkTrackEvents = 'None';	
				
				s.tl($(this),'o',cus_link_name);
			}
			
		});

		// Open trackex in a new window
		$(".trackex").click(function() {
			window.open( $(this).attr('href') );
    		return false;
		});
	}
	
	/*
	 * renew cancel form field toggles
	 */
	if($("#renewal-option") && $("#renewal-form")){
		$("#renewal-option input").click(function() {
			// Set Defaults
			$("#renewal-option input").attr("checked","");
			$("#renewal-form h2").text("Payment Methods");
			
			$(this).attr("checked","checked");
			var opt = $(this).attr("id");
			if(opt == 'renew'){ renew(); }
			if(opt == 'cancel'){ cancel(); }
		});
		var chck = $("#renewal-option input:checked").attr('id');
		if(chck == 'cancel'){
			$("#renewal-form h2").text("Cancellation Information");
			$("#renewal-form input:radio").parent().parent().fadeOut();
			$("#renewal-form input#title").parent().fadeOut();
			$("button").html('<img src="/images/myaccount/ec_gotocart.gif" width="16" height="16" alt="" /> Submit');
		}

		function renew(){
			$("#renewal-form h2").text("Payment Methods");
			$("#renewal-form input:radio").parent().parent().fadeIn();
			$("#renewal-form input#title").parent().fadeIn();
			$("button").html('<img src="/images/myaccount/ec_gotocart.gif" width="16" height="16" alt="" /> Submit Renewal');
		}

		function cancel(){
			$("#renewal-form h2").text("Cancellation Information");
			$("#renewal-form input:radio").parent().parent().fadeOut();
			$("#renewal-form input#title").parent().fadeOut();
			$("button").html('<img src="/images/myaccount/ec_gotocart.gif" width="16" height="16" alt="" /> Submit');
		}
	}

	/*
	 * open any anchor with a class="new-window" in a new window
	 */
	try
	{
		if($(".new-window")){
			$(".new-window").click(function() {
				var viewportHeight = jQuery(window).height();
				var viewportWidth = jQuery(window).width();
				var viewportPosition = ((viewportWidth / 2) - 350);
				newWindow = window.open("2_renew.htm", "newWin", "width=700,height="+viewportHeight+",left="+viewportPosition+",toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=yes,resizable=yes")
				newWindow.focus();
				return false; //stop link from working normally
			});
		}
	} catch(e) {  }

	try
	{
		if($(".new-win")){
			$(".new-win").click(function() {
				var viewportHeight = jQuery(window).height();
				var viewportWidth = jQuery(window).width();
				var viewportPosition = ((viewportWidth / 2) - 350);
				newWindow = window.open(this.href, "newWin", "width=800,height="+viewportHeight+",left="+viewportPosition+",toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=yes,resizable=yes")
				newWindow.focus();
				return false; //stop link from working normally
			});
		}
	} catch(e) {  }
	
	try
	{
		if($(".new-win-form")){
			$(".new-win-form").click(function() {
				var viewportHeight = jQuery(window).height();
				var viewportWidth = jQuery(window).width();
				var viewportPosition = ((viewportWidth / 2) - 350);
				newWindow = window.open($(this).closest('form').attr('action'), "newWin", "width=800,height="+viewportHeight+",left="+viewportPosition+",toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=yes,resizable=yes")
				newWindow.focus();
				return false; //stop link from working normally
			});
		}
	} catch(e) {  }

	/*
	 * close any window that we opened with the above function
	 */	
	if($("#close")){
		$("#close").click(function() {
			self.close();
			return false;
		});
	}
	
	/*
	 * Fancy Box Pop-Up
	 */
	if($("a.modal,a.modalSmall,a.modalMedium,a.modalLarge")){
		
		$("a.modal,a.modalSmall").fancybox({
			'frameWidth':			500,
			'frameHeight':			300,
			'zoomSpeedIn':			300,
			'zoomSpeedOut':			300,
			'hideOnContentClick': 	false,
			'callbackOnClose': 		function(){ window.location.reload(); }
		});
		
		$("a.modalMedium").fancybox({
			'frameWidth':			650,
			'frameHeight':			350,
			'zoomSpeedIn':			300,
			'zoomSpeedOut':			300,
			'hideOnContentClick': 	false,
			'callbackOnClose': 		function(){ window.location.reload(); }
		});
		
		$("a.modalMediumTall").fancybox({
			'frameWidth':			650,
			'frameHeight':			500,
			'zoomSpeedIn':			300,
			'zoomSpeedOut':			300,
			'hideOnContentClick': 	false,
			'callbackOnClose': 		function(){ window.location.reload(); }
		});
		
		$("a.modalLarge").fancybox({
			'frameWidth':			800,
			'frameHeight':			500,
			'zoomSpeedIn':			300,
			'zoomSpeedOut':			300,
			'hideOnContentClick': 	false,
			'callbackOnClose': 		function(){ window.location.reload(); }
		});
	}
	
	/*
	 * Table Sorter
	 */
	try{$("#recent-table").tablesorter({widgets: ['zebra'], headers: { 1:{ sorter: false }, 2:{ sorter: false }, 3:{ sorter: false }}});}catch(e){}
	try{$("#all-table").tablesorter({widgets: ['zebra'], headers: { 1:{ sorter: false }, 2:{ sorter: false }, 3:{ sorter: false }}});}catch(e){}
	try{$(".tablesorter").tablesorter({widgets: ['zebra'], headers: { 2:{ sorter: false }, 3:{ sorter: false }, 4:{ sorter: false }, 5:{ sorter: false }, 7:{ sorter: false }}});}catch(e){}
	
}); // end document.ready

jQuery.extend({
/**
* Returns get parameters.
* If the desired param does not exist, null will be returned
* @example value = $.getURLParam("paramName");
*/ 
 getURLParam: function(strParamName){
	  var strReturn = "";
	  var strHref = window.location.href;
	  var bFound=false;
	  
	  var cmpstring = strParamName + "=";
	  var cmplen = cmpstring.length;

	  if ( strHref.indexOf("?") > -1 ){
	    var strQueryString = strHref.substr(strHref.indexOf("?")+1);
	    var aQueryString = strQueryString.split("&");
	    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
	      if (aQueryString[iParam].substr(0,cmplen)==cmpstring){
	        var aParam = aQueryString[iParam].split("=");
	        strReturn = (aParam[1] == "undefined") ? "undefined" : aParam[1];
	        bFound=true;
	        break;
	      }
	      
	    }
	  }
	  if (bFound==false) return null;
	  return strReturn;
	}
});

function readCookie2(name) { 
	var nameEQ = name + "="; 
	var ca = document.cookie.split(';'); 
	for(var i=0;i < ca.length;i++) { 
		var c = ca[i]; 
		while (c.charAt(0)==' ') c = c.substring(1,c.length); 
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); 
	} 
	return null;
} 

function createCookie(name,value) { 
	var expires = ""; 
	document.cookie = name+"="+value+expires+"; path=/"; 

} 

function get_cookie ( cookie_name ) 
{ 
  var results = document.cookie.match ( cookie_name + '=(.*?)(;|$)' );  

  if ( results ) 
    return ( unescape ( results[1] ) ); 
  else 
    return null; 
} 


