//Slider logic
function slideList(theButton, orient, direction) {
	var output = 'Output\n';
	if (orient == "horizontal") {
		var theList = $(theButton).parent().find(".horizontalSlider ul");
		var elementCount = Number($(theList).children().length);
		output += 'elementCount = ' + elementCount + '\n';
		var currentLocation = parseInt($(theList).css("right"));
		output += 'currentLocation = ' + currentLocation + '\n';
		var slideAmount = parseInt($(theList).find("li.page").width());
		output += 'slideAmount = ' + slideAmount + '\n';
		var pageCount = $(theList).parent().width() / slideAmount;
		output += 'pageCount = ' + pageCount + '\n';
		$(".horizontalSlider ul").width(slideAmount * elementCount);
		var animateParams = { right: (direction * slideAmount) + currentLocation }
		
		if (currentLocation == 0 && direction == -1) {
			$(theList).animate({ right: slideAmount * (elementCount - 1) }, 1000, "swing");
		}
		
		//alert(output);
		
	} else { //vertical
		var theList = $(theButton).parent().find(".verticalSlider ul");
		var elementCount = Number($(theList).children().length);
		var currentLocation = parseInt((theList).css("bottom"));
		var slideAmount = parseInt($(theList).find("li.page").height())+ parseInt($(theList).find("li").css("margin-top"));        
		var pageCount = $(theList).parent().height() / slideAmount;
		$(".verticalSlider ul").height(slideAmount * elementCount);
		var animateParams = { bottom: (direction * slideAmount) + currentLocation }
		
		//If you are at the top (or left) and you want to go back
		//Wrap backk to the end
		if (currentLocation == 0 && direction == -1) { 
			$(theList).animate({ bottom: slideAmount * (elementCount - pageCount) }, 1000, "swing");
		}
	}
	//If you are at the end (and want to go forward) wrap to the beginning
	if (currentLocation * direction >= slideAmount*(elementCount - pageCount)) {
		$(theList).animate({ right: 0 }, 1000, "swing");
		$(theList).animate({ bottom: 0 }, 1000, "swing");
	//Otherwise just move the desired direction
	}else if ((currentLocation - direction !== 1) && (direction * currentLocation < slideAmount * (elementCount - 1)) && (currentLocation % slideAmount == 0)) {
		$(theList).animate( animateParams , 1000, "swing");
	}
};

function equalHeight(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}

function expand(accordionItem) {
	if($(accordionItem).next().css('display') == 'block') {
		$(accordionItem).toggleClass('selected');
		$(accordionItem).next().slideUp('normal');
	} else {
		$(accordionItem).toggleClass('selected');
		$(accordionItem).next().slideDown('normal');
	}
}

$(document).ready(function() {
	
	if($.browser.msie && $.browser.version.substr(0,1)<7) {
		$("#nav li").hover(
			function() { $(this).addClass('hover'); },
			function() { $(this).removeClass('hover'); }
		);
		
		$("#subject li").hover(
			function() { $(this).addClass('hover'); },
			function() { $(this).removeClass('hover'); }
		);
	}
	
	$('#order .accordion').addClass('selected');
	
	$('.accordion').click(function(){
		expand($(this));
	});
	
	$("#bookTabs").tabs();
	
	$("#search input[type='text']").addClass('idleField');
	$("#search input[type='text']").defaultvalue("Enter Author, Title, Keyword or ISBN");
	
	equalHeight($("#recommendations li"));
	
	//Slider button actions
	$(".horizontalNext").click(function() {
		slideList(this, "horizontal", 1);
	});
	
	$(".horizontalPrevious").click(function() {
		slideList(this, "horizontal", -1);
	});
	
	$(".verticalNext").click(function() {
		slideList(this, "vertical", 1);
	});
	
	$(".verticalPrevious").click(function() {
		slideList(this, "vertical", -1);
	});
	
	
	// buttons
	$('.button').click(function(){
		if($(this).attr('rel') != '') {
			var form = $($(this).attr('rel'));
			
			$(form).find('#command').val($(this).attr('id'));
			
			$(form).submit();
		}
	});
});
