//
//  PreviousDocumentButtonView.js
//  CoverPage
//
//  Created by Richard Anaya on 1/9/2013.
//  Copyright (c) 2014 Evernote. All rights reserved.
//

var PreviousDocumentButtonView = function(resources){
    this.resources = resources;
    this.isEnabled = true;
};

PreviousDocumentButtonView.prototype.showUI = function() {
    this.isVisible = true;
    $encpJquery(document.body).append('<div class="presentationPreviousDocumentButton"></div>');
    $encpJquery('.presentationPreviousDocumentButton').click( _.bind(this.onPreviousDocumentButtonClicked,this));
    $encpJquery('.presentationPreviousDocumentButton').hide();
    this.isVisible = false;
    this.onMouseMoveBound = _.bind(this.onMouseMove,this);
    document.addEventListener('mousemove', this.onMouseMoveBound);
    $encpJquery('.presentationPreviousDocumentButton').get(0).onselectstart = function () { return false; }
    this.clicked = new Signal();
    this.showCursorRequested = new Signal();
    this.hideCursorRequested = new Signal();
    this.wasHidden = false;

    var _this = this;
    $encpJquery('.presentationPreviousDocumentButton').hover(function(){
        _this.hideCursorRequested.dispatch();
    }, function(){
        _this.showCursorRequested.dispatch();
    })
};

PreviousDocumentButtonView.prototype.show = function() {
    this.isEnabled = true;
}

PreviousDocumentButtonView.prototype.hide = function() {
    this.isEnabled = false;
    $encpJquery('.presentationPreviousDocumentButton').hide();
}   

PreviousDocumentButtonView.prototype.onPreviousDocumentButtonClicked = function(){
    this.clicked.dispatch();
};

PreviousDocumentButtonView.prototype.setViewingImage = function(img){
    if(img.width > img.height){
        $encpJquery('.presentationPreviousDocumentButton').get(0).style.backgroundSize = "30px 30px,"+Math.floor(img.width*130.0/img.height)+"px 130px";
    }
    else {
        $encpJquery('.presentationPreviousDocumentButton').get(0).style.backgroundSize = "30px 30px,"+"130px "+Math.floor(img.height*130.0/img.width)+"px";
    }
    $encpJquery('.presentationPreviousDocumentButton').css("background-image",'url(images/doc_viewer_leftnav_icon.svg), url('+img.src+')');
};

PreviousDocumentButtonView.prototype.onMouseMove = function(event){
    if(!this.isEnabled){
        return;
    }
    var x = event.clientX;
    var y = event.clientY;
    var windowWidth = window.innerWidth;
    var windowHeight = window.innerHeight;
    var windowTop = 0;

    var buttonWidth = 40;
    var buttonHeight = 100;
    var textHeight = 42;

    var innerHitBoxWidth = 60;
    var innerHitBoxHeight = 140;

    var outerHitBoxWidth = 160;
    var outerHitBoxHeight = 400;

    var fadeTime = 1000/6.0;
    var textFadeTime = 1/3;
    var textMoveTime = 1/2;

    $encpJquery('.presentationPreviousDocumentButton').css('top',windowTop+(windowHeight-buttonHeight)/2+"px");
    if(x<outerHitBoxWidth && y > (windowHeight-outerHitBoxHeight)/2 && y < (windowHeight+outerHitBoxHeight)/2 ){
        if(!this.isVisible){
            $encpJquery('.presentationPreviousDocumentButton').tween({
                left:{
                    start: -134,
                    stop: -4,
                    time: 0,
                    duration: textMoveTime,
                    units: 'px',
                    effect: 'easeOut'
                }
            }).play();
            $encpJquery('.presentationPreviousDocumentButton').show();
            this.isVisible = true;
        }
    }
    else {

        if(this.isVisible){
            $encpJquery('.presentationPreviousDocumentButton').tween({
                left:{
                    start: -4,
                    stop: -134,
                    time: 0,
                    duration: textMoveTime,
                    units: 'px',
                    effect: 'easeOut'
                }
            }).play();
            this.isVisible = false;
        }
    }
};

PreviousDocumentButtonView.prototype.removeUI = function() {
    $encpJquery('.presentationPreviousDocumentButton').remove();
    document.removeEventListener('mousemove', this.onMouseMoveBound);
};

PreviousDocumentButtonView.prototype.destroy = function() {
    this.removeUI();
};