/**
 * Editor
 */

function select_selectAll( select_name )
{
	select = document.getElementById( select_name );

	if ( select )
	{
		select.style.visibility = "hidden";
		options = select.options;

		if ( options )
		{
			for( var iLoop = 0; iLoop < options.length; iLoop++ )
			{
				if ( options[ iLoop ].selected )
				{
					document.getElementById( "itemSelected" ).value = options[ iLoop ].value;
					continue;
				}

				options[ iLoop ].selected = true;
			}
		}
	}

	return true;
}

function select_moveUp( select_name )
{
	// For each option in the list (except
	// the first, and unless the first is selected)...

	options = document.getElementById( select_name );

	if ( options.length == 0 )
	{
		alert( 'There must be at least one item before you can use the \'up\' button.\n\nPlease use the \'Add\' button to create one' );
		return false;
	}

	if ( options[ 0 ].selected )
	{
		alert( 'The selected item is already the first in the list. It cannot be moved up' );
		return false;
	}

	for( var iLoop = 1; iLoop < options.length; iLoop++ )
	{
		option = options[ iLoop ];

		// ...if it's not selected, move on...

		if ( !option.selected )
			continue;

		// ...otherwise, move it up

		tempText = options[ iLoop - 1 ].text;
		tempValue = options[ iLoop - 1 ].value;
		tempSelected = options[ iLoop - 1 ].selected;
		options[ iLoop - 1 ].text = option.text;
		options[ iLoop - 1 ].value = option.value;
		options[ iLoop - 1 ].selected = option.selected;
		options[ iLoop ].text = tempText;
		options[ iLoop ].value = tempValue;
		options[ iLoop ].selected = tempSelected;
	}

	return false;
}

function select_moveDown( select_name )
{
	// For each option in the list (except
	// the last, and unless the last is selected)...

	options = document.getElementById( select_name );

	if ( options.length == 0 )
	{
		alert( 'There must be at least one item before you can use the \'down\' button.\n\nPlease use the \'Add\' button to create one' );
		return false;
	}

	if ( options[ options.length - 1 ].selected )
	{
		alert( 'The selected item is already the last in the list. It cannot be moved down' );
		return false;
	}

	for( var iLoop = options.length - 2; iLoop >= 0; iLoop-- )
	{
		option = options[ iLoop ];

		// ...if it's not selected, move on...

		if ( !option.selected )
			continue;

		// ...otherwise, move it down

		tempText = options[ iLoop + 1 ].text;
		tempValue = options[ iLoop + 1 ].value;
		tempSelected = options[ iLoop + 1 ].selected;
		options[ iLoop + 1 ].text = option.text;
		options[ iLoop + 1 ].value = option.value;
		options[ iLoop + 1 ].selected = option.selected;
		options[ iLoop ].text = tempText;
		options[ iLoop ].value = tempValue;
		options[ iLoop ].selected = tempSelected;
	}

	return false;
}

function editor_open()
{
	window.open( "edit/edit.jsp" + location.search, "editor" ).focus();
}

function editor_openUploader()
{
	_width = 300;
	_height = 130;
	_top = ( screen.height - _height ) / 2;
	_left = ( screen.width - _width ) / 2;
	uploader = window.open( "uploader.jsp", "uploader", "top=" + _top + ",left=" + _left + ",width=" + _width + ",height=" + _height + ",resizeable=no,scrollbars=no,toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no" );

	return false;
}

/**
 * Pop-up
 */

var iMouseOverPopup = 0;

function popup_mouseOver( highlighted_menu_item, menu )
{
	iMouseOverPopup = 3;

	if ( !menu )
		menu = highlighted_menu_item.parentNode;

	var menu_items = menu.childNodes;

	for( var menu_item_index = 0; menu_item_index < menu_items.length; menu_item_index++ )
	{
		var menu_item = menu_items[menu_item_index];

		var lastIndexOf = menu_item.id.lastIndexOf( "_" );
		var immediate_sub_menu_id = "menu_" + menu_item.id.substring( lastIndexOf + 1, menu_item.id.length );
		var immediate_sub_menu = document.getElementById( immediate_sub_menu_id );

		if ( menu_item == highlighted_menu_item )
		{
			menu_item.style.backgroundColor = "#CDFFC1";

			if ( immediate_sub_menu )
				immediate_sub_menu.style.visibility = "visible";
		}
		else
		{
			menu_item.style.backgroundColor = "transparent";
			popup_hide( immediate_sub_menu );
		}
	}
}

function popup_mouseOut()
{
	iMouseOverPopup = 2;
}

function popup_click( highlighted_menu_item, url )
{
	document.location = url;

	return false;
}

function popup_hideWhenInactive( menu_id )
{
	switch( iMouseOverPopup )
	{
		case 2:		iMouseOverPopup = 1;
					break;

		case 1:		iMouseOverPopup = 0;
					popup_mouseOver( null, document.getElementById( menu_id ));
					break;
	}

	setTimeout( "popup_hideWhenInactive('" + menu_id + "')", 750 );
}

function popup_hide( menu )
{
	if ( !menu )
		return;

	var menu_items = menu.childNodes;

	for( var menu_item_index = 0; menu_item_index < menu_items.length; menu_item_index++ )
	{
		var menu_item = menu_items[menu_item_index];

		var lastIndexOf = menu_item.id.lastIndexOf( "_" );
		var immediate_sub_menu_id = "menu_" + menu_item.id.substring( lastIndexOf + 1, menu_item.id.length );
		var immediate_sub_menu = document.getElementById( immediate_sub_menu_id );

		menu_item.style.backgroundColor = "transparent";
		popup_hide( immediate_sub_menu );
	}

	menu.style.visibility = "hidden";
}

/**
 * Tabstrip
 */

function tabstrip_selecttab( tabstrip_id, selected_id )
{
	var tabstrip = document.getElementById( "tabstrip_" + tabstrip_id );
	var tabTops = tabstrip.childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes;
	var tabBodies = tabstrip.childNodes[0].childNodes[1].childNodes[0].childNodes;

	var tabFirst = tabBodies[0];
	var tabZOrder = tabFirst.style.zorder;
	var tabPixelLeft = 0;
	var tabPixelTop = 0;

	while( tabFirst )
	{
		tabPixelLeft += tabFirst.offsetLeft;
		tabPixelTop += tabFirst.offsetTop;
		tabFirst = tabFirst.offsetParent;
	}

	if ( !selected_id )
	{
		var selected_cookie = getCookie( tabstrip_id, document.cookie );

		if ( selected_cookie )
		{
			selected_id = unescape( selected_cookie[1] );
		}
	}

	var tab_selected = "tab_" + selected_id;
	var found = false;

	for( var tab_index = 0; tab_index < tabBodies.length; tab_index++ )
	{
		var tabTop = tabTops[tab_index + 1];
		var tabBody = tabBodies[tab_index];

		if ( tabBody.id == tab_selected )
		{
			if ( tab_index > 0 )
			{
				tabBody.style.top = tabPixelTop + "px";
				tabBody.style.left = tabPixelLeft + "px";
			}

			tabBody.style.visibility = "visible";
			tabTop.style.borderBottomColor = "#CCCCCC";
			document.cookie = tabstrip_id + "=" + escape( selected_id );

			found = true;
		}
		else
		{
			tabBody.style.visibility = "hidden";
			tabTop.style.borderBottomColor = "#888888";
		}
	}

	if ( !found )
	{
		tabTops[1].style.borderBottomColor = "#CCCCCC";
		tabBodies[0].style.visibility = "visible";
	}

	return false;
}

/**
 * General
 */

function putFocusInFirstForm()
{
	for ( var iLoopForms = 0; iLoopForms < document.forms.length; iLoopForms++ )
	{
		_form = document.forms[iLoopForms];

		for ( var iLoopElements = 0; iLoopElements < _form.elements.length; iLoopElements++ )
		{
			_element = _form.elements[iLoopElements];

			if ( _element.type != "text" &&
				 _element.type != "textarea" &&
				 _element.type != "file" &&
				 _element.type != "password" &&
				 _element.type.indexOf( "select" ) != 0 )
			{
				continue;
			}

			_element.focus();
			return;
		}
	}
}

function showHourGlass()
{
	document.body.style.cursor = "wait";

	for ( var iLoopForms = 0; iLoopForms < document.forms.length; iLoopForms++ )
	{
		_form = document.forms[iLoopForms];

		for ( var iLoopElements = 0; iLoopElements < _form.elements.length; iLoopElements++ )
		{
			_form.elements[iLoopElements].style.cursor = "wait";
		}
	}

	return true;
}

function getCookie( strName, cookie )
{
	var regexp = new RegExp( strName + "=([^;]+)" );

	return regexp.exec( cookie );
}

function openLink( id )
{
	var link = document.getElementById( id );

	if ( link.target == "" )
		document.location = link.href;
	else
		window.open( link.href, link.target );

	return false;
}
