// Scrolling lists stuff
// Scrolling Speed (in milliseconds)
var scrollSpeed = 400;
// Elements per scroll unit
var elementsPerUnit = 1;
// How many elements are visible
var visibleElements = 4;

// Width of the scroll list (in pixels)
var scrollAreaWidth;
// How much you want to scroll (in pixels)
var scrollUnit;

// Scroll ahead function (pass .scrollable to it)
function scrollAhead(e) {
	
	if ($(e).find(".next").hasClass("disabled") || (Math.abs($(e).find("ul").css("margin-left").replace("px","")) + scrollAreaWidth) > (($(e).find("ul").children().length - 1) * scrollUnit)) return false;
				
	$(e).find("ul").animate({
		marginLeft: "-=" + scrollUnit + "px"
	}, scrollSpeed, "swing", function() {
		$(e).find(".prev").removeClass("disabled");
		if ((Math.abs($(this).css("margin-left").replace("px","")) + scrollAreaWidth) > ((Math.floor(($(this).children().length - 1) / elementsPerUnit)) * scrollUnit)) {
			$(e).find(".next").addClass("disabled");	
			$(this).queue("fx", []);
			$(this).stop();
		}
	});
	
}

// Scroll back function (pass .scrollable to it)
function scrollBack(e) {
	
	if ($(e).find(".prev").hasClass("disabled") || $(e).find("ul").css("margin-left").replace("px","") >= 0) return false;
					
	$(e).find("ul").animate({
		marginLeft: "+=" + scrollUnit + "px"
	}, scrollSpeed, "swing", function() {
		if ($(this).children().length > elementsPerUnit) $(e).find(".next").removeClass("disabled");
		if ($(this).css("margin-left").replace("px","") >= 0) {
			$(e).find(".prev").addClass("disabled");	
			$(this).queue("fx", []);
			$(this).stop();
		}	
	});
	
}

// pass number of elements to jump and the .scrollable they're in
function jumpTo(j, e) {

	if ($(e).find(".next").hasClass("disabled") || (Math.abs($(e).find("ul").css("margin-left").replace("px","")) + scrollAreaWidth) > (($(e).find("ul").children().length - 1) * scrollUnit)) return false;
				
	$(e).find("ul").animate({
		marginLeft: "-=" + (j * scrollUnit) + "px"
	}, 1, "swing", function() {
		$(e).find(".prev").removeClass("disabled");
		if ((Math.abs($(this).css("margin-left").replace("px","")) + scrollAreaWidth) > ((Math.floor(($(this).children().length - 1) / elementsPerUnit)) * scrollUnit)) {
			$(e).find(".next").addClass("disabled");	
			$(this).queue("fx", []);
			$(this).stop();
		}
	});
	
}

$(document).ready(function(){
	
	// Scrolling functions
	$(".scrollable").each(function() {
	
		if ($(this).css("display") == "block") {
			// Width of the scroll list (in pixels)
			scrollAreaWidth = parseInt($(this).find(".itemcontainer").width());
			// How much you want to scroll (in pixels)
			scrollUnit = parseInt($(this).find("li").width()) * elementsPerUnit + parseInt($(this).find("li").css("marginRight").replace("px", "")) * elementsPerUnit;
		}
		
		// Disable the left arrow if the left margin is 0
		if ($(this).find("ul").css("margin-left") == "0px") {
			$(this).find(".prev").addClass("disabled");	
		}	
		
		// Disable the right arrow if the left margin is ul width - scroll area
		//alert($(this).find("ul").css("width").replace("px","") > 0 && ($(this).find("ul").css("margin-left").replace("px","") >= ($(this).find("ul").css("width").replace("px","") - scrollAreaWidth)));
		if ($(this).find("ul").css("width").replace("px","") > 0 && ($(this).find("ul").css("margin-left").replace("px","") >= ($(this).find("ul").css("width").replace("px","") - scrollAreaWidth))) {
			$(this).find(".next").addClass("disabled");	
		}
		
		// Scroll to next items	
		$(this).find(".next").click(function() {
			
			scrollAhead($(this).parents(".scrollable"));
					
		});
		
		// Scroll to prev items
		$(this).find(".prev").click(function() {
						
			scrollBack($(this).parents(".scrollable"));
					
		});
		
	});


	//	Expand Read More Read Less content
	$('#info').hide();
	$(".read a.less").hide();

	$(".read a.more").click(function(){
		$("#info").slideToggle("slow");
		$(".read a.more").hide();
		$(".read a.less").show();
		return false;
	});
	
	$(".read a.less").click(function(){
		$("#info").slideToggle("slow");
		$(".read a.less").hide();
		$(".read a.more").show();
		return false;
	});
	
});
