var ContextMenus		= new Array;
var ContextMenuX;
var ContextMenuY;

function browser_dimensions()
{
  var myWidth = 0, myHeight = 0;

  if( typeof( window.innerWidth ) == 'number' )
  {
    //Non-IE
    myWidth  = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth  = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth  = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }

  return [myWidth, myHeight];
}

function show_context_menu(e, name, title, var_names, var_values)
{
	nofizz(e);

	if(name == "")
		return false;

	hide_context_menus();

	t = document.getElementById(name+'_template').innerHTML;

	t = t.gsub('-=-=-', title);
	t = t.gsub('!!', "'");

	if(var_names)
	{
		for(i=0; i < var_names.length; i++)
		{
			t = t.gsub('%'+var_names[i]+'%', var_values[i]);
		}
	}

	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop  + document.documentElement.scrollTop;
	}

	m = document.getElementById(name+'_menu');
	m.innerHTML		= t;
	m.style.left = posx - 5 + 'px';
	m.style.top  = posy - 5 + 'px';
	m.style.display	= 'block';

	ContextMenus.push(name);
	ContextMenuX = posx - 5 + 'px';
	ContextMenuY = posy - 5 + 'px';

	d = browser_dimensions();

	if( (m.offsetTop + m.clientHeight) > d[1])
	{
		m.style.top = d[1] - m.clientHeight + 'px';
	}

	if( (m.offsetLeft + m.clientWidth) > d[0])
	{
		m.style.left = d[0] - m.clientWidth + 'px';
	}
}

function disable_context_menu_options(name, options)
{

	m = $(name+'_menu');
	if(! m)
		return false;

	opts = m.getElementsByClassName('context_menu_option');
	opt2 = options;


	for(i=0; i < opts.length; i++)
	{
		for(x=0; x < options.length; x++)
		{
			if(opts[i].attributes.name.value == options[x])
			{
				opts[i].style.display = 'none';
			}
		}
	}
}

function hide_context_menu(name)
{
	m = document.getElementById(name+'_menu');
	m.style.display	= 'none';

}

function hide_context_menus()
{
	for(i=0; i < ContextMenus.length; i++)
	{
		e = document.getElementById(ContextMenus[i]+'_menu');
		if(e)
			e.style.display = 'none';
	}

}

function pimps_search(type, searchterm)
{
	new Ajax.Request('/search/', {
		method: 'post',
		parameters: { type: type, searchterm: searchterm },
		onSuccess: function(transport) {
			$('search_results_container').innerHTML = transport.responseText;
			$('search_results').pimps_highlight_rows();
		}
	});


}

var search_display = 0;

function toggle_display_search()
{
	d = $('search_tool').getElementsByClassName('component');

	if(search_display == 0)
	{
		for(i=0; i < d.length; i++)
		{
			d[i].show();

		}
		search_display = 1;
		$('searchterm').focus();
		return true;
	}

	hide_display_search();

}


function hide_display_search()
{
	if(! $('search_tool'))
		return false;

	d = $('search_tool').getElementsByClassName('component');

	for(i=0; i < d.length; i++)
		d[i].hide();

	search_display = 0;
	return true;
}


