var editing 		= false;
var editing_obj 	= null;
var value			= '';
var field			= '';

//Enter admin
function AdminLogin()
{
	LoadPage("logon.php");
}

//Cancel logging in
function LoginCancel()
{
	Dim(false); 
}

//Try logging in
function LoginDo()
{
	LoadPage("logon.php?user=" + $('user').value + "&password=" + $('pass').value);
}

//Mouse is over an editing panel
function EditPanel_Over(obj)
{
	if (editing)
		return;
	obj.style.backgroundColor = "#eEe9b9";
}

//Mouse is out an editing panel
function EditPanel_Out(obj)
{
	if (editing)
		return;
		
	obj.style.backgroundColor = "";
}

//Begin editing a panel
function EditPanel_Edit(obj, thefield, width, height)
{
	if (editing)
		return;

	//Store what we're editing
	field = thefield;
	editing = true;
	editing_obj = obj;
	value = obj.innerHTML;

	//Update the page content list
	new Ajax.Request('admin/page_inline.php', 
	{
		method: 'get',
		parameters: '?&edit&field=' + field,
		onSuccess: function(transport, json)
		{
			var response = transport.responseText || "";
			value_translated = response;
			
			//Auto size if we have no size specified
			if (width == 0)				width = 190;
			if (height == 0)				height = 90;			

			//Create a textarea containing the field
			obj.innerHTML = "<textarea name=\"editing_txt\" id=\"editing_txt\" style=\"overflow: hidden; border: 1px dashed #EC7700; background-Color: #FEF9C9; margin: 0px; padding: 0px; width:" + width + "px; height:" + height + "px\">" + value_translated + "</textarea>";
			obj.style.border = "";
			obj.style.backgroundColor = "";

			//Show the save/cancel buttons and hide the edit button
			$(thefield + '_edit').style.display 	= 'none';
			$(thefield + '_cancel').style.display 	= 'inline';
			$(thefield + '_save').style.display 	= 'inline';
			document.getElementById('editing_txt').select();
			document.getElementById('editing_txt').focus();
			
			//tinyMCE.init({mode : "textareas", theme : "simple"});	
		}
	});	
}

//Cancel editing a field
function EditPanel_Cancel()
{
	if (!editing)
		return;
	
	$(field + '_edit').style.display 	= 'inline';
	$(field + '_cancel').style.display 	= 'none';
	$(field + '_save').style.display 	= 'none';
		
	editing_obj.innerHTML = value;
	editing_obj.style.border = "";
	editing_obj.style.backgroundColor = "";
	editing = false;
}

//Save an edited field and bring it into the page
function EditPanel_Save()
{
	if (!editing)
		return;

	//Show the edit icon and hide the cancel/save buttons
	$(field + '_edit').style.display 	= 'inline';
	$(field + '_cancel').style.display 	= 'none';
	$(field + '_save').style.display 	= 'none';

	//Get the updated content and make it safe
	document.getElementById('editing_txt').enabled = false;
	value = document.getElementById('editing_txt').value;
	value_translated = value;
	value_translated = (value_translated.split("\n")).join('%0A');
	value_translated = (value_translated.split("\r")).join('');
	editing_obj.innerHTML = "Saving...";

	//Update the page content list
	new Ajax.Updater(editing_obj.id, 'admin/page_inline.php?&save&field=' + field + '&value=' + value_translated, 
	{
		method: 'get',
		onSuccess: function()
		{
			editing_obj.style.border = "";
			editing_obj.style.backgroundColor = "";
			//editing_obj.style.padding = "4px";		
			editing = false;
		}
	});		
}

//Dim the page
function Dim(todim)
{
	if (todim)
	{
		$('darkBackgroundLayer').style.display = 'block';
		//$('flashlogo').style.visibility = 'hidden';
	}
	else
	{
		$('darkBackgroundLayer').style.display = 'none';
		//$('flashlogo').style.visibility = '';		
	}
}

//Dim the page in
var Opacity;
function DimPage()
{	
	if (Opacity >= 1)
	{
		Opacity = 1;
		$('popup').style.opacity 			= Opacity;
		$('darkBackgroundLayer').style.opacity 	= Opacity;
		$('popup').style.filter  			= "alpha(opacity=" + (Opacity * 100) + ")";
		$('darkBackgroundLayer').style.filter  	= "alpha(opacity=" + (Opacity * 100) + ")";
		return;
	}
	
	$('popup').style.opacity 			= Opacity;
	$('darkBackgroundLayer').style.opacity 	= Opacity;
	$('popup').style.filter  			= "alpha(opacity=" + (Opacity * 100) + ")";
	$('darkBackgroundLayer').style.filter  	= "alpha(opacity=" + (Opacity * 100) + ")";
	Opacity += 0.2;
	
	setTimeout("DimPage()", 100);
}

//Load a page into the popup dialog and render it
function LoadPage(URL)
{
	Dim(true);
	
	Opacity=1;
	DimPage();
	scroll(0,0);
	$('popup').style.display='block';
	$('popup_content').innerHTML = "<table width=500 height=300 style=\"font-size: 8pt\"><tr><td align=center valign=middle><img src=\"admin/res/admin_uploading.gif\"><br>Loading Content...<br>Please wait<br></tr></td></table>";
		
	//Update the page content list
	new Ajax.Updater('popup_content', URL, 
	{	
		method: 'get',
		onSuccess: function(transport, json)
		{
			$('popup').style.display='block';
			//$('popup').style.opacity=1;
		}
	});	
}

//Post the contact form via ajax
function PostContact(form)
{
	Dim(true);
	
	$('popup').style.display='block';
	$('popup_content').innerHTML = "<table width=500 height=300 style=\"font-size: 8pt\"><tr><td align=center valign=middle><img src=\"admin/res/admin_uploading.gif\"><br>Sending email...<br>Please wait<br></tr></td></table>";
	
	//Update the page content list
	new Ajax.Updater('popup_content', 'ajax.php?contact', 
	{	
		method: 'post',
		parameters: {To: form.to.value, From: form.from.value, Email: form.email.value, Phone: form.phone.value, Message: form.message.value},
		onSuccess: function(transport, json)
		{
			$('popup').style.display='block';
			form.reset();
		}
	});	
}
