/*
Copyright (c) 2007-2008 Key Ingredient Corporation.
Written by Jesse Lovelace <jesse@keyingredient.com>
*/
/* Extend Element to support prototype-ish hide() and show() */
Element.implement({
	show: function(inline) {
		if (inline==true)
			this.style.display='inline';
		else
			this.style.display='block';
		this.style.visibility='visible';
	},
	hide: function() {
		this.style.display='none';
		this.style.visibility='hidden';
	},
	is_hidden: function() {
		if (this.style.display=='none' || this.style.visibility=='hidden')
			return true;
		else
			return false;
	},
	toggle: function(inline) {
		if (this.style.visibility=='hidden' || this.style.display=='none')
			this.show(inline);
		else
			this.hide();
	},
	appear: function(inline) {
		this.show(inline);
		new Fx.Tween(this.getProperty('id'), 'opacity').start(0,1);
	},
	disappear: function() {
		new Fx.Tween(this.getProperty('id'), 'opacity').start(1,0);
		this.hide();
	},
	copyText: function() {
		if(document.selection && document.selection.createRange().text != ''){ 
			// IS IE
			return document.selection.createRange().text;
		} else{ // Not IE.. assume Mozilla?
			var startPos = this.selectionStart;
			var endPos = this.selectionEnd;
			return this.value.substring(startPos, endPos);
		}
	}
});

function goThere(url) {
	document.location.href=url;
}

/* Give a visual response to an ajax call */
function ajaxRespond(str) {
	var tip_element = $('ajax-ok');
	if (tip_element != null)
		tip_element.title = str;
	else 
		tip_element = new Element('a', {'title':str, 'id':'ajax-ok', 'style':'position:absolute;top:10px;left:10px;'}).inject(document.body);
		
	var content = str.split('::');
	tip_element.store('tip:title', content[0]);  
	tip_element.store('tip:text', content[1]);
	
	var status_tip = new Tips('ajax-ok', {
		onShow: function(toolTip) {
			toolTip.fade('in');	
		},
		onHide: function(toolTip) {
			toolTip.fade('out');
		},
		showDelay: 0,
		hideDelay:5000, 
		fixed:true
	});
	status_tip.elementEnter(null, tip_element);
	status_tip.elementLeave(null);
}

/* Send a form via ajax */
function sendAjaxForm(form_id, div_id, callable) {
	var form_to_send = $(form_id)
	new Request({
		url: form_to_send.get('action'), 
		method: form_to_send.get('method'), 
		data: form_to_send,
		onSuccess: function(responseText, responseXML) {
			ajaxRespond(responseText);
			if (div_id)
				$(div_id).toggle();
			form_to_send.reset();
			if (callable)
				callable();
		},
		onFailure: function(responseText, responseXML) {
			ajaxRespond(responseText);
		}
	}).send();
}

/* Some utility stuff. */
function HexR(h) {return parseInt((HexCut(h)).substring(0,2),16);}
function HexG(h) {return parseInt((HexCut(h)).substring(2,4),16);}
function HexB(h) {return parseInt((HexCut(h)).substring(4,6),16);}
function HexCut(h) {return (h.charAt(0)=="#") ? h.substring(1,7):h;}

var searchType = 'recipe';
function swapSearchText(newtype) {
	var el = $('id_q');
	if (newtype) {
		if (el.get('value')=='Live search' || el.get('value')=='Live ' + searchType + ' search') {
			if (newtype=='all')
				el.set('value','Live search');
			else
				el.set('value','Live ' + newtype + ' search');
		}
		searchType = newtype;
	} else {
		if (el.get('value')=='Live ' + searchType + ' search')
			el.set('value','');
	}
	el.focus();
}
window.addEvent('domready', function(){
	$('liveSearchButton').addEvent('click', function(e) {
			e = new Event(e).stop();
			$('id_searchPopup').toggle();
		});
	// Setup events for livesearch
	$('id_q').addEvents({
		'mousedown': function() {
			swapSearchText();
		},
		'keydown': function(e) {
		if (e.keyCode==13)
			document.live_search.submit();
		}
	});
	$$('div#id_searchPopup a').each(function(el) {
		el.addEvent('click', function(e) {
			e = new Event(e).stop();
			$('id_t').set('value', el.title.toLowerCase());
			swapSearchText(el.title.toLowerCase());
			$('id_searchPopup').toggle();
		});
	});
	// Show flashed messages
	if ($('flashes')) {
		var element = $('flashes');
		var content = element.get('title').split('::');
		element.store('tip:title', content[0]);  
		element.store('tip:text', content[1]);
		
		var flashes = new Tips(element, { 
			onShow: function(toolTip) {
				toolTip.fade('in');	
			},
			onHide: function(toolTip) {
				toolTip.fade('out');	
			},
			showDelay: 0,
			hideDelay:7000, 
			fixed:true
		});
		flashes.elementEnter(null, element);
		flashes.elementLeave(null);
	}
	
	// Show tooltips
	var Helpers = new Tips($$('.Help'),{
		onShow: function(toolTip) {
			toolTip.fade('in');	
		},
		onHide: function(toolTip) {
			toolTip.fade('out');	
		},
		showDelay:1000
	});
	var HelpersAlt = new Tips($$('.HelpAlt'),{
		onShow: function(toolTip) {
			toolTip.fade('in');	
		},
		onHide: function(toolTip) {
			toolTip.fade('out');	
		},
		showDelay:1000
	});
});
