/**
 * LightBox3dot
 * 
 * @author Vladimir Shushkov <vladimir@shushkov.ru>
 */

var LightBox3dot = {
    
    init: function(options) {
        
        var collections = {
            __common__: []
        };
        var popUp = new LightBox3dot.PopUp;
        
        var showCollection = function(event) {
            Event.stop(event);
            popUp.close();
            
            if (this.rel.indexOf('lightbox[') !== -1) {
                popUp.setCollection(collections[this.collectionName], this.collectionIndex);
            } else {
                popUp.setCollection([{url: this.href, title: this.title}], this.collectionIndex);
            }
            popUp.start();
        };
        
        $$('a[rel^=lightbox]').each(function(item) {
            if (!item.rel || item.rel.indexOf('lightbox') === -1) {
                return;
            }
            var isCommon = item.rel.indexOf('lightbox[') === -1;
            var collectionName = isCommon ?  '__common__' : item.rel;
            if (collections[collectionName] === undefined) {
                collections[collectionName] = [];
            }
            item.collectionName = collectionName;
            item.collectionIndex = isCommon ? 0 : collections[collectionName].length;
            item.observe('click', showCollection);
            
            collections[collectionName].push({url: item.href, title: item.title});
        });
        
    }
    
};

LightBox3dot.PopUp = Class.create({
    
    initialize: function() {
        this._col = [];
        this._i = 0;
        this._box = null;
        this._img = null;
        this._imgBox = null;
        this._descBox = null;
        this._btnPrev = null;
        this._btnNext = null;
    },
    
    setCollection: function(collection, index) {
        this._col = collection;
        if (index !== undefined) {
            this._i = parseInt(index);
        }
        this._tweakBtn();
    },
    
    getCurrentItem: function() {
        return this._col[this._i];
    },
    
    close: function() {
        this.empty();
        this.getBox().style.width = '';
        this.getImgBox().style.height = '';
        if (this._box !== null && this._box.parentNode) {
            this.getBox().parentNode.removeChild(this.getBox());
        }
    },
    
    start: function(value) {
        document.body.appendChild(this.getBox());
        this._show();
        
    },
    
    _show: function(value) {
        this._setPreload(true);
        this._center();
        this._setDesc(this.getCurrentItem().title);
        this.getImg().onload = this._hImageLoad.bindAsEventListener(this);
        this.getImg().src = this.getCurrentItem().url;
    },
    
    _resize: function() {
        this.getBox().style.width = this.getImg().offsetWidth +'px';
        this.getImgBox().style.height = this.getImg().offsetHeight +'px';
        this.getBtnPrev().style.height =
        this.getBtnNext().style.height = this.getImg().offsetHeight +'px';
        this._center();
    },
    
    _center: function() {
        this.getBox().style.marginTop = '-'+ (this.getBox().offsetHeight / 2) +'px';
        this.getBox().style.marginLeft = '-'+ (this.getBox().offsetWidth / 2) +'px';
    },
    
    _setPreload: function(state) {
        this.getBox().firstChild.className = state == true ? 'wrapBox preload' : 'wrapBox';
    },
    
    _tweakBtn: function() {
        this.getBtnPrev().style.display = this._i === 0 ? 'none' : 'block';
        this.getBtnNext().style.display = this._i === (this._col.length - 1) ? 'none' : 'block';
    },
    
    _setDesc: function(value) {
        if (!value) {
            this.getDescBox().innerHTML = '';
            this.getDescBox().style.display = 'none';
        } else {
            this.getDescBox().innerHTML = value;
            this.getDescBox().style.display = 'block';
        }
    },
    
    getBox: function() {
        if (this._box === null) {
            this._box = document.createElement('div');
            this._box.className = 'lightBox3dot';
            
            var wrap = this._box.appendChild(document.createElement('div'));
            wrap.className = 'wrapBox';
            
            var btnClose = wrap.appendChild(document.createElement('div'));
            btnClose.className = 'btnClose';
            
            var navLinks = wrap.appendChild(document.createElement('div'));
            navLinks.className = 'navLinks';
            
            this._btnPrev = navLinks.appendChild(document.createElement('a'));
            this._btnPrev.href = '#';
            this._btnPrev.className = 'btnPrev';
            this._btnPrev.style.display = 'none';
            
            this._btnNext = navLinks.appendChild(document.createElement('a'));
            this._btnNext.href = '#';
            this._btnNext.className = 'btnNext';
            this._btnPrev.style.display = 'none';
            
            this._imgBox = wrap.appendChild(document.createElement('div'));
            this._imgBox.className = 'imageBox';
            
            this._img = this._imgBox.appendChild(document.createElement('img'));
            
            this._descBox = wrap.appendChild(document.createElement('div'));
            this._descBox.className = 'description';
            this._descBox.style.display = 'none';
            
            $(this._btnPrev).observe('click', this._hBtnPrev.bindAsEventListener(this));
            $(this._btnNext).observe('click', this._hBtnNext.bindAsEventListener(this));
            $(btnClose).observe('click', this._hBtnClose.bindAsEventListener(this));
			$(this._img).observe('click', this._hBtnClose.bindAsEventListener(this));
        }
        return this._box;
    },
    
    getBtnPrev: function() {if (this._btnPrev === null) {this.getBox();}return this._btnPrev;},
    getBtnNext: function() {if (this._btnNext === null) {this.getBox();}return this._btnNext;},
    getImgBox: function() {if (this._imgBox === null) {this.getBox();}return this._imgBox;},
    getImg: function() {if (this._img === null) {this.getBox();}return this._img;},
    getDescBox: function() {if (this._descBox === null) {this.getBox();}return this._descBox;},
    
    empty: function() {
        this._col = [];
        this._i = 0;
    },
    
    _hImageLoad: function(event) {
        this._img.onLoad = null;
        this._setPreload(false);
        this._resize();
    },
    
    _hBtnPrev: function(event) {
        Event.stop(event);
        if (this._i <= 0) {
            return;
        }
        this._i--;
        this._setDesc();
        this._tweakBtn();
        this._show();
    },
    
    _hBtnNext: function(event) {
        Event.stop(event);
        if (this._i >= (this._col.length - 1)) {
            return;
        }
        this._i++;
        this._setDesc();
        this._tweakBtn();
        this._show();
    },
    
    _hBtnClose: function(event) {
        Event.stop(event);
        this.close();
    }
    
});


document.observe('dom:loaded', function () { LightBox3dot.init(); });
