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

var PageUpButtonView = function(resources){
    this.resources = resources;
};

PageUpButtonView.prototype.showUI = function() {
    $encpJquery(document.body).append('<div class="presentationPageUpButton"></div>');
    $encpJquery('.presentationPageUpButton').hide();
    $encpJquery('.presentationPageUpButton').click( _.bind(this.onPageUpButtonClicked,this));
    $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('.presentationPageUpButton').hover(function(){
        _this.hideCursorRequested.dispatch();
        _this.isHovering = true;
    }, function(){
        _this.isHovering = false;
        _this.showCursorRequested.dispatch();
    })
};

PageUpButtonView.prototype.onPageUpButtonClicked = function(){
    this.clicked.dispatch();
};

PageUpButtonView.prototype.shouldMouseBeVisible = function(x,y){
    var windowWidth = window.innerWidth;
    var windowHeight = window.innerHeight;

    var innerHitBoxWidth = 300;
    var innerHitBoxHeight = 60;

    if(y<innerHitBoxHeight && x > (windowWidth-innerHitBoxWidth)/2 && x < (windowWidth+innerHitBoxWidth)/2){
        return false
    }
    return true;
}

PageUpButtonView.prototype.onMouseMove = function(event){
    if(this.lastX == event.clientX && this.lastY == event.clientY){ return; }
    this.lastX = event.clientX;
    this.lastY = event.clientY;

    if(!this.enabled){ return; }

    var x = event.clientX;
    var y = event.clientY;
    var windowWidth = window.innerWidth;
    var windowHeight = window.innerHeight;

    var buttonWidth = 100;
    var buttonHeight = 40;

    var innerHitBoxWidth = 300;
    var innerHitBoxHeight = 60;

    var outerHitBoxWidth = 400;
    var outerHitBoxHeight = 120;

    var fadeTime = 1000/6.0;

    if(this.overrideWithin  || y<outerHitBoxHeight && x > (windowWidth-outerHitBoxWidth)/2 && x < (windowWidth+outerHitBoxWidth)/2 ){
        $encpJquery('.presentationPageUpButton').css('top',0+"px");
        $encpJquery('.presentationPageUpButton').css('left',(windowWidth-buttonWidth)/2+"px");
        $encpJquery('.presentationPageUpButton').fadeIn(fadeTime);
        if( y<innerHitBoxHeight && x > (windowWidth-innerHitBoxWidth)/2 && x < (windowWidth+innerHitBoxWidth)/2){
            $encpJquery('.presentationPageUpButton').addClass('hover')
        }
        else {
            $encpJquery('.presentationPageUpButton').removeClass('hover');
        }
    }
    else {
        this.fadeOut();
    }
};

PageUpButtonView.prototype.fadeOut = function() {
    var fadeTime = 1000/6.0;
    $encpJquery('.presentationPageUpButton').fadeOut(fadeTime);
};

PageUpButtonView.prototype.enable = function() {
    this.enabled = true;
};

PageUpButtonView.prototype.disable = function() {
    if(this.isHovering){
        this.isHovering = false;
        this.showCursorRequested.dispatch();
    }
    this.enabled = false;
    this.fadeOut();
};

PageUpButtonView.prototype.destroy = function() {
    $encpJquery(document).off('mousemove.presentationmode');
    $encpJquery('.presentationPageUpButton').remove();
};

PageUpButtonView.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.onMouseMove({clientX:window.innerWidth/2,clientY:window.innerHeight/2});
    },time);
};