//function to divide article into pages, fires onload
function paginate() {
    var tempAry = document.getElementsByTagName( "div" ); //get all divs in document

    //iterate through divs; if has class of "page," add to pages array
    for( var i = 0; i < tempAry.length; i++ ) {
        if( tempAry[ i ].className.indexOf( "page" ) > -1 ) {
            pages.push( tempAry[ i ] );
        }
    }

    //create page controls if there are multiple pages
    var body = document.getElementsByTagName( "body" ).item( 0 ); //get reference to page body
    var controlDiv = document.createElement( "div" );

    controlDiv.className = "page_control";

    if( pages.length > 1 ) {
        //create control for previous page
        var prevLink = document.createElement( "a" ); //link to previous page

        prevLink.href = "javascript:prev()";
        prevLink.id = "ctl_prev";
        prevLink.appendChild( document.createTextNode( "\u00ab back" ) );
        controlDiv.appendChild( prevLink );
        controlDiv.appendChild( document.createTextNode( " | " ) );

        //create links to individual pages
        for( var i = 1; i <= pages.length; i++ ) {
            var link = document.createElement( "a" );
            var text = document.createTextNode( i );

            link.href = "javascript:moveTo( " + i + " )";
            link.id = "ctl_link" + i;
            link.appendChild( text );
            controlDiv.appendChild( link );

            //add separator between links
            controlDiv.appendChild( document.createTextNode( " | " ) );
        }

        //create control for next page
        var nextLink = document.createElement( "a" ); //link to next page

        nextLink.href = "javascript:next()";
        nextLink.id = "ctl_next";
        nextLink.appendChild( document.createTextNode( "next \u00bb" ) );
        controlDiv.appendChild( nextLink );
    }

    //create email and print controls
    var ctlP = document.createElement( "p" ); //holds control links
    var emailLink = document.createElement( "a" ); //link for email
    var printLink = document.createElement( "a" ); //link for printing

    ctlP.style.textAlign = "right";
    controlDiv.appendChild( ctlP );

    emailLink.href = "/includes/SendLink.aspx";
    emailLink.target = "_blank";
    emailLink.id = "ctl_email";
    emailLink.appendChild( document.createTextNode( "e-mail this story" ) );
    emailLink = ctlP.appendChild( emailLink );
    addEvent( emailLink, "click", function( e ) { window.open( '/includes/SendLink.aspx', '', 'width=500,height=550,scrollbars=yes' ); if( e.preventDefault ) { e.preventDefault(); } else { e.returnValue = false } } );

    ctlP.appendChild( document.createTextNode( " | " ) );

    printLink.href = "javascript:window.print()";
    printLink.id = "ctl_print";
    printLink.appendChild( document.createTextNode( "print this article" ) );
    ctlP.appendChild( printLink );

    //attach to document
    document.getElementById( "content" ).appendChild( controlDiv );

    //move to first page of article
    moveTo( 1 );
}

//shows and hides pages as required
function togglePageVisibility( page, state ) {
    try {
        if( state == "show" ) {
            page.style[ "display" ] = "block";
        } else if( state == "hide" ) {
            page.style[ "display" ] = "none";
        }
    } catch( e ) {
        alert( "Page visibility toggle failed because of error: " + e );
    }
}

//add CSS class name to object
function addClassName( page, cl ) {
    if( page ) {
        if( page.className != "" ) {
            page.className += " " + cl;
        } else {
            page.className = cl;
        }
    }
}

//remove CSS class name from object
function removeClassName( page, cl ) {
    var name = new RegExp( "\\b" + cl + "\\b" );

    if( name.test( page.className ) ) {
        page.className = page.className.replace( name, "" );
    }
}

//moves article to designated page (parameter is integer)
function moveTo( num ) {
    num--;
	window.scroll( 0, 0 );

    //make sure page is within bounds
    if( num > -1 && num < pages.length ) {
        var n = document.getElementById( "ctl_next" );
        var p = document.getElementById( "ctl_prev" );

        for( var i = 0; i < pages.length; i++ ) {
            var link = document.getElementById( "ctl_link" + ( i + 1 ) );

            if( i == num ) {
                togglePageVisibility( pages[ i ], "show" );
                addClassName( link, "selected" );
            } else {
                togglePageVisibility( pages[ i ], "hide" );
                removeClassName( link, "selected" );
            }
        }

        if( num == ( pages.length -1 ) ) {
            addClassName( n, "hidden" );
        } else {
            removeClassName( n, "hidden" );
        }
        if( num == 0 ) {
            addClassName( p, "hidden" );
        } else {
            removeClassName( p, "hidden" );
        }

        //update current page
        current = num + 1;
    }
}

//moves article to next page, stops at end
function next() {
    moveTo( current + 1 );
}

//moves article to previous page, stops at beginning
function prev() {
    moveTo( current - 1 );
}

//declare global variables
var pages = new Array(); //array of references to page divs
var current = 0; //holds number of current page
var pageLoad = new Object(); //event listener for page load

// create event listener for page onload so as not to conflict with other onload handlers
addEvent( window, "load", paginate );