var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};


/* global variables */
var num_sheets = 3;
var game_mode = 0;

var width = 16;
var height = 12;
var lose_height = 2;
var block_width = 26;
var block_padding = 2;
var b_total_w = block_width + block_padding;
var total_width = width * (block_width + block_padding);

var btable = new Array(width);
var ft = 5;

var square_width = block_width * 2;
var sq_half = square_width / 2;
var init_sq_width = square_width - 16;

var square_count = 0;
var squares_total = 0;
	
var curpos_x = (width / 2) - 1;
var curpos_y = 0;

var startpos_x = (width / 2) - 1;
var startpos_y = 0;

var movesafe = false;

var block_hs = Array(width);

/* displayed squares */
var all_squares = new Array(width);

var is_ie = false;
var MODNUM = 200;
var TIME_INT = 30;
var DARK = 2;
var LIGHT = 1;
var NONE = 0;
var SIXTY = 1;

/* temporaries */
var max_sqs = 30;
var t_x = new Array( max_sqs );
var t_y = new Array( max_sqs );
var t_w = new Array( max_sqs );
var t_sq = new Array( max_sqs );
var t_int = new Array(max_sqs);
var t_id;

var paused = false;

/* locks ****/
var hs_lock = 0;
var bt_lock = 0;
var drop_lock = 0;

function lock_b(lc) {
	while(lc) {}
	lc = 1;
}

function unlock_b(lc) {
	lc = 0;
}

/* functions *************************/	
function writet(str) {
	//document.getElementById("temp_text").innerHTML += str + "<br>\n";
}
function cleart() {
	document.getElementById("temp_text").innerHTML = "";
}

/* find and display squares of 4 */
function animate_sq(id) {
	if (t_w[id] <= square_width) {
		var temp = (sq_half - (t_w[id] / 2));

		t_sq[id].style.width = t_w[id] + "px";
		t_sq[id].style.height = t_w[id] + "px";	
		t_sq[id].style.top = (t_y[id] * b_total_w) + temp + "px";
		t_sq[id].style.left = (t_x[id] * b_total_w) + temp + "px";	
		t_w[id] += 2;
		
		t_sq[id].style.visibility = "visible";		
	}
	else {
		clearInterval(t_int[id]);
	}
}

function display_square(x, y, color) {
	var o = square_count;
	square_count++;	

	/* mark btable if unmarked */
	if (btable[x][y] / ft < 1) {
		btable[x][y] += ft;
	}
	if (btable[x + 1][y] / ft < 1) {
		btable[x + 1][y] += ft;
	}
	if (btable[x][y + 1] / ft < 1) {
		btable[x][y + 1] += ft;
	}
	if (btable[x + 1][y + 1] / ft < 1) {
		btable[x + 1][y + 1] += ft;
	}		
		
	if (square_count >= max_sqs) {
		square_count = 0;
	}
		
	t_x[o] = x;
	t_y[o] = y;
	t_id = "square" + o;
	
	var sq = document.createElement("div");
	
	if (color == DARK) {
		sq.className = "dark_square";
	}
	else if (color == LIGHT) {
		sq.className = "light_square";
	}
	
	sq.id = t_id;
	sq.title = x * MODNUM + y;
	t_w[o] = init_sq_width;		
	sq.style.visibility = "hidden";
	t_sq[o] = sq;

	all_squares[x].push(sq);
	document.getElementById("squares").appendChild(sq);	
	t_int[o] = window.setInterval('animate_sq(' + o + ')', 20);	
}

/* generate functions **************************/
function generate_rand_block(x, y) {
	var rand = Math.floor(Math.random() * 2);	
	var block = document.getElementById("b" + x + "," + y);
	
	if (rand == 0) {		
		block.className = "dark";
	}
	else {
		block.className = "light";
	}
}

function generate_rand_start() {
	generate_rand_block(startpos_x, startpos_y);	
	generate_rand_block(startpos_x + 1, startpos_y);
	generate_rand_block(startpos_x + 1, startpos_y + 1);
	generate_rand_block(startpos_x, startpos_y + 1);
	
	curpos_x = startpos_x;
	curpos_y = startpos_y;
}

/* Messages ************************/
function display_message(text) {
	var x = document.createTextNode(text);
	var n = document.getElementById("messages").firstChild; 
	if (n) {
	   n.nodeValue = text;
	}  
}

function clear_message() {
	var n = document.getElementById("messages");
	if (n.firstChild) {
	   n.firstChild.nodeValue = "";
	}   
}

/* RESETTING **********************/
function block_clear(block) {
	block.className =  "block";
}

function block_clear_xy(x, y) {
	var block = document.getElementById("b" + x + "," + y);
	block.className = "cleared";
}

function clear_grid() {
	var i;
	var j;
	var b;
	var sq;
	var sqs = document.getElementById("squares");	
	for (i = 0; i < height; i++) {
		for (j = 0; j < width; j++) {
			b = document.getElementById("b" + j + "," + i);
			block_clear(b);
			btable[j][i] = NONE;			
		}			
	}

	for (j = 0; j < width; j++) {
		block_hs[j] = height;
		while (all_squares[j].length > 0) {
			sq = all_squares[j].pop();
			sqs.removeChild(sq);
		}
	}	

	square_count = 0;
	squares_total = 0;
	var n = document.getElementById("squares_num").firstChild;
	if (n) {
		n.nodeValue = squares_total;
	}	
}

/* check for boxes of 4 ************/
function check_box(x1, y1, c11, c12, x2, y2, c21, c22) {
	if (c11 == c12) {
		if (x1 > 0) {
			if ((btable[x1 - 1][y1] % ft == c11) && (btable[x1 - 1][y1 + 1] % ft == c11)) {
				display_square(x1 - 1, y1, c11);
			}
		}
		if ((btable[x1 + 1][y1] % ft == c11) && (btable[x1 + 1][y1 + 1] % ft == c11)) {
				display_square(x1, y1, c11);
		}
	}
	if ((y1 + 2) < height) {
		if (x1 > 0) {
			if ((btable[x1 - 1][y1 + 1] % ft == c12) && (btable[x1 - 1][y1 + 2] % ft == c12) && (btable[x1][y1 + 2] % ft == c12)) {
				display_square(x1 - 1, y1 + 1, c12);
			}
		}
		if ((btable[x1][y1 + 2] % ft == c12) && (btable[x1 + 1][y1 + 1] % ft == c12) && (btable[x1 + 1][y1 + 2] % ft == c12)) {
			display_square(x1, y1 + 1, c12);
		}
	}

	if (c21 == c22) {
		if ((y2 > (y1 + 1)) && (btable[x2 - 1][y2] % ft == c21) && (btable[x2 - 1][y2 + 1] % ft == c21)) {
			display_square(x2 - 1, y2, c21);	
		}
		if ((x2 + 1) < width) {
			if ((btable[x2 + 1][y2] % ft == c21) && (btable[x2 + 1][y2 + 1] % ft == c21)) {
				display_square(x2, y2, c21);			
			}
		}	
	}
	if ((y2 + 2) < height) {
		if ((y2 > y1) && (btable[x2 - 1][y2 + 1] % ft == c22) && (btable[x2 - 1][y2 + 2] % ft == c22) && (btable[x2][y2 + 2] % ft == c22)) {
			display_square(x2 - 1, y2 + 1, c22);
		}
		
		if ((x2 + 1) < width) {
			if ((btable[x2][y2 + 2] % ft == c22) && (btable[x2 + 1][y2 + 1] % ft == c22) && (btable[x2 + 1][y2 + 2] % ft == c22)) {
				display_square(x2, y2 + 1, c22);
			}
		}
	}	
}

/* dropping ***********************/

var drop_cols = new Array(2);
drop_cols[0] = new Array(height - lose_height);
drop_cols[1] = new Array(height - lose_height); 

var classes = new Array(2);
classes[0] = new Array(2);
classes[1] = new Array(2);

var intervals = new Array(2);
var drop_pos = new Array(2);

var a1, a2;

function slide_sq_animate(curx) {
	var cury1 = drop_pos[0];
	var cury2 = drop_pos[1];
	var yh1 = block_hs[curx] - 2;
	var yh2 = block_hs[curx + 1] - 2;	
	drop_pos[0] += 1;
	drop_pos[1] += 1;

	if (cury1 < yh1) {
		if (cury1 >= drop_cols[0].length) {
			document.getElementById("temp_text").innerHTML += "!!!! ";
		}
		drop_cols[0][cury1 + 2].className = classes[0][1];
		drop_cols[0][cury1 + 1].className = classes[0][0];
		block_clear(drop_cols[0][cury1]);
	}
	if (cury2 < yh2) {
		drop_cols[1][cury2 + 2].className = classes[1][1];
		drop_cols[1][cury2 + 1].className = classes[1][0];
		block_clear(drop_cols[1][cury2]);
	}
	//if ((cury1 >= yh1) && (cury2 >= yh2)) {
	if (((cury1 + 1) >= yh1) && ((cury2 + 1) >= yh2)) {
		//clearInterval(intervals[0]);		
		/* record colors in btable */
		btable[curx][yh1] = a1[0];
		btable[curx][yh1 + 1] = a1[1];

		/* record colors in btable */
		btable[curx + 1][yh2] = a2[0];
		btable[curx + 1][yh2 + 1] = a2[1];

		// adjust height of column
		block_hs[curx] -= 2;
		// adjust height of column
		block_hs[curx + 1] -= 2;
		
		writet(">> DROP X: " + curx + "  >h1 = " + (height - block_hs[curx]) + " >h2 = " + (height - block_hs[curx + 1]));
		movesafe = true;
		check_box(curx, yh1, a1[0], a1[1], curx + 1, yh2, a2[0], a2[1]);
		generate_rand_start();
	}
	else {
		window.setTimeout('slide_sq_animate(' + curx + ")", 10);
	}	
}

function slide_sq(x1, y1, yh1, yh2) {
	var x2 = x1 + 1;
	var h1 = yh1 - y1 + 1;
	var h2 = yh2 - y1 + 1;	
	
	for (i = 0; i <= h1; i++) {
		drop_cols[0][i] = document.getElementById("b" + x1 + "," + (i + y1));
	}	
	for (i = 0; i <= h2; i++) {
		drop_cols[1][i] = document.getElementById("b" + x2 + "," + (i + y1));
	}	
	
	window.setTimeout('slide_sq_animate(' + x1 + ")", 10);
}

function drop_sq(i, cur_y, num) {
	var y1 = cur_y + 1;
	var b1; 	
	var orig0 = document.getElementById("b" + i + "," + cur_y);
	var orig1 = document.getElementById("b" + i + "," + y1);	
	var c = Array(3);
	c[0] = LIGHT;  // top block color
	c[1] = LIGHT;  // bottom block color
		
	c[2] = block_hs[i]; // ypos of top block	
	b1 = c[2] + 1;

	if ((c[2] - 2) < lose_height) {
		c[2] = lose_height;
		lose_game();	
		return;
	}
	if (orig0.className == "dark") {
		c[0] = DARK;
	}
	if (orig1.className == "dark") {
		c[1] = DARK;
	}

	drop_pos[num] = 0;
	classes[num][0] = orig0.className;
	classes[num][1] = orig1.className;
	
	return c;
}

function drop() {		
	movesafe = false;
	a1 = drop_sq(curpos_x, curpos_y, 0);
	a2 = drop_sq(curpos_x + 1, curpos_y, 1);	
	
	writet(">> DROP START: " + curpos_x);
	slide_sq(curpos_x, curpos_y, height - lose_height, height - lose_height);	
	//check_box(curpos_x, a1[2], a1[0], a1[1], curpos_x + 1, a2[2], a2[0], a2[1]);
}

/* basic movements **************************/
function strafe(x, y, dir) {
	var x1 = x + 1;
	var y1 = y + 1;
	var block00 = document.getElementById("b" + x + "," + y);
	var block10 = document.getElementById("b" + x1 + "," + y);
	var block11 = document.getElementById("b" + x1 + "," + y1);	
	var block01 = document.getElementById("b" + x + "," + y1);
		
	if (dir == 0) {
		if (x > 0) {
			var x2 = x - 1;
			document.getElementById("b" + x2 + "," + y).className =  block00.className;
			document.getElementById("b" + x2 + "," + y1).className =  block01.className;			
			block00.className =  block10.className;
			block01.className =  block11.className;
			
			block_clear(block10);
			block_clear(block11);			
			
			curpos_x -= 1;
		}
	}
	else {
		if (x < width) {
			var x2 = x1 + 1;
			document.getElementById("b" + x2 + "," + y).className =  block10.className;
			document.getElementById("b" + x2 + "," + y1).className =  block11.className;			
			block10.className =  block00.className;
			block11.className =  block01.className;
						
			block_clear(block00);
			block_clear(block01);			

			curpos_x += 1;
		}		
	}
	
	//document.getElementById("temp_text_2").innerHTML = curpos_x;
}

function rotate(x, y, dir) {
	var x1 = x + 1;
	var y1 = y + 1;
	var block00 = document.getElementById("b" + x + "," + y);
	var block10 = document.getElementById("b" + x1 + "," + y);
	var block11 = document.getElementById("b" + x1 + "," + y1);	
	var block01 = document.getElementById("b" + x + "," + y1);
			
	var temp = block00.className;
	
	if (dir == 0) {
		block00.className =  block10.className;
		block10.className =  block11.className;
		block11.className =  block01.className;
		block01.className =  temp;	
	}
	else {
		block00.className =  block01.className;
		block01.className =  block11.className;
		block11.className =  block10.className;
		block10.className =  temp;		
	}
	//document.getElementById("temp_text").firstChild.nodeValue(temp);
}

function rotate_ccw(x, y) {
	rotate(x, y, 0);
}
function rotate_cw(x, y) {
	rotate(x, y, 1);
}

/* clearing bar **********************************/
var bar_x;
var bar_i;
var squarebox;

/* clear one 2x2 block at x, y */
function clear_blocks(x, y, b_table) {
	var x2 = x + 1;
	var y2 = y + 1;
	
	/* clear in btable */
	if (b_table[x][y] / ft >= 1) {
		b_table[x][y] = NONE;
	}
	if (b_table[x][y2] / ft >= 1) {
		b_table[x][y2] = NONE;
	}
	
	if (b_table[x2][y] / ft >= 1) {
		b_table[x2][y] = NONE;
	}
	if (b_table[x2][y2] / ft >= 1) {
		b_table[x2][y2] = NONE;
	}			
	
	/* clear in display */	
	block_clear_xy(x, y);
	block_clear_xy(x, y2);
	block_clear_xy(x2, y);
	block_clear_xy(x2, y2);
	//window.setTimeout('block_clear_xy(' + x2 + ',' + y + ')', TIME_INT * width / 2);
	//window.setTimeout('block_clear_xy(' + x2 + ',' + y2 + ')', TIME_INT * width / 2);	 
}

/* check new boxes made */
function check_d_box(x, y, b_table) {
	var x1 = x + 1;
	var y1 = y + 1;
	var clr1 = b_table[x][y] % ft;
	
	if ((b_table[x][y1] % ft == clr1) && (b_table[x1][y] % ft == clr1) && (b_table[x1][y1] % ft == clr1)) {
		display_square(x, y, clr1);
	}
}

/* remove squares, but not the blocks */
function remove_squares(x, y, table) {
	/* unmark btable if marked */
	if (table[x][y] / ft > 1) {
		table[x][y] -= ft;
	}
	if (table[x + 1][y] / ft > 1) {
		table[x + 1][y] -= ft;
	}
	if (table[x][y + 1] / ft > 1) {
		table[x][y + 1] -= ft;
	}
	if (table[x + 1][y + 1] / ft > 1) {
		table[x + 1][y + 1] -= ft;
	}	
}

/* consolidate column */ 
function drop_col(x, checknext, b_table) {
	var efound = false;
	var top = -1;
	var numb = height - block_hs[x];
	var nume = 0;
	var blocks = new Array();
	var i;
	var h = height - 1;
	var last = 0;
	var sqs = document.getElementById("squares");
	var sq;
	var x1; 
	var y1;
	
	for (i = 0; i < numb; i++) {
		if (b_table[x][h - i] == NONE) {
			nume++;
			
			if (!efound) {
				efound = true;
				top = h - i;
			}			
		}
		if (efound) {
			blocks.push(document.getElementById("b" + x + "," + (h - i)));
		}	
	}
	
	block_hs[x] = block_hs[x] + nume;
	writet("Consolidating: X: " + x + " > numb = " + numb + " > nume = " + nume);
			
	var len = blocks.length;
	for (i = 0; i < len; i++) {	
		if (b_table[x][top - i] == NONE) {
			blocks[i].className = "block";
			continue;		
		}
		else {
			blocks[last].className = blocks[i].className;
			blocks[i].className = "block";
			b_table[x][top - last] = b_table[x][top - i];
			b_table[x][top - i] = NONE;
			last++;
		}
	}
	
	len = len - nume;
	/* delete all boxes made in that column after bar passed */
	while (all_squares[x - 1].length > 0) {
		sq = all_squares[x - 1].pop();
		sqs.removeChild(sq);	
		x1 = Math.floor(sq.title / MODNUM);
		y1 = sq.title % MODNUM;
		remove_squares(x1, y1, b_table);
	}

	/* check x, next column for new boxes made */
	if (checknext) {
		for (i = 0; i < len; i++) {
			check_d_box(x, top - i, b_table);
		}		
	}	
	/* check prev, x column for new boxes made */
	for (i = 0; i < len; i++) {
		check_d_box(x - 1, top - i, b_table);
	}
}

var prevdrop = -1;

function clear_sqs(x, b_table) {
	var sq;
	var sqs = document.getElementById("squares");
	var x1 = -1;
	var y1;

	var newp = false;
	
	if (x == 0) {
		cleart();
	}
	writet(x);
	
	while (all_squares[x].length > 0) {
		sq = all_squares[x].pop();
		sqs.removeChild(sq);
		squares_total++;
		square_count--;
		x1 = Math.floor(sq.title / MODNUM);
		y1 = sq.title % MODNUM;
		clear_blocks(x, y1, b_table);	
		
		newp = true;
	}

	var n = document.getElementById("squares_num").firstChild;
	if (n) {
		n.nodeValue = squares_total;
	}

	if (newp) {
		if (prevdrop < 0) {
			prevdrop = x;
		}	
	}
	if (!newp) {
		if (prevdrop >= 0) {
			for (var i = prevdrop; i < x; i++) {
				drop_col(i, false, b_table);
			}
			drop_col(x, true, b_table);
			prevdrop = -1;
		}
	}
}
function move_bar() {
	var oldb = bar_x;
	document.getElementById("timebar").style.left = oldb + "px";
	bar_x += 2;
	
	if ((bar_x) > total_width) {
		bar_x = 0;
	}
	else {
		if ((oldb % (b_total_w)) == 0) {
			clear_sqs(Math.floor(oldb / b_total_w), btable);
			//display_message(Math.floor(oldb / b_total_w));
		}
	}
}

/* gameplay *******/
var time_left;
var int_time;
var high_score = 0;


/* user controls ***********/
var first_play = 0;

function change_pause() {
	document.getElementById("pausebutton").onclick = start_bar;
	movesafe = false;
	document.getElementById("pausebutton").innerHTML = "Resume Game";		
}
function change_resume() {
	document.getElementById("pausebutton").onclick = stop_bar;
	movesafe = true;
	document.getElementById("pausebutton").innerHTML = "Pause Game";		
}
function stop_bar() {
	clearInterval(bar_i);	
	window.setTimeout(change_pause,0);
	
	if (game_mode == SIXTY) {
		clearInterval(int_time);
	}	
	return false;
}
function start_bar() {
	bar_i = window.setInterval(move_bar, TIME_INT);
	
	if (game_mode == SIXTY) {
	   int_time = window.setInterval(count_60, 1000);
	}   
	window.setTimeout(change_resume,0);
	return false;
}
function init_bar() {
	paused = false;	
	document.getElementById("timebar").style.height = (height * (block_width + block_padding) - block_padding) + "px";
	document.getElementById("limitbar").style.width = (width * (block_width + block_padding) - block_padding) + "px";
	bar_x = 0;
	bar_i = window.setInterval(move_bar, TIME_INT);
}

function change_pausem() {
	document.getElementById("musicbutton").onclick = play_music;
	document.getElementById("musicbutton").innerHTML = "Play Music";		
}
function change_resumem() {
	document.getElementById("musicbutton").onclick = pause_music;
	document.getElementById("musicbutton").innerHTML = "Pause Music";		
}

function play_music() {
	if (first_play == 0) {
		document.getElementById("musicbutton").innerHTML = "Loading...";
		soundManager.createSound({id:'bg1',url:'m-fb.mp3'});
		first_play = 1;
	}	
	soundManager.resume('bg1');				
	window.setTimeout(change_resumem,0);
	return false;
}
function pause_music() {
	soundManager.pause('bg1');
	window.setTimeout(change_pausem,0);
	return false;
}

function lose_game() {
	document.getElementById("lose_game").style.display = 'block';	
	clearInterval(int_time);
	clearInterval(bar_i);
	movesafe = false;
}

/* 60 second mode  */
var orig_color;

function end_60() {
	if (squares_total > high_score) {
		high_score = squares_total;
		document.getElementById("high_score").innerHTML = high_score;
	}
	movesafe = false;
	clearInterval(bar_i);
	document.getElementById("time_left").style.color = orig_color;		
	document.getElementById("end_game_60").style.display = 'block';
	document.getElementById("current_score").innerHTML = squares_total;
	//restart_game();
}

function count_60() {
	time_left--;
	game_mode = SIXTY;
	document.getElementById("time_left").innerHTML = time_left;
	if (time_left <= 15) {
		orig_color = document.getElementById("time_left").style.color;
	    document.getElementById("time_left").style.color = "#ED3200";		 
	}
	if (time_left <= 0) {
		clearInterval(int_time);
		end_60();
	}
}

function start_60() {
	window.setTimeout(change_resume, 10);

	document.getElementById("choose_game").style.display = 'none';
	time_left = 60;
	document.getElementById("time_left").innerHTML = time_left;
	
	int_time = window.setInterval(count_60, 1000);
	
	generate_rand_start();
	init_bar();	
	movesafe = true;	
}

/* controls ******************/
function handleInput_space(e) {
	if (!e) { 
		e = window.event;
	}
	var key = String.fromCharCode(e.keyCode);
	
	if (key == ' ') {
		if (paused) {
			paused = false;				
			stop_bar();	
			start_bar();
		}
		else {
			stop_bar();	
			paused = true;			
		}	
	}
}

function handleInput(e) {
	if (!e) { 
		e = window.event;
	}
	
	var key = String.fromCharCode(e.keyCode);
	
	if (movesafe) {
		if (key == 'A') {
			strafe(curpos_x, curpos_y, 0);
		}
		else if (key == 'D') {
			strafe(curpos_x, curpos_y, 1);
		}
		else if (key == 'S') {
			drop();		
		}	
		else if (key == 'K') {
			rotate_ccw(curpos_x, curpos_y);
		}
		else if (key == 'L') {
			rotate_cw(curpos_x, curpos_y);
		}	
	}			
}
function restart_game() {
	movesafe = false;

	clearInterval(bar_i);	
	clear_grid();
	
	curpos_x = (width / 2) - 1;
	curpos_y = 0;
	
	if (game_mode == SIXTY) {
		clearInterval(int_time);
		time_left = 60;
	}	
		
	document.getElementById("lose_game").style.display = 'none';
	document.getElementById("end_game_60").style.display = 'none';
	document.getElementById("choose_game").style.display = 'block';
	/*	
	generate_rand_start();
	init_bar();
	movesafe = true;
	*/
	return false;
}

/* INIT *******************/
function ie_correct(num) {
	if (BrowserDetect.browser.indexOf("Explorer") != -1) {
	   if (BrowserDetect.version == 7) {
	   	is_ie = true;
		  var st = document.styleSheets[num];
		  st.addRule("#timebar","top:10px");
	      document.getElementById("limitbar").style.top = (2 * (block_width + block_padding) - block_padding + 10) + "px";
		  st.addRule(".dark_square","margin-top:10px");
		  st.addRule(".light_square","margin-top:10px");
		  TIME_INT = 20;  
	   }
	}
}

function init_grid() {
	var i = 0;
	var j = 0;
	
	document.getElementById("limitbar").style.top = (2 * (block_width + block_padding) - block_padding) + "px";	
	var old = document.getElementById("stage_container").getAttribute("style");
	document.getElementById("stage_container").style.width = total_width + "px";
	var st = document.styleSheets[0];
	
	if (st.addRule) {
	  	st.addRule(".message","width:" + (total_width - 52) + "px");
	}
	else if (st.insertRule) {
		st.insertRule(".message { width: "+ (total_width - 52) + "px;}", st.cssRules.length);
	}	
	
	for (i = 0; i < height; i++) {
		for (j = 0; j < width; j++) {
			var b = document.createElement("div");
			b.className = "block";
			/* b.className = "block"; */
			b.id = "b" + j + "," + i;	
			/* setAttribute("style", "background: url(images/t1-light.gif) no-repeat"); */

			document.getElementById("stage").appendChild(b);
		}		
	}

	for (j = 0; j < width; j++) {
		block_hs[j] = height;
		btable[j] = new Array(height);
		all_squares[j] = new Array();
	}
	curpos_x = (width / 2) - 1;
	curpos_y = 0;
	square_count = 0;
	
	ie_correct(0);
}


function init() {
	BrowserDetect.init();	
	document.onkeypress = handleInput;
	document.onkeydown = handleInput;
	document.onkeyup = handleInput_space;
	
	for (var i = 2; i <= num_sheets; i++) {
		document.styleSheets[i].disabled = true;
	}
	document.getElementById("loading").style.display = 'block';	
	
	init_grid();	
	document.getElementById("choose_game").style.display = 'block';		
	document.getElementById("loading").style.display = 'none';		
}

window.onload = init;

soundManager.onload = function() {
}

/* themes */
function change_theme(num) {	
	for (var i = 1; i <= num_sheets; i++) {
		if (i != num) {
			document.styleSheets[i].disabled = true;
		}
		else {
			document.styleSheets[i].disabled = false;
		}
	}
	ie_correct(num);	
	return false;
}