/** RSH must be initialized after the
    page is finished loading. */

function dhtml_initialize() {
  // initialize RSH
  dhtmlHistory.initialize();
  
  // add ourselves as a listener for history
  // change events
  dhtmlHistory.addListener(handleHistoryChange);
  
  // determine our current location so we can
  // initialize ourselves at startup
  var initialLocation = 
                dhtmlHistory.getCurrentLocation();
  
  // if no location specified, use the default
  if (initialLocation == null)
    initialLocation = null; //initial location is set by outside factors
  
  // now initialize our starting UI
  updateUI(initialLocation, null);
}

/** A function that is called whenever the user
    presses the back or forward buttons. This
    function will be passed the newLocation,
    as well as any history data we associated
    with the location. */
function handleHistoryChange(newLocation, historyData) {
  // use the history data to update our UI
  updateUI(newLocation, historyData);                           
}

/** A simple method that updates our user
    interface using the new location. */
function updateUI(newLocation, historyData) {
   var historyMessage;
  if (historyData != null){
    eval(historyData.message);
  }
}

function ShowDetails()
{
	ActivateTab(2);
	document.location.hash='FullWiki';
}

function ActivateTab(iTab)
{

	document.getElementById('TabsBannerInner').innerHTML=DisplayNames[iTab];
	if(iTab == 0)
	{
		dhtmlHistory.add( 'stores', {message: 'ActivateTab(0);'})
		if(!ContentLoaded[0])
		{
			document.getElementById("Stores_at").innerHMTL = "Loading....";
			GetAjax("Ajax/IngredientTabs.asp", "type=stores&id="+IngredientID+"&widgetStart=1000", "Stores_at", "StartUp(1000);");
			ContentLoaded[0] = true;
		}
		document.getElementById('Stores_at').style.display='block';
		SetActive('Stores');
	}
	else
	{
	
		document.getElementById('Stores_at').style.display='none';
		SetUnActive('Stores');
	}
	
	if(iTab == 1)
	{
		dhtmlHistory.add( 'recipes', {message: 'ActivateTab(1);'})
		if(!ContentLoaded[1])
		{
			document.getElementById("Recipes_at").innerHMTL = "Loading....";
			GetAjax("Ajax/IngredientTabs.asp", "type=recipes&id="+IngredientID+"&widgetStart=2000", "Recipes_at", "StartUp(2000);");
			ContentLoaded[1] = true;
		}
	
		document.getElementById('Recipes_at').style.display='block';
		SetActive('Recipes');
	}
	else
	{
		document.getElementById('Recipes_at').style.display='none';
		SetUnActive('Recipes');
	}

	if(iTab == 2)
	{
		dhtmlHistory.add( 'description', {message: 'ActivateTab(2);'})
		if(!ContentLoaded[2])
		{
			document.getElementById("Description_at").innerHMTL = "Loading....";
			GetAjax("Ajax/IngredientTabs.asp", "type=details&id="+IngredientID+"&widgetStart=3000", "Description_at", "StartUp(3000);");
			ContentLoaded[2] = true;
		}
	
		document.getElementById('Description_at').style.display='block';
		SetActive('Description');
	}
	else
	{
		document.getElementById('Description_at').style.display='none';
		SetUnActive('Description');
	}
	
	iView = iTab;
}


function RefreshStores(Ingredient,page,sort)
{

	document.getElementById("Stores_at").innerHMTL = "Loading....";
	GetAjax("Ajax/IngredientTabs.asp", "type=Stores&id="+Ingredient+"&sort="+sort+"&page="+page+"&widgetStart=2000", "Stores_at", "StartUp(2000);");
	ContentLoaded[0] = true;
}

function RefreshRecipes(Ingredient,page,sort)
{

	document.getElementById("Recipes_at").innerHMTL = "Loading....";
	GetAjax("Ajax/IngredientTabs.asp", "type=Recipes&id="+Ingredient+"&sort="+sort+"&page="+page+"&widgetStart=2000", "Recipes_at", "StartUp(2000);");
	ContentLoaded[1] = true;
}



function startWiki()
{
	ActivateTab(2);
	
	editButon = document.getElementById('EditButton1');
	if(!CheckLogin(null,'Please sign in <br> to make changes','BuildEditBox();'))
	{
		editButon.innerHTML = 'Loading...';
		return;
	}
	else
		editButon.innerHTML = 'Edit';

	if( $('wikiBox').getSize().size.y < 200)
		$("wikiBox").style.height = "200px";
		
	if( $('wikiBox').innerHTML.indexOf("<span class=\"smallgreytext\">please add...</span>") > 0)
		$('wikiBox').innerHTML = "";
		
	tinyMCE.execCommand('mceAddControl', false, "wikiBox");
	document.getElementById("wikiOffTop").style.display = "none";
	document.getElementById("wikiOnTop").style.display = 'block';
	document.getElementById("wikiOnBottom").style.display = 'block';
	
	document.location.hash='saveWiki';
	
}

function closeWiki()
{
	tinyMCE.execCommand('mceRemoveControl', false, "wikiBox");
	document.getElementById("wikiOffTop").style.display = "block";
	document.getElementById("wikiOnTop").style.display = 'none';
	document.getElementById("wikiOnBottom").style.display = 'none';
}

function saveWiki()
{	
	document.getElementById("wikiContent").innerHTML = tinyMCE.getInstanceById('wikiBox').getHTML();
	document.wikiForm.submit();
}


tinyMCE.init({
			mode : "exact",
			content_css : "css/wysiwyg.css",
			plugins : "searchreplace",
			theme_advanced_toolbar_location : "top",
			theme_advanced_buttons1 : "undo,redo,separator,search,replace,separator,formatselect",
			theme_advanced_buttons2 : "bold,italic,underline,strikethrough,separator,justifyleft,justifycenter,justifyright,justifyfull,outdent,indent,separator,bullist,numlist,sub,sup,charmap",
			theme_advanced_buttons3 : "",
			theme_advanced_blockformats : "p,h1,h2,h3"
});


function BuildEditBox()
{

	//first make sure that the ingredient tab has data loaded
	if (!ContentLoaded[2])
	{
		document.getElementById("Description_at").innerHMTL = "Loading....";
		GetAjax("Ajax/IngredientTabs.asp", "type=details&id="+IngredientID+"&widgetStart=3000", "Description_at", "StartUp(3000);BuildEditBox()");
		ContentLoaded[2] = true;
		return;
	}

	startWiki();
}

