﻿/**
* jQuery.fn.dualSlider - Dual sliders, why not?
* Date: June 2010
*
* @author Rob Phillips http://www.robertjamesphillips.com (Front End Developer - Hugo & Cat - http://www.hugoandcat.com)
* @version 0.3
* @web http://www.hugoandcat.com/DualSlider/index.html
*
* Requirements:
* jquery.1.3.2.js - http://jquery.com/
* jquery.easing.1.3.js - http://gsgd.co.uk/sandbox/jquery/easing/
* jquery.timers-1.2.js - http://plugins.jquery.com/project/timers
*
* 0.2 - Tested and fixed for IE6+, auto loops, manual pause/play controls
*     - Disabled buttons until animation finishes - Thanks for the bug John.
* 0.3 - Now with a seamless loop, instead of that nasty rewind...was 'too much' apparently
*
**/


(function ($) {

    $.fn.dualSliderSmaller = function (options) {

        // default configuration properties
        var defaults = {
            auto: true,
            autoDelay: 10000,
            easingCarousel: 'swing',
            easingDetails: 'easeOutBack',
            durationCarousel: 500,
            durationDetails: 600
            , carouselwidth: 137
            , imagesPerRow: 4

        };

        var options = $.extend(defaults, options);

        this.each(function () {

            var obj = $(this);
            var carousel;
            var carouselTotal = $(".backgroundsX", obj).children().length;
            //alert('carouselTotal: ' + carouselTotal);

            var carouselPosition = 1;
            var carouselLinkIndex = 1;
            var carouselLinks = "";
            var carouselwidth = options.carouselwidth  // $(obj).width();
            var detailWidth = $(".panel .details_wrapper", obj).width();
            var locked = false;

            //$(".paging", obj).show();



            //alert(carouselTotal);
            if (options.auto == true) {

                //Creat duplicates for seamless looping
                $(".backgroundsX", obj).prepend($(".backgroundsX .item:last-child", obj).clone().css("margin-left", "-" + carouselwidth + "px"));
                $(".backgroundsX", obj).append($(".backgroundsX .item:nth-child(2)", obj).clone());

                //$(".details", obj).prepend($(".details .detail:last-child", obj).clone().css("margin-left", "-" + detailWidth + "px"));
                // $(".details", obj).append($(".details .detail:nth-child(2)", obj).clone());

                $(".previousX", obj).show();
                //$(".play, .pause", obj).show();
            }
            else {
                $(".previousX", obj).hide();
                //$(".play, .pause", obj).hide();
            }


            //Set main background width
            $(".backgroundsX", obj).css("width", ((carouselTotal + 1) * carouselwidth) + 100 + "px");


            //Set main detail width
            //$(".details_wrapper .details", obj).css("width", ((carouselTotal + 1) * carouselwidth) + "px");

            //            if (1 < carouselTotal) {
            //                for (i = 1; i <= carouselTotal; i++) {
            //                    (i == 1) ? carouselLinks += "<a rel=\"" + carouselLinkIndex + "\" title=\"Go to page " + carouselLinkIndex + " \" class=\"link" + carouselLinkIndex + " selected\" href=\"javascript:void(0)\">" + carouselLinkIndex + "</a>" : carouselLinks += "<a rel=\"" + carouselLinkIndex + "\"  title=\"Go to page " + carouselLinkIndex + " \" class=\"link" + carouselLinkIndex + "\" href=\"javascript:void(0)\" >" + carouselLinkIndex + "</a>";
            //                    carouselLinkIndex++;
            //                }
            //                $("#numbers", obj).html(carouselLinks);
            //            }
            //Bind carousel controls
            $(".nextX", obj).click(function () {
                //alert('next called');
                carouselPage(parseInt(carouselPosition + 1), false);
                lock();
            });

            $(".previousX", obj).click(function () {
                //alert('previous called');
                carouselPage(parseInt(carouselPosition - 1), false);
                lock();
            });

            //            $("#numbers a", obj).click(function () {
            //                carouselPage($(this).attr("rel"), false);
            //                lock();
            //            });

            //            $(".pause", obj).click(function () {
            //                autoPause();
            //            });
            //            $(".play", obj).click(function () {
            //                autoPlay();
            //            });

            function lock() {
                locked = true;
            }

            function unLock() {
                locked = false;
            }

            function checkPreviousNext() {
                //$("#numbers a", obj).removeClass("selected");
                // $("#numbers .link" + carouselPosition, obj).addClass("selected");
                //alert(carouselTotal);
                if (options.auto == false) {
                    //alert('checkPreviousNext: ' + (carouselPosition * options.imagesPerRow));

                    (carouselPosition * options.imagesPerRow >= carouselTotal) ? $(".nextX", obj).hide() : $(".nextX", obj).show();
                    (carouselPosition < 2) ? $(".previousX", obj).hide() : $(".previousX", obj).show();
                }
            }

            function adjust() {
                //alert('carouselPosition:' + carouselPosition + ' - carouselTotal: ' + carouselTotal + ' -  options.imagesPerRow: ' + options.imagesPerRow);

                if (carouselPosition < 1) {

                    $(".backgroundsX", obj).css("margin-left", (-1 * ((carouselTotal - 1) * carouselwidth)));
                    //$(".details", obj).css("margin-left", (-1 * ((carouselTotal - 1) * detailWidth)));
                    carouselPosition = carouselTotal;
                }
                if ((carouselPosition * options.imagesPerRow) > carouselTotal) {

                    //$(".backgroundsX", obj).css("margin-left", 0);



                    //$(".details", obj).css("margin-left", 0);
                    //carouselPosition = 1;
                }

            }

            function carouselPage(x, y) {

                //$(".details", obj).fadeOut();
                //$(".details", obj).hide();
                if (locked != true) {

                    var offSetWhenMovingFwd = 0;

                    if (x > carouselPosition) {
                        // home page off setting - clicking next
                        if (4 == options.imagesPerRow) {
                            offSetWhenMovingFwd = 0;

                            if (1 < carouselPosition) {
                            }
                        }
                        if (3 == options.imagesPerRow) {

                            offSetWhenMovingFwd = 25;

                            if (1 < carouselPosition) {
                                offSetWhenMovingFwd = 25 * carouselPosition;
                            }
                        }
                    }
                    if (x < carouselPosition) {
                        //alert('x ' + x + '>> carouselPosition: ' + carouselPosition);

                        // home page off setting
                        if (4 == options.imagesPerRow) {
                            //offSetWhenMovingFwd = parseFloat(50);
                        }
                        if (3 == options.imagesPerRow) {

                            offSetWhenMovingFwd = 45;
                        }
                    }

                    //console.log("New page: " + x);
                    carouselPosition = parseFloat(x);
                    
                    //Cancel timer if manual click
                    //if (y == false) autoPause();

                    
                    var slideAcrossBy = 0;

                    if (x == 2) {
                        slideAcrossBy = ((options.imagesPerRow * (carouselwidth)) * (x - 1))
                    }
                    else {
                        slideAcrossBy = ((options.imagesPerRow * (carouselwidth)) * x);
                    }
                    slideAcrossBy = ((options.imagesPerRow * (carouselwidth)) * (x - 1))

                    var newPage = slideAcrossBy - offSetWhenMovingFwd; 


                    //alert('newPage: ' + newPage);

                    var newPageDetail = (x * detailWidth) - detailWidth;

                    if (newPage != 0) {
                        newPage = newPage * -1;
                        newPageDetail = newPageDetail * -1;
                    }

                    $(".backgroundsX", obj).animate({
                        marginLeft: newPage
                    }, {
                        "duration": options.durationCarousel, "easing": options.easingCarousel,

                        complete: function () {

                            //                            //alert(newPageDetail);
                            //                            //Now animate the details
                            //                            $(".details", obj).animate({

                            //                                marginLeft: newPageDetail
                            //                            }, {
                            //                                "duration": options.durationDetails, "easing": options.easingDetails,
                            //                                complete: function () {

                            //                                    //alert("finished details");
                            //                                    adjust();
                            //                                    checkPreviousNext();
                            //                                    unLock();
                            //                                    $(".details", obj).fadeIn();
                            //                                }

                            //                            });

                            carouselPosition = parseFloat(x); // * options.imagesPerRow;

                            //adjust();
                            //alert('abut to prevNext');
                            checkPreviousNext();
                            unLock();
                        }
                    });
                }
            }

            function autoPause() {
                $(".pause", obj).hide();
                $(".play", obj).show();
                $("body").stopTime("autoScroll");
            }

            function autoPlay() {
                $(".pause", obj).show();
                $(".play", obj).hide();
                $("body").everyTime(options.autoDelay, "autoScroll", function () {
                    alert('autoplay ? eh');
                    carouselPage(carouselPosition + 1, true);
                    lock();
                });
            }

            if (options.auto == true) {
                autoPlay();
            }

        });

    };


    $.fn.dualSlider = function (options) {

        // default configuration properties
        var defaults = {
            auto: true,
            autoDelay: 10000,
            easingCarousel: 'swing',
            easingDetails: 'easeOutBack',
            durationCarousel: 500,
            durationDetails: 600
        };

        var options = $.extend(defaults, options);

        this.each(function () {

            var obj = $(this);
            var carousel;
            var carouselTotal = $(".backgrounds", obj).children().length;

            //alert('details count: ' + $(".details", obj).children().length);
           // alert('details width: ' + $(".details", obj).width());

            var carouselPosition = 1;
            var carouselLinkIndex = 1;
            var carouselLinks = "";
            var carouselwidth = $(obj).width();
           // var detailWidth = $(".panel .details_wrapper", obj).width();
           // alert('detailWidth: ' + detailWidth);
            var locked = false;

            $(".paging", obj).show();



            //alert(carouselTotal);
            if (options.auto == true) {

                //Creat duplicates for seamless looping
                $(".backgrounds", obj).prepend($(".backgrounds .item:last-child", obj).clone().css("margin-left", "-" + carouselwidth + "px"));
                $(".backgrounds", obj).append($(".backgrounds .item:nth-child(2)", obj).clone());

                $(".details", obj).prepend($(".details .detail:last-child", obj).clone().css("margin-left", "-" + detailWidth + "px"));
                $(".details", obj).append($(".details .detail:nth-child(2)", obj).clone());

                $(".previous", obj).show();
                $(".play, .pause", obj).show();
            }
            else {
                $(".previous", obj).hide();
                $(".play, .pause", obj).hide();
            }


            //Set main background width
            $(".backgrounds", obj).css("width", ((carouselTotal + 1) * carouselwidth) + 100 + "px");


            //Set main detail width
            $(".details_wrapper .details", obj).css("width", ((carouselTotal + 1) * carouselwidth) + "px");

            if (1 < carouselTotal) {
                for (i = 1; i <= carouselTotal; i++) {
                    (i == 1) ? carouselLinks += "<a rel=\"" + carouselLinkIndex + "\" title=\"Go to page " + carouselLinkIndex + " \" class=\"link" + carouselLinkIndex + " selected\" href=\"javascript:void(0)\">" + carouselLinkIndex + "</a>" : carouselLinks += "<a rel=\"" + carouselLinkIndex + "\"  title=\"Go to page " + carouselLinkIndex + " \" class=\"link" + carouselLinkIndex + "\" href=\"javascript:void(0)\" >" + carouselLinkIndex + "</a>";
                    carouselLinkIndex++;
                }
                $("#numbers", obj).html(carouselLinks);
            }
            //Bind carousel controls
            $(".next", obj).click(function () {
              
                carouselPage(parseInt(carouselPosition + 1), false);
                lock();
            });

            $(".previous", obj).click(function () {
               
                carouselPage(parseInt(carouselPosition - 1), false);
                lock();
            });

            $("#numbers a", obj).click(function () {
                
                carouselPage($(this).attr("rel"), false);
                lock();
            });

            $(".pause", obj).click(function () {
                autoPause();
            });
            $(".play", obj).click(function () {
                autoPlay();
            });

            function lock() {
                locked = true;
            }

            function unLock() {
                locked = false;
            }


            function checkPreviousNext() {
                $("#numbers a", obj).removeClass("selected");
                $("#numbers .link" + carouselPosition, obj).addClass("selected");

                //  alert('carouselPosition: ' + carouselPosition + ', carouselTotal: ' + carouselTotal);
                if (options.auto == false) {

                    (carouselPosition == carouselTotal) ? $(".next", obj).hide() : $(".next", obj).show();
                    (carouselPosition < 2) ? $(".previous", obj).hide() : $(".previous", obj).show();
                }
            }

            function adjust() {

                if (carouselPosition < 1) {

                    $(".backgrounds", obj).css("margin-left", (-1 * ((carouselTotal - 1) * carouselwidth)));
                    $(".details", obj).css("margin-left", (-1 * ((carouselTotal - 1) * detailWidth)));
                    carouselPosition = carouselTotal;

                }
                if (carouselPosition > carouselTotal) {

                    $(".backgrounds", obj).css("margin-left", 0);
                    $(".details", obj).css("margin-left", 0);
                    carouselPosition = 1;
                }

            }

            function carouselPage(x, y) {

                $(".details", obj).fadeOut();

                var detailWidth = $(".panel .details_wrapper", obj).width();
                //alert('detailWidth: ' + detailWidth);

                if (locked != true) {

                    //console.log("New page: " + x);
                    carouselPosition = parseFloat(x);
                    //Cancel timer if manual click
                    if (y == false) autoPause();

                    var newPage = (x * carouselwidth) - carouselwidth;
                    var newPageDetail = (x * detailWidth) - detailWidth;

                    if (newPage != 0) {
                        newPage = newPage * -1;
                        newPageDetail = newPageDetail * -1;
                    }

                    $(".backgrounds", obj).animate({
                        marginLeft: newPage

                    }, {
                        "duration": options.durationCarousel, "easing": options.easingCarousel,

                        complete: function () {
                        
                            //alert(newPageDetail);
                            //Now animate the details
                            $(".details", obj).animate({

                                marginLeft: newPageDetail
                            }, {
                                "duration": options.durationDetails, "easing": options.easingDetails,
                                complete: function () {

                                    //alert("finished details");
                                    adjust();
                                    checkPreviousNext();
                                    unLock();
                                    $(".details", obj).fadeIn();
                                }

                            });
                            //adjust();
                            //checkPreviousNext();
                            //unLock();
                        }
                    });
                }
            }

            function autoPause() {
                $(".pause", obj).hide();
                $(".play", obj).show();
                $("body").stopTime("autoScroll");
            }

            function autoPlay() {
                $(".pause", obj).show();
                $(".play", obj).hide();
                $("body").everyTime(options.autoDelay, "autoScroll", function () {
                  
                    carouselPage(carouselPosition + 1, true);
                    lock();
                });
            }

            if (options.auto == true) {
                autoPlay();
            }

        });

    };

    // Not used in H&H
    $.fn.rhsSlider = function (options) {

        // default configuration properties
        var defaults = {
            auto: true,
            justFade: false,
            heightOffset: 0,
            autoDelay: 10000,
            easingCarousel: 'swing',
            easingDetails: 'easeOutBack',
            durationCarousel: 500,
            durationDetails: 300
        };



        var options = $.extend(defaults, options);

        this.each(function () {

            var obj = $(this);
            var carousel;
            var carouselTotal = $(".backgrounds", obj).children().length;
            var carouselPosition = 1;
            var carouselLinkIndex = 1;
            var carouselLinks = "";
            var carouselwidth = $(obj).width();
            var locked = false;



            $(".backgrounds .item", obj).hide();

            var startSlide = $(".backgrounds .item:first-child", obj);

            if (options.auto == true) {
                //Creat duplicates for seamless looping
                //$(".backgrounds", obj).prepend($(".backgrounds .item:last-child", obj).clone());
                //$(".backgrounds", obj).append($(".backgrounds .item:nth-child(2)", obj).clone());
            }



            //$(obj).animate({ "height": startSlide.height() + options.heightOffset }, { duration: "fast" });

            $(obj).height(startSlide.height() + options.heightOffset)

            //$(obj).attr('visibility', 'visible');

            $(startSlide).show();

            for (i = 1; i <= carouselTotal; i++) {
                (i == 1) ? carouselLinks += "<a rel=\"" + carouselLinkIndex + "\" title=\"Go to page " + carouselLinkIndex + " \" class=\"link" + carouselLinkIndex + " selected\" href=\"javascript:void(0)\">" + carouselLinkIndex + "</a>" : carouselLinks += "<a rel=\"" + carouselLinkIndex + "\"  title=\"Go to page " + carouselLinkIndex + " \" class=\"link" + carouselLinkIndex + "\" href=\"javascript:void(0)\" >" + carouselLinkIndex + "</a>";
                carouselLinkIndex++;
            }
            $(".numbers", obj).html(carouselLinks);

            $(".numbers a", obj).click(function () {
                carouselPage($(this).attr("rel"), false);
                //alert('idx ' + $(this).attr("rel"));
                locked = true;
            });
            function lock() {
                locked = true;
            }

            function unLock() {
                locked = false;
            }


            function checkPreviousNext() {

                $(".numbers a", obj).removeClass("selected");
                $(".numbers .link" + carouselPosition, obj).addClass("selected");
            }

            function adjust() {
                //alert("carouselPosition " + carouselPosition + "<br />");
                if (carouselPosition < 1) {
                    carouselPosition = carouselTotal;
                }
                if (carouselPosition > carouselTotal) {
                    carouselPosition = 1;
                }
                //alert("carouselPosition " + carouselPosition + "<br />");
            }

            function carouselPage(x, y) {

                //alert("New page: " + x);

                //$(".details", obj).hide();
                // (locked != true) {

                //console.log("New page: " + x);
                carouselPosition = parseFloat(x);
                //Cancel timer if manual click
                //if (y == false) autoPause();
                if (carouselPosition > carouselTotal) {
                    carouselPosition = 1;
                }

                $(".backgrounds .item", obj).fadeOut("slow");

                var currentSlide = $(".backgrounds .item:nth-child(" + carouselPosition + ")", obj);


                if ($(obj).height() != currentSlide.height() + options.heightOffset)
                    $(obj).height(currentSlide.height() + options.heightOffset)
                //$(obj).animate({ "height": currentSlide.height() + options.heightOffset }, { duration: "fast" });



                $(currentSlide).fadeIn("slow");



                adjust();

                checkPreviousNext();

                unLock();

                //}


            }

            function autoPause() {

                $("body").stopTime("autoScrollRhs");
            }

            function autoPlay() {

                $("body").everyTime(options.autoDelay, "autoScrollRhs", function () {

                    carouselPage(carouselPosition + 1, true);
                    lock();
                });
            }


            if (options.auto == true) {
                autoPlay();
            }




            // alert($(obj).attr('visibility'));

        });

    };


})(jQuery);




