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

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

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

PageDownButtonView.prototype.onPageDownButtonClicked = function(){
    this.clicked.dispatch();
};

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

    var innerHitBoxWidth = 300;
    var innerHitBoxHeight = 60;

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

PageDownButtonView.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>=windowHeight-outerHitBoxHeight && x > (windowWidth-outerHitBoxWidth)/2 && x < (windowWidth+outerHitBoxWidth)/2 ){
        $encpJquery('.presentationPageDownButton').css('bottom',0+"px");
        $encpJquery('.presentationPageDownButton').css('left',(windowWidth-buttonWidth)/2+"px");
        $encpJquery('.presentationPageDownButton').fadeIn(fadeTime);
        if( y>=windowHeight-innerHitBoxHeight && x > (windowWidth-innerHitBoxWidth)/2 && x < (windowWidth+innerHitBoxWidth)/2){
            $encpJquery('.presentationPageDownButton').addClass('hover')
        }
        else {
            $encpJquery('.presentationPageDownButton').removeClass('hover');
        }
    }
    else {
        this.fadeOut();
    }
};

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

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

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

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

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