//
function PhotoView(){

    this.targetImgText = "photo-view-imgtext";
    this.playbutton = "photo-view-playbutton";
    this.pausebutton = "photo-view-pausebutton";
    this.targetImages = "photo-view-images";
    this.speedId = "photo-view-speed";
    this.currenpageId = "photo-view-currentpage";

    this.length = 0;
    this.albumid = 0;
    this.contextPath = "";
    this.currentIndex = 0;
    this.items = new Array();
    this.isPlaying = false;
    this.isPause = false;
    this.playInterval = 0;

    this.isTween = true;
    this.step = 110;

    this.init = function(len, albumid, contextPath){
        this.length = len;
        this.albumid = albumid;
        this.contextPath = contextPath;
        this.currentIndex = 0;

        var i = 0;
        while(i < this.length){
            var element = $('photo-view-imageid-' + i);
            if(element != null){
                this.items[i] = element.value;
            }
            i++;
        }

        this.build();
    };

    this.build = function(){
        this.setCurrentpage();
        new Ajax.Updater(this.targetImgText, this.contextPath + '/photo/' + this.albumid + '.shtml?photo=' + this.items[this.currentIndex],
            {
                evalScripts:true,
                onFailure: function(){ alert("Таны хүсэлт олдсонгүй.") }
            });
    };

    this.onclick = function(cI){
        if(this.currentIndex != cI){
            this.currentIndex = cI;
            this.playReset();
            this.build();
        }
    };
    
    this.prev = function(){
        if(this.currentIndex == 0){
            this.currentIndex = this.length - 1;
        }
        else{
            this.currentIndex --;
        }
        this.playReset();
        this.build();
    };

    this.next = function(){
        if(this.currentIndex == this.length - 1){
            this.currentIndex = 0;
        }
        else{
            this.currentIndex ++;
        }
        this.playReset();
        this.build();
    };

    /* for play */

    this.play = function(){
        this.setPlayPauseButton(false);
        this.isPlaying = true;
        this.isPause = false;
        if(this.isPause){
            this.playContinue();
        }
        else{
            this.currentIndex = 0;
            this.build();
        }
    };

    this.playContinue = function(){
        
        if(this.isPlaying){

            var speed = 3000;
            var elementSpeed = $(this.speedId);
            if(elementSpeed != null){
                speed = elementSpeed.value;
            }
            
            if(this.currentIndex == this.length - 1){
                this.playInterval = setTimeout('photoView.stop()', speed);
            }
            else{
                this.currentIndex ++;
                this.playInterval = setTimeout('photoView.build()', speed);
            }
        }
    };

    this.pause = function(){

        this.setPlayPauseButton(true);
        this.isPlaying = false;
        this.isPause = true;
        clearTimeout(this.playInterval);
    };

    this.stop = function(){

        this.playReset();
        this.currentIndex = 0;
        this.build();
    };

    this.playReset = function(){
        this.setPlayPauseButton(true);
        this.isPlaying = false;
        this.isPause = false;
        clearTimeout(this.playInterval); 
    };

    this.setPlayPauseButton = function(showPlayButton){
        if(showPlayButton){
            var elementPlayButton = $(this.playbutton);
            if(elementPlayButton != null){
                elementPlayButton.style.display = "";
            }
            var elementPauseButton = $(this.pausebutton);
            if(elementPauseButton != null){
                elementPauseButton.style.display = "none";
            }
        }
        else{
            var elementPlayButton = $(this.playbutton);
            if(elementPlayButton != null){
                elementPlayButton.style.display = "none";
            }
            var elementPauseButton = $(this.pausebutton);
            if(elementPauseButton != null){
                elementPauseButton.style.display = "";
            }
        }
    };

    /* end of play */

    /* for tween */

    this.moveLeft = function(){
        var element = $(this.targetImages);
        if(element != null){
            this.tween(element, parseInt(element.offsetLeft), parseInt(element.offsetLeft) + this.step);
        }
    };

    this.moveRight = function(){
        var element = $(this.targetImages);
        if(element != null){
            this.tween(element, parseInt(element.offsetLeft), parseInt(element.offsetLeft) - this.step);
        }
    };

    this.tween = function(element, x0, x1){
        var self = this;
        var tween = new Tween(element.style, "left", Tween.regularEaseOut, x0, x1, 0.5, "px");
        tween.onMotionFinished = function(){
            self.isTween = true;
            self.move(element, x0, x1);
        };
        if(this.isTween){
            tween.start();
            this.isTween = false;
        }
    };

    this.move = function(element, x0, x1){
        if(x0 > x1){
            element.insert({bottom:element.childElements()[0]});
            element.style.left = (parseInt(element.style.left) + this.step) + "px";
        }
        else{
            element.insert({top:element.childElements()[element.childElements().length-1]});
            element.style.left = (parseInt(element.style.left) - this.step) + "px";
        }
    };

    this.setCurrentpage = function(){
        var element = $(this.currenpageId);
        if(element != null){
            element.innerHTML = (this.currentIndex+1);
        }
    };

    /* end of tween */

}

var photoView = new PhotoView();