var EHDI = EHDI || Object.create(null);

EHDI.components = EHDI.components || Object.create(null);

EHDI.components.pauseButton = function() {
    EHDI.displays.Button.call(this, EHDI.Assets.images["btn_pause"], EHDI.Assets.images["btn_pause2"], null, null);

    this.isPaused = false;
    this.isEndGame = false;
    
    EHDI.GAME.soundManager.autoResumeToggle(false);
    
    this.setOnClickFunction(this.sfxThenPause.bind(this));
    
    this.position.set(EHDI.GAME.sceneManager.getStageWidth() * 0.95, EHDI.GAME.sceneManager.getStageHeight() * 0.075);
    
    EHDI.GAME.pauseButton = this;

    this.pauseTheGame = EHDI.GAME.pauseButton.pauseGame.bind(EHDI.GAME.pauseButton).bind(EHDI.GAME.pauseButton);
    this.checkIfInterrupt = EHDI.GAME.pauseButton.checkInterrupt.bind(EHDI.GAME.pauseButton);
    
    window.addEventListener("blur", this.pauseTheGame);
    window.addEventListener("pagehide", this.pauseTheGame);
    window.addEventListener("webkitvisibilitychange", this.checkIfInterrupt);
    window.addEventListener("visibilitychange", this.checkIfInterrupt);
    
};

EHDI.components.pauseButton.prototype = Object.create(EHDI.displays.Button.prototype);

EHDI.components.pauseButton.prototype.sfxThenPause = function() {
    EHDI.GAME.soundManager.playSFX("button_sfx");
    this.pauseGame();
}

EHDI.components.pauseButton.prototype.checkInterrupt = function() {
    if (window.document.webkitHidden || window.document.hidden) {
			this.pauseGame();
    }
};

EHDI.components.pauseButton.prototype.pauseGame = function() {
    if(this.isPaused || this.isEndGame)
        return;
    this.isPaused = true;
    EHDI.GAME.sceneManager.pushPopUp(new EHDI.popup.PausePopUp(), {y : new EHDI.scene.TransitionParameter(-EHDI.GAME.sceneManager.getStageHeight(), EHDI.GAME.sceneManager.getStageHeight() * 0.5), duration : 0.25});
}; 

EHDI.components.pauseButton.prototype.resumeGame = function() {
    this.isPaused = false;
};

EHDI.components.pauseButton.prototype.endGame = function() {
    this.isEndGame = true;
//    this.hidePauseButton();
    window.removeEventListener("blur", this.pauseTheGame);
    window.removeEventListener("pagehide", this.pauseTheGame);
    window.removeEventListener("webkitvisibilitychange", this.checkIfInterrupt);
    window.removeEventListener("visibilitychange", this.checkIfInterrupt);
};

EHDI.components.pauseButton.prototype.destroy = function() {
    EHDI.GAME.soundManager.autoResumeToggle(true);
	EHDI.aka.Sprite.prototype.destroy.apply(this, arguments);
}

EHDI.components.pauseButton.prototype.hidePauseButton = function() {
    TweenLite.to(this, 0.25, {y : -this.y});
};