function slideshow()
{   
	this.slider_timer = 0;
	this.slide_speed  = 7; //number of seconds
    this.auto_run 	  = 1;
    
	this.page = 1; 
	this.xml = '';

	this.load_xml = function(xml)
    {	
        var urlString = '';
        var myConn = new XHConn();
        if (!myConn) alert("XMLHTTP not available. Try a newer/better browser.");
        
        var fnWhenDone = function(oXML){ slide.xml2obj(oXML.responseText); slide.load_slides(); }

        myConn.connect("_includes/"+xml, "GET", urlString, fnWhenDone);
    }
    
    this.xml2obj = function(xml)
    {
    	this.xml = xml2json.parser(xml);
    }
    
    this.load_slides = function()
    {
    	var buttons = document.getElementById('buttons');
        
    	pages = slide.xml.pages.page;
        
    	//remove prev
        document.getElementById('slide_prev').style.visibility = 'hidden';
        
        this.page = 1;
        
    	//load buttons
        for(var i = 0; i < (pages.length > 6 ? 6 : pages.length); i++)
        {
    		span = document.createElement('span');
        	span.id = pages[i].id;
            span.className = (i == 0 ? 'button on' : 'button off');
            span.onclick = function(){ slide.goto(this.id); }
            
            span_content = document.createTextNode(i+1);
            span.appendChild(span_content);
            
            buttons.insertBefore(span, document.getElementById('slide_next'));
		}
    	
    	//load first slide
        slide.load_page( pages[0].name );
        if( this.auto_run ) slide.start('next');
    }
    
    this.clear_slides = function()
    {
    	var buttons = document.getElementById('buttons').getElementsByTagName('span');
        button_len = buttons.length-1;
        
        for(var i = 1; i < button_len; i++)
        {
        	document.getElementById('buttons').removeChild(buttons[1]);
        }
    }
    
    this.load_page = function(page)
    {
    	var urlString = '';
        var myConn = new XHConn();
        if (!myConn) alert("XMLHTTP not available. Try a newer/better browser.");
        
        var fnWhenDone = function(oXML){ document.getElementById('content').innerHTML = oXML.responseText; }

        myConn.connect("_pages/"+page, "GET", urlString, fnWhenDone);
    }
}

function start(direction)
{
	this.slider_timer = (direction == 'next' ? setInterval("slide.next()", this.slide_speed*1000) : setInterval("slide.prev()", this.slide_speed*1000));
}

function stop()
{
	clearInterval ( this.slider_timer );
}

function prev()
{  
   	pages = this.xml.pages.page;

	if( this.auto_run )
    {
    	slide.stop();
        slide.start('prev');
    }

	if( this.auto_run && parseInt(this.page) == 1 )
    { 
        this.page += 1;
        document.getElementById(parseInt(this.page)).className = 'button on';
        document.getElementById(parseInt(this.page)-1).className = 'button off'; 
        
        slide.goto(this.page);
        slide.stop();
        slide.start('next');

        return;
	}

	if( !document.getElementById(parseInt(this.page) - 1) ){ slide.shift('right'); return; }
    
    slide.goto(this.page - 1);
}

function next()
{
	pages = this.xml.pages.page;	

	if( this.auto_run )
    {
    	if( this.page == pages.length )
        {
        	if( pages.length == 6 )
            {        	   
                slide.goto(1);
                return;
            }
            else
            {
            	document.getElementById('slide_next').style.visibility = 'visible';
                
            	slide.stop();
        		slide.clear_slides();
        		slide.load_slides();
                return;
			}
        }
        else
        {
    		slide.stop();
        	slide.start('next');
        }
    }

	if( this.auto_run && parseInt(this.page) == pages.length )
    { 
    	if( !document.getElementById(parseInt(this.page) + 1) )
        {
        	this.page -= 1;
            document.getElementById(parseInt(this.page)).className = 'button on';
            document.getElementById(parseInt(this.page)+1).className = 'button off'; 
            
            slide.goto(this.page);
            slide.stop();
            slide.start('prev');
        }
        else
        {
            this.page = 1; 
            document.getElementById(this.page).className = 'button on';
            document.getElementById(pages.length).className = 'button off'; 
            
            slide.goto(1);
            slide.stop();
            slide.start('next');
        }
        
        return;
	}

	if( !document.getElementById(parseInt(this.page) + 1) ){ slide.shift('left'); return; }

    slide.goto(this.page + 1);
}

function shift(direction)
{
	pages = this.xml.pages.page;
    
    var buttons = document.getElementById('buttons').getElementsByTagName('span');
    
    //remove all buttons at once
    for(var i = 1; i <= 6; i++){ document.getElementById('buttons').removeChild(buttons[1]); }
    
    //check direction and put in new buttons
	if( direction == 'left' ){ new_page = parseInt(this.page)+1; counter = (parseInt(this.page)+1)-6; }
    else{ new_page = parseInt(this.page)-1; counter = parseInt(this.page)-2; }
    
    var buttons = document.getElementById('buttons');
    for(var i = 1; i <= 6; i++)
    {                 
        //create new button
        span = document.createElement('span');
        span.id = pages[counter].id;
        span.className = (counter == new_page ? 'button on' : 'button off');
        span.onclick = function(){ slide.goto(this.id); }
        span_content = document.createTextNode(pages[counter].id);
        span.appendChild(span_content);

        //add new button in its place
        buttons.insertBefore(span, document.getElementById('slide_next'));

        counter++;
    }
     
    slide.goto(new_page);
}

function goto(num)
{    
	pages = this.xml.pages.page;
    
    //change next/prev
    document.getElementById('slide_prev').style.visibility = ( num-1 <= 0 ? 'hidden' : 'visible' );
    document.getElementById('slide_next').style.visibility = ( num >= pages.length ? 'hidden' : 'visible');
    
    //change button
    if( document.getElementById(this.page) ) document.getElementById(this.page).className = 'button off';
    document.getElementById(num).className = 'button on';
    
    this.page = parseInt(num);
   	slide.load_page( pages[this.page-1].name );
    //this.start();
}

slideshow.prototype.start  = start;
slideshow.prototype.stop   = stop;
slideshow.prototype.prev   = prev;
slideshow.prototype.next   = next;
slideshow.prototype.goto   = goto;
slideshow.prototype.shift  = shift;

var slide = new slideshow();
if(window.attachEvent)
{
    window.attachEvent("onload",function(){
	slide.load_xml('xml.slideshow.php');
    document.getElementById('slide_prev').onclick = function(){ slide.prev(); if(slide.auto_run){ slide.stop(); slide.start('prev'); } };
    document.getElementById('slide_next').onclick = function(){ slide.next(); if(slide.auto_run){ slide.stop(); slide.start('next'); } };
    });
}
else
{
    window.addEventListener("load", function(){ 
	slide.load_xml('xml.slideshow.php');
    document.getElementById('slide_prev').onclick = function(){ slide.prev(); if(slide.auto_run){ slide.stop(); slide.start('prev'); } };
    document.getElementById('slide_next').onclick = function(){ slide.next(); if(slide.auto_run){ slide.stop(); slide.start('next'); } };
    },false);
}