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

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

PreviousNoteButtonView.prototype.showUI = function() {
    $encpJquery(document.body).append('<div class="presentationPreviousNoteButton"></div>');
    $encpJquery(document.body).append('<div class="presentationPreviousNoteTitle"></div>');
    $encpJquery('.presentationPreviousNoteButton').hide();
    $encpJquery('.presentationPreviousNoteTitle').hide();
    $encpJquery('.presentationPreviousNoteTitle').html("");
    $encpJquery('.presentationPreviousNoteTitle').get(0).onselectstart = function () { return false; }
    $encpJquery('.presentationPreviousNoteButton').click( _.bind(this.onPreviousNoteButtonClicked,this));
    this.previousTitleVisible = 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('.presentationPreviousNoteButton').hover(function(){
        if(_this.isVisible) {
            _this.hideCursorRequested.dispatch();
        }
    }, function(){
        if(_this.isVisible) {
            _this.showCursorRequested.dispatch();
        }
    })
    $encpJquery('.presentationPreviousNoteButton').opacity(0);
    $encpJquery('.presentationPreviousNoteTitle').opacity(0);
};

PreviousNoteButtonView.prototype.setTitle = function(title){
    $encpJquery('.presentationPreviousNoteTitle').html(title);
};

PreviousNoteButtonView.prototype.onPreviousNoteButtonClicked = function(){
    this.clicked.dispatch();
};
PreviousNoteButtonView.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<outerHitBoxWidth && y > (windowHeight-outerHitBoxHeight)/2 && y < (windowHeight+outerHitBoxHeight)/2;
   	var isInInner = x<innerHitBoxWidth && y > (windowHeight-innerHitBoxHeight)/2 && y < (windowHeight+innerHitBoxHeight)/2;

    if(this.overrideWithin || isInOuter){
        $encpJquery('.presentationPreviousNoteButton').css('top',windowTop+(windowHeight-buttonHeight)/2+"px");
        $encpJquery('.presentationPreviousNoteTitle').css('top',windowTop+(windowHeight-textHeight)/2+"px");
        if(!this.fadingIn){
            this.fadingIn = true;
            $encpJquery('.presentationPreviousNoteButton').opacity(100);
            $encpJquery('.presentationPreviousNoteButton').fadeIn(fadeTime,function(){
                _this.fadingIn = false;
            });
        }
        if(isInInner){
            if(this.previousTitleVisible == false){
                $encpJquery('.presentationPreviousNoteTitle').opacity(0);
                $encpJquery('.presentationPreviousNoteTitle').show();
                $encpJquery('.presentationPreviousNoteTitle').tween({
                    left:{
                        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.previousTitleVisible = true;
            $encpJquery('.presentationPreviousNoteButton').addClass('hover')
        }
        else {

            $encpJquery('.presentationPreviousNoteButton').removeClass('hover');
            if(this.previousTitleVisible)              {
                $encpJquery('.presentationPreviousNoteTitle').tween({
                    left:{
                        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.previousTitleVisible = false;
            }
        }
    }
    else {
        this.fadeOut();
    }
};

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

PreviousNoteButtonView.prototype.fadeOut = function() {
    var fadeTime = 1000/6.0;
    this.previousTitleVisible = false;
    $encpJquery('.presentationPreviousNoteButton').fadeOut(fadeTime);
    $encpJquery('.presentationPreviousNoteTitle').fadeOut(fadeTime);
};

PreviousNoteButtonView.prototype.show = function() {
    $encpJquery(".presentationPreviousNoteButton").show();
    $encpJquery(".presentationPreviousNoteTitle").show();
    this.isVisible = true;
};

PreviousNoteButtonView.prototype.hide = function() {
    $encpJquery(".presentationPreviousNoteButton").hide();
    $encpJquery(".presentationPreviousNoteTitle").hide();
    this.nextTitleVisible = false;
    this.isVisible = false;
};

PreviousNoteButtonView.prototype.destroy = function(){
    $encpJquery(document).off('mousemove.presentationmode');
    $encpJquery('.presentationPreviousNoteButton').remove();
    $encpJquery('.presentationPreviousNoteTitle').remove();
};

PreviousNoteButtonView.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);
};