var currentPage = new Array();
var currentInterval = null;

$(document).ready(function () {

    if ($(".rotator").length) {
        $(".rotator").rotator(); // set up any rotators on the page

        if ($.browser.msie)
            $(".rotator").css("display", "block");
    }

    BuildMegaMenus();

    currentInterval = setInterval(checkHash, 100);

    // this will force the page to scroll again, even if the # tag hasn't
    // changed...you may have scrolled the page manually and rehit the
    // same link...
    $("a").click(function () {
        if ($(this).attr("href") && $(this).attr("href").indexOf("#") == 0) {
            // begins with hash
            currentHash = ""; // force the scroll baby!!
        }
    });

    setInterval(RotateImage, 4000);

});

var currentImage = 0;
function RotateImage() {
    $($(".image-rotate")[currentImage]).fadeOut("fast");

    $($(".image-rotate")[currentImage]).removeClass("image-rotate-selected");

    currentImage++;
    if (currentImage >= $(".image-rotate").length) {
        currentImage = 0;
    }

    $($(".image-rotate")[currentImage]).fadeIn("fast");
    $($(".image-rotate")[currentImage]).addClass("image-rotate-selected");
}

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);
            });
        }
    }
}

//----------------------
// javascript implementation of the c# string.format method
String.prototype.format = function () {
    var formatted = this;
    for (var i = 0; i < arguments.length; i++) {
        var regexp = new RegExp('\\{' + i + '\\}', 'gi');
        formatted = formatted.replace(regexp, arguments[i]);
    }
    return formatted;
};

function isTablet() {

    // For use within normal web clients 
    var isiPad = navigator.userAgent.match(/iPad/i) != null;

    // For use within iPad developer UIWebView
    // Thanks to Andrew Hedges!
    var ua = navigator.userAgent;
    var isiPad = /iPad/i.test(ua) || /iPhone OS 3_1_2/i.test(ua) || /iPhone OS 3_2_2/i.test(ua);

    return isiPad;

}
