var busy = false, current = "none";

var root, top, bottom, left, right;
var curr_type, type_array, typediv, titlediv;
var curr_day, daydiv;
var close, scrolldiv, loading;
var visited, max_days;
var digits, colors, num_days, num_types;
var close_click = false;

function setup()
{
	root		= document.getElementById("root");
	top			= document.getElementById("top");
	bottom		= document.getElementById("bottom");
	left		= document.getElementById("left");
	right		= document.getElementById("right");
	typediv		= document.getElementById("typediv");
	titlediv	= document.getElementById("titlediv");
	daydiv		= document.getElementById("daydiv");
	close		= document.getElementById("close");
	_edit		= document.getElementById("edit");	
	scrolldiv	= document.getElementById("scrolldiv");
	loading 	= document.getElementById("loading");
	
	visited		= new Array();
	max_days	= 10;
	
    root.style.height = (num_types * 20 + 1) + "px";
	root.style.width = (num_days * 20 + 1) + "px";
    titlediv.style.top = (100 + num_types * 20) + 'px';

	curr_day = "0000-00-00";
	
	digits = new Array();
	for (i = 0; i < 10; i++)
	{
		digits[i] = new Image();
		digits[i].src = "images/digit" + i + ".gif";
	}
	
	window.onresize = getHeight;
	getHeight();
	
	//document.onkeydown = handleKeyDown;
}

function handleKeyDown(e)
{
	if (document.all)
		alert(window.event.keyCode)
	else
		alert(e.which);
	return false;
}

function getHeight()
{
	var height;
	if (window.innerHeight > 0)
		height = window.innerHeight;
	else if (document.body.offsetHeight > 0)
		height = document.body.offsetHeight;
	else
		height = 1000;
	
	left.style.height = height + "px";
	right.style.height = height + "px";
}

function handleMouseOver(cell, type, day, id)
{
	if (!busy)
	{
		if (current != cell)
			document.getElementById(cell).style.backgroundColor = colors['dark'][getj(cell)];
		select(cell, type, day);
	}
}

function handleMouseOut(cell, color)
{
	if (!busy)
	{
		if (current != cell && visited[cell] != true)
			document.getElementById(cell).style.backgroundColor = color;
		unselect();
	}
}

function handleMouseDown(target, color)
{
	if (current != target)
		document.getElementById(target).style.backgroundColor = color;
	//alert(document.getElementById(target).style.backgroundColor);
}

function handleMouseUp(cell, id)
{
	if (current != cell)
	{
		document.getElementById(cell).style.backgroundColor = colors['dark'][getj(cell)];
		if (!busy)
		{
			if (current == 'none')
				openCell(cell, id);
			else
				closeCell(current, 'openCell("' + cell + '", ' + id + ');');
		}
	}
}

function select(target, type, day)
{
	var t = document.getElementById(target);
	var tx = getx(target);
	var ty = gety(target);
	
	if (type != null)
	{
		typediv.removeChild(typediv.firstChild);
		typediv.appendChild(document.createTextNode(type_array[type]));
		typediv.style.top = ty + "px";
		
		while(titlediv.childNodes.length > 0)
			titlediv.removeChild(titlediv.firstChild);
		
		var titles_arr = titles[target];
		for (i = 0; i < titles_arr.length; i++)
		{
			titlediv.appendChild(document.createTextNode(titles_arr[i]));
			titlediv.appendChild(document.createElement('br'));
		}
	}
	
	/*if (day != null)
	{
		if (day != curr_day)
		{
			if (day.substr(0, 1) != curr_day.substr(0, 1))
				document.images['yr1'].src = digits[day.substr(0, 1)].src;
			if (day.substr(1, 1) != curr_day.substr(1, 1))
				document.images['yr2'].src = digits[day.substr(1, 1)].src;
			if (day.substr(2, 1) != curr_day.substr(2, 1))
				document.images['yr3'].src = digits[day.substr(2, 1)].src;
			if (day.substr(3, 1) != curr_day.substr(3, 1))
				document.images['yr4'].src = digits[day.substr(3, 1)].src;
			if (day.substr(5, 1) != curr_day.substr(5, 1))
				document.images['mo1'].src = digits[day.substr(5, 1)].src;
			if (day.substr(6, 1) != curr_day.substr(6, 1))
				document.images['mo2'].src = digits[day.substr(6, 1)].src;
			if (day.substr(8, 1) != curr_day.substr(8, 1))
				document.images['dy1'].src = digits[day.substr(8, 1)].src;
			if (day.substr(9, 1) != curr_day.substr(9, 1))
				document.images['dy2'].src = digits[day.substr(9, 1)].src;
			curr_day = day;
		}
		daydiv.style.left = (getx(target) + 7) + "px";
		daydiv.style.top = "10px";
	}*/

	top.style.borderColor = colors['light'][getj(target)];
	left.style.borderColor = colors['light'][getj(target)];
	bottom.style.borderColor = colors['light'][getj(target)];
	right.style.borderColor = colors['light'][getj(target)];
	
	titlediv.style.color =  colors['dark'][getj(target)];
	typediv.style.color =  colors['dark'][getj(target)];
	
	top.style.top = (ty - 1) + "px";
	left.style.left = (tx - 1) + "px";
	
	bottom.style.top = (ty + parseInt(t.style.height)) + "px";
	right.style.left = (tx + parseInt(t.style.width)) + "px";
	
	if (current != "none")
		titlediv.style.top = (80 + num_types * 20 + parseInt(document.getElementById(current).style.height)) + "px";
	else
		titlediv.style.top = (100 + num_types * 20) + 'px';
	titlediv.style.left = right.style.left;
}


function unselect()
{
	typediv.style.top = "-100px";
	titlediv.style.top = "-100px";
	daydiv.style.top = "-100px";	
	top.style.top = "-1px";
	bottom.style.top = "-1px";
	left.style.left = "-1px";
	right.style.left = "-1px";
}

function openCell(target, id)
{
	var t = document.getElementById(target);
	do_edit = false;
	
	if (current != target)
	{
		if (visited[target] != true)
			parent.frames[0].location = "load.php?id=" + id + "&cell=" + target;
		busy = true;
		curr_type = getj(target);
		current = target;
		select(current, curr_type, curr_day);
		t.style.width = "19px";
		t.style.height = "19px";
		t.style.cursor = "default";
		openCell(target, id);
	}
	else if (parseInt(t.style.width) < 259)
	{
		var col = document.getElementById("col_" + geti(target));
		t.style.width = (parseInt(t.style.width) + 40) + "px";
		right.style.left = (getx(target) + parseInt(t.style.width)) + "px";
		titlediv.style.left = right.style.left;
		root.style.width = (parseInt(t.style.width) + (num_days - 1) * 20 + 2) + "px";
		col.style.left = (parseInt(t.style.width) + 1) + "px";
		setTimeout("openCell('" + target + "', " + id + ")", 20);
	}
	else if (parseInt(t.style.height) < 259)
	{
		var row = document.getElementById("row_" + geti(target) + "_" + getj(target));
		t.style.height = (parseInt(t.style.height) + 40) + "px";
		bottom.style.top = (gety(target) + parseInt(t.style.height)) + "px";
		titlediv.style.top = (80 + num_types * 20 + parseInt(t.style.height)) + "px";
		root.style.height = (parseInt(t.style.height) + (num_types - 1) * 20 + 2) + "px";
		row.style.top = (parseInt(t.style.height) + 1) + "px";
		for(i = 0; i < num_days; i++)
			if (i != geti(target) && document.getElementById("row_" + i + "_" + getj(target)))
				document.getElementById("row_" + i + "_" + getj(target)).style.top = (parseInt(t.style.height) + 1) + "px";
		setTimeout("openCell('" + target + "', " + id + ")", 20);
	}
	else if (close.style.top != (gety(target) + 5) + "px")
	{
		close.style.top = (gety(target) + 5) + "px";
		close.style.left = (getx(target) + parseInt(t.style.width) - 16) + "px";
		
		_edit.style.top = (gety(target) + 5) + "px";
		_edit.style.left = (getx(target) + parseInt(t.style.width) - 32) + "px";
		
		if (visited[target] != true)
			showLoading(target, id);
		else
			openCell(target, id);
	}
	else
	{
		loading.style.visibility = "hidden";
		t.firstChild.style.visibility = "visible";
		var contentHeight = t.firstChild.offsetHeight;
		var barHeight = (200 / contentHeight) * 225;
		
		if (contentHeight > 250)
		{
			scrolldiv.style.left = (getx(target) + 243) + "px";
			scrolldiv.style.top = (gety(target) + 20) + "px";
			scrolldiv.style.visibility = "visible";
			scrolldiv.style.height = barHeight;
			t.firstChild.style.top = "20px";
			Drag.init(scrolldiv, null, (getx(target) + 243), (getx(target) + 243), (gety(target) + 20), (gety(target) + 255 - barHeight));
			// the number of pixels the thumb can travel vertically (max - min)
			scrollTravel = scrolldiv.maxY - scrolldiv.minY;
	
			// the ratio between scroller movement and thumbMovement
			ratio = (t.firstChild.offsetHeight - 235) / scrollTravel;
			
			scrolldiv.onDrag = function(x, y) {
				scrolldiv.style.backgroundColor = "#FFF";
				t.firstChild.style.top = (20 - Math.round((y - scrolldiv.minY) * ratio)) + "px";
			}
			
			scrolldiv.onDragStart = function() {
				close_click = false;
				scrolldiv.style.backgroundColor = "#FFF";
			}
			
			scrolldiv.onDragEnd = function() {
				scrolldiv.style.backgroundColor = "";
			}
		}
		busy = false;
		unselect();
		visited[target] = true;
	}
}

function closeCell(target, callback)
{
	var t = document.getElementById(target);
	if (busy == false)
	{
		busy = true;
		if (callback != "")
			unselect();
		close.style.top = "-100px";
		_edit.style.top = "-100px";
		t.firstChild.style.visibility = "hidden";
		scrolldiv.style.visibility = "hidden";
		closeCell(target, callback);
	}
	else if (parseInt(t.style.height) > 20)
	{
		var row = document.getElementById("row_" + geti(target) + "_" + getj(target));
		t.style.height = (parseInt(t.style.height) - 40) + "px";
		if (callback == "")
		{
			bottom.style.top = (gety(target) + parseInt(t.style.height)) + "px";
			titlediv.style.top = (80 + num_types * 20 + parseInt(t.style.height)) + "px";
		}
		row.style.top = (parseInt(t.style.height) + 1) + "px";
		root.style.height = (parseInt(t.style.height) + (num_types - 1) * 20 + 2) + "px";
		for(i = 0; i < num_days; i++)
			if (i != geti(target) && document.getElementById("row_" + i + "_" + getj(target)))
				document.getElementById("row_" + i + "_" + getj(target)).style.top = (parseInt(t.style.height) + 1) + "px";
		setTimeout("closeCell('" + target + "', '" + callback + "')", 20);
	}
	else if (parseInt(t.style.width) > 20)
	{
		
		var col = document.getElementById("col_" + geti(target));
		t.style.width = (parseInt(t.style.width) - 40) + "px";
		if (callback == "")
		{
			right.style.left = (getx(target) + parseInt(t.style.width)) + "px";
			titlediv.style.left = right.style.left;
		}
		col.style.left = (parseInt(t.style.width) + 1) + "px";
		root.style.width = (parseInt(t.style.width) + (num_days - 1) * 20 + 2) + "px";
		setTimeout("closeCell('" + target + "', '" + callback + "')", 20);
	}
	else
	{
		
		if (do_edit == true) {
			location.href='./user_login.php?postid=' + edit_src;
		}
		
		t.style.cursor = "hand";
		if (callback == "")
		{
			busy = false;
			unselect();
			current = "none";
		}
		else
			setTimeout("eval('" + callback + "');", 40); // Used in the case when clicking from one cell to another
	}		
}

// Returns the date index
function geti(target)
{
	var arr = target.split("_");
	return arr[1];
}

// Returns the topic index
function getj(target)
{
	var arr = target.split("_");
	return arr[2];
}

function getx(target)
{
	var x = 100 + 20 * parseInt(geti(target));
	if (current != "none" && parseInt(geti(target)) > parseInt(geti(current)))
		x += 240;
	//if (document.getElementById("debug").checked)
	//	alert(parseInt(geti(target)) > parseInt(geti(current)));
	return x;
}

function gety(target)
{
	var y = 100 + 20 * parseInt(getj(target));
	if (current != "none" && parseInt(getj(current)) < parseInt(getj(target)))
		y += 240;
	return y;
}

function showLoading(target, id)
{
	if (document.getElementById(target).childNodes.length == 0)
	{
		loading.style.left = getx(target) + "px";
		loading.style.top = gety(target) + "px";
		loading.style.visibility = "visible";
		setTimeout("showLoading('" + target + "', " + id + ")",  250);
	}
	else
		openCell(target, id);
}