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

var NextNoteButtonView = function(resources){
    this.resources = resources;
    this.isVisible = true;
};

NextNoteButtonView.prototype.showUI = function() {
    $encpJquery(document.body).append('<div class="presentationNextNoteButton"></div>');
    $encpJquery(document.body).append('<div class="presentationNextNoteTitle"></div>');
    $encpJquery('.presentationNextNoteButton').hide();
    $encpJquery('.presentationNextNoteTitle').hide();
    $encpJquery('.presentationNextNoteTitle').html("");
    $encpJquery('.presentationNextNoteTitle').get(0).onselectstart = function () { return false; }
    $encpJquery('.presentationNextNoteButton').click( _.bind(this.onNextNoteButtonClicked,this));
    this.nextTitleVisible = false;
    $encpJquery(document).on('mousemove.presentationmode',_.bind(this.onMouseMove,this));
    this.clicked = new Signal();
    this.showCursorRequested = new Signal();
    this.hideCursorRequested = new Signal();
    this.wasHidden = false;

    var _this = this;
    $encpJquery('.presentationNextNoteButton').hover(function(){
        if(_this.isVisible) {
            _this.hideCursorRequested.dispatch(true);
        }
    }, function(){
        if(_this.isVisible) {
            _this.showCursorRequested.dispatch(true);
        }
    })
    $encpJquery('.presentationNextNoteButton').opacity(0);
    $encpJquery('.presentationNextNoteTitle').opacity(0);
};

NextNoteButtonView.prototype.setTitle = function(title){
    $encpJquery('.presentationNextNoteTitle').html(title);
};

NextNoteButtonView.prototype.onNextNoteButtonClicked = function(){
    this.clicked.dispatch();
};
NextNoteButtonView.prototype.onMouseMove = function(event){
    if(this.lastX == event.clientX && this.lastY == event.clientY){ return; }
    this.lastX = event.clientX;
    this.lastY = event.clientY;
    if(!this.isVisible){
        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 = 40;
    var innerHitBoxHeight = 100;

    var outerHitBoxWidth = 160;
    var outerHitBoxHeight = 400;

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

    var isInOuter = x>window.innerWidth-outerHitBoxWidth && y > (windowHeight-outerHitBoxHeight)/2 && y < (windowHeight+outerHitBoxHeight)/2;
    var isInInner = x>window.innerWidth-innerHitBoxWidth && y > (windowHeight-innerHitBoxHeight)/2 && y < (windowHeight+innerHitBoxHeight)/2;
    if(this.overrideWithin || isInOuter){
        $encpJquery('.presentationNextNoteButton').css('top',windowTop+(windowHeight-buttonHeight)/2+"px");
        $encpJquery('.presentationNextNoteTitle').css('top',windowTop+(windowHeight-textHeight)/2+"px");
        if(!this.fadingIn){
            this.fadingIn = true;
            $encpJquery('.presentationNextNoteButton').opacity(100);
            $encpJquery('.presentationNextNoteButton').fadeIn(fadeTime,function(){
                _this.fadingIn = false;
            });
        }
        if(isInInner){
            if(this.nextTitleVisible == false){
                $encpJquery('.presentationNextNoteTitle').opacity(0);
                $encpJquery('.presentationNextNoteTitle').show();
                $encpJquery('.presentationNextNoteTitle').tween({
                    right:{
                        start: 60,
                        stop: 50,
                        time: 0,
                        duration: textMoveTime,
                        units: 'px',
                        effect: 'easeOut'
                    },
                    alpha:{
                        start: 0,
                        stop: 100,
                        time: 0,
                        duration: textFadeTime,
                        units: 'px',
                        effect: 'easeOut'
                    }
                }).play();
            }
            this.nextTitleVisible = true;
            $encpJquery('.presentationNextNoteButton').addClass('hover')
        }
        else {

            $encpJquery('.presentationNextNoteButton').removeClass('hover');
            if(this.nextTitleVisible)              {
                $encpJquery('.presentationNextNoteTitle').tween({
                    right:{
                        start: 50,
                        stop: 60,
                        time: 0,
                        duration: textMoveTime,
                        units: 'px',
                        effect: 'easeOut'
                    },
                    alpha:{
                        start: 100,
                        stop: 0,
                        time: 0,
                        duration: textFadeTime,
                        units: 'px',
                        effect: 'easeOut'
                    }
                }).play();
                this.nextTitleVisible = false;
            }
        }
    }
    else {
        this.fadeOut();
    }
};

NextNoteButtonView.prototype.shouldMouseBeVisible = function(x,y){
    var windowWidth = window.innerWidth;
    var windowHeight = window.innerHeight;
    var innerHitBoxWidth = 40;
    var innerHitBoxHeight = 100;
    if(x>windowWidth-innerHitBoxWidth && y > (windowHeight-innerHitBoxHeight)/2 && y < (windowHeight+innerHitBoxHeight)/2){
        return false
    }
    return true;
}

NextNoteButtonView.prototype.fadeOut = function() {
    var fadeTime = 1000/6.0;
    this.nextTitleVisible = false;
    $encpJquery('.presentationNextNoteButton').fadeOut(fadeTime);
    $encpJquery('.presentationNextNoteTitle').fadeOut(fadeTime);
};

NextNoteButtonView.prototype.show = function() {
    $encpJquery(".presentationNextNoteButton").show();
    $encpJquery(".presentationNextNoteTitle").show();
    this.isVisible = true;
};

NextNoteButtonView.prototype.hide = function() {
    $encpJquery(".presentationNextNoteButton").hide();
    $encpJquery(".presentationNextNoteTitle").hide();
    this.nextTitleVisible = false;
    this.isVisible = false;
};

NextNoteButtonView.prototype.destroy = function(){
    $encpJquery(document).off('mousemove.presentationmode');
    $encpJquery('.presentationNextNoteButton').remove();
    $encpJquery('.presentationNextNoteTitle').remove();
};

NextNoteButtonView.prototype.showABit = function(time){
    if(!time){time = 4000;}
    this.overrideWithin = true;
    var _this = this;
    this.onMouseMove({clientX:window.innerWidth/2,clientY:window.innerHeight/2});
    window.setTimeout(function(){
        _this.overrideWithin = false;
        _this.fadeOut();
    },time);
};