$(function() {
			
    //remove js-disabled class
    $("#viewer").removeClass("js-disabled");
			
    //create new container for images
    $("<div>").attr("id", "container2").css({
        position:"absolute"
    }).width($(".wrapper").length * 165).height(120).appendTo("div#viewer");
			  	
    //add images to container
    $(".wrapper").each(function() {
        $(this).appendTo("div#container2");
    });
				
    //work out duration of anim based on number of images (1 second for each image)
    var duration = $(".wrapper").length * 3000;
				
    //store speed for later (distance / time)
    var speed = (parseInt($("div#container2").width()) + parseInt($("div#viewer").width())) / duration;
								
    //set direction
    var direction = "ltr";
				
    //set initial position and class based on direction
    (direction == "rtl") ? $("div#container2").css("left", $("div#viewer").width()).addClass("rtl") : $("div#container2").css("left", 0 - $("div#container2").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
                $(this).css({
                    left:$("div#brand-slider").width(), 
                    right:""
                });
							
                //restart animation
                animator($(this), duration, "rtl");											
            });
        } else {
					
            //add direction class
            el.removeClass("rtl").addClass("ltr");
					
            //animate the el
            el.animate({
                left:$("div#viewer").width() + "px"
            }, time, "linear", function() {
												
                //reset container position
                $(this).css({
                    left:0 - $("div#container2").width()
                });
							
                //restart animation
                animator($(this), duration, "ltr");	
            });
        }
    }
				
    //start anim
    animator($("div#container2"), duration, direction);
				
    //pause on mouseover
    $("a.wrapper").live("mouseover", function() {
				  
        //stop anim
        $("div#container2").stop(true);
    });
				
    //restart on mouseout
    $("a.wrapper").live("mouseout", function(e) {
					
        //work out total travel distance
        var totalDistance = parseInt($("div#container2").width()) + parseInt($("div#viewer").width());
														
        //work out distance left to travel
        var distanceLeft = ($("div#container2").hasClass("ltr")) ? totalDistance - (parseInt($("div#container2").css("left")) + parseInt($("div#container2").width())) : totalDistance - (parseInt($("div#viewer").width()) - (parseInt($("div#container2").css("left")))) ;
					
        //new duration is distance left / speed)
        var newDuration = distanceLeft / speed;
				
        //restart anim
        animator($("div#container2"), newDuration, $("div#container2").attr("class"));

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

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