var mediafieldid;
function showMediaUploadPopup(url,imgid)
{
	mediafieldid = imgid;
	var x = 0, y = 0;
	
	if (document.all) {
		x = window.screenTop + 100;
		y = window.screenLeft + 100;
	}
	else if (document.layers) {
		x = window.screenX + 100;
		y = window.screenY + 100;
	}else {// firefox, need to switch the x and y?
		y = window.screenX + 100;
		x = window.screenY + 100;
	}
	window.open(url,'upload', 'top=' + x + ',left=' + y + ',screenX=' + x + ',screenY=' + y + ',location=no,toolbar=no,menubar=no,width=380,height=140')
}

function showMediaDownloadPopup(url)
{
	var x = 0, y = 0;
	
	if (document.all) {
		x = window.screenTop + 100;
		y = window.screenLeft + 100;
	}
	else if (document.layers) {
		x = window.screenX + 100;
		y = window.screenY + 100;
	}else {// firefox, need to switch the x and y?
		y = window.screenX + 100;
		x = window.screenY + 100;
	}
	window.open(url,'download','top=' + x + ',left=' + y + ',screenX=' + x + ',screenY=' + y + ',location=no,toolbar=no,menubar=no,width=310,height=140,resizable=yes')
}

function setAjaxIndicatorLocation(indicatorMarkupId, x ,y)
{
	var indicator = document.getElementById(indicatorMarkupId);
	indicator.style.top = x +"px";
	indicator.style.left = y +"px";
}

function requestFocus(focusId)
{
	if (focusId != "" && focusId != null)
	{
		var toFocus = document.getElementById(focusId);
		if (toFocus != null && toFocus.focus) {
			try {
				toFocus.focus();
			} catch (ignore) {
			}
		}
	}
}

function focusIfUnchanged(focusId, value)
{
	if (focusId != "" && focusId != null)
	{
		var toFocus = document.getElementById(focusId);
		if (toFocus != null && toFocus.focus && toFocus.value == value) {
			try {
				toFocus.focus();
			} catch (ignore) {
			}
		}
	}
}

var focusedValue = null;
var focusedElement = null;
function storeValueBeforeUpdate()
{
	focusedElement = Wicket.Focus.getFocusedElement();
	if (typeof(focusedElement) != "undefined" && focusedElement != null)
	{
		focusedValue = focusedElement.value;
	}
	else
	{
		focusedElement = null;
	}
}

function restoreValueAfterUpdate()
{
	var element = Wicket.Focus.getFocusedElement();
	if (focusedElement != null && element != null  
		&& element.id == focusedElement.id 
		&& typeof(element) != "undefined"
		&& element.value != focusedValue)
	{
		element.value = focusedValue;
		focusedElement = null;
	}
}

function ignoreOnFocus(componentId)
{
	if (focusedElement != null && focusedElement.id && focusedElement.id == componentId)
	{
		return true;
	}
	return false;
}

function testEnterKey(e, script) 
{
     var code;
     
     if (!e) e = window.event;
     if (!e) return true;
     if (e.keyCode) code = e.keyCode;
     else if (e.which) code = e.which;
     
     if(code==13)
     {
        if (script) script();
	    return false;
     }
     return true;
}

function rearrageTabsInTabPanel(tabPanelId)
{
	var tabPanel = document.getElementById(tabPanelId);
	if (tabPanel)
	{
		var ul = tabPanel.getElementsByTagName("ul")[0];
		var tabs = ul.getElementsByTagName("li");
		var selectedRowIndex = -1;
	
		// Split the tabs into rows. The tabs in a row will be stored into an array,
		// and all the arrays representing the rows will be stored into another array
		// (a matrix-like structure, but with possibly different sized rows).
		var rows = new Array();
		var currentRow;
		var prevTop;
		for (var i=0; i<tabs.length; i++)
		{
			var tab = tabs[i];
			// The coordinates of the <li> element seems to be wrongly reported in various browsers 
			// (in firefox they appear all on the same line, in IE the first <li> is reported wrong,
			// in Opera they work ok). So we'll just use the coordinates of the inner <a> element,
			// which seem to be reported ok in all browsers.
			var anch = tab.getElementsByTagName("a")[0];
			var currentTop = anch.offsetTop;
			// If this is the first tab, we don't have a previous tab to compare with.
			// We just create a new row-array.
			if (i==0)
			{
				currentRow = new Array();
			}
			// If not the first tab, compare with previous element. If the vertical coordinate is
			// different, then start a new row-array.
			else if (prevTop != currentTop)
			{
				rows.push(currentRow);
				currentRow = new Array();
			}
			// If we get the selected tab, remember its index.
			if ((tab.className == "selected_tab") || (tab.className == "disabled_selected_tab"))
				selectedRowIndex = rows.length;
			currentRow.push(tab);
			prevTop = currentTop;
		}
		rows.push(currentRow);
		
		// If we have no selected row, then just take the first row.
		if (selectedRowIndex == -1)
			selectedRowIndex = 0;
		
		if (rows.length > 1)
		{		
			// This will be the index of the new top row (based on circular
			// shift of rows).	
			var newFirstRow = (selectedRowIndex - 1 + rows.length) % rows.length;

			// Now create separate <ul> elements for each row and move
			// the tabs into these new elements. We'll make some adjustment
			// to the style of the <ul>s, in order to have correct spacing 
			// between rows.
			var domRows = new Array();
			for (var i=0; i<rows.length; i++)
			{
				var domRow = document.createElement("ul");
				domRow.className = "tabs";
				if (i == selectedRowIndex)
				{
					domRow.style.marginTop = "0px";
					domRow.style.paddingTop = "0px";
				}
				else if (i == newFirstRow)
				{
					domRow.style.borderBottom = "0px";
					domRow.style.marginBottom = "0px";
					domRow.style.paddingBottom = "0px";
				}
				else
				{
					domRow.style.marginTop = "0px";
					domRow.style.marginBottom = "0px";
					domRow.style.borderBottom = "0px";
					domRow.style.paddingTop = "0px";
					domRow.style.paddingBottom = "0px";
				}
				for (var j=0; j<rows[i].length; j++)
				{
					var tab = rows[i][j];
					ul.removeChild(tab);
					domRow.appendChild(tab);
				}
				domRows.push(domRow);
			}

			
			// Now remove the initial <ul> and add the newly created ones.
			// Add them in the correct order, so that the selected row
			// will be placed at the bottom.
			var firstControl = ul;
			for (var i=selectedRowIndex; i<rows.length; i++)
			{
				tabPanel.insertBefore(domRows[i], firstControl);
				firstControl = domRows[i];
			}
			for (var i=0; i<selectedRowIndex; i++)
			{
				tabPanel.insertBefore(domRows[i], firstControl);
				firstControl = domRows[i];
			}
			tabPanel.removeChild(ul);	
		}
	}
}

function testStyleSheets()
{
	if(document.styleSheets.length >= 29)
	{
		window.location.reload();
	}
}