var MazeTimer = Class.create({

    initialize: function() {
        
        this.timerDivId = 'timer';
        this.clockId = 'time';
        this.initialTimeText = "Use the arrow keys to begin.";
        this.alive = false;
        
        // Draw clock
        var divTimer = new Element('div', {'id': this.timerDivId});
        divTimer.update("Current Time:<br />");
        var spanClock = new Element('span', {'id': this.clockId});
        spanClock.update(this.initialTimeText);
        divTimer.appendChild(spanClock);
        $('leftContainer').appendChild(divTimer);
        
    },

    start: function() {
    
        if (!this.alive) {
    
            this.start = new Date();
            this.alive = true;
            
            // start timer
            this.intervalId = setInterval(this.update.bind(this), 5000);
            this.update();
        
        }

    },
    
    update: function(defaultStr) {
        
        if (!this.alive) { return; }
        
        var currentTime = new Date();
        var diffSecs = (currentTime.getTime() - this.start.getTime()) / 1000;
        var diffMinutes = Math.floor(diffSecs / 60);
        
        var str;
        if (typeof(defaultStr) == "string") {
            str = defaultStr;
        } else if (diffSecs < 30) {
            str = "Just started...";
        } else if (diffSecs < 60) {
            str = "Over 30 seconds...";
        } else if (diffMinutes == 1) {
            str = "Over a minute...";
        } else {
            str = "Over " + diffMinutes + " minutes...";
        }
        
        $(this.clockId).update(str);
        
    },

    stop: function() {
        
        this.end = new Date();
        var diffSecs = (this.end.getTime() - this.start.getTime()) / 1000;
        var diffMinutes = Math.floor(diffSecs / 60);
        diffSecs = Math.floor(diffSecs % 60);
        
        var str = "Congrats! You finished in ";
        if (diffMinutes > 1) {
            str += diffMinutes + " minutes and ";
        } else if (diffMinutes == 1) {
            str += "1 minute and ";
        }
        str += diffSecs + " seconds.";
        
        $(this.clockId).update(str);
        
        clearInterval(this.intervalId);
        this.alive = false;
        
    }

});
