var WN_cnote = true; // need this to reutilise menu code
var CC_fadelock = 0;
var FI = new Array();


function initialise()
{
	//load public notes menu
	if (PUBLICNOTES.length) U_MMyNotes = new U_Menu('u_select_1', 'U_MMyNotes', PUBLICNOTES, selectNote);
	
	//Set up HotKey events
	document.onkeypress = quickKeys;
}

function selectNote(id)
{
	CURRENTNOTE = id;
}

function addNote()
{
	eval("WN_Note"+COUNTER+" = new WN_Widget('WN_Note'+COUNTER,'wn_'+COUNTER);");
	eval("WN_Note"+COUNTER+".createNote("+CURRENTNOTE+");");

	COUNTER++;	
}

function quickKeys(e)
{
	var evt = (window.event)? event : e;
	var keyChar = String.fromCharCode(evt.which);

	if (keyChar == 'f' && evt.altKey)
		addFavorite();
}

// FAVORITES
function addFavorite()
{
	// set up ajax object
	ajax_obj = new AJAX_Object();
	ajax_obj.xmlSetLoading(ajaxSave);
	
	ajax_obj.xmlInit(resultFavorite);
	ajax_obj.xmlRequest(API,'a=favorite&d=mb&bid='+BID);
}

function resultFavorite(xml)
{
	if (xml.getElementsByTagName('result').length)
	{
		if (xml.getElementsByTagName('result')[0].firstChild.nodeValue == 1)
		{
			//hide favorites thing
			startFadeOut('favorites', function(){});			

			writeToConsole('This Message Board has been added to you list of favorites');
		}
		else if (xml.getElementsByTagName('result')[0].firstChild.nodeValue == 2)
		{
			//hide favorites thing
			startFadeOut('favorites', function(){});

			writeToConsole('This Message Board is already in your favorites',2);
		}
		else
			writeToConsole('There was an error saving this Message Board to your favorites',1);
	}
	else writeToConsole('There was an error saving this Message Board to your favorites',1);
}

// RATINGS
function rateBoard(rating)
{
	// set up ajax object
	ajax_obj = new AJAX_Object();
	ajax_obj.xmlSetLoading(ajaxSave);
	
	ajax_obj.xmlInit(resultRating);
	ajax_obj.xmlRequest(API,'a=rate&d=mb&bid='+BID+'&value='+rating);
}

function resultRating(xml)
{
	if (xml.getElementsByTagName('result').length)
	{
		if (xml.getElementsByTagName('result')[0].firstChild.nodeValue == 1)
		{
			var rating = xml.getElementsByTagName('rating')[0].firstChild.nodeValue;
			
			//Update stars and disable rating mechanism
			for (var x = 0; x < 5; x++)
			{
				document.getElementById('rt_'+x).className = ((x < rating)? 'star' : '');
				document.getElementById('rt_'+x).onclick = document.getElementById('rt_'+x).onmouseover = document.getElementById('rt_'+x).onmouseout = function(){return false};
			}
			document.getElementById('rating').innerHTML = 'Rated: ';

			writeToConsole('You have given this Message Board a rating of '+rating);
		}
		else
			writeToConsole('There was an error rating this Message Board',1);
	}
	else writeToConsole('There was an error rating this Message Board',1);
}

//STARS
function rateStars(id)
{
	var star = id.substr(3);

	for (var x = 0; x < star; x++)
		document.getElementById('rt_'+x).style.backgroundPosition = '0 11px';
	for (x = parseInt(star)+1; x < 5; x++)
		document.getElementById('rt_'+x).style.backgroundPosition = '0 0';
}

function stopStars()
{
	for (var x = 0; x < 5; x++)
		document.getElementById('rt_'+x).style.backgroundPosition = '';
}



/**************** General Funactions ********************/

function writeToConsole(text, error)
{
	// remove contents
	var c_contents = document.getElementById('cc_console');
	while (c_contents.childNodes.length) c_contents.removeChild(c_contents.firstChild);

	if (error > 1) c_contents.className = 'cc_error';
	
	if (!error) c_contents.style.backgroundImage = 'url(img/ic_good.gif)';
	else if (error > 0) c_contents.style.backgroundImage = 'url(img/ic_bad.gif)';
	else c_contents.style.backgroundImage = 'url(img/ic_loading.gif)';

	c_contents.appendChild(document.createTextNode(text));

	if (CC_fadelock) clearTimeout(CC_fadelock);
	CC_fadelock = 0;

	startFadeIn('cc_console');
	if (error != 1 && error != -1) CC_fadelock = setTimeout('startFadeOut("cc_console",'+function(){void(null)}+')',2000);
}

function ajaxSave()
{
	writeToConsole('Saving...',-1);
}

function startFadeIn(id)
{
	var is_fade = searchFades(id);
	
	document.getElementById(id).style.display = 'block';

	if (is_fade == -1)
		FI.push(new Array(id,setTimeout("fadeIn("+FI.length+")",10),0,1,null));
	else if (FI[is_fade][3] != 1)
	{
		FI[is_fade][3] = 1;
		clearTimeout(FI[is_fade][1]);
		eval('FI[is_fade][1] = setTimeout("fadeIn('+is_fade+')",10);');
	}
}

function fadeIn(f_item)
{

	var n_opacity = Math.round(FI[f_item][2]) + 10;

	if (n_opacity < 100)
	{
		if (document.addEventListener)
			document.getElementById(FI[f_item][0]).style.opacity = n_opacity/100;
		else
			document.getElementById(FI[f_item][0]).style.filter = 'alpha(opacity = '+(n_opacity)+')';
			//document.getElementById(FI[f_item][0]).filters.item("DXImageTransform.Microsoft.Alpha").opacity = (n_opacity * 100);
			
		FI[f_item][2] = n_opacity;

		eval('FI[f_item][1] = setTimeout("fadeIn('+f_item+')",25);');
	}
	else
	{
		if (document.addEventListener)
			document.getElementById(FI[f_item][0]).style.opacity = 0.999;
		else
			document.getElementById(FI[f_item][0]).style.filter = 'alpha(opacity = 100)';
			//document.getElementById(FI[f_item][0]).filters.item("DXImageTransform.Microsoft.Alpha").opacity = 100;
		FI[f_item][0] = '';
	}
}

function startFadeOut(id, eF)
{
	var is_fade = searchFades(id);

	if (is_fade == -1)
		FI.push(new Array(id,setTimeout("fadeOut("+FI.length+")",10),100,0,eF));
	else if (FI[is_fade][3] != 0)
	{
		FI[is_fade][3] = 1;
		clearTimeout(FI[is_fade][1]);
		eval('FI[is_fade][1] = setTimeout("fadeOut('+is_fade+')",10);');
		FI[is_fade][4] = eF;
	}
}

function fadeOut(f_item)
{
	var n_opacity = Math.round(FI[f_item][2]) - 10;

	if (n_opacity >= 10)
	{
		if (document.addEventListener)
			document.getElementById(FI[f_item][0]).style.opacity = n_opacity/100;
		else
			document.getElementById(FI[f_item][0]).style.filter = 'alpha(opacity = '+n_opacity+')';
			//document.getElementById(FI[f_item][0]).filters.item("DXImageTransform.Microsoft.Alpha").opacity = (n_opacity * 100);
			
		FI[f_item][2] = n_opacity;
		
		eval('FI[f_item][1] = setTimeout("fadeOut('+f_item+')",25);');
	}
	else
	{
		if (document.addEventListener)
			document.getElementById(FI[f_item][0]).style.opacity = 0;
		else
		{
			document.getElementById(FI[f_item][0]).style.filter = 'alpha(opacity = 0)';
			//document.getElementById(FI[f_item][0]).filters.item("DXImageTransform.Microsoft.Alpha").opacity = 0;
			document.getElementById(FI[f_item][0]).style.display = 'none';
		}
			
		FI[f_item][4]();
		FI[f_item][0] = '';
	}
}

function searchFades(id)
{
	for (var x = 0; x < FI.length; x++)
	{
		if (FI[x][0] == id)
		{
			return x;
			exit;
		}
	}
	return -1;
}
