/*
---

*/

MiniGallery = new Class({

    ll : false,
    lastActive : 0,
    bigImage : [],

    initialize: function (ele){





		this.element = $(ele);
        this.elements = {};
        if (this.element) {

            this.elements.showroom = this.element.getChildren('.showroom')[0].getChildren('img');
            this.elements.title = this.element.getChildren('.desc')[0];
            this.elements.buttons = this.element.getChildren('.buttons')[0].getChildren('li');
            this.elements.buttonsParent = this.element.getChildren('.buttons')[0];

            this.attach();

        }


        return this;
	},

    /**
     * + Projedem vsechny LI s klikama
     *
     *
     */
    attach:function(){
        this.elements.buttons = this.element.getChildren('.buttons')[0].getChildren('li');
        this.elements.buttons.each(function(item, index) {
            Log.write(item);

            item.getChildren('a')[0].addEvent('click', function(e) {
                e.stop();
                this.itemSetActive(index);
            }.bind(this));
        }, this);

        return this;
    },


    itemSetActive : function (oid){
        if ($type(this.elements.buttons[this.lastActive]) == 'element'){
            this.elements.buttons[this.lastActive].removeClass('active');
        }

        activeElement = this.elements.buttons[oid];
        if ($type(activeElement) == 'element'){
            activeElement.addClass('active');
            activeElementA = activeElement.getChildren('a')[0];
            activeElementImg = activeElementA.getChildren('img')[0];

            this.lastActive = oid;

            this.showDetail(oid);

            this.elements.title.set('text', activeElementImg.get('alt'));

        }



        return this;
    },

    showDetail : function (oid){

        if ($defined(this.bigImage[oid])){
            this.elements.showroom.set('src', this.bigImage[oid].src );
        }else{
            if  ($type(this.ll) == 'element')  this.ll.destroy();
            // TODO : dat zpet a odladit na 1
            // this.ll = Page.showLoading();

            myImg = this.bigImage[oid] = new Asset.image(activeElementA.get('href'));
            (function(){
                if (myImg.complete) this.refreshDetail(oid);
            }).bind(this).delay(500);

            this.elements.showroom.set('src', activeElementA.get('href') );

        }
    },

    refreshDetail : function (oid){
        this.elements.showroom.set('src', this.bigImage[oid].src );
        // TODO :
        // this.ll.destroy();
    },

    rebuild: function (items){

        Log.write(this);

        this.bigImage = [];

        sh = this.elements.showroom;
        ti = this.elements.title;
        bu = this.elements.buttons;
        ul = this.elements.buttonsParent;

        ul.empty();
        Log.write(ul);



        if ($defined(items[0])){
            erste = items[0];

            sh.set('src' , erste.img );
            sh.set('alt' , erste.description );
            ti.set('text' , erste.description );
        }


        items.each(function(item, index) {
            li = new Elements.from('<li><a href="'+ item.img +'"><img src="'+ item.imgTh +'" alt="'+ item.description +'" width="80" height="60" /></a></li>')[0];
            ul.grab(li);
        }, this);

        this.attach();
        this.itemSetActive(0);


    }




});

MiniGalleryYouTube = new Class({

    videoList : false,

    Extends: MiniGallery,

    initialize: function (ele){
        this.element = $(ele);

        this.elements = {};

        this.YTVideos = [];

        if (this.element){

            this.elements.showroom = this.element.getChildren('.showroom')[0].getChildren('span')[0];
            this.elements.title = this.element.getChildren('.desc')[0];
            this.elements.buttons = this.element.getChildren('.buttons')[0].getChildren('li');

            // zjistime vsechna videa
            this.elements.buttons.each(function(item, index) {
                this.YTVideos[index] =item.getChildren('a')[0].get('rel');
            }, this);

            this.YTPL = new YoutubePL({
                                    select : this.elements.buttons,
                                    videoContainer : this.elements.showroom,
                                    videos: this.YTVideos
                                    });
            this.YTPL.run();

            this.attach();


        }



        return this;
	},

    showDetail : function (oid){

        this.YTPL.play(oid);
    },

    initErste : function (){
        this.YTPL.run();
    }

});

