/** 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 RefreshReviews(recipe,sort,page)
{
	$("Reviews_at").innerHMTL = "Loading....";
	new Ajax("Ajax/RecipeTabs.asp", {method: "get",update: $("Reviews_at")}).request("type=reviews&id="+recipe+"&sort="+sort+"&page="+page);
	ContentLoaded[0] = true;
}




function doAddReview()
{
	buttonElm = $("button_ReviewStore");

	if(!CheckLogin(buttonElm,'Please sign in <br> to enter a review','doAddReview();'))
		return;


	$('button_ReviewStore').style.display='none';
	$('Review_div').style.display='block';
	doShowReview();
	$("ReviewText").focus();
}

function doShowReview()
{
	ActivateTab(0); 
	document.location.hash='reviewBlock';
}

function StoreMatchPopover(sPopoverThis, recipe, store, type){
	GetPopup(sPopoverThis, "StoreMatchInfo", "AJAX/RecipeStoreMatches.asp", "recipe="+recipe+"&store="+store+"&type="+type, -50, -50)
}
	
function ShowReviewArea(buttonElm)
{
	if(!CheckLogin(buttonElm,'Please sign in <br> to enter a review','ShowReviewArea();'))
		return;

	$('Review_div').style.height = $('button_ReviewStore').style.height;			
	$('Review_div').style.display='block';
	$('button_ReviewStore').style.display='none';  
	ButtonOff($('button_ReviewStore'));
	OpenObject('Review_div', 150, 20, 15)
	ActivateTab(0);
	
	$("ReviewText").focus();
}


//#######################   The Ajax functions
function submitTag(tagName){
	new Json.Remote("Ajax/setRecipeTag.asp", {onComplete: submitTagAction,method: 'get'}).send({'recipe': PageID,'tag': tagName, 'retry':'submitTag("'+tagName+'");','rndId':Math.random()});
	return false;
}

function submitTagAction(response){

	if(response.success)
	{
		//tag now associated with the store..
		
		//try to match a current tag
		var tagElm;
		var curTags = $('Tag_Collection_Tags').getElements('a');
		for(i = 0; i < curTags.length;i++)
		{
			if(curTags[i].getProperty("name") == response.success.tag)
			{
				tagElm = curTags[i];
				break;
			}
		}
		
		if(!tagElm) 
		{
			tagElm = document.createElement("a");
			
			tagElm.setAttribute('Name',response.success.tag);
			$("Tag_Collection_Tags").appendChild(tagElm);
		}
		
		//set all parameters
		tagElm.setAttribute("score",response.success.score);
		tagElm.setAttribute("weight",response.success.weight);
		tagElm.setAttribute("yourscore",response.success.yourScore);
		
		//fill in the details
		tagElm.innerHTML = response.success.tag;
		tagElm.style.backgroundColor='#666';
		
		//now animate it to call attention to the change
		fx = new Fx.Styles(tagElm, {duration:400, wait:false});
		fx.start({
			'background-color': '#666'
		}).chain(function() {
			this.start.delay(2000, this, {
				'background-color': '#FFFFFF'
			});
		});
		
		
	}
	else if(response.error)
	{
		if(response.error.errorCode == 1 && response.error.retry)
			CallSignInDiv("Sign in to add a tag",response.error.retry);
		else
			alert(response.error.errorDescription);
	}		
	else
			alert("An unknown error has occured. Please report this to cooks compass");
}