var CPhotoFeed = new Class({
    options: {
        container: 'photofeedcontainer',
        containerSize: (143 + 6) * 3,
        feed: 'photofeed',
        frameSize: 143 + 6,
        canselect : false,
        photosclass : null,
        photosactiveclass : null,
        buttons: {
            prev: {
                id: 'pfeed_prev',
                activeImage: '/img/tur/but_prev.gif',
                inactiveImage: '/img/tur/but_prev_inactive.gif'                
            },
            next: {
                id: 'pfeed_next',
                activeImage: '/img/tur/but_next.gif',
                inactiveImage: '/img/tur/but_next_inactive.gif'
            },
            first: {
                id: 'pfeed_first',
                activeImage: '/img/tur/but_first.gif',
                inactiveImage: '/img/tur/but_first_inactive.gif'
            },
            last: {
                id: 'pfeed_last',
                activeImage: '/img/tur/but_last.gif',
                inactiveImage: '/img/tur/but_last_inactive.gif'
            }
        },
        listeners: {
            photoselect : null
        }
    },
    initialize: function(options) {
        this.setOptions(options);
        this.prevbtn = $(this.options.buttons.prev.id);
        this.nextbtn = $(this.options.buttons.next.id);
        this.firstbtn = $(this.options.buttons.first.id);
        this.lastbtn = $(this.options.buttons.last.id);
        this.containerel = $(this.options.container);           
        this.containerel.setStyle('height', this.options.containerSize);
        this.scroll = new Fx.Scroll(this.options.container, {
                wait: false,
                duration: 1000,
                offset: {'x': 0, 'y': 0},
                transition: Fx.Transitions.linear.easeInOut                
        }).addEvent('complete', function () {
                    this.refreshButtons();
                }.bind(this));        
        this.nextbtn.addEvent('click', function() {
            this.next();
        }.bind(this));
        this.prevbtn.addEvent('click', function() {
            this.prev();
        }.bind(this));
        this.firstbtn.addEvent('click', function() {
            this.first();
        }.bind(this));
        this.lastbtn.addEvent('click', function() {
            this.last();
        }.bind(this));
        this.refreshButtons();
        if (this.options.canselect) {
            this.photos = $$("."+this.options.photosclass);
            this.photoIdx = -1;
            this.selectPhoto(0);            
            for(var i=0;i<this.photos.length;i++) {
                this.photos[i].setStyle('cursor', 'pointer');
                this.photos[i].photoIdx = i;
                this.photos[i].photofeed = this;
                this.photos[i].addEvent('click', function () {
                    this.photofeed.selectPhoto(this.photoIdx);
                });
            }
        }
    },
    selectPhoto : function (idx) {        
        if (idx != this.photoIdx && idx >= 0 && idx < this.photos.length) {
            if (this.photos[this.photoIdx]) this.photos[this.photoIdx].className = this.options.photosclass;
            this.photoIdx = idx;
            if (this.photos[this.photoIdx]) this.photos[this.photoIdx].className = this.options.photosactiveclass;
            if (this.options.listeners.photoselect) this.options.listeners.photoselect(this.photoIdx, this.photos[this.photoIdx], this);
            var photoY = this.photoIdx * this.options.frameSize;
            var scrollY1 = this.containerel.getScroll().y;
            var scrollY2 = this.containerel.getScroll().y + this.containerel.getSize().y - this.options.frameSize;
            if (photoY < scrollY1 || photoY > scrollY2) this.scroll.start(0, photoY);
        }
    },    
    disableBtn : function (btnname) {
        this[btnname+'btn'].src = this.options.buttons[btnname].inactiveImage;
        this.options.buttons[btnname].disabled=true;
    },
    enableBtn : function (btnname) {

        this[btnname+'btn'].src = this.options.buttons[btnname].activeImage;
        this.options.buttons[btnname].disabled=false;
    },
    checkDisabled : function (btnname) {
        return (this.options.buttons[btnname].disabled==true);        
    },
    refreshButtons: function () {
       if (this.containerel.getScroll().y >= (this.containerel.getScrollSize().y - this.containerel.getSize().y)) {
            this.disableBtn('last');
            this.disableBtn('next');        
       }
       else {
             this.enableBtn('last');
             this.enableBtn('next');            
       }
       if (this.containerel.getScroll().y <= 0) {
             this.disableBtn('first');
             this.disableBtn('prev');
       }
       else {
            this.enableBtn('first');
            this.enableBtn('prev');                    
       }
    },
    next: function () {
       if (!this.checkDisabled('next')) {                    
          this.scroll.start(0, this.containerel.getScroll().y + this.options.frameSize);
       }
    },    
    prev: function () {
        if (!this.checkDisabled('prev')) {           
           this.scroll.start(0, this.containerel.getScroll().y - this.options.frameSize);
        }
    },
    first: function () {
       if (!this.checkDisabled('first')) {
          this.scroll.toTop();          
       }
    },
    last: function () {        
         if (!this.checkDisabled('last')) {
            this.scroll.toBottom();            
         }
    }
});
CPhotoFeed.implement(new Options, new Events);

var CPhotoViewer = new Class({
    options: {
        displayFullImage: 'display_fullimage',
        displayPhoto: 'display_photo',
        displayText: 'photoname_display',
        progressclass : 'photoviewer_progress',
        progressmsg : 'Загрузка...',
        photos : [],        
        buttons: {
            prev: {
                id: 'prev_prev',
                activeImage: '/img/tur/imgprev.gif',
                inactiveImage: '/img/tur/imgprev_inactive.gif'                
            },
            next: {
                id: 'prev_next',
                activeImage: '/img/tur/imgnext.gif',
                inactiveImage: '/img/tur/imgnext_inactive.gif'
            },
            first: {
                id: 'prev_first',
                activeImage: '/img/tur/imgfirst.gif',
                inactiveImage: '/img/tur/imgfirst_inactive.gif'
            },
            last: {
                id: 'prev_last',
                activeImage: '/img/tur/imglast.gif',
                inactiveImage: '/img/tur/imglast_inactive.gif'
            }
        },
        listeners: {
            photoselect : null
        }
    },
    initialize: function(options) {
        this.setOptions(options);
        this.prevbtn = $(this.options.buttons.prev.id);
        this.nextbtn = $(this.options.buttons.next.id);
        this.firstbtn = $(this.options.buttons.first.id);
        this.lastbtn = $(this.options.buttons.last.id);
        this.displayel = $(this.options.displayPhoto);
        this.displaytextel = $(this.options.displayText);
        this.displaylinkel = $(this.options.displayFullImage);
        this.photos = this.options.photos;
        this.prevbtn.setStyle('cursor', 'pointer');
        this.nextbtn.setStyle('cursor', 'pointer');
        this.firstbtn.setStyle('cursor', 'pointer');
        this.lastbtn.setStyle('cursor', 'pointer');
        this.nextbtn.addEvent('click', function() {
            this.next();
        }.bind(this));
        this.prevbtn.addEvent('click', function() {
            this.prev();
        }.bind(this));
        this.firstbtn.addEvent('click', function() {
            this.first();
        }.bind(this));
        this.lastbtn.addEvent('click', function() {
            this.last();
        }.bind(this));
        this.refreshButtons();
        this.photoIdx = 0;
        this.progress = new Element('div', {
            'class' : this.options.progressclass,

            'styles': {
                'display' : 'none',
                'position': 'absolute',
                'top': this.displayel.getParent('td').getPosition().y + 'px',
                'left': (this.displayel.getParent('td').getPosition().x - 1) + 'px'
            }
        });
        if (Browser.Engine.trident) 
        {
           var objBody = document.getElementsByTagName("body").item(0);
           objBody.insertBefore(this.progress, objBody.firstChild);
        }
        else {
          var objBody = $(document.body);
          objBody.grab(this.progress, 'bottom');      
        }
        //this.displayel.getParent().grab(this.progress);
        this.progress.set('html', '<table width="100%" height="100%"><tr><td align="center" valign="middle"><img src="/img/tur/ajax-loader.gif" align="absmiddle">&nbsp;&nbsp;'+this.options.progressmsg+'</td></tr></table>');
        var img = new Image();
        img.src = '/img/tur/ajax-loader.gif';
        this.refreshButtons();
    },
    selectPhoto : function (idx) {        
        if (idx != this.photoIdx && idx >= 0 && idx < this.photos.length) {
            this.photoIdx = idx;            
            this.refreshButtons();
            if (this.options.listeners.photoselect) this.options.listeners.photoselect(this.photoIdx, this.photos[this.photoIdx], this);            
            var img = new Image();
            this.progress.setStyle('display', '');
            img.onload =  function (e) {
                this.displayel.src = this.photos[this.photoIdx].cache_medium;
                this.progress.setStyle('display', 'none');
                this.displaytextel.set('text',this.photos[this.photoIdx].name);
                this.displaylinkel.setProperty('href',this.photos[this.photoIdx].cache_fullimage);
                
            }.bind(this);            
            img.src = this.photos[this.photoIdx].cache_medium;
        }
    },    
    disableBtn : function (btnname) {
        this[btnname+'btn'].src = this.options.buttons[btnname].inactiveImage;
        this.options.buttons[btnname].disabled=true;
    },
    enableBtn : function (btnname) {
        this[btnname+'btn'].src = this.options.buttons[btnname].activeImage;
        this.options.buttons[btnname].disabled=false;
    },
    checkDisabled : function (btnname) {
        return (this.options.buttons[btnname].disabled==true);        
    },
    refreshButtons: function () {
       if (this.photoIdx >= (this.photos.length-1)) {
            this.disableBtn('last');
            this.disableBtn('next');        
       }
       else {
             this.enableBtn('last');
             this.enableBtn('next');            
       }
       if (this.photoIdx <= 0) {
             this.disableBtn('first');
             this.disableBtn('prev');
       }
       else {
            this.enableBtn('first');
            this.enableBtn('prev');                    
       }
    },
    next: function () {
       if (!this.checkDisabled('next')) {
          this.selectPhoto(this.photoIdx+1);
          //this.scroll.start(0, this.containerel.getScroll().y + this.options.frameSize);
       }
    },    
    prev: function () {
        if (!this.checkDisabled('prev')) {
            this.selectPhoto(this.photoIdx-1);
           //this.scroll.start(0, this.containerel.getScroll().y - this.options.frameSize);
        }
    },
    first: function () {
       if (!this.checkDisabled('first')) {
          this.selectPhoto(0);
          //this.scroll.toTop();          
       }
    },
    last: function () {        
         if (!this.checkDisabled('last')) {            
            this.selectPhoto(this.photos.length-1);
            //this.scroll.toBottom();            
         }
    }
});
CPhotoViewer.implement(new Options, new Events);