var $jQ = jQuery.noConflict();
$jQ(function() {
			
			  //remove js-disabled class
				$jQ("#viewer").removeClass("js-disabled");
			
			  //create new container for images
				$jQ("<div>").attr("id", "container").css({ position:"absolute"}).width($jQ(".wrapper").length * 300).height(170).appendTo("div#viewer");
			  	
				//add images to container
				$jQ(".wrapper").each(function() {
					$jQ(this).appendTo("div#container");
				});
				
				//work out duration of anim based on number of images (1 second for each image)
				var duration = $jQ(".wrapper").length * 10000;
				
				//store speed for later (distance / time)
				var speed = (parseInt($jQ("div#container").width()) + parseInt($jQ("div#viewer").width())) / duration;
					
				//set direction
				var direction = "rtl";
				
				//set initial position and class based on direction
				(direction == "rtl") ? $jQ("div#container").css("left", $jQ("div#viewer").width()).addClass("rtl") : $jQ("div#container").css("left", 0 - $jQ("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
							$jQ(this).css({ left:$jQ("div#imageScroller").width(), right:"" });
							
							//restart animation
							animator($jQ(this), duration, "rtl");
							
							//hide controls if visible
							($jQ("div#controls").length > 0) ? $jQ("div#controls").slideUp("slow").remove() : null ;			
											
						});
					} else {
					
					  //add direction class
						el.removeClass("rtl").addClass("ltr");
					
						//animate the el
						el.animate({ left:$jQ("div#viewer").width() + "px" }, time, "linear", function() {
												
							//reset container position
							$jQ(this).css({ left:0 - $jQ("div#container").width() });
							
							//restart animation
							animator($jQ(this), duration, "ltr");
							
							//hide controls if visible
							($jQ("div#controls").length > 0) ? $jQ("div#controls").slideUp("slow").remove() : null ;			
						});
					}
				}
				
				//start anim
				animator($jQ("div#container"), duration, direction);
				
				//pause on mouseover
				$jQ("a.wrapper").live("mouseover", function() {
				  
					//stop anim
					$jQ("div#container").stop(true);
					
					//show controls
					($jQ("div#controls").length == 0) ? $jQ("<div>").attr("id", "controls").appendTo("div#outerContainer").css({ opacity:0.7 }).slideDown("slow") : null ;
					($jQ("a#rtl").length == 0) ? $jQ("<a>").attr({ id:"rtl", href:"#rtl", title:"rtl" }).appendTo("#controls") : null ;
					($jQ("a#ltr").length == 0) ? $jQ("<a>").attr({ id:"ltr", href:"#ltr", title:"ltr" }).appendTo("#controls") : null ;
					
					//variable to hold trigger element
					var title = $jQ(this).attr("title");
					
					//add p if doesn't exist, update it if it does
					($jQ("p#title").length == 0) ? $jQ("<p>").attr("id", "title").text(title).appendTo("div#controls") : $jQ("p#title").text(title) ;
				});
				
				//restart on mouseout
				$jQ("a.wrapper").live("mouseout", function(e) {
				  
					//hide controls if not hovering on them
					(e.relatedTarget == null) ? null : (e.relatedTarget.id != "controls") ? $jQ("div#controls").slideUp("slow").remove() : null ;
					
					//work out total travel distance
					var totalDistance = parseInt($jQ("div#container").width()) + parseInt($jQ("div#viewer").width());
														
					//work out distance left to travel
					var distanceLeft = ($jQ("div#container").hasClass("ltr")) ? totalDistance - (parseInt($jQ("div#container").css("left")) + parseInt($jQ("div#container").width())) : totalDistance - (parseInt($jQ("div#viewer").width()) - (parseInt($jQ("div#container").css("left")))) ;
					
					//new duration is distance left / speed)
					var newDuration = distanceLeft / speed;
				
					//restart anim
					animator($jQ("div#container"), newDuration, $jQ("div#container").attr("class"));

				});
												
				//handler for ltr button
				$jQ("#ltr").live("click", function() {
				 					
					//stop anim
					$jQ("div#container").stop(true);
				
					//swap class names
					$jQ("div#container").removeClass("rtl").addClass("ltr");
										
					//work out total travel distance
					var totalDistance = parseInt($jQ("div#container").width()) + parseInt($jQ("div#viewer").width());
					
					//work out remaining distance
					var distanceLeft = totalDistance - (parseInt($jQ("div#container").css("left")) + parseInt($jQ("div#container").width()));
					
					//new duration is distance left / speed)
					var newDuration = distanceLeft / speed;
					
					//restart anim
					animator($jQ("div#container"), newDuration, "ltr");
				});
				
				//handler for rtl button
				$jQ("#rtl").live("click", function() {
										
					//stop anim
					$jQ("div#container").stop(true);
					
					//swap class names
					$jQ("div#container").removeClass("ltr").addClass("rtl");
					
					//work out total travel distance
					var totalDistance = parseInt($jQ("div#container").width()) + parseInt($jQ("div#viewer").width());

					//work out remaining distance
					var distanceLeft = totalDistance - (parseInt($jQ("div#viewer").width()) - (parseInt($jQ("div#container").css("left"))));
					
					//new duration is distance left / speed)
					var newDuration = distanceLeft / speed;
				
					//restart anim
					animator($jQ("div#container"), newDuration, "rtl");
				});
			});
