var grid_width = 8;
var grid_height = 16;
var view_height = 8;
var imgs = ["images/bubble-blue.gif", "images/bubble-red.gif", "images/bubble-green.gif", "images/bubble-yellow.gif", "images/bubble-orange.gif", "images/bubble-pink.gif", "images/bubble-teal.gif", "images/bubble-purple.gif", "images/bubble-white.gif", "images/bubble-black.gif"];

var ids = [];
var clicked = null;
var round;
var score;
var gameboard_id;

//window.onload = newGame;

window.onload = preload_images;

function preload_images() {
	var tempbub;
	for (img in imgs)
		tempbub = newImage(imgs[img]);
}

function newImage(file) {
	if (document.images) {
		obj = new Image();
		obj.src = file;
		return obj;
	}
}

function newGame() {
	score = 0;
	round = 1;
	$('score').innerHTML = score;
	clicked = null;
	populate_board(round + 1);
}

function get_bonus(scr) {
	switch (scr) {
		case 2:
			return 1;
		case 3:
			return 3;
		case 4:
			return 5;
		case 5:
			return 10;
	}
	return 0;
}

function do_move(bubble) {
	bubbles = get_marked_bubbles();
	
	for (i = 0; i < bubbles.length; i++) {
		id = ids[bubbles[i] - i];
		setTimeout('opacit("' + id + '")', (i + 1) * 50 + (250 * i));
		setTimeout('remove_bubble("' + id + '")', (i + 1) * 250);
		remove_array_element(id);
	}
	
	score += get_bonus(bubbles.length);
	$('score').innerHTML = score;
	
	$("bubblesleft").innerHTML = ids.length;
	
	if (ids.length < 1) {
		clicked = null;
		populate_board(++round + 1);
	}
}

function opacit(id) {
	$(id).style.opacity = 0;
}

function remove_bubble(id) {
	$(id).style.opacity = 1;
	$(id).style.display = 'none';
}

function joinbubbles(bubs) {
	arr = [];
	for (z = 0; z < bubs.length; z++)
		arr[arr.length] = bubs[z].id;
	return arr.join(', ');
}

function get_marked_bubbles() {
	arr = [];
	for (i = 0; i < ids.length; i++)
		if ($(ids[i]).style.opacity == 0.5)
			arr[arr.length] = i;
	return arr;
}

function unmark_all_bubbles() {
	for (i = 0; i < ids.length; i++)
		$(ids[i]).style.opacity = 1;
}

function mark_linked_bubbles(bubble) {
	pos = array_pos(bubble.id, ids);
	bubble.style.opacity = 0.5;
	
	bubl = $(ids[pos - 1]);
	if (bubl != null && bubl.style.opacity != 0.5 && bubble.src == bubl.src && pos % grid_width > 0) {
		bubl.style.opacity = 0.5;
		//mark_linked_bubbles(bubl);
	}
	
	bubr = $(ids[pos + 1]);
	if (bubr != null && bubr.style.opacity != 0.5 && bubble.src == bubr.src && (pos % grid_width < grid_width - 1 || pos == ids.length - 2)) {
		bubr.style.opacity = 0.5;
		//mark_linked_bubbles(bubr);
	}
	
	bubu = $(ids[pos - 8]);
	if (bubu != null && bubu.style.opacity != 0.5 && bubble.src == bubu.src && pos > grid_width - 1) {
		bubu.style.opacity = 0.5;
		//mark_linked_bubbles(bubu);
	}
	
	bubd = $(ids[pos + 8]);
	if (bubd != null && bubd.style.opacity != 0.5 && bubble.src == bubd.src && pos < ids.length - 1 % grid_width && pos + 8 < grid_width * view_height) {
		bubd.style.opacity = 0.5;
		//mark_linked_bubbles(bubd);
	}
}

function button_click(bubble) {
	if (clicked == bubble) {
		do_move(bubble);
	} else {
		unmark_all_bubbles();
		clicked = bubble;
		mark_linked_bubbles(bubble);
		if (get_marked_bubbles().length < 2) {
			unmark_all_bubbles();
			clicked = null;
		}
	}
}

function populate_board(colors) {
	gameboard_id = 'game_' + round;
	ids = [];
	html = '';
	for (i = 1; i <= grid_width * grid_height; i++) {
		ids[ids.length] = 'bubble' + i + '_' + round;
		rnd = rand(1, colors);
		html += '<img src="' + imgs[rnd - 1] + '" id="bubble' + i + '_' + round + '" onclick="button_click(this);" />';
	}
	$("game").innerHTML = '<div id="' + gameboard_id + '"></div>';
	$(gameboard_id).innerHTML = html;
	$("bubblesleft").innerHTML = ids.length;
}

function remove_array_element(id) {
	new_ids = [];
	for (x = 0; x < ids.length; x++)
		if (id != ids[x])
			new_ids[new_ids.length] = ids[x];
	ids = new_ids;
}

function array_pos(needle, haystack) {
	for (x = 0; x < haystack.length; x++)
		if (haystack[x] == needle)
			return x;
	return -1;
}

function $(id) {
	return document.getElementById(id);
}

function rand(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}