
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)
		{
			gallery.prep_large;
			elm.addEvent('click',gallery.large);
		}
    },

	prep_large: function ()
	{
		//replace hyperlink and add slimbox
		$('gallery_large').href = $('image_link').href;
		$('gallery_large').rel = 'lightbox';
	},

	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_container');
        //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);

window.addEvent('domready',function(){
	if (Browser.Engine.trident)
	{
		$$('#product_preview a span.image img').each(function(el){
			var href = el.getParent().getParent().href;
			el.addEvent('click', function(e){
				e = new Event(e).stop();
				window.location = href;
			});
		});
	}
});