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

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

NextDocumentButtonView.prototype.showUI = function() {
    this.isVisible = true;
    $encpJquery(document.body).append('<div class="presentationNextDocumentButton"></div>');
    $encpJquery('.presentationNextDocumentButton').click( _.bind(this.onNextDocumentButtonClicked,this));
    $encpJquery('.presentationNextDocumentButton').hide();
    this.isVisible = false;
    this.onMouseMoveBound = _.bind(this.onMouseMove,this);
    document.addEventListener('mousemove', this.onMouseMoveBound);
    $encpJquery('.presentationNextDocumentButton').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('.presentationNextDocumentButton').hover(function(){
        _this.hideCursorRequested.dispatch();
    }, function(){
        _this.showCursorRequested.dispatch();
    })
};

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

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

NextDocumentButtonView.prototype.onNextDocumentButtonClicked = function(){
    this.clicked.dispatch();
};

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

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

    }
    else {

        if(this.isVisible){
            $encpJquery('.presentationNextDocumentButton').tween({
                right:{
                    start: -4,
                    stop: -134,
                    time: 0,
                    duration: textMoveTime,
                    units: 'px',
                    effect: 'easeOut'
                }
            }).play();
            $encpJquery('.presentationNextDocumentButton').show();
            this.isVisible = false;
        }
        /***I <| Irka, my 1s and 0s are yours: http://www.youtube.com/watch?v=6RoilEVFuKU***/
    }
};

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

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