
var promptDelayMax=1000;

var Player = function() {
    this.thumb=new Thumb("b");
    this.lockCallback=function () {};
    this.thumb.lock(bind(this, 'lockCallback'));
    

    this.pushDelayTimer = $.timer( 10000, bind(this, 'pushPrompt') );
    this.pushDelayTimer.stop();

    this.resistDelayTimer = $.timer( 10000, bind(this, 'resistPrompt') );
    this.resistDelayTimer.stop();
    
    this.pushPromptTimer = $.timer(5000, function() { $("#push").click(); });
    this.pushPromptTimer.stop();
    
    this.resistPromptTimer = $.timer(5000, function() { $("#resist").click(); });
    this.resistPromptTimer.stop();

    this.energyMeter = new EnergyMeter("player");
    this.pinnedCallback=function() {};
    this.energyMeter.empty( bind(this, 'pinnedCallback') );

    this.initialize = function () {
        this.thumb.reset();
        this.energyMeter.initialize();
    }

    this.playerMove = function ( event ) {
        var player=event.data;
        player.thumb.move($(this));
    }

    this.lock = function(callBack) {
        this.lockCallback=callBack;
    }

    this.start = function() {
       var obj=this;
        $(".position").bind('click', obj, obj.playerMove );        
    }

    this.stop = function() {
        this.pushDelayTimer.stop();
        this.resistDelayTimer.stop();
        this.pushPromptTimer.stop();
        this.resistPromptTimer.stop();
        var obj=this;
        $(".position").unbind('click', obj.playerMove );
    }

//  LOCK METHODS
    var obj=this;
    
    this.randomPromptX = function() {
        return Math.floor(Math.random()*52);
    }
    
    this.randomPromptY = function() {
        return Math.floor(Math.random()*59);
    }
    
    this.push = function( callback ) {
        this.pushCallback=callback;
        this.pushDelayTimer.reset( Math.floor(Math.random() * promptDelayMax));
    }
    
    this.pushPrompt = function() {
        this.pushDelayTimer.stop();
        this.startPrompt=new Date();
        this.pushPromptTimer.reset(2000);
        var obj=this;
        $("#push").bind( 'click', obj, obj.pushClick );
        $("#push").css("top",this.randomPromptX()).
            css("left",this.randomPromptY()).appendTo($(this.thumb.position)).show();
    }

    this.pushClick = function(event) {
        var player=event.data;
        var endPrompt=new Date();
        player.pushPromptTimer.stop();
        var responseTime=endPrompt.getTime()-player.startPrompt.getTime();
        $("#push").hide().unbind('click');
        player.pushCallback(responseTime);
    }

    this.resist = function( callback ) {
        this.resistCallback=callback;
        this.resistDelayTimer.reset( Math.floor(Math.random() * promptDelayMax));
    }
    
    this.resistPrompt = function() {
        this.resistDelayTimer.stop();
        this.startPrompt=new Date();
        var positions=$(".position").not("#"+this.thumb.position.attr('id'));
        var position=positions[ Math.floor( Math.random() * positions.length ) ];
        this.resistPromptTimer.reset(6000);
        var obj=this;
        $("#resist").bind( 'click', obj, obj.resistClick );
        $("#resist").css("top",this.randomPromptX()).
            css("left",this.randomPromptY()).appendTo($(position)).show();
    }

    this.resistClick = function(event) {
        var player=event.data;
        var endPrompt=new Date();
        player.resistPromptTimer.stop();
        var responseTime=endPrompt.getTime()-player.startPrompt.getTime();
        $("#resist").hide().unbind('click');
        player.resistCallback(responseTime);
    }

    this.reduceEnergy = function( damage ) {
        this.energyMeter.reduce(damage);
    }

    this.pinned = function(callback) {
        this.pinnedCallback=callback;
    }
}
