/*
	layoutTab_open(url, label, [allowduplicate]);
		Purpose of function
		   To allow a way to open layout tabs without having to keep track of tabs already open.
		Function Options
		   url = url of page you want to load
		   lable = Name of Tab you are opening
		   allowdupicate = Optional, allow duplicate tab to open based on url
		Example
		   layoutTab_open('/development/docs_layout_javascript', 'Layout Javascript', false); 
*/
function layoutTab_open(url, label, allowduplicate)
{
	var openTab = true;
	
	if (typeof(allowduplicate) == "undefined")
	{
		allowduplicate = false;
	}
	
	var duplicateIndex = layoutTab_getIndex(url);	
	if (allowduplicate == false && duplicateIndex > -1)
	{
		$mainTabs.tabs("select", duplicateIndex);
		openTab = false;
	}
	
	if (duplicateIndex == -1)
	{
		$mainOpenedTabs.push(url);
	}
	
	if (openTab == true)
	{
		var currentIndex = $mainTabs.tabs("length");
		$mainTabs.tabs("add", url, label, currentIndex);
		$mainTabs.tabs("select", currentIndex);
	}
}

/*
	layoutTab_close([index]);
		Purpose of function
		   Close a layout tab by index. Home layout tab can not be closed.
		Function Options
		   index = Optional, index of tab you want closed. If no index given, will close current viewed layout tab.
		Example
		   layoutTab_close(1); 
*/
function layoutTab_close(index)
{
	if (typeof(index) == "undefined")
	{
		index = $mainTabs.tabs('option', 'selected');
	}
	if (index > 0)
	{
		$mainTabs.tabs("remove", index);
		$mainOpenedTabs.remove(index);
	}
}

/*
	layoutTab_closeAll();
		Purpose of function
		   Close all open layout tabs, except Home layout tab.
		Function Options
		  No options available
		Example
		  layoutTab_closeAll(); 
*/
function layoutTab_closeAll()
{
	$mainTabs.tabs("select", 0);
	var currentIndex = $mainTabs.tabs("length");
	for (i=currentIndex; i > 0; i=i-1)
	{
		$mainTabs.tabs("remove", i);
	}
	set_default_openTabs();
}

/*
	layoutTab_reload([index]);
		Purpose of function
		   Reload layout tab by index. If no index given, will reload currently viewed layout tab.
		Function Options
		   index = Optional, index of tab you want closed. If left empty, will close current viewed layout tab.
		Example
		  layoutTab_reload(1); 
*/
function layoutTab_reload(index)
{
	if (typeof(index) == "undefined")
	{
		var index = $mainTabs.tabs('option', 'selected');
	}
	$mainTabs.tabs( "load" , index );
}

/*
	layoutTab_load(url);
		Purpose of function
		   Load a new page into the current layout tab.
		Function Options
		  url = url of page you want to load
		Example
		  layoutTab_load('/development/docs_layout_javascript'); 
*/
function layoutTab_load(url)
{
	var tabindex = $mainTabs.tabs('option', 'selected');
	$mainTabs.tabs( "url" , tabindex, url );
	$mainOpenedTabs[tabindex] = url;
	layoutTab_reload();
}

/*
	layoutTab_getIndex([url]);
		Purpose of function
		   Get index of a layout tab by url used to open tab. Will return -1 if not found.
		Function Options
		  url = Optional, url of page you want to load. If no url given will return currently viewed layout tab index.
		Example
		  var layoutTabIndex = layoutTab_getIndex('/development/docs_layout_javascript'); 
*/
function layoutTab_getIndex(url)
{
	if (typeof(url) == "undefined")
	{
		var index = $mainTabs.tabs('option', 'selected');
	}
	else
	{
		var index = jQuery.inArray(url, $mainOpenedTabs);
	}
	return index;
}

/*
	layout_reload(side);
		Purpose of function
		   Reload a layout side (west/east/north/south/center).
		Function Options
		  side = Side of layout you want to reload; west, east, north, south, center
		Example
		  layout_reload('east'); 
*/
function layout_reload(side)
{
	$('.ui-layout-'+side).load(
		'/layout/layout_'+side+'/load', 
		function()
		{
			load_accordion();						
			if (side == 'center')
			{
				$mainTabs.tabs( "destroy" );
				load_tabs();
				resizeWidgets();
			}
		}
	);
}

/*
	layout_load(url, side, [size], [visible]);
		Purpose of function
		   Load url into a layout side (west/east/north/south/center) and be able to set size and visibility.
		Function Options
		  url = url of page you want to load
		  side = Side of layout you want to load; west, east, north, south, center
		  size = Optional, size of side in pixels
		  visible = Optional, show or hide
		Example
		  layout_load('/layout/layout_south/load','north','50', 'hide'); 
*/
function layout_load(url, side, size, visible)
{
	if (typeof(visible) != "undefined" && visible != '')
	{
		if (visible == 'show')
		{
			$mainLayout.open(side);
		}
		if (visible == 'hide')
		{
			$mainLayout.close(side);
		}
	}
	if (typeof(size) != "undefined" && size != '')
	{
		$mainLayout.sizePane(side, size);
	}
	$('.ui-layout-'+side).load(
		url, 
		function()
		{
			load_accordion();
			if (side == 'center')
			{
				$mainTabs.tabs( "destroy" );
				load_tabs();
				resizeWidgets();
			}
		}
	);
}
