var $j = jQuery.noConflict();
                    
$j(document).ready(function() {
	$j('ul#filter a').click(function() {
		$j(this).css('outline','none');
		$j('ul#filter .current').removeClass('current');
		$j(this).parent().addClass('current');
		
		var filterVal = $j(this).text().replace(' ','-');
				
		if(filterVal == 'All') {
			$j('ul# li.hidden').fadeIn('slow').removeClass('hidden');
		} else {
			
			$j('ul.course-list li').each(function() {
				if(!$j(this).hasClass(filterVal)) {
					$j(this).fadeOut('normal').addClass('hidden');
				} else {
					$j(this).fadeIn('slow').removeClass('hidden');
				}
			});
		}
		
		return false;
	});
	
		
	$j('.more').click(function(){
       $j(this).next().toggle("slow");        
    });
    
    			  //remove js-disabled class
				$j("#viewer").removeClass("js-disabled");
			
			  //create new container for images
				$j("<div>").attr("id", "container").css({ position:"absolute"}).width($j(".wrapper").length * 170).height(170).appendTo("div#viewer");
			  	
				//add images to container
				$j(".wrapper").each(function() {
					$j(this).appendTo("div#container");
				});
				
				//work out duration of anim based on number of images (3 second for each image)
				var duration = $j(".wrapper").length * 3000;
				
				//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", $j("div#viewer").width()).addClass("rtl") : $j("div#container").css("left", 0 - $j("div#container").width()).addClass("ltr") ;
				
				//animator function
				var animator = function(el, time, dir) {
				 
					//which direction to scroll
					if(dir == "rtl") {
					  
					  //add direction class
						el.removeClass("ltr").addClass("rtl");
					 		
						//animate the el
						el.animate({ left:"-" + el.width() + "px" }, time, "linear", function() {
												
							//reset container position
							$j(this).css({ left:$j("div#imageScroller").width(), right:"" });
							
							//restart animation
							animator($j(this), duration, "rtl");
							
							//hide controls if visible
							($j("div#controls").length > 0) ? $j("div#controls").slideUp("slow").remove() : null ;			
											
						});
					} else {
					
					  //add direction class
						el.removeClass("rtl").addClass("ltr");
					
						//animate the el
						el.animate({ left:$j("div#viewer").width() + "px" }, time, "linear", function() {
												
							//reset container position
							$j(this).css({ left:0 - $j("div#container").width() });
							
							//restart animation
							animator($j(this), duration, "ltr");
							
							//hide controls if visible
							($j("div#controls").length > 0) ? $j("div#controls").slideUp("slow").remove() : null ;			
						});
					}
				}
				
				//start anim
				animator($j("div#container"), duration, direction);
				
				//pause on mouseover
				$j("a.wrapper").live("mouseover", function() {
				  
					//stop anim
					$j("div#container").stop(true);
					
					//show controls
					($j("div#controls").length == 0) ? $j("<div>").attr("id", "controls").appendTo("div#outerContainer").css({ opacity:0.7 }).slideDown("slow") : null ;
					($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) ;
				});
				
				//restart on mouseout
				$j("a.wrapper").live("mouseout", function(e) {
				  
					//hide controls if not hovering on them
					(e.relatedTarget == null) ? null : (e.relatedTarget.id != "controls") ? $j("div#controls").slideUp("slow").remove() : null ;
					
					//work out total travel distance
					var totalDistance = parseInt($j("div#container").width()) + parseInt($j("div#viewer").width());
														
					//work out distance left to travel
					var distanceLeft = ($j("div#container").hasClass("ltr")) ? totalDistance - (parseInt($j("div#container").css("left")) + parseInt($j("div#container").width())) : totalDistance - (parseInt($j("div#viewer").width()) - (parseInt($j("div#container").css("left")))) ;
					
					//new duration is distance left / speed)
					var newDuration = distanceLeft / speed;
				
					//restart anim
					animator($j("div#container"), newDuration, $j("div#container").attr("class"));

				});
												
				//handler for ltr button
				$j("#ltr").live("click", function() {
				 					
					//stop anim
					$j("div#container").stop(true);
				
					//swap class names
					$j("div#container").removeClass("rtl").addClass("ltr");
										
					//work out total travel distance
					var totalDistance = parseInt($j("div#container").width()) + parseInt($j("div#viewer").width());
					
					//work out remaining distance
					var distanceLeft = totalDistance - (parseInt($j("div#container").css("left")) + parseInt($j("div#container").width()));
					
					//new duration is distance left / speed)
					var newDuration = distanceLeft / speed;
					
					//restart anim
					animator($j("div#container"), newDuration, "ltr");
				});
				
				//handler for rtl button
				$j("#rtl").live("click", function() {
										
					//stop anim
					$j("div#container").stop(true);
					
					//swap class names
					$j("div#container").removeClass("ltr").addClass("rtl");
					
					//work out total travel distance
					var totalDistance = parseInt($j("div#container").width()) + parseInt($j("div#viewer").width());

					//work out remaining distance
					var distanceLeft = totalDistance - (parseInt($j("div#viewer").width()) - (parseInt($j("div#container").css("left"))));
					
					//new duration is distance left / speed)
					var newDuration = distanceLeft / speed;
				
					//restart anim
					animator($j("div#container"), newDuration, "rtl");
				}); 
});
