if(!garant){
    var garant = {};
}
var log = window.console ? window.console.log : function(){};

var props = function(obj){
    for(var i in obj){
        log(i, obj[i]);
    }
};

/*
 * Garant gallery. Consist of a thumbnails, a simple slide show, and slimbox.
 */
garant.gallery = function(){
    
    var that = {};

    /**
     * Attach click handlers to the elements.
     * 
     * @param {List MootoolsElm} is the elements to attach click handler to.   
     */
    that.addClickHandler = function(elements, handler){
        for(var i = 0; i < elements.length; i++){
            log(elements[i].addEvent('click', handler));
        }
    };

    /*
     * Setup slimbox on all links in #images
     */
    that.setupSlimBox = function(){
        var options = {
            // false uses the mootools default transition
	    resizeTransition: false,		
	    counterText: "Billede {x} af {y}"	
        };
        // slimbox on links
        $$('#images a').slimbox(options);
    };
    
    /**
     * Attach click handlers to thumbnails. On thumbnail click, set
     * as current image in slideshow.
     * 
     * @param {List MootoolsElm} thumbnails is the elements to attach 
     * the handler to.
     */
    that.setupThumbnailsClickHandlers = function(thumbnails){
        var updateMainImage = function(element){
            var thumbnailId = element.target.id;
            var number = thumbnailId.split('thumbnail')[1];
            that.slideShow.show(number);
        };
        that.addClickHandler(thumbnails, updateMainImage);
    };
    
    /*
     * Setup simple image slide show.
     */
    that.setupSlideShow = function() {
        that.slideShow = new SimpleImageSlideShow(
            $('mainImage'), {
	        startIndex: 0,
	        slides: $$('#images a img'),
	        currentIndexContainer: 'imgCount',
	        maxContainer: 'imgMax',
	        nextLink: 'next',
	        prevLink: 'prev'
	    });

        // overwrite the setCounters  method from clientcide, it doesn't set the 
        // slide number correctly.
        that.slideShow.setCounters = function(){
            $(that.slideShow.options.currentIndexContainer).set('html', new Number(this.now) + 1);
	    $(that.slideShow.options.maxContainer).set('html', this.slides.length);
        };
    };

    that.init = function(){
        var thumbnails = $$('#thumbnails img');

        that.setupSlideShow();
        that.setupSlimBox();
        that.setupThumbnailsClickHandlers(thumbnails);
        
        return that;        
    };

    return that.init();
};

// called from the page
// window.addEvent('load', garant.gallery);		
// );		
