var selectable_field = 0;
var mulselectable_fields = new Array();
var keyPressed = 0;

var marked_field_color = "#E4E4E4";
var selected_field_color = "#E4E4FF";

function mark_field(id)
{
	$(id).style.backgroundColor = marked_field_color;
	$(id).style.cursor = "pointer";
}

function unmark_field(id)
{
	$(id).style.background = "none";
}

function getClientWidth()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}

function initFunction()
{
	$('main').focus();
}


/* ************************ */
/* *** Events handliers *** */
/* ************************ */
function handleKeyDown(evt)
{
	evt = (evt) ? evt : ((window.event) ? event : null);
	if(evt)
	{
		keyPressed = evt.keyCode;
	}
}

function handleKeyUp(evt)
{
	keyPressed = 0;
}

function handleBeforeUnloadWindow(event)
{
	// Hide start details window if it presents and opened
	try
	{
		details_window.close();
	}
	catch(err)
	{
	}
}

/* ************************** */
/* *** Multiple selection *** */
/* ************************** */

function mark_mulselectable_filed(id)
{
	$(id).style.backgroundColor = marked_field_color;
	$(id).style.cursor = "pointer";
}

function unmark_mulselectable_filed(id)
{
	if(mulselectable_fields.indexOf(id) >= 0)
		$(id).style.backgroundColor = selected_field_color;
	else
		$(id).style.background = "none";
	$(id).style.cursor = "default";
}

function select_mulselectable_field(id, parent, classname)
{	
	remove_document_selection();			
	switch(keyPressed)
	{
		/* Simple mouse click */
		case 0: 
			mulselectable_fields.each(function(item){
				$(item).style.background = "none";
			});
			mulselectable_fields.clear();
			single_mulselect(id);
			break;
		/* Ctrl pressed */
		case 17:
			var index = mulselectable_fields.indexOf(id);
			
			if(index != -1)
				single_muldeselect(index);
			else
				single_mulselect(id);
				
			break;
		/* Shift pressed */
		case 16:
			var last_element = mulselectable_fields.last();
			mulselectable_fields.each(function(item){
				$(item).style.background = "none";
			});
			mulselectable_fields.clear();
			
			selector =  'tr[class="' + classname + '"]';
			
			var start_collecting = false;
			var elements = $(parent).getElementsBySelector(selector);
			elements.each(function(item) {
				if(item.id == id || item.id == last_element)
					start_collecting = start_collecting ? false : true;
				if(start_collecting)
				{
					mulselectable_fields.push(item.id);
					single_mulselect(item.id);
				}
			});
			
			single_mulselect(id);
			single_mulselect(last_element);
			
			break; 
	}
}

function remove_document_selection()
{
	if (document.selection)
    {
        document.selection.empty();
    }
    else
    {
        window.getSelection().removeAllRanges();
    }
}

function single_mulselect(elem)
{
	$(elem).style.backgroundColor = selected_field_color;
	$(elem).style.cursor = "pointer";
	
	var index = mulselectable_fields.indexOf(elem);
	if(index == -1)
		mulselectable_fields.push(elem);
}

function single_muldeselect(elem)
{
	mulselectable_fields.splice(elem, 1);
	$(elem).style.background = "none";
}

/* ************************** */
/* **** Single selection **** */
/* ************************** */
function mark_selectable_field(id)
{
	$(id).style.backgroundColor = marked_field_color;
	$(id).style.cursor = "pointer";
}

function unmark_selectable_field(id)
{
	if(id == selectable_field)
		$(id).style.backgroundColor = selected_field_color;
	else $(id).style.background = "none";
	
	$(id).style.cursor = "default";
}

function select_selectable_field(id)
{	
	$(id).style.backgroundColor = selected_field_color;
	$(id).style.cursor = "pointer";
	
	if(selectable_field != 0 && selectable_field != id)
	{
		$(selectable_field).style.background = "none";
		
	}
	selectable_field = id;
}

function unselect_selectable_field()
{
	if(selectable_field)
	{
		$(selectable_field).style.background = "none";
		selectable_field = 0;
	}
}

/* ************************** */
/* ******** Widgets ********* */
/* ************************** */
function showPopupWidget(widget_id, widget_caption)
{
	var caption_elem = widget_id + "_caption";
	var content_elem =  widget_id + "_popup_content";
	$(caption_elem).innerHTML = widget_caption;
	$(content_elem).innerHTML = "Loading ...";
	$(widget_id).style.top = "5px";
	$(widget_id).style.left = getClientWidth()/2 - $(widget_id).getWidth()/2 + "px";
	$(widget_id).show();	
}

function hidePopupWidget(widget_id)
{
	var content_elem =  widget_id + "_popup_content";
	
	$(widget_id).hide();
	$(content_elem).innerHTML = "";
}

/* *********************************** */
/* ******** Global functions ********* */
/* *********************************** */

function trim(string)
{
	return string.replace(" ", "");
}

function are_you_sure()
{
	return confirm('Are you sure?');
}
