// JavaScript Document
$j(function() {
   //remove js-disabled class
	$j("#viewer").removeClass("js-disabled");
   //create new container for images
	$j("<div>").attr("id", "container").css({ position:"absolute"}).width($j(".mb").length * 250).height(250).appendTo("div#viewer");
	//add images to container
	
	$j(".mb").each(function() {
		$j(this).appendTo("div#container");
	});
	
	//work out duration of anim based on number of images (1 second for each image)
	var duration = $j(".mb").length * 1200; // was 1000
	//store speed for later (distance / time)
	var speed = (parseInt($j("div#container").width()) + parseInt($j("div#viewer").width())) / duration;
	//set direction
	var direction = "rtl";
	//set initial position and class based on direction
	(direction == "rtl") ? $j("div#container").css("left", 0) : $j("div#container").css("left", 0).addClass("ltr") ;
	//function to calculate the minimum left value of the last slider element when in view
	var lastCrew_x = function() {
		var crewBox = $j("div#viewer");
		return ((($j(".mb").length) * -200) + crewBox.width());
	}
	//animator function
	var animator = function(el, time, dir) {
		var rtlMax = lastCrew_x();
		var ltrMax = 0;
		
		//which direction to scroll
		if(dir == "rtl") { // right button
		  //add direction class
			el.removeClass("ltr").addClass("rtl");
			//animate the el
			el.animate({ left:rtlMax + "px" }, time, "linear");
		} else { // left button
		  //add direction class
			el.removeClass("rtl").addClass("ltr");
			//animate the el
			el.animate({ left:ltrMax + "px" }, time, "linear");
		}
	}
	
	//show title on mouseover
	$j("a.mb").live("mouseover", function(e) {
		//show controls
		($j("div#controls").length == 0) ? $j("<div>").attr("id", "controls").appendTo("div#outerContainer").css({ opacity:0.7 }).slideDown("slow") : null ;
		
		//alert(($j("a#rtl").length == 0) + " - " + ($j("a#ltr").length == 0));
		($j("a#rtl").length == 0) ? $j("<a>").attr({ id:"rtl", href:"#", title:"rtl" }).appendTo("#controls") : null ;
		($j("a#ltr").length == 0) ? $j("<a>").attr({ id:"ltr", href:"#", title:"ltr" }).appendTo("#controls") : null ;
		//variable to hold trigger element
		var title = $j(this).attr("title");
		//add p if doesn't exist, update it if it does
		($j("p#title").length == 0) ? $j("<p>").attr("id", "title").text(title).appendTo("div#controls") : $j("p#title").text(title) ;
	});
	//hide title on mouseout
	$j("a.mb").live("mouseout", function(e) {
		//alert(e.relatedTarget.id);
		//hide controls if not hovering on them
		(e.relatedTarget == null) ? null : (e.relatedTarget.id != "controls") ? $j("div#controls").slideUp("slow").remove() : null ;
	});
	
	// on button mouseout - checks if the scroller is at the end and removes an appropriate button if so
	var getCrewCoords = function() {
		var crewBox = $j("div#viewer");
		var firstCrew = $j(".mb:first");
		var lastCrew = $j(".mb:last");
		var lastXMax = (crewBox.width() - 200) * -1;
		
		var xfirstPosition = crewBox.offset().left - firstCrew.offset().left;
		var xlastPosition = crewBox.offset().left - lastCrew.offset().left;
		if (xfirstPosition < 1) {
			$j("img#ltr").css({"visibility":"hidden"});
		}
		if (xlastPosition >= lastXMax) {
			$j("img#rtl").css({"visibility":"hidden"});
		}
	}
	// current x coordinate (css.position.left) of first slider element
	var getSliderPosition = function() {
		var crewBox = $j("div#viewer");
		var firstCrew = $j(".mb:first");
		return (crewBox.offset().left - firstCrew.offset().left);
	}
									
	//handler for left hand arrow button
	$j("#ltr").live("mouseover", function() {
		$j("img#rtl").css({"visibility":"visible"});
		//stop anim
		$j("div#container").stop(true);
		//swap class names
		$j("div#container").removeClass("rtl").addClass("ltr");
		//new duration is distance left / speed)
		var newDuration = getSliderPosition() / speed;
		//restart anim
		animator($j("div#container"), newDuration, "ltr");
	});
	//handler for ltr button
	$j("#ltr").live("mouseout", function() {
		//stop anim
		$j("div#container").stop(true);
		getCrewCoords();
	});
	
	//handler for right hand arrow button
	$j("#rtl").live("mouseover", function() {
		$j("img#ltr").css({"visibility":"visible"});
		//stop anim
		$j("div#container").stop(true);
		//swap class names
		$j("div#container").removeClass("ltr").addClass("rtl");
		//new duration is distance left / speed)
		var newDuration = Math.abs((lastCrew_x() + getSliderPosition()) / speed);
		//restart anim
		animator($j("div#container"), newDuration, "rtl");
	});
	//handler for ltr button
	$j("#rtl").live("mouseout", function() {
		getCrewCoords();
		//stop anim
		$j("div#container").stop(true);
	});
});

