var yellow = '#FFFCA4';
var green = '#CFEB32';

var mask, score, time = 0, hits;
var freq, disp, points, threshold, level = 1;
var timeouts = [];

var scoreDiv,timerDiv,statusDiv,buttonDiv,levelDiv;


function start(box){
    if(time > 0){return;}
    scoreDiv = document.getElementById('squasherScore_' + box);
    timerDiv = document.getElementById('squasherTimer_' + box);
    statusDiv = document.getElementById('squasherStatus_' + box);
    buttonDiv = document.getElementById('squasherBtn_' + box);
    levelDiv = document.getElementById('squasherLevel_' + box);
    document.getElementById('smellSquasherVisitor_' + box).style.display = 'none';
    document.getElementById('smellSquasher_' + box).style.display = 'block';
    buttonDiv.style.display = 'none';
    statusDiv.style.display = 'none';
    statusDiv.innerHTML = '';
    mask = 0;
    time = 60;
    hits = 0;
    if (score > 0){
        for (var i = 0 ; i < 25 ; i++){
            var e = document.getElementById('cell_' + box + '_' + i);
            e.style.background = '';
            e.style.backgroundColor = (i%2 == 0 ? yellow : green);
        }
    } 
    if (level == 1) {
        score = 0;
        scoreDiv.innerHTML = score;
    } 
    var levelData = levels[level - 1];
    freq = levelData[0];
    disp = levelData[1];
    points = levelData[2];
    threshold = levelData[3];
    setTimeout('decr()', 1000);
    on(box, Math.floor(Math.random()*25));
}

function end(){
    if (hits >= threshold){
        if (level < levels.length - 1){
            buttonDiv.className = 'button';
            statusDiv.innerHTML = 'Congrats! You have passed Level ' + level + ' by squashing ' + threshold + ' or more smells. Are you ready for Level ' + (level+1) + '?'; 
        } else if (level == levels.length - 1){
            buttonDiv.className = 'button';
            statusDiv.innerHTML = 'Congrats! You\'ve made it to Level ' + levels.length + ', the last level! Good Luck!';
        } else {
            statusDiv.innerHTML = 'Great job! Your final score is: ' + score + '. <a href="/widgets/smell_squasher/top_players.php">Click here</a> to see the Top Scores for this game on CafeMom?! Would you like to play again?';
            buttonDiv.className = 'buttonAgain';
            submitResult();
            level = 0;
        }
        level++;
    } else {
        if (level < levels.length){
            statusDiv.innerHTML = 'Your final score after Level ' + level + ' is: ' + score + '. Next time, squash ' + threshold + ' or more smells to get to Level ' + (level+1) + '! <a href="/widgets/smell_squasher/top_players.php">Click here</a> to see the Top Scores for this game on CafeMom?! Would you like to play again?'; 
        } else {
            statusDiv.innerHTML = 'Great job! Your final score is: ' + score + '. <a href="/widgets/smell_squasher/top_players.php">Click here</a> to see the Top Scores for this game on CafeMom?! Would you like to play again?';
        } 
        buttonDiv.className = 'buttonAgain';
        submitResult();
        level = 1;
    }
    statusDiv.style.display = 'block';
    buttonDiv.style.display = 'block';
    levelDiv.innerHTML = 'Level ' + level;
}

function submitResult(){
    ajax_getPage("/widgets/smell_squasher/actions/save_result_ajax.php?score=" + score,
                      function(resultdata){
                      }
    );
}

function decr(){
    timerDiv.innerHTML = --time ;
    if (time > 0){
        setTimeout('decr()', 1000);
    } else {
        end();
    }
}

function on(box, id){
    if(time <= 0)return; 
    var bit = 1 << id ;
    while ((mask | bit) == mask){
        id = Math.floor(Math.random()*25);
        bit = 1 << id ;
    }
    mask = mask | bit ;
    var e = document.getElementById('cell_' + box + '_' + id);
    e.style.background = (id%2==0 ? smells_yellow[Math.floor(Math.random()*smells_yellow.length)] : smells_green[Math.floor(Math.random()*smells_green.length)]);
    var key = setTimeout("off(" + box + "," + id + ")", disp);
    timeouts[id] = key;
    setTimeout("on(" + box + "," + Math.floor(Math.random()*25) + ")", freq);
}

function off(box, id){
    if(time <= 0){return;}
    var e = document.getElementById('cell_' + box + '_' + id);
    e.style.background = '';
    e.style.backgroundColor = (id%2 == 0 ? yellow : green);
    mask = mask & ~(1<<id);
}

function resetCell(box, id){
    var e = document.getElementById('cell_' + box + '_' + id);
    if (mask == (mask | (1<<id))){return;}
    e.style.background = '';
    e.style.backgroundColor = (id%2 == 0 ? yellow : green);
}

function checkClick(e, box, id){
    if(time <= 0)return;
    if (mask == (mask | (1<<id))){
        clearTimeout(timeouts[id]);
        e.style.background = squashed +(id%2 == 0 ? 'yellow' : 'green')+ '.gif")';
        setTimeout('resetCell(' + box + ',' +id+ ')', 700);
        score += points;
        hits++;
        scoreDiv.innerHTML = score;
        mask = mask & ~(1<<id);
    }
}
