﻿// Javascript to handle the information rotator
Rotator = function(elemid, fadein, fadeout, autoperiod, callBack) {
    // Normal information content
    this._infoContent = [];
    this._rotateNum = -1;
    this._id = elemid;
    this._fadeuptimer = "fadeup_" + elemid;
    this._fadedntimer = "fadedn_" + elemid;
    this._waittimer = "wait_" + elemid;
    this._div = document.getElementById(elemid);
    this._currentopacity = 0.1;
    this._fadein = fadein * 100.; // Fade specified in total time, divide by ten for steps from 0.1 to 1
    this._fadeout = fadeout * 100.;
    this._fadedelay = this._fadeout * 10. + 500.; // Delay to account for time taken to fade out    
    this._autoperiod = autoperiod * 1000; // Autoperiod is cycle given in seconds, multiply by 1000 for ms    
    this._callBack = callBack; // Callback function for one round
    this._stop = false;
};

Rotator.prototype = {

    load: function(content) {
        this._infoContent.length = 0;
        for (var i = 0; i < content.length; ++i) this._infoContent[this._infoContent.length] = content[i];
    },

    stop: function() {
        if (window[this._fadeuptimer]) clearTimeout(window[this._fadeuptimer]); // If timer is on, turn it off
        if (window[this._fadedntimer]) clearTimeout(window[this._fadedntimer]); // If timer is on, turn it off
        if (window[this._waittimer]) clearTimeout(window[this._waittimer]); // If timer is on, turn it off
        this._infoContent.length = 0;
        this._stop = true;
    },

    rotate: function() {
        ++this._rotateNum;
        if (this._rotateNum >= this._infoContent.length) {
            if (this._callBack !== null && this._callBack !== undefined) {
                eval(this._callBack);
                return;
            }
            this._rotateNum = 0;
        }
        $get(this._id).innerHTML = this._infoContent[this._rotateNum].adHTML;
        if (AppMode.OS == 'Mac') this._stop = true;
        this._stop = true;
    },

    turnpage: function() {
        if (this._stop) { return; }
        var me = this;
        if (window[this._fadeuptimer]) clearTimeout(window[this._fadeuptimer]); // If timer is on, turn it off
        if (window[this._fadedntimer]) clearTimeout(window[this._fadedntimer]); // If timer is on, turn it off
        if (window[this._waittimer]) clearTimeout(window[this._waittimer]); // If timer is on, turn it off
        this.fadedn();
        window[this._waittimer] = setTimeout(function() { me.nextpage(); }, this._fadedelay);
    },

    nextpage: function(event) {
        if (this._stop) { return; }
        this.rotate(); // Get next div to display
        this.fadeup(); // Fade-up rotator div from 0.1
    },

    setopacity: function(alpha) {
        var style = this._div.style;
        if (style.MozOpacity !== undefined) { //Moz and older
            style.MozOpacity = alpha;
        }
        else if (style.filter !== undefined) { //IE
            style.filter = "alpha(opacity=0)";
            this._div.filters.alpha.opacity = (alpha * 100);
        }
        else if (style.opacity !== undefined) { //Opera
            style.opacity = alpha;
        }
        this._currentopacity = alpha;
    },

    autorotator: function() {
        if (this._stop) { return; }
        var me = this;
        this.turnpage(); //Show that DIV
        window["timer"] = setTimeout(function() { me.autorotator(); }, this._autoperiod);
    },

    fadedn: function() {
        if (AppMode.OS == "Mac") return;
        if (this._stop) { return; }
        var me = this;
        if (this._currentopacity > 0.0) {
            this.setopacity(this._currentopacity - 0.1);
            window[this._fadedntimer] = setTimeout(function() { me.fadedn(); }, this._fadeout);
        }
    },

    fadeup: function() {
        if (AppMode.OS == "Mac") return;
        if (this._stop) { return; }
        var me = this;
        if (this._currentopacity < 1.0) {
            this.setopacity(this._currentopacity + 0.1);
            window[this._fadeuptimer] = setTimeout(function() { me.fadeup(); }, this._fadein);
        }
    }
};

// Javascript to handle the book pages in this VE Mashup
Placemark = function(data) {
    //Constructor
    this._data = data;
    if (data.icon === '') data.icon = "pin_green.png";
};

// Javascript to handle the book pages in this VE Mashup
BookPages = function(element, author, title) {
    //Constructor
    this._instance = null;
    this._pageNumberSequence = 0;
    this._pageNumberActual = 0;
    this._pageCatid = 0;
    this._pageSTYLE = null;
    this._pageHTML = null;
    this._pages = Array();

    this._author = author;
    this._title = title;
    this._element = element;
    this._bCover = false;
    this._bClickCover = true;
    this._bFirstClick = true;

    this._fadeout = 100;
    this._fadein = 100;
    this._fadedelay = this._fadeout * 10. + 500.;
    this._currentopacity = 0.1;
};

BookPages.prototype = {

    load_pages: function(pages, page) {
        this._pages.length = 0;
        for (var i = 0; i < pages.length; ++i) this._pages[this._pages.length] = pages[i];
    },

    _render_page: function(page) {
        this._pageNumberSequence = this._pages[page].pageNumberSequence;
        this._pageNumberActual = this._pages[page].pageNumberActual;
        this._pageHTML = this._pages[page].pageHTML;
        this._pageCatid = this._pages[page].pageCatid;

        if ((page == 0 || page == this._pages.length - 1) &&
            (this._pageCatid == 99 || this._pageCatid == innerPages
            || this._pages.length == 1)) {
            this._bCover = true;
            $get('vpage').style.display = 'none';
            $get('cover').innerHTML = this._pageHTML;
            $get('cover').style.display = 'block';
            $get('rbottom').style.display = 'none';
        }
        else {
            this._bCover = false;
            $get('vpage').innerHTML = this._pageHTML;
            $get('vpage').style.display = 'block';
            $get('rbottom').style.display = 'block';
            $get('cover').style.display = 'none';
            if (AppMode.Browser.indexOf('xplorer') > 0) this._set_opacity($get('cover'), 1.0);
        }
    },

    _show_page: function(dir) {
        var pageToShow = this._pageNumberSequence + dir;
        if (pageToShow < 0) pageToShow = this._pages.length - 1;
        if (pageToShow >= this._pages.length) pageToShow = 0;
        this._render_page(pageToShow);
    },

    _first_click: function() {
        totd(3);
        this._bFirstClick = false;
    },

    _cover_click: function() {
        if (this._bFirstClick) {
            this._first_click();
        }
        if (this._pages.length == 1 && this._pagesCatid != innerPages) { return; }
        if (this._bClickCover) {
            this._turn_page(1);
            return;
        }
        if (this._pageCatid == innerPages) this._bClickCover = true;
    },

    _turn_page: function(dir) {

        var anim;
        // For IE page turning use the ACT animation sequence
        if (AppMode.Browser.indexOf('xplorer') > 0) {
            if (dir > 0) anim = $find('AnimPageNext');
            else anim = $find('AnimPagePrev');
            anim._onClick.play();
            return;
        }

        // For non-IE page turning use a home-grown sequence
        this._other_turn_page(dir);
    },

    _other_turn_page: function(dir) {

        var me = this;
        this.dir = dir;
        if (window["pageuptimer"]) clearTimeout(window["pageuptimer"]); // If timer is on, turn it off
        if (window["pagedntimer"]) clearTimeout(window["pagedntimer"]); // If timer is on, turn it off
        if (window["pagetimer"]) clearTimeout(window["pagetimer"]); // If timer is on, turn it off
        this._fade_dn();
        window["pagetimer"] = setTimeout(function() {
            me._next_page(me.dir);
        }, this._fadedelay);
    },

    _next_page: function(dir) {
        this._show_page(dir); // Get next div to display
        this._fade_up(); // Fade-up page div from 0.1
    },

    _set_opacity: function(element, alpha) {
        var style = element.style;
        if (style.MozOpacity !== undefined) { //Moz and older
            style.MozOpacity = alpha;
        }
        else if (style.filter !== undefined) { //IE
            style.filter = "alpha(opacity=0)";
            element.filters.alpha.opacity = (alpha * 100);
        }
        else if (style.opacity !== undefined) { //All other browsers
            style.opacity = alpha;
        }
        this._currentopacity = alpha;
    },

    _fade_dn: function() {
        var me = this;
        if (AppMode.OS == 'Mac') return;
        if (this._currentopacity > 0.0) {
            this._set_opacity(this._element, this._currentopacity - 0.1);
            window["pagedntimer"] = setTimeout(function() { me._fade_dn(); }, this._fadeout);
        }
    },

    _fade_up: function() {
        var me = this;
        if (AppMode.OS == 'Mac') return;
        if (this._currentopacity < 1.0) {
            this._set_opacity(this._element, this._currentopacity + 0.1);
            window["pageuptimer"] = setTimeout(function() { me._fade_up(); }, this._fadein);
        }
    }
};


// Javascript for handling animations when gotten to by keystrokes

function keyUp(keyevent) {
    var anim;
    var e = event || window.event;
    var code = e.charCode || e.keyCode;

    if (code == 39 || code == 34) anim = $find('AnimPageNext');
    else if (code == 37 || code == 33) anim = $find('AnimPagePrev');
    else return false;
    anim._onClick.play();
}

function initDOM() {
    //if ( !AppMode.GE.Installed || !AppMode.VE.Installed ) document.getElementById('img_install').style.visibility='visible';
    syncGlobes();

    // Set which globe is default, assume VE unless otherwise indicated by AppMode. initOptions may change this.
    useVE = AppMode.VE.Available;

    // Set state of globes
    if (!useVE) swapGlobes();
    swapImages();

    // If we're only using one globe then don't show the glass button to swap
    if (AppMode.GE.Available && AppMode.VE.Available) $get('img_globe').style.display = "block";

    var main = document.getElementById('main');
    var mainPos = findPos(main);

    var tabPos = findPos('tabcol');
    var rightEdge = tabPos.left + 28;

    var sash = document.getElementById('sash_l');
    sash.style.position = "absolute";
    sash.style.zIndex = "0";
    sash.style.top = mainPos.top + 60 + "px";
    sash.style.left = mainPos.left - 96 + "px";
    sash.style.display = "block";

    var sash = document.getElementById('sash_r');
    sash.style.position = "absolute";
    sash.style.zIndex = "0";
    sash.style.top = mainPos.top + 600 + "px";
    sash.style.left = rightEdge - 54 + "px";
    sash.style.display = "block";

}

function showPageSel(page) {
    returnToBook(page);
    bookPages._bClickCover = false;
}

function tocDiv() {
    var pgCount = bookPages._pages.length;
    var html = '<center><div class="copyright">Copyrighted Material</div><img style="padding:8px" src="images/pccoversm.png" border="0" />';
    html += '<br /><span class="tocTitle">Table of Contents</span><br />Sample Chapter</center><br />';
    html += '<div style="margin:12px 0 10px 0; font: bold 8pt arial, sans serif; color:Red">Click a selection:<br /></div>';
    html += '<div id="tocText">'
    html += '<div class="pagesel_new" title="Overview Map" onclick="getPM(35);"><u>Overview Map</u></div>';
    html += '<div class="pagesel_new" title="Front Cover" onclick="showPageSel(0);"><u>Front Cover</u></div>';
    html += '<div>Pages: </div><div class="pageToc">';

    for (var i = 1; i < pgCount - 1; ++i) {
        var p = 33 + i + bookStart;
        if (i == pgCount) html += '<div class="pagesel" title="Page ' + i + '" onclick="showPageSel(' + i + ');">' + p + '</div>';
        else html += '<div class="pagesel" title="Page ' + p + '" onclick="showPageSel(' + i + ');"><u>' + p + '</u><span style="color:#000000">&nbsp;|&nbsp;</span></div>';
    }
    html += '</div><div class="pagesel_new" title="Placemark Index" onclick="showPMIndex();"><u>Placemark Index</u></div>';
    html += '<div class="pagesel_new" title="About the Author" onclick="showPageSel(' + (pgCount - 1) + ');"><u>About the Author</u></div></div>';
    html += '<center><div class="ReturnToBook"><a href="javascript:returnToBook();">Return to Book</a></div></center>';
    return html;
}

function infoRotatorLoad() {
    var anim;
    // Info rotator for IE
    if ((AppMode.Browser.toLowerCase().indexOf('xplorer') > 0 || AppMode.Browser.toLowerCase().indexOf('afari') > 0) && AppMode.OS != 'Mac') {
        rotInfo.rotate();
        anim = $find('AnimRotator')
        anim._onClick.stop();
        anim._onClick.play();
    }
    else {
        // Info rotator for FF and others
        rotInfo.autorotator();
    }
}
