// main.js : to be added before the </body> tag


// initiate search in the top bar
function Search()
{
	var val = $('txtSearchCanvas').value.clean();
	if (val != "")
	{
		setTimeout(function() {window.location.href = "/canvases/search/all/" + EncodeHTML(val);}, 1);
		return false;
	}
}
if ($('txtSearchCanvas')) $('txtSearchCanvas').addEvent('keydown', function(event){ if (event.key == 'enter') { Search();} });
if ($('btnSearchCanvas')) $('btnSearchCanvas').addEvent('click', function() { Search(); });


// overlay help text on textboxes
function TogglePlaceholder(obj, value, isFocus)
{
	var curr = obj.value.trim();
	var isEmpty = (curr.length < 1 || curr == value);
	obj.style.color = (isFocus) ? "#000000" : (isEmpty) ? "#999999" : "#000000";
	if (isFocus)
	{
		if (isEmpty)
			obj.value = "";
		else
			obj.value = curr;
	}
	else
	{
		if (isEmpty)
			obj.value = value;
		else
			obj.value = curr;
	}
}
if ($('txtPicURL')) $('txtPicURL').addEvent('focus', function() { TogglePlaceholder($('txtPicURL'), 'http://', true); });
if ($('txtPicURL')) $('txtPicURL').addEvent('blur', function() { TogglePlaceholder($('txtPicURL'), 'http://', false); });
if ($('txtPicURL')) $('txtPicURL').fireEvent('blur');
if ($('txtSearchCanvas')) $('txtSearchCanvas').addEvent('focus', function() { TogglePlaceholder($('txtSearchCanvas'), 'Twitter user or keyword', true); });
if ($('txtSearchCanvas')) $('txtSearchCanvas').addEvent('blur', function() { TogglePlaceholder($('txtSearchCanvas'), 'Twitter user or keyword', false); });
if ($('txtSearchCanvas')) $('txtSearchCanvas').fireEvent('blur');

// load a new Canvas
function ChangeCanvas(newid, target)
{
	// fade old canvas
	//if ($('divSayTweet').getElement('img')) $('divSayTweet').getElement('img').fade(0.5);
	var myHTMLRequest = new Request.HTML({
		method: "get",
		url:'/canvases/get/' + newid,
		headers : {'X-Requested-With' : ''},
		onComplete: function(responseTree, responseElements, responseHTML, responseJavaScript) { 
				try
				{
					eval(responseHTML);
				}
				catch (e)
				{
					alert("Oops an error occured.  We'll get on this right away!");
				}
			}
		}).send('target=' + target + '&st=1&credits=0');
}
