
// trundles our carousel about...
function carousel_slide(direction) {
	// get an array of what our slide offsets will be based on the frame widths.
	var offsets = new Array();
	var n = 0;
	$("#carousel_items > .carousel_item").each(function() {
		offsets.push(n * -1);
		n += $(this).width();
	});
	
	// get our current offset.
	current = $("#carousel_items").position();
	current = current.left;
	
	// see where that offset sits on our array of offsets.
	for(i=0;i<offsets.length;i++) {
		if(offsets[i] == current) {
			break;
		}
	}
	
	// look at the direction they're going and adjust our index accordingly.
	i = (direction == "next") ? (i + 1) : (i - 1);
	i = (i < 0) ? offsets.length - 1 : i;
	i = (i == offsets.length) ? 0 : i;
	
	// slide there already.
	//alert(offsets[i]);
	$("#carousel_items").animate({"left" : offsets[i]}, "fast");
}

// shows carousel frame
function carousel_showframe(n) {
	// if we've been handed a number, then assume they mean frame N
	if(typeof n == "number") {
		frame = "#carousel_frames .carousel_frame:eq(" + n + ")";
	}
	// otherwise they've handed us an actual object
	else {
		frame = n;
	}
	
	pos = $(frame).position();
	newtop = pos.top * -1;
	newleft = pos.left * -1;
	$("#carousel_frames_slider").animate({"top" : newtop, "left" : newleft}, "slow");
	//alert(pos.left + " - " + pos.top);
	
	// deal with menu selection stuff
	$("#carousel_menu div").removeClass("carousel_menu_selected");
	$("#carousel_menu div").addClass("carousel_menu_normal");
	$("#carousel_menu div:eq(" + n + ")").removeClass("carousel_menu_normal");
	$("#carousel_menu div:eq(" + n + ")").addClass("carousel_menu_selected");
}

// attach click handlers to the carousel menu list
$(document).ready(function() {
	$("#carousel_menu div").click(function() {
		clearInterval(carousel_autoscroll);
		i = 0;
		thisthis = this;
		$("#carousel_menu div").each(function() {
			if($(this)[0] == $(thisthis)[0]) {
				carousel_showframe(i);
				return;
			}
			i++;
		});
		
	});
});

// make it so the menu list automatically scrolls up and down
$(document).ready(function() {
	$("#carousel_menu").mousemove(function(event) {
		holder_height = $("#carousel_menu_holder").height();
		menu_height = $("#carousel_menu").outerHeight(true);
		scrollable_range = menu_height - holder_height;
		
		// if there's enough to bother scrolling with
		if(scrollable_range > 0) {
			holder_offset = $("#carousel_menu_holder").offset();
			scroll_padding = 40;
			holder_y = holder_offset.top;
			padded_y = holder_y + scroll_padding;
			mouse_y = Math.max(event.pageY, padded_y);
			padded_height = holder_height - (2 * scroll_padding);
			mouse_proportion = Math.min(1, ((mouse_y - padded_y) / padded_height));
			new_top = Math.round((mouse_proportion * scrollable_range) * -1);
			
			//alert(change);
			$("#carousel_menu").css("top", new_top);
		}
		
		//alert(event.pageY);
	});
});

// set it automatically ticking through on load
$(document).ready(function() {
	carousel_autoscroll = setInterval(function() {
		i = 0;
		$("#carousel_menu div").each(function() {
			if($(this).hasClass("carousel_menu_selected")) {
				carousel_showframe((i + 1) % $("#carousel_menu div").length);
				return;
			}
			i++;
		});
	}, 5000);
});


