//
// template.js
// handles template
//

var template_last_highlighted_cell = 'undefined';

function template_handle_nav_events() 
{
	if (! javascript_loaded)
	{
		exit;
	}
	
	eventSrcID = (event.srcElement) ? event.srcElement.id : 'undefined';
	eventtype = event.type;

	// afhandelen mouseover event
	if (eventtype == 'mouseover')
	{
		// indien event afgevuurd door een cell (<td id = "cell + i">)
		// waarin i = index (starting from 0)
		if (eventSrcID.slice(0,4) == 'cell')
		{
			var index = eventSrcID.slice(4,5);
			
			if (index != selected)
			{
				template_highlight_cell(eval('cell' + index));
			}
			else 
			{
				// resetting backgroundcolors
				template_highlight_cell('undefined');
			}
		}
		// all elements outside tree should have no id's set
		// to make this work properly
		else if (eventSrcID == 'prv')
		{
			// resetting backgroundcolors
			template_highlight_cell('undefined');
		}
		else if (eventSrcID == 'nxt')
		{
			// resetting backgroundcolors
			template_highlight_cell('undefined');
		}
		else if (eventSrcID == '')
		{
			// resetting backgroundcolors
			template_highlight_cell('undefined');
		}
	}
	else if (eventtype == 'mousedown')
	{
		var changed = false;
				
		if (eventSrcID.slice(0,4) == 'cell')
		{
			var index = eventSrcID.slice(4,5);
			
			if (index != selected)
			{
				var newselected = index;
				changed = true;
			}
		}
		else if (eventSrcID == 'prv')
		{
			if (selected > 1)
			{
				var newselected = parseInt(selected) - 1;
				changed = true;
			}
		}
		else if (eventSrcID == 'nxt')
		{
			if (selected < noftemplates)
			{
				var newselected = parseInt(selected) + 1;
				changed = true;
			}
		}

		if (changed == true)
		{
			var start = filename.lastIndexOf("_");
			var eind = filename.lastIndexOf(".");
			var newfilename = filename.substr(0, start+1) + newselected + filename.substr(eind);		
	
			// resetting backgroundcolors
			template_select_cell(eval('cell' + selected), eval('cell' + newselected));
			selected = newselected;
			filename = newfilename;

			top.window.content.document.location.href = "../frameset/" + template + "?filename=" + newfilename;
		}
	}
}

function template_highlight_cell(obj)
{	
	// laatste cell terugzetten
	if (template_last_highlighted_cell != 'undefined')
	{
		template_last_highlighted_cell.className = "template_nrs_normal";
	}

	if (obj != 'undefined')
	{
		obj.className = "template_nrs_hilight";
	}

	// onthoud laatst geopende cell
	template_last_highlighted_cell = obj;
}

function template_select_cell(desel_obj, sel_obj)
{
	// laatste cell terugzetten
	if (template_last_highlighted_cell != 'undefined')
	{
		template_last_highlighted_cell.className = "template_nrs_normal";
		template_last_highlighted_cell = 'undefined';
	}

	sel_obj.className = "template_nrs_selected";
	desel_obj.className = "template_nrs_normal";
}
