// if debug=true, then use console for debugging
var _debug = false

// auto-flip pages
var _auto = false

// define the chapters
var _firstFrames = new Array (0, 1, 6, 11, 16);
//lastFrames = new Array  (0, 5, 10, 14, 20);
var _lastFrames = new Array  (0, 1, 6, 11, 16); 

var _currChapter = 1;
var _currPage = _firstFrames[1];

var _timeOutFn = null

// used for the number 1 2 3 4 5 
function addPointer() { $(this).addClass("cursor_pointer"); }
function removePointer() { $(this).removeClass("cursor_pointer"); }

// the 'true' in all these functions signifies that this page change is 'manual' as opposed to triggered by the auto-flip

function showChapter1() { show_chapter(1, true); return false; }
function showChapter2() { show_chapter(2, true); return false; }
function showChapter3() { show_chapter(3, true); return false; }
function showChapter4() { show_chapter(4, true); return false; }

function showPage1() { show_page(_currChapter, 1, true); return false; }
function showPage2() { show_page(_currChapter, 2, true); return false; }
function showPage3() { show_page(_currChapter, 3, true); return false; }
function showPage4() { show_page(_currChapter, 4, true); return false; }
function showPage5() { show_page(_currChapter, 5, true); return false; }

function init_infobox() {

    // prepare the content
    // autoDelay is the standard page flip time, manualDelay is the page flip after the user manually changes chapter,
    // loopDelay is the delay from the last chapter back to the first
    $('#infoContent').infobox({ autoDelay: 10000, manualDelay: 20000, loopDelay: 20000 });

    // chapter selectors 
    $('#infoboxChapter1').click(showChapter1).hover(addPointer, removePointer);
    $('#infoboxChapter2').click(showChapter2).hover(addPointer, removePointer);
    $('#infoboxChapter3').click(showChapter3).hover(addPointer, removePointer);
    $('#infoboxChapter4').click(showChapter4).hover(addPointer, removePointer);


    $('#F7rUaTty59E').dialog({ autoOpen:false, width:670, height: 575, modal: true, closeOnEscape: true, position: ['center', 20]});
}

function video_popup() {
    $('#F7rUaTty59E').dialog("open");
}

(function($){  

    $.fn.infobox = function(vars) {
            pictures    = $("#infoContent .infoItem img");
            texts       = $("#infoContent .infoItem p");
                
            autoDelay     = vars.autoDelay; // auto page switching
            manualDelay   = vars.manualDelay; // after a manual page switch
            loopDelay     = vars.loopDelay; // after reaching the last page

            show_chapter(1);

            // start timer for first _timeOutFn
            if (_auto) 
                _timeOutFn = setTimeout(onTimeout, autoDelay);
            
            }

})(jQuery);  


function setManualTimeout() { 
    if (_auto) {
        clearTimeout(_timeOutFn);
        _timeOutFn = setTimeout(onTimeout, manualDelay);
    }
}

function onTimeout() {
    
    // the delay before calling this again
    var delay = autoDelay;

    // timer has elapsed, flip to next page
    var pagesInChapter = _lastFrames[_currChapter] - _firstFrames[_currChapter] + 1

    // if we're at the end of the chapter, then choose the next one
    // we use >= here because the chapter may not start at page 1
    if (_currPage >= pagesInChapter) {

        // if we're at the last chapter, then long delay
        if (_currChapter == 4) 
            var delay = loopDelay;

        // show the next chapter
        show_chapter( (_currChapter%4)+1, false)

    } else {

        _currPage = (_currPage%pagesInChapter)+1
        show_page(_currChapter, _currPage, false);
    }

    if (_debug) {
        try {
            console.log("onTimeout currpage = "+_currPage + " delay = "+delay);
        } catch(e) {}
    }

    if (_auto) 
        _timeOutFn = setTimeout(onTimeout, delay);
}

function show_page(chapter, page, manual) {

    // if the user manually changes page, then she deosn't want the slideshow to work for a while,
    // in that case we reset the current timer, and set off a new, longer one instead
    // the second picture after that resumes the normal slide speed
    if (manual) 
        setManualTimeout();
    
    _currPage = page;

    if (_debug) {
        try {
        console.log("show_page : chapter, page, manual "+chapter+" "+page+" "+manual);    
        } catch(e) {}
    }

   // set the title
    var title = $("#infoTitle"+_currChapter+"-"+page).text();
    $("#infoboxTitle h2").text(title);    

    // set the text 
    var html = $("#infoText"+_currChapter+"-"+page).html();
    $("#infoboxText").html(html);

    // change the picture
    var src = $("#infoPicture"+_currChapter+"-"+page).attr("src");
    $("#infoboxPicture1").attr({"src":src});

    var bg = $("#infoboxChapter"+chapter).css("background-color");
    //$("#infoboxPage"+page).css({'color': '#fd9e00'})        

}

function show_frame(chapter, frame, manual) {

    // the actual chapters are each 5 pages long
    // we have since removed some from view, but the graphics are still there
    var page = frame-(chapter-1)*5;
    show_page(chapter, page, manual);
}

function show_chapter(chapter, manual) {

    _currChapter = chapter;
    currFrame = _firstFrames[chapter];

    // highlight the current tab
    for (var i=1; i<=4; i++) {
        if (chapter == i)
            $("#infoboxChapter"+chapter).addClass("current");
        else
            $("#infoboxChapter"+i).removeClass("current");
    }

    // set the action text
    var action = $("#infoChapter"+_currChapter+"ActionLink").text();
    $("#infoboxActionLink").text(action);   

    // set the action link
    var source_link = $("#infoText"+_currChapter+"-1"+" .infoActionLink");

    // update link attributes
    $("#infoboxActionLink").attr('href', source_link.attr("href"));  
    $("#infoboxActionLink").attr('title', source_link.attr("title"));  
    $("#infoboxActionLink").attr('alt', source_link.attr("title"));  

    // set the picture link if specified, else same as action
    var source_pic = $("#infoText"+_currChapter+"-1"+" .infoPictureLink");

    if (source_pic.attr("href")) {
        var pic_href = source_pic.attr("href");
        var pic_title = source_pic.attr("title");
    } else {
        pic_href = source_link.attr("href");
        pic_title = source_link.attr("title");
    }

    $("#infoboxPictureLink").attr('href', pic_href);  
    $("#infoboxPictureLink").attr('title', pic_title);  
    $("#infoboxPictureLink").attr('alt', pic_title);  


    // activate/disactivate the popup functionality
    var pic = $('a#infoboxPictureLink'); 

    pic.unbind("click", video_popup);
    if (_currChapter == 1) {
        pic.bind("click", video_popup);
    }

    show_frame(chapter, currFrame, manual);
}











