var Gallery = Class.create();
Gallery.prototype = {
    // Constructor
    initialize: function(aAttach) {
        this.aAttach = $A(aAttach);
        
        this.options = Object.extend({
          imgID:               "img-gallery",
          slideElementID:      "slide-arrow",
          firstElementID:      "first-arrow",
          lastElementID:       "last-arrow",
          prevElementID:       "prev-arrow",
          nextElementID:       "next-arrow",
          loadIcon:            "/design/pic/gallery/loading.gif",
          indexImage:          "indexImage",
          toolbarImage:        "PhotoViewerToolbarImg",
          toolbarImageStopSrc:     "/design/pic/gallery/toolbarn.png",
          toolbarImagePlaySrc:     "/design/pic/gallery/toolbar2n.png",
          slideShowInterval:     2000,
          width:100,
          height:100
        	}, arguments[1] || {});
        
        // Event bindings
        this.slideShow = this._slideShow.bindAsEventListener(this);
        if (this.options.firstElementID) this.firstShow = this._firstShow.bindAsEventListener(this);
        if (this.options.lastElementID) this.lastShow = this._lastShow.bindAsEventListener(this);
        this.prevShow = this._prevShow.bindAsEventListener(this);
        this.nextShow = this._nextShow.bindAsEventListener(this);
        
        Event.observe(this.options.slideElementID, "click", this.slideShow);
        if (this.options.firstElementID) Event.observe(this.options.firstElementID, "click", this.firstShow);
        if (this.options.lastElementID) Event.observe(this.options.lastElementID, "click", this.lastShow);
        Event.observe(this.options.prevElementID, "click", this.prevShow);
        Event.observe(this.options.nextElementID, "click", this.nextShow);
        
        // Init data
        this._init();
    },
    
    // Destructor
    destroy: function() {
        Event.stopObserving(this.options.firstElementID, "click", this.firstShow);
        Event.stopObserving(this.options.lastElementID, "click", this.lastShow);
        Event.stopObserving(this.options.prevElementID, "click", this.prevShow);
        Event.stopObserving(this.options.nextElementID, "click", this.nextShow);
    },
    
    /* "Private" functions */
    _init: function() {
        this.isLoading = false;
        this.currentIndex = 0;
        this.slideShow = false;
        this.slideShowHandler = null;
        this.newFoto = new Image();
         if (navigator.appName != "Opera")
         {
           load_img = new Image();
           load_img.setAttribute('src',this.options.loadIcon);
         }        
    },
    
    _prevShow: function(event) {
        if (this.currentIndex <= 0) this.currentIndex = this.aAttach.length-1;
        else this.currentIndex--;
        this._show();	  
        return false;
    },
    
    _nextShow: function(event) {          
        if (this.currentIndex+1 > this.aAttach.length-1) this.currentIndex = 0;
        else this.currentIndex++;
        this._show();	  
        return false;
    },
    
    _firstShow: function(event) {   
        this.currentIndex = 0;             
        this._show(this.currentIndex);	  
        return false;
    },
    
    _lastShow: function(event) {                
        this.currentIndex = this.aAttach.length-1;             
        this._show();	  
        return false;
    },
    
    _circleShow: function(event) {                  
        if (this.currentIndex+1 > this.aAttach.length-1) this.currentIndex = 0;
        else this.currentIndex++;
        this._show();	  
        return false;
    },
    
    _slideShow: function(event) {                
        if (this.slideShow)
        {
            $(this.options.toolbarImage).src = this.options.toolbarImageStopSrc;
            if (this.slideShowHandler) clearInterval(this.slideShowHandler);
            this.slideShow = false;
        }
        else
        {
            $(this.options.toolbarImage).src = this.options.toolbarImagePlaySrc;
            if (this.slideShowHandler) clearInterval(this.slideShowHandler);
            this.slideShowHandler = setInterval(this._circleShow.bind(this), this.options.slideShowInterval);
            this.slideShow = true;
        }
        return false;
    },
    
    _show: function() {     
        
        if(this.isLoading) return;
        this.isLoading = true;
        $(this.options.indexImage).update('<img src="'+this.options.loadIcon+'" width="16" height="16" />');
        $(this.options.imgID).style.border = 0;
        this.newFoto.setAttribute('src', this.aAttach[this.currentIndex]['src']);
        this.newFoto.setAttribute('alt', this.aAttach[this.currentIndex]['title']);
        this.newFoto.setAttribute('title', this.aAttach[this.currentIndex]['title']);
        //this.newFoto.setAttribute('width', this.aAttach[this.currentIndex]['width']);
        //this.newFoto.setAttribute('height', this.aAttach[this.currentIndex]['height']);
        this.newFotoListener = this._setFoto.bindAsEventListener(this);
        Event.observe(this.newFoto, "load", this.newFotoListener);
        
        return false;
    },
    
    _setFoto: function()
    {
        this.isLoading = false;
        this.imSize = this._getImageSize(this.newFoto);
        this.imNewSize = this._calcSize(this.imSize[0], this.imSize[1]);
        $(this.options.imgID).style.width = this.imNewSize[0]+'px';
        $(this.options.imgID).style.height = this.imNewSize[1]+'px';
        $(this.options.imgID).src = this.newFoto.src;
        $(this.options.imgID).parentNode.href = this.newFoto.src;
        $(this.options.imgID).parentNode.title = this.newFoto.title;
        $(this.options.imgID).alt = this.newFoto.alt;
        $(this.options.imgID).title = this.newFoto.title;
        $(this.options.indexImage).update(this.aAttach[this.currentIndex]['title']+' '+(this.currentIndex+1)+'/'+this.aAttach.length);
        return false;
    },
    
    _getImageSize: function(id) {
        var oHlpr = document.createElement('IMG');
        var oPic = $(id);
        oHlpr.style.visibility = 'hidden';
        oHlpr.style.position = 'absolute';
        oHlpr.top = 0; oHlpr.left = 0;
        oHlpr.src = oPic.src;
        document.body.appendChild(oHlpr);
        var imSize = [oHlpr.offsetWidth,oHlpr.offsetHeight];
        document.body.removeChild(oHlpr);
        return imSize;
    },
    
    _calcSize: function (nW, nH)
    {
        var w  = nW;
        var h  = nH;

        if (nW > this.options.width)
        {
            w  = this.options.width;
            h  = Math.floor(nH * this.options.width/nW);
            if (h > this.options.height)
            {
              h  = this.options.height;
              w  = Math.floor(nW * this.options.height/nH);
            }
        }
        else if (nH > this.options.height)
        {
            h  = this.options.height;
            w  = Math.floor(nW * this.options.height/nH);            
        }

        return [w, h];
    }
}