jQuery(document).ready(function(){
    //RM: Support for clickable cart
    jQuery('#shoppingcart').bind('click', function() {
        location.href= jQuery('#shoppingcart a').attr('href');

        return false;
    });

    // END
    //remove js-disabled class
    jQuery("#viewer").removeClass("js-disabled");

    //create new containerScrollerScroller for images
    jQuery("<div>").attr("id", "containerScroller").css({ position:"absolute"}).width(jQuery(".wrapperScroller").length * 120).height(120).appendTo("div#viewer");

    //add images to containerScroller
    jQuery(".wrapperScroller").each(function() {
        jQuery(this).appendTo("div#containerScroller");
    });

    //work out duration of anim based on number of images (1 second for each image)
    var duration = jQuery(".wrapperScroller").length * 5000;

    //store speed for later (distance / time)
    var speed = (parseInt(jQuery("div#containerScroller").width()) + parseInt(jQuery("div#viewer").width())) / duration;

    //set direction
    var direction = "rtl";

    //set initial position and class based on direction
    (direction == "rtl") ? jQuery("div#containerScroller").css("left", jQuery("div#viewer").width()).addClass("rtl") : jQuery("div#containerScroller").css("left", 0 - jQuery("div#containerScroller").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 containerScroller position
                jQuery(this).css({ left:jQuery("div#imageScroller").width(), right:"" });

                jQuery("div#imageScroller").css({height: '120px'});

                //restart animation
                animator(jQuery(this), duration, "rtl");
            });
        } else {

            //add direction class
            el.removeClass("rtl").addClass("ltr");

            //animate the el
            el.animate({ left:jQuery("div#viewer").width() + "px" }, time, "linear", function() {

                //reset containerScroller position
                jQuery(this).css({ left:0 - jQuery("div#containerScroller").width() });

                //restart animation
                animator(jQuery(this), duration, "ltr");
            });
        }
    }

    //start anim
    animator(jQuery("div#containerScroller"), duration, direction);

    //pause on mouseover
    jQuery("a.wrapperScroller").live("mouseover", function() {

        //stop anim
        jQuery("div#containerScroller").stop(true);

        //variable to hold trigger element
        var title = jQuery(this).attr("title");

        //add p if doesn't exist, update it if it does
        jQuery("p#title").text(title) ;
    });

    //restart on mouseout
    jQuery("a.wrapperScroller").live("mouseout", function(e) {
        //work out total travel distance
        var totalDistance = parseInt(jQuery("div#containerScroller").width()) + parseInt(jQuery("div#viewer").width());

        //work out distance left to travel
        var distanceLeft = (jQuery("div#containerScroller").hasClass("ltr")) ? totalDistance - (parseInt(jQuery("div#containerScroller").css("left")) + parseInt(jQuery("div#containerScroller").width())) : totalDistance - (parseInt(jQuery("div#viewer").width()) - (parseInt(jQuery("div#containerScroller").css("left")))) ;

        //new duration is distance left / speed)
        var newDuration = distanceLeft / speed;

        //restart anim
        animator(jQuery("div#containerScroller"), newDuration, jQuery("div#containerScroller").attr("class"));

    });

    //handler for ltr button
    jQuery("#ltr").live("click", function() {

        //stop anim
        jQuery("div#containerScroller").stop(true);

        //swap class names
        jQuery("div#containerScroller").removeClass("rtl").addClass("ltr");

        //work out total travel distance
        var totalDistance = parseInt(jQuery("div#containerScroller").width()) + parseInt(jQuery("div#viewer").width());

        //work out remaining distance
        var distanceLeft = totalDistance - (parseInt(jQuery("div#containerScroller").css("left")) + parseInt(jQuery("div#containerScroller").width()));

        //new duration is distance left / speed)
        var newDuration = distanceLeft / speed;

        //restart anim
        animator(jQuery("div#containerScroller"), newDuration, "ltr");

        return false;
    });

    //handler for rtl button
    jQuery("#rtl").live("click", function() {

        //stop anim
        jQuery("div#containerScroller").stop(true);

        //swap class names
        jQuery("div#containerScroller").removeClass("ltr").addClass("rtl");

        //work out total travel distance
        var totalDistance = parseInt(jQuery("div#containerScroller").width()) + parseInt(jQuery("div#viewer").width());

        //work out remaining distance
        var distanceLeft = totalDistance - (parseInt(jQuery("div#viewer").width()) - (parseInt(jQuery("div#containerScroller").css("left"))));

        //new duration is distance left / speed)
        var newDuration = distanceLeft / speed;

        //restart anim
        animator(jQuery("div#containerScroller"), newDuration, "rtl");

        return false;
    });
});

