var createGoalLB = null;

function showCBubble(elem)
{
	elem.style.display = 'block';

	p = elem.parentNode;
	x =	getPosX(p);
	y = getPosY(p);
	elem.style.position = 'absolute';
	elem.style.left = x+15;
	elem.style.top = y - (elem.offsetHeight * .9);
}
function hideCBubble(elem)
{
	elem.style.display = 'none';
}
function showDoBtn()
{
	doButton = $('doBtn');
	if(typeof doButton != "undefined" && doButton != null)
		$('doBtn').style.display = "inline";
}
function checkNotEmpty()
{
	var elem = $('autocomplete');
	if(typeof elem != "undefined" && elem != null)
	{
		if(elem.value.length == 0 || elem.value=='type goal here')
		{
			$('addNewGoalBtn').disabled = true;
			elem.value = "type your goal here";
			elem.className='unselected';
			hideGoalRecommendBox();
		}
		else
		{
			$('addNewGoalBtn').disabled = false;
		}
	}
}
function clearDefaultGoal()
{
	var elem = $('autocomplete');
	if(typeof elem != "undefined" && elem != null)
	{
		if(elem.value == "type your goal here")
		{
			elem.value = "";
			elem.className = 'selected';
		}
	}
}
function checkGoal()
{
	var elem = $('autocomplete');
	if(elem.value.length == 0 || elem.value=='type your goal here')
	{

	}
	else
	{

		var ran_unrounded=Math.random()*5;
		var ran_number=Math.floor(ran_unrounded);

		var link=$('createGoal');
		var pData = $('autocomplete').value;
		link.href="/myoh/goals/action,checkGoal/?title=" + encodeURIComponent(pData) + '&rand=' + ran_number;
	}
}
function checkSearchTerm()
{
	var qs = window.location.search;
	if(qs.length > 0)
	{
		qs = qs.substring(1,qs.length);
		values = qs.split('&');
		for(i=0;i<values.length;i++)
		{
			value = values[i].split('=');
			if(value[0] == 'goal')
			{
				$('autocomplete_search').value = (value[1]).replaceAll('+', " ");
				return;
			}
		}
	}
}
function addGoalCategory(title, id, url)
{
	var aw = new AjaxWindow(id);
	aw.title = title;
	aw.width = 800;
	aw.open('new Fuction(false)', null, url, getCategories, '');
}
function getCategories()
{
	var ajax = new Ajax.Updater('goal_cat_list','/iframe/uzone,goals/action,getCategories/');
}
function showEditOptions(elem,id)
{
	var cat_id = $('cat_id_'+id);
	cat_id.style.display="none";
	elem.style.display = "none";
	var opts = elem.parentNode.childNodes[1];
	opts.style.display = 'inline';
}
function hideEditOptions(elem, id)
{
	var cat_id = $('cat_id_'+id);
	cat_id.style.display="inline";
	var opts = elem.parentNode;
	opts.style.display = 'none';

	var opts = elem.parentNode.parentNode.childNodes[0];
	opts.style.display = 'inline';
}

function showCloud(cloudType)
{
	$('normallink').style.fontWeight='normal';
	$('normalview').style.display='none';
	$('cloudlink').style.fontWeight='bold';
	$(cloudType).style.display='block';
}
function showNormal(cloudType)
{
	$('normallink').style.fontWeight='bold';
	$('normalview').style.display='block';
	$('cloudlink').style.fontWeight='normal';
	$(cloudType).style.display='none';
}
function markDeleted(url,elem)
{
	elem.disabled = true;
	var ajax = new Ajax.Updater(elem.parentNode, url, {method: 'get', parameters: "", on404:function(){alert('There was an error saving.  Try again');}});
}
function showGoalRecommendBox()
{
	outerdim = Element.getDimensions($('addmygoal'));
	inputshimdim = Element.getDimensions($('input_shim'));

	$('goal_choices').innerHTML = '<div style="text-align: center;"><img id="loadingGoals" src="/shared/images/others/loading.gif"> </div>';

	if(IE)
	{
		$('goal_choices_shim').style.width = (outerdim.width) + "px";
		$('goal_choices').style.width = inputshimdim.width+"px";
	}
	else
	{
		$('goal_choices_shim').style.width = (outerdim.width-2) + "px";
		$('goal_choices').style.width = (inputshimdim.width-20)+"px";
	}

	$('goal_choices').style.display = "block";
	$('goal_choices_shim').style.display = "block";

	var ran_unrounded=Math.random()*5;
	var ran_number=Math.floor(ran_unrounded);

	var pData = $('autocomplete').value;

	var link = "/goals/action,recommendGoals/?title=" + encodeURIComponent(pData) + '&' + ran_number;

	new Ajax.Request(link,{method:'get', onSuccess:handleRecommendResults, onFailure:hideGoalRecommendBox});
}

function handleRecommendResults(t)
{
	middle = Element.getDimensions($('goal_choices'));
	if(middle.height < 100)
	{
		$('goal_choices_shim').style.height = "350px";
		$('goal_choices').style.height = "325px";
	}

	$('goal_choices').innerHTML = t.responseText;

	outer = Element.getDimensions($('goal_choices_shim'));
	middle = Element.getDimensions($('goal_choices'));
	inner = Element.getDimensions($('recGoalBox'));

	$('goal_choices').style.display = 'none';

	midchange = inner.height - middle.height+33;
	outchange = inner.height - outer.height+38;
	if(midchange > 100)
	{
		$('goal_choices_shim').style.height = "350px";
		$('goal_choices').style.height = "325px";
		outer = Element.getDimensions($('goal_choices_shim'));
		middle = Element.getDimensions($('goal_choices'));
		midchange = inner.height - middle.height+33;
		if(IE)
		outchange = inner.height - outer.height+38;
		else
		outchange = inner.height - outer.height+50;

	}
	new Effect.ReSize($('goal_choices'),{direction:'vert', amount: midchange});
	new Effect.ReSize($('goal_choices_shim'),{direction:'vert', amount: outchange});
	$('goal_choices').style.display = 'block';

	Event.observe('closeRecGoals','click',hideGoalRecommendBox);
	Event.observe('createGoal','mouseover',checkGoal);

	createGoalVar = $('createGoal');
	if(typeof createGoalVar != 'undefined' && createGoalVar != null)
	{
		valid = createGoalLB = new lightbox($('createGoal'));
	}
	//initialize();
}

function hideGoalRecommendBox()
{
	$('goal_choices').style.display = "none";
	$('goal_choices_shim').style.display = "none";
	if(typeof $('createGoal') != 'undefined' && $('createGoal') != null)
	{
		Event.stopObserving($('createGoal'),'click', createGoalLB.activate);
		Event.stopObserving($('createGoal'),'mouseover',checkGoal);
	}
	if(typeof $('closeRecGoals') != 'undefined' && $('closeRecGoals') != null)
	{
		Event.stopObserving($('closeRecGoals'),'click',hideGoalRecommendBox);
	}
}

function checkSubmit(event)
{
	switch(event.keyCode)
	{
		case Event.KEY_TAB:
		case Event.KEY_RETURN:
		if(elem.value.length == 0 || elem.value=='type goal here')
		{
			hideGoalRecommendBox();
		}
		else
			showGoalRecommendBox();
			return;
		
		case Event.KEY_ESC:
			hideGoalRecommendBox();
			return;
	}

	
	checkNotEmpty();
	clearDefaultGoal();
}

function initGoalsJs()
{
	//does not need to run if it's in the iframe
	elem = $('addNewGoalBtn');
	if(typeof elem != 'undefined' && elem != null)
	{
		Event.observe('addNewGoalBtn','click', showGoalRecommendBox);
		Event.observe('autocomplete','keypress', checkSubmit);
		Event.observe('autocomplete','click', clearDefaultGoal);
		Event.observe('autocomplete','blur', checkNotEmpty);
	}
	url = window.location.href.toString();
	if(url.search(/tab,pop/i) != -1)
	{
		showNormal();	
	}
}

function finishGoalForm(url)
{
	window.location.reload(true);
}
function closeLb()
{
	valid.deactivate();
}

function afterDoneAddingTags(goalId)
{
	var link = "/goals/action,getTagsAjax/goal_id," + goalId + '/';
	new Ajax.Updater('goal_tags', link);
}

Event.observe(window,'load',checkNotEmpty);
//Event.observe(window,'load',checkSearchTerm);
Event.observe(window,'load',initGoalsJs);

