﻿var currentInterval = null;

/*  ROTATOR PLUGIN By COLIN WISEMAN */
(function ($) {

    // this is a complicated beastie as it encompasses more than just rotation of pages now
    // due to style considerations - some of the rotation elements have a "content" block
    // that will slide the "block".  because we need to remember the current page, this was
    // the most logical way to build it.

    var Rotator = function (element) {
    
        var elem = $(element);
        var id = "";

        var obj = this;

        var nexts = $(elem).find(".next"); // each rotator has a next button
        var prevs = $(elem).find(".previous"); // each rotator has a back button
        var rms = $(elem).find(".read-more"); // some of the rotators have a "content" page that will allow the user to slide to a specific block.
        var pageLinks = elem.find(".pager ul li");

        // the current chapter is the vertical scroll of the page
        // if the current chapter is the same as the chapter of the link that has been clicked,
        // then the rotator will scroll the web page to the correct chapter
        var currentChapter = "";
        var currentPage = 0; //  the current page is the horizontal scroll of the rotator

        var siblingBlocks = null;
        var current = 0;
        var holder = null;
        var moving = false;

        var autoRotate = null;
        var autoRotateWait = 5000;

        // a public method that returns the current "chapter" that the web page is on
        this.setChapter = function (chapter) {
            currentChapter = chapter;
        }

        // a public method that returns wether or not this instance of the rotator is currently moving
        this.isMoving = function () {
            return moving;
        }

        // a public method that basically handles all the movement
        this.scrollTo = function (chapter, page) {
            
            try
            {
                scrollToPage(page);
            }
            catch(ex)
            {
                
            }
            

            currentChapter = chapter;
        }

        function indexMoveTimed()
        {            
            if(!moving)
            {
                var moveTo = currentPage + 1;
                if(moveTo >= pageLinks.length)
                    moveTo = 0;

                if (moveTo == 0)
                    location.hash = id;
                else
                    location.hash = id + "/" + moveTo;
            }
        }

        function indexMove(obj, index) {
            $(obj).click(function (e) {

                if(!moving)
                {
                    var aBetterEventObject = $.Event(e);
                    // Now you can do what you want: (Cross-browser)
                    aBetterEventObject.preventDefault();
                    if ($(obj).attr("rel") != undefined) {
                        index = parseInt($(obj).attr("rel"));
                    }

                    if (index == 0)
                        location.hash = id;
                    else
                        location.hash = id + "/" + index;
                }
            });
        }

        function scrollToPage(page) // not a public method
        {
            if(!moving)
            {
                var moveBy = 0;
                moving = true;
                // clear the autorotate while we are doing stuff.
                clearInterval(autoRotate);

                if (page > currentPage) {
                    for (ii = currentPage; ii < page; ii++) {
                        // moveBy += parseInt($(siblingBlocks[ii]).css("width"));
                        moveBy += parseInt($(siblingBlocks[ii]).css("width")) +
                                            parseInt($(siblingBlocks[ii]).css("margin-right")) +
                                            parseInt($(siblingBlocks[ii]).css("margin-left")) +
                                            parseInt($(siblingBlocks[ii]).css("padding-right")) +
                                            parseInt($(siblingBlocks[ii]).css("padding-left"));
                    }

                    siblingBlocks.each(function (index, obj) {
                        var currentLeft = parseInt($(this).css("left"));
                        // subtract the moveBy as we are moving to the right
                        var newLeft = currentLeft - moveBy;
                        $(this).animate({ left: newLeft + "px" }, "1000", function () { 
                        
                            if (index == $(siblingBlocks).length - 1) {
                                // finished, restart!
                                moving = false;
                                autoRotate = setInterval(indexMoveTimed, autoRotateWait);
                            }

                         });
                    });
                }
                else {
                    for (ii = page; ii < currentPage; ii++) {
                        // moveBy += parseInt($(siblingBlocks[ii]).css("width"));                    
                        moveBy += parseInt($(siblingBlocks[ii]).css("width")) +
                                            parseInt($(siblingBlocks[ii]).css("margin-right")) +
                                            parseInt($(siblingBlocks[ii]).css("margin-left")) +
                                            parseInt($(siblingBlocks[ii]).css("padding-right")) +
                                            parseInt($(siblingBlocks[ii]).css("padding-left"));

                    }

                    siblingBlocks.each(function (index, obj) {
                        var currentLeft = parseInt($(this).css("left"));

                        // add the moveBy as we are moving to the left
                        var newLeft = currentLeft + moveBy;
                        $(this).animate({ left: newLeft + "px" }, "1000", function () { 
                    
                            if (index == $(siblingBlocks).length - 1) {
                                // finished, restart!
                                moving = false;
                                autoRotate = setInterval(indexMoveTimed, autoRotateWait);
                            }

                         });
                    });
                }

                currentPage = parseInt(page);

                if (pageLinks != null) {
                    pageLinks.removeClass("selected");
                    $(pageLinks[currentPage]).addClass("selected");
                }

            }
        }

        this.init = function (settings) {
            // each rotator has a block holder, think of the block holder as a "window", where the blocks slide past, making them visible.
            
            try {
                id = $(element).parents("div").attr("id").replace("-page", "");
            }
            catch (ex) {

            }

            if(currentInterval == null)
                currentInterval = setInterval(checkHash, 100);

            pager = settings['pager'];

            holder = $(elem).children("." + settings['window-class'])[0];
            if (holder != null) {
                // now get all the blocks that are related - siblings :D
                // we will now move the blocks to the correct position, so that the developer doesn't
                // need to work all that out!       
                siblingBlocks = $(holder).children("." + settings['item-class']);
                
                $(siblingBlocks).each(function (index, obj) {
                    $(this).css("left", current);
                    $(this).css("top", 0);

                    // the next block's position will be the current position + the width of the one we just placed
                    current += parseInt($(this).css("width")) +
                                        parseInt($(this).css("margin-right")) +
                                        parseInt($(this).css("margin-left")) +
                                        parseInt($(this).css("padding-right")) +
                                        parseInt($(this).css("padding-left"));



                    // current += parseInt($(this).css("width")); // the next block's position will be the current position + the width of the one we just placed

                    $(this).attr("index", index);
                    index++;
                });

                $(holder).find("a.previous,a.next").each(function (index, obj) {
                    // we generally use # as the href on a tags that we want to "work", but don't want to go anywhere.  This helps with styling.
                    // but this messes around with movement on the page.
                    // remove the # tag at this point so that the styles are applied, but the hash tag doesn't work.
                    $(this).attr("href", "");
                });

                $(nexts).click(function (e) {

                    var aBetterEventObject = $.Event(e);
                    // Now you can do what you want: (Cross-browser)
                    aBetterEventObject.preventDefault(); // stop any clicks from redirecting the site without our say so.                    
                    if (!moving) // if it is moving, we won't want to set a page, otherwise things might go gooey!
                    {
                        var nextIndex = currentPage + 1;
                        if (nextIndex < siblingBlocks.length)
                            location.hash = id + "/" + nextIndex;
                        else
                            location.hash = id;
                    }
                   
                });

                $(prevs).click(function (e) {

                    var aBetterEventObject = $.Event(e);
                    // Now you can do what you want: (Cross-browser)
                    aBetterEventObject.preventDefault(); // stop any clicks from redirecting the site without our say so.

                    if (!moving) // if it is moving, we won't want to set a page, otherwise things might go gooey!
                    {
                        var previousIndex = currentPage - 1;
                        if (previousIndex >= 0) {
                            if (previousIndex == 0)
                                location.hash = id;
                            else
                                location.hash = id + "/" + previousIndex;
                        }
                        else {
                            // previous index < 0
                            location.hash = id + "/" + ($(siblingBlocks).length - 1);
                        }
                    }

                });

                $(rms).each(function (index, obj) {
                    indexMove(this, index);
                });

                $(pageLinks).each(function (index, obj) {
                    indexMove(this, index);
                });

                var pager = elem.find(".pager");

                if (pager.length) {
                    var ul = $(pager[0]).find("ul");
                    if (ul.length) {
                        var lis = $(ul[0]).find("li");
                        var length = 0;

                        $(lis).each(function (index, obj) {
                            // the $(elem).outerWidth() doesn't seem to work on dynamic elements
                            length += parseInt($(obj).css("width")) +
                                        parseInt($(obj).css("margin-right")) +
                                        parseInt($(obj).css("margin-left")) +
                                        parseInt($(obj).css("padding-right")) +
                                        parseInt($(obj).css("padding-left"));
                        });


                        $(ul[0]).css("width", length + "px");
                    }
                }
            }

            autoRotate = setInterval(indexMoveTimed, autoRotateWait);
        };
    }



    $.fn.rotator = function (options) {
        var settings = {
            'window-class': 'block-holder',
            'item-class': 'block'
        };

        return this.each(function () {
            if (options) {
                $.extend(settings, options);
            }

            var element = $(this);

            // Return early if this element already has a plugin instance
            if (element.data('rotator')) {
                element.data('rotator', null);
            }

            var rotator = new Rotator(this);
            $(window).load(function()
            {
                rotator.init(settings); 
            });
            

            // Store plugin object in this element's data
            element.data('rotator', rotator);

        });
    };

})(jQuery);

var currentHash = "";
function checkHash() {
    var parts = location.hash.split("/");
    var chapter = parts[0];
    var page = parts[1];

    if (chapter.length > 0);
    {
        if (page == null || page == undefined)
            page = 0;

        if (location.hash != currentHash &&
                location.hash != "" &&
                chapter.length > 1) { // has something changed?

            var item = $(chapter + "-page .rotator").data("rotator");

            if (item != null) {
                if (!item.isMoving()) { // if the rotator is currently moving, then we shouldn't try and move it again...else BOOM!!                    
                    item.scrollTo(chapter, page); //  now pass in which chapter we want to go to, and the related page                
                    currentHash = location.hash;                    
                }
            }
            else {
                if ($(chapter + "-page").length) {
                    $.scrollTo(chapter + "-page", "1000");
                    currentHash = location.hash;
                }
            }

            // this is a VERY important thing to do
            // as each rotator doesn't know the other exists, we need to tell each rotator
            // which chapter the web page is currently on.  then the rotator will move horizontally
            // but might not be fully in view.  so set the chapter, and get the rotator fully in view
            $(".rotator").each(function (index, obj) {
                var _item = $(this).data("rotator");
                _item.setChapter(chapter);
            });
        }
    }
}
