/**
 * This file is part of xmlpres.
 *
 * xmlpres is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * xmlpres is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with xmlpres; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * You may contact the author by mail:
 * jakob@westhoffswelt.de
 *
 * Or write to:
 * Jakob Westhoff
 * Kleiner Floraweg 35
 * 44229 Dortmund
 * Germany
 *
 * The latest version of xmlpres can be obtained from:
 * svn://svn.pureenergy.cc/xmlpres
 */


var JXmlpres = function() 
{
    var numSlides = 0;
    while ( document.getElementById( 'slide' + numSlides ) != undefined ) 
    {
        numSlides++;
    }
    this.numSlides = numSlides;

    this.showPage( 0 );
    this.registerEventHandler();

    // Initialize external syntax highlighter
    dp.SyntaxHighlighter.HighlightAll('code');
}

JXmlpres.prototype.registerEventHandler = function() 
{
    var self = this;
    var handler = function( event ) 
    {
        var key = null;
        if( document.all ) //ie
        { 
            key = window.event.keyCode;
        } 
        else // not ie
        {
            key = event.which;
        }
        self.onKeyPress.call( self, key );
    }

    if( !document.all ) // not ie
    {
        window.captureEvents( Event.KEYUP );
    }
    else //ie
    {
        document.onkeypress = handler;
    }
    document.onkeyup = handler;
}

JXmlpres.prototype.onKeyPress = function( key ) 
{
    switch( key ) 
    {
        case 37: //left arrow
            this.showPage( this.currentPage - 1 );
        break;
        case 39: //right arrow
            this.showPage( this.currentPage + 1 );
        break;
    }
}

JXmlpres.prototype.showPage = function( page )
{
    if ( page < 0 ) 
    {
        page = 0;
    }

    if ( page >= this.numSlides ) 
    {
        page = this.numSlides - 1;
    }

    for( var i=0; i<this.numSlides; i++ ) 
    {
        var el = document.getElementById( 'slide' + i );

        if ( i == page ) 
        {
            el.style.display = "block";
            continue;
        }
        el.style.display = "none";
    }

    this.currentPage = page;
}
