window.DOMLoadManager = window.DOMLoadManager || new (new Class({
    pages : {},
    initialize : function()
    {
        window.addEvent("domready", this.executeCallbacks.bind(this));
    },
    register : function(page, callback)
    {

        if ((typeof page != "string") && !(page instanceof Array))
        {
            return false;
        }
        if (typeof page == "string")
        {
            if (page.indexOf(",") != -1)
            {
                page = page.split(",");
            }
        }

        if (page instanceof Array)
        {
            for (i = 0; i < page.length; i++)
            {
                this.addToStack(page[i], callback);
            }
        }
        else
        {
            this.addToStack(page, callback);
        }

    },
    addToStack : function(id, callback)
    {
        if (typeof( this.pages[id] ) == 'undefined')
        {
            this.pages[id] = [];
        }
        this.pages[id].push(callback);
    }.protect(),
    executeCallbacks : function()
    {
        var current_page = document.getElementsByTagName('body')[0].getAttribute('id');
        if (current_page != false && typeof( this.pages[current_page] ) != 'undefined')
        {
            this.runIt(this.pages[current_page]);
        }
        if (typeof( this.pages['*'] ) != 'undefined')
        {
            this.runIt(this.pages['*']);
        }
    },
    runIt : function(arr)
    {
        if (! ( arr instanceof Array ))
        {
            return;
        }
        for (var i = arr.length - 1; i >= 0; i--)
        {
            arr[i]();
        }
    }.protect()
}))();

ICE = (function(){
	return {
		Carousel : new Class({
			initialize: function(target){
				this.carouselContainer = $(target);
				this.carouselRail = this.carouselContainer.getElement(".carousel-box-inner");
				this.count = 0;
				this.forward = true;
				this.startup();
			},
			startup : function(){
				this.width = this.carouselContainer.getSize().x;
				this.slides = this.carouselContainer.getElements(".carousel-slide");
				this.slides.each(function(item,index){
					item.setStyles({left : index*this.width+"px"});
				}.bind(this));
				this.move.periodical(3500,this);
			},
			move : function(){
				
				if(this.forward){
					this.count++;
				}else{
					this.count--;
				}
				
				if(this.count>=this.slides.length-1){
					this.forward = false;
				}
				if(this.count==0){
					this.forward = true;
				}
				this.carouselRail.tween("margin-left",-(this.count*this.width));
			}
		}),
		Gallery : new Class({
			initialize : function(target,linkRoot){
				this.target = $(target);
				this.linkRoot = $(linkRoot);
				this.links = this.linkRoot.getElements("a");
				this.links.each(function(item,index){
					var link = item.get("href");
					item.addEvent("click",function(e){
						e.stop();
						this.fetch(link);						
					}.bind(this));
				}.bind(this));
			},
			fetch : function(link){
				var url = link;
				if(url != ""){
					this.target.load(url);
				}
			}
		})
	}
})();
window.DOMLoadManager.register("home",function(){
	new ICE.Carousel("homepageCarousel");
});
window.DOMLoadManager.register("gallery",function(){
	new ICE.Gallery("ajaxUpdate","galleryPicker");
});


