
var gallery = {

    add_events : function()
    {
        var elm = $('gallery_next');
        if(elm != null) elm.addEvent('click',gallery.swap_next);
        var elm = $('gallery_previous');
        if(elm != null) elm.addEvent('click',gallery.swap_previous);
        //var elm = $('gallery_large');
        //if(elm != null) elm.addEvent('click',gallery.large);
    },
    /*
    large : function (e)
    {
        //detach default onclick event
        var event=new Event(e);
        event.stop();
        
        //get our images
        var aElm = gallery.get_images();
                
        //check which one is current        
        var current = gallery.get_current(aElm);

        var theElm = aElm[current];        
	    
        //do everything smoothbox way after here...
        // remove click border
	    theElm.blur();
	    // get caption: either title or name attribute
	    var caption = theElm.title || theElm.name || "";
	    // get rel attribute for image groups
	    var group = theElm.rel || false;
	    // display the box for the elements href
	    TB_show(caption, theElm.href, group);
	    theElm.onclick=TB_bind;
	    return false;
    },
    */
    swap_next : function(e)
    {
        gallery.swap(e,'next');
    },
    
    swap_previous : function(e)
    {
        gallery.swap(e,'next');
    },
    
    swap : function(e,direction)
    {
        //detach default onclick event
        var event=new Event(e);
        event.stop();

        //get our images
        var aElm = gallery.get_images();
                
        //check which one is current        
        var current = gallery.get_current(aElm);

        //found current now update display        
        aElm[current].style.display = 'none';

        //now inc one way
        if(direction == 'next')
        {
            current ++;
        }
        else
        {
            current --;
        }
        var len = aElm.length;
        if(current==len) current = 0;
        if(current<0) current = len-1;
        
        //found new current now update display
        aElm[current].style.display = '';
        
    },
    
    get_images : function()
    {
        //first get all our image elements
        var divElm = $('image_gallery');
        //now cycle through all a tags
        var aElm = divElm.getElements('a');
        
        return aElm;
    },
    
    get_current : function(aElm)
    {
        //check which one is current        
        var current = null;
        aElm.each(function(elm,index){
            if(elm.style.display!='none')
            {
                current = index;
            }    
        });
        if(current == null)
        {
            alert('expecting an image?');
            return 0;
        }
        return current;
    }
}

window.addEvent('load',gallery.add_events);


