function ShowMarker(pos)
{
	el = $('point');
	c = $('chart');
	p = pos.toString().split(',');
	xy = Position.page(c);
	xyo = Position.realOffset(c);
   
	x = Number(p[0]) + xy[0] + xyo[0] - Math.round(el.getWidth()/2);
	y = Number(p[1]) + xy[1] + xyo[1] - Math.round(el.getHeight()/2);
 
	el.style.left = x + 'px';
	el.style.top = y + 'px';
	el.show();
}

function HideMarker() {
	if(typeof(LastMarker) == Function)
		LastMarker();
	else
		$('point').hide();
}

function RenderTip(text,id) {
	el = $('chart-tip-box');
	tip = $('chart-tip-content');
	c = $(id) || $('chart');

	//el.innerHTML != text  is a fix for FireFox which prevents "flashing" as you mouseover
	if(!el.visible() || el.innerHTML != text) {
		tip.update(text);
		xy = Position.positionedOffset(c);
		x = c.getWidth() + xy[0] - el.getWidth() - 50;
		y = xy[1] + 25;
		pageOffset = [0,0];
		if(c.id != 'chart')
			pageOffset = Position.realOffset(c);

		el.style.left = x + 'px';
		el.style.top = y  + pageOffset[1] + 'px';
		//this is a fix for IE, which mysteriously requires the height to be large enough to fit the contents
		//setting this inane number, actually only renders the box with the height of its content
		//and effectively ignores the 300px setting
		el.style.height = '60px';
		el.show();
	}
	else
		el.hide();
}

function CloseTip() {
	$('chart-tip-box').hide();
}

Validation.add('validate-emails', 'Invalid Email Addresses: ', function (v, element) {
		if (v != "") {
			var emails = v.split(/,\s*|;\s*|\n\s*/);
			var good = [];
			var bad = [];
			for (i = 0; i < emails.length; i++) {
				if (/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/.test(emails[i])) {
					good.push(emails[i]);
				}
				else {
					if(emails[i].length > 0)
						bad.push(emails[i]);
				}
			}
			if (bad.length > 0) {
				element.value = "";
				for (i = 0; i < good.length; i++) {
					element.value += good[i]
					if (i < good.length - 1) 
						element.value += ","
				}
				if ($('advice-validate-emails-email_recipients') != null) { //only run this function if advice is all ready on page
					$('advice-validate-emails-email_recipients').innerHTML = "These email addresses are invalid:<br />"
					for (i = 0; i < bad.length; i++) {
					
						$('advice-validate-emails-email_recipients').innerHTML += bad[i]
						if (i < bad.length - 1) 
							$('advice-validate-emails-email_recipients').innerHTML += "<br />"
						
					}
				}
				return false;
			}
			else {
				$('advice-validate-emails-email_recipients').innerHTML = "";
				$('advice-validate-emails-email_recipients').style.display = "none";
				element.value = "";
				for (i = 0; i < good.length; i++) {
					element.value += good[i]
					if (i < good.length - 1) 
						element.value += ","
				}
				return true;
			}
		}
		else{
			$('advice-validate-emails-email_recipients').innerHTML = "";
			$('advice-validate-emails-email_recipients').style.display = "none";
		}
});

function CreateValidator(id)
{
	var valid = new Validation(id, {
		onSubmit: false,
		useTitles: true,
		onFormValidate: function(result, form) {}
	});
	return valid;
}

function positionToolTip(element, options){
	element = $(element);
	console.log(element);
	var tooltip = $("tool-tip");
	var offset = element.cumulativeOffset();
	console.log(offset);
	var dim = element.getDimensions();
	console.log(dim);
	
	var tooltipLeft = offset.left - tooltip.getWidth()/2 + dim.width/2;
	var tooltipTop = offset.top + dim.height;
	tooltipLeft += options.left == null ? 0 : options.left;
	tooltipTop += options.top == null ? 0 : options.top;
	tooltip.style.left = tooltipLeft + "px";
	tooltip.style.top = tooltipTop + "px";
	
	if (!options.hidden) {
		new Effect.Appear(tooltip, {
			delay: 3
		});
	}
}

function hideToolTip(){
	new Effect.Fade("tool-tip");
}

	
function searchRecipes(){
	new Effect.Fade("recipe_search_results",{
		duration: 0.3,
		queue:'front'
	});
	new Effect.Appear("searching_recipes", {
		duration: 0.3,
		queue: 'front'
	});
	
	new Ajax.Request("/recipe/recipe_search",{
		evalScripts: true,
		asynchronous: true,
		parameters: Form.serialize($('recipe_search'))
	});
}
	
function howDidYouHear(){
	var el = $('hdyh-txt');
	var hdyh = $('demo_request_how_did_you_hear');
	var ohdyh = $('other_how_did_you_hear');
	ohdyh.value = hdyh.getValue();
	if (hdyh.value == 'Other' && !el.visible()){
		ohdyh.value = '';
		el.show();
	}else{
		el.hide();
	}
}
