/*global View window document $ InputManager setTimeout clearTimeout Image*/

var ANIMATION_SPEED = 400;

function PhotoViewer(album){
    this.album = album;
}

PhotoViewer.prototype.init = function (){
    this.preloadImages();

    View.detect();

    this.controls = new ViewerScreenControls(this);
    this.controls.init();

    this.fullscreen = false;

    this.slideManager = new SlideManager(this, album);
    this.currentPhoto = null;

    this.animatingPhoto = false;

    this.loadingIndicatorTimeout = null;

    var self = this;
    $(window).bind("resize", function(){
        View.detect();
        self.slideManager.repositionSlides();
        self.controls.reposition();
    });

    $(document).bind("keydown", function(e){
        return self.keyPressed(e.which);
    });

    $("a.button").bind("focus", function(){
        this.blur();
    });

    if (urlHash == "play"){
        this.startSlideshow();
    } else {
        var photo = this.getPhotoFromUrl();
        this.showPhoto(photo);
    }

    this.controls.reposition();
}

PhotoViewer.prototype.removeHash = function (){
    if ("pushState" in history){
        history.pushState("", document.title, window.location.pathname);
    } else {
        window.location.hash = "";
    }
}

PhotoViewer.prototype.setCurrentPhoto = function (photo){
    this.currentPhoto = photo;
    var photoIndex = this.album.getPhotoIndex(photo);
    $("#thumbs li.current").removeClass("current");
    $("#thumbs li:nth-child(" + (1 + photoIndex) + ")").addClass("current");
    Thumbs.scrollToPhotoWithIndex(this.album.getPhotoIndex(photo));

    if(!this.slideshow){
        if(photo.id != this.album.photos[0].id){
            window.location.hash = photo.id;
        } else {
            this.removeHash();
        }
    }

}

PhotoViewer.prototype.showPhoto = function (photo){
    if(photo && this.currentPhoto != photo){
        this.slideManager.showPhoto(photo);
    }
}

PhotoViewer.prototype.showPhotoWithIndex = function (index){
    var photo = this.album.photos[index];
    this.showPhoto(photo);
}

PhotoViewer.prototype.showNextPhoto = function (){
    this.showPhoto(this.album.getNextPhoto(this.currentPhoto));
}

PhotoViewer.prototype.showPreviousPhoto = function (){
    this.showPhoto(this.album.getPreviousPhoto(this.currentPhoto));
}

PhotoViewer.prototype.getPhotoFromUrl = function (){
    var urlHashAsInt = parseInt(urlHash);

    if(!isNaN(urlHashAsInt) && typeof this.album.photos[urlHashAsInt] != undefined){
        var id = parseInt(urlHash);
        var photo = this.album.getPhotoById(id);
        if (photo) return photo;
    }

    return this.album.photos[0];
}

PhotoViewer.prototype.getPhotoPosition = function (photo){
    var currentPhotoIndex = this.album.getPhotoIndex(this.currentPhoto);
    var photoIndex = this.album.getPhotoIndex(photo);

    if (photoIndex < currentPhotoIndex) return "left";
    return "right";
}

PhotoViewer.prototype.startFullscreen = function(){
    if(!this.fullscreen){
        this.fullscreen = true;

        $("#head").css("opacity", "0.9");
        $("#bottomPanel").css("opacity", "0.9");
        $("#slideshowControls").css("opacity", "0.9");

        this.controls.hide();

        View.detect();
        this.slideManager.repositionSlides();

        $("a.button.fullscreen").addClass("exitFullscreen");
        $("body").bind("mousemove", jQuery.proxy(this.controls.show, this.controls));
    }
}

PhotoViewer.prototype.stopFullscreen = function(){
    this.fullscreen = false;

    $("#head").css("opacity", "1");
    $("#bottomPanel").css("opacity", "1");
    $("#slideshowControls").css("opacity", "1");

    $("body").unbind("mousemove", jQuery.proxy(this.controls.show, this.controls));
    this.controls.show();
    $("a.button.fullscreen").removeClass("exitFullscreen");
    View.detect();
    this.controls.reposition();
    this.slideManager.repositionSlides();
}

PhotoViewer.prototype.startSlideshow = function(){
    this.startFullscreen();
    this.slideshow = new Slideshow(this);
    this.slideshow.start();
}

PhotoViewer.prototype.stopSlideshow = function(){
    this.slideshow.stop();
    this.slideshow = null;
    this.stopFullscreen();
}

PhotoViewer.prototype.preloadImages = function (){
    var laImages = [];

    laImages[0] = new Image();
    laImages[0].src="/inc/css/images/aperture.gif";

    laImages[1] = new Image();
    laImages[1].src="/inc/css/images/nextPhoto.gif";

    laImages[2] = new Image();
    laImages[2].src="/inc/css/images/previousPhoto.gif";

    laImages[3] = new Image();
    laImages[3].src="/inc/css/images/slideshow.gif";

    laImages[4] = new Image();
    laImages[4].src="/inc/css/images/fullscreen.gif";

    laImages[5] = new Image();
    laImages[5].src="/inc/css/images/logo-bw.gif";

    laImages[6] = new Image();
    laImages[6].src="/inc/css/images/scrollerLeft.gif";

    laImages[7] = new Image();
    laImages[7].src="/inc/css/images/scrollerRight.gif";
}

PhotoViewer.prototype.isCurrentPhotoFirst = function(){
    if (this.album.getPhotoIndex(this.currentPhoto) === 0) return true;
    return false;
}

PhotoViewer.prototype.isCurrentPhotoLast = function(){
    if (this.album.photos[this.album.getPhotoIndex(this.currentPhoto) + 1]){
        return false;
    } else {
        return true;
    }
}

PhotoViewer.prototype.showLoadingIndicator = function(){
    this.loadingIndicatorTimeout = setTimeout(function(){
        $('#photo .loadingIndicator').css("display", "block");
    }, 300);
}

PhotoViewer.prototype.hideLoadingIndicator = function(){
    clearTimeout(this.loadingIndicatorTimeout);
    $('#photo .loadingIndicator').css("display", "none");
}

PhotoViewer.prototype.keyPressed = function(pKeyCode){
    switch(pKeyCode){
        case 32:
            // space
            if(!this.slideshow){
                this.startSlideshow();
            }
            return false;
        case 37:
            // arrow left
            if(!this.slideshow){
                this.showPreviousPhoto();
            }
            return false;
        case 39:
            // arrow right
            if(!this.slideshow){
                this.showNextPhoto();
            }
            return false;
        case 27:
            // arrow right
            if(this.slideshow){
                this.stopSlideshow();
            }

            if(this.fullscreen){
                this.stopFullscreen();
            }
            return false;
        default:
            return true;
    }
}

function Album(){
    this.photos = [];
}

Album.prototype.getPhotoIndex = function (photo){
    return $.inArray(photo, this.photos);
}

Album.prototype.getPhotoById = function (id){
    for(index in this.photos){
        if (this.photos[index].id == id) return this.photos[index];
    }
    return null;
}

Album.prototype.getNextPhoto = function (photo){
    var photoIndex = this.getPhotoIndex(photo);
    if (typeof(this.photos[photoIndex + 1]) == 'undefined'){
        return null;
    }
    return this.photos[photoIndex + 1];
}

Album.prototype.getPreviousPhoto = function (photo){
    var photoIndex = this.getPhotoIndex(photo);
    if (typeof(this.photos[photoIndex - 1]) == 'undefined'){
        return null;
    }
    return this.photos[photoIndex - 1];
}

function Photo(id, photoUrl, thumbUrl, downloadUrl, width, height){
    this.id = id;
    this.photoUrl = photoUrl;
    this.thumbUrl = thumbUrl;
    this.downloadUrl = downloadUrl;
    this.width = width;
    this.height = height;

    this.ratio = width / height;
}

Photo.prototype.thumbWidth = function(){
    if(this.ratio>=1){
        return Math.floor(100 * this.ratio);
    } else {
        return 100;
    }
};

Photo.prototype.thumbHeight = function(){
    if(this.ratio<=1){
        return Math.floor(100 / this.ratio);
    } else {
        return 100;
    }
};

function Slideshow(photoViewer){
    this.timer = null;
    this.photoViewer = photoViewer;
};


Slideshow.prototype.showSlideshowNextPhoto = function(){
    clearTimeout(this.timer);
    this.timer = null;
    if(this.photoViewer.isCurrentPhotoLast()){
        this.photoViewer.stopSlideshow();
    } else {
        this.photoViewer.showNextPhoto();
        this.setSlideshowTimer();
    }
},

Slideshow.prototype.setSlideshowTimer = function(){
    this.timer = setTimeout(jQuery.proxy(this.showSlideshowNextPhoto, this), 4000);
}

Slideshow.prototype.pause = function(){
    clearTimeout(this.timer);
    this.timer = null;
    $("a.button.pauseSlideshow").css({
        "background-image": "url(/inc/css/images/slideshow.gif)"
    });
}

Slideshow.prototype.resume = function(){
    this.setSlideshowTimer();
    $("a.button.pauseSlideshow").css({
        "background-image": "url(/inc/css/images/pauseSlideshow.gif)"
    });
}

Slideshow.prototype.isPaused = function(){
    if (this.timer === null) return true;
    return false;
}

Slideshow.prototype.start = function(){
     window.location.hash = "play";
     this.setSlideshowTimer();
}

Slideshow.prototype.stop = function(){
    clearTimeout(this.timer);
    this.timer = null;
}
