/* Inline Edit */

/*Interface_Mode = false;

function Open_Interface(Key_Event)
{
	if (Key_Event.metaKey)
	{
		Interface_Mode = true;
//		document.addEventListener("keyup", Close_Interface, true);
		document.removeEventListener("keydown", Open_Interface, true);
//		console.log("Open");
		
//		Show_Toolbar();
	}
}

function Close_Interface(Key_Event)
{
	if (!Key_Event.metaKey)
	{
		Interface_Mode = false;
		document.removeEventListener("keyup", Close_Interface, true);
//		console.log("Close");
		
//		Hide_Toolbar();
		
		Remove_Highlight()
	}
}

function Show_Toolbar()
{
	Ramp_Value({"Command": "$('Jelly_Toolbar').style.top = VALUE + 'px';", "Start_Value": -50, "End_Value": 0});
}

function Hide_Toolbar()
{
	//Ramp_Value({"Command": "$('Jelly_Toolbar').style.top = VALUE + 'px';", "Start_Value": 0, "End_Value": -50});
}

*/

function Document_Click()
{
	if (Inline_Menu_Element)
	{
		Inline_Menu_Element.parentNode.removeChild(Inline_Menu_Element);
		Inline_Menu_Element = null;
	}
}

Hover_Item_Handled = false;
function Hover_Item(Parameters /* Event, Target, Title */)
{
	Mouse_Event = Parameters["Event"];	
	
	// On command key, if something's not already open.
	if (!Hover_Item_Handled && Mouse_Event.metaKey && !(Edit_Item) && (!Inline_Menu_Element))
	{
		Hover_Item_Handled = true;
		setTimeout("Hover_Item_Handled = false", 5);
		Highlight_Item(Parameters["Target"], Parameters["Title"]);
	}
	else if (!Hover_Item_Handled && !Mouse_Event.metaKey && !(Edit_Item) && !(Inline_Menu_Element))
	{
		Remove_Highlight();
	}
}


Edit_Item = null;
Edit_Item_Style = null;
Edit_Item_Display = null;
Editable_Item = null;
Select_Item_Handled = false;

Inline_Menu_Element = null; 
function Select_Item(Parameters /* (Event, Target, Item_Title, Item_Namespace, Item_URL, Property_Title, Property_Namespace, Property_URL, Template_Title, Template_Namespace, Template_URL) */  ) 
{
//	Remove_Highlight();
	Mouse_Event = Parameters["Event"];
	
	if (!Select_Item_Handled && Mouse_Event.metaKey && !(Edit_Item))
	{						
		// Cross browser event bubbling!!!
		Select_Item_Handled = true;
		setTimeout("Select_Item_Handled = false", 200);	
	
		// Generate Inline Menu
		Inline_Offset = new Array(Mouse_Event.pointerX(), Mouse_Event.pointerY());
		
		Inline_Menu_Element = document.createElement("div");
		Inline_Menu_Element.className = "Inline_Menu";
		Inline_Menu_Element.style.left = Inline_Offset[0] + "px";
		Inline_Menu_Element.style.top = Inline_Offset[1] + "px";
		
		Inline_Menu_Element_HTML = "<ul>";
		
		Inline_Menu_Element_HTML += "<li class=\"Inline_Menu_Title\">" + Parameters["Menu_Title"] +"</li>";
		
		if (Parameters['Property_Title'])
		{	
			Property_Click = "Open_URL_Inline({'Target_ID':'" + Parameters["Target"].id + "', 'Namespace': '" + Parameters["Property_Namespace"] + "', 'URL': '" + Parameters["Property_URL"]+ "'});";
			Property_Title = "Edit " + Parameters["Property_Title"] + "...";
			Inline_Menu_Element_HTML += "<li><a href=\"#\" onclick=\"" + Property_Click +"return false;\">" + Property_Title+ "</a></li>";
		}		
		if (Parameters['Item_Title'])
		{	
			Item_Click = "Open_URL_In_Window({'Target_ID':'" + Parameters["Target"].id + "', 'Namespace': '" + Parameters["Item_Namespace"] + "', 'URL': '" + Parameters["Item_URL"]+ "'});";
			Item_Title = "Edit " + Parameters["Item_Title"] + "...";
			Inline_Menu_Element_HTML += "<li><a href=\"#\" onclick=\"" + Item_Click +"return false;\">" + Item_Title+ "</a></li>";
		}
		if (Parameters['Template_Title'])
		{	
			Template_Click = "Open_URL_In_Window({'Target_ID':'" + Parameters["Target"].id + "', 'Namespace': '" + Parameters["Template_Namespace"] + "', 'URL': '" + Parameters["Template_URL"]+ "'});";
			Template_Title = "Edit Template...";
			Inline_Menu_Element_HTML += "<li><a href=\"#\" onclick=\"" + Template_Click +"return false;\">" + Template_Title+ "</a></li>";
		}
		Inline_Menu_Element_HTML  += "</ul>";
		Inline_Menu_Element.innerHTML = Inline_Menu_Element_HTML;

		document.body.appendChild(Inline_Menu_Element);
	}
}

// Inline data edit window
function Open_URL_Inline(Parameters /* (Target_ID,  URL) */ )
{
	if (Inline_Menu_Element)
	{
		Inline_Menu_Element.parentNode.removeChild(Inline_Menu_Element);
		Inline_Menu_Element = null;
	}
	
	Edit_Item = $(Parameters["Target_ID"]);

	//Add a lightbox.
	Light_Box( Edit_Item );
	
	// Move title above lightbox.
	$('Item_Title').style.zIndex = $('Lightbox_Left').style.zIndex + 1;
	
	// Remove borders
	$('Item_Border_Left').style.display = "none";
	$('Item_Border_Right').style.display = "none";
	$('Item_Border_Top').style.display = "none";
	$('Item_Border_Bottom').style.display = "none";
	
	// Hide Edit Item
	Edit_Item_Size = Find_Size(Edit_Item, 1);
	console.log(Edit_Item_Size[0]);
	Edit_Item_Style = document.defaultView.getComputedStyle(Edit_Item, "");
	Edit_Item_Display = "" + Edit_Item_Style.display;
	Edit_Item.style.display = "none";
	
	// Make Editable Item
	Editable_Item = document.createElement("div");
	Editable_Item.id = Edit_Item.id + "_Editable";
	Editable_Item  = Edit_Item.parentNode.insertBefore(Editable_Item, Edit_Item);
	Editable_Item.style.width = Edit_Item_Size[0] + "px";
	Editable_Item.style.height = Edit_Item_Size[1] + "px";
	Fill({"ID":Editable_Item.id, "URL": Parameters["URL"], "Transition": "None"});
}

function Open_URL_In_Window(Parameters /* (Target,  Namespace, URL) */)
{
	if (Inline_Menu_Element)
	{
		Inline_Menu_Element.parentNode.removeChild(Inline_Menu_Element);
		Inline_Menu_Element = null;
	}
	
	Remove_Highlight();
	
	Edit_Item = $(Parameters["Target_ID"]);
	
	Add_Window(Parameters["Namespace"], Parameters["URL"]);
}
		
Design_Mode_Timeout = 0;
function Design_Mode(Namespace)
{
	if (!$(Namespace + "_Design"))
	{
		if (Design_Mode_Timeout < 10)
		{
			setTimeout(function () {
				Design_Mode(Namespace);
			}, 25);
			Design_Mode_Timeout ++;
		}
		else
			Design_Mode_Timeout = 0;
	}
	{
		// yo
		Design_Mode_Timeout = 0;
		
		// Copy values
		Edit_Namespace = Namespace;
		console.log($(Edit_Namespace + "_Design").contentWindow.document.designMode);
		$(Edit_Namespace + "_Design").contentWindow.document.designMode = "on";
		console.log($(Edit_Namespace + "_Design").contentWindow.document.designMode);
		$(Edit_Namespace + "_Design").contentWindow.document.body.innerHTML =  $(Edit_Namespace).value;
	
		// Set font
		$(Edit_Namespace + "_Design").contentWindow.document.body.style.fontSize = Edit_Item_Style.fontSize;
		$(Edit_Namespace + "_Design").contentWindow.document.body.style.color = Edit_Item_Style.color;
		$(Edit_Namespace + "_Design").contentWindow.document.body.style.fontFamily = Edit_Item_Style.fontFamily;
		// weird move by me, tristan cuz it was always so dark
		$(Edit_Namespace + "_Design").contentWindow.document.body.style.backgroundColor = "white";//Edit_Item_Style.fontFamily;
		
		// Set font
		
		
		
		// Set size
		Editable_Item_Size = Find_Size(Editable_Item, 1);
		$(Edit_Namespace + "_Design").style.height = Editable_Item_Size[1] + "px";
		$(Edit_Namespace + "_Design").style.width = Editable_Item_Size[0] + "px";
		$(Edit_Namespace + "_Design").contentWindow.document.body.style.overflow = "hidden";
		Design_Mode_Resize(true);
		
		// Set event handlers
		$(Edit_Namespace + "_Design").contentWindow.document.addEventListener("keyup", Design_Mode_Resize, true);
	}
}

function Design_Mode_Resize()
{
	if ($(Edit_Namespace + "_Design").contentWindow.document.body.scrollHeight >= $(Edit_Namespace + "_Design").offsetHeight)
	{
		$(Edit_Namespace + '_Design').style.height =  $(Edit_Namespace + "_Design").contentWindow.document.body.scrollHeight + 'px';
		Editable_Item.style.height =  $(Edit_Namespace + "_Design").contentWindow.document.body.scrollHeight + 'px';
		Light_Box($(Edit_Namespace + '_Design'));
	}
}

function Remove_Highlight()
{
	Border_Left_Item = $('Item_Border_Left');
	Border_Right_Item = $('Item_Border_Right');
	Border_Top_Item = $('Item_Border_Top');
	Border_Bottom_Item = $('Item_Border_Bottom');
	Title_Item = $('Item_Title');
	
	if (Border_Left_Item)
	{
		Border_Left_Item.style.display ="none";
		Border_Right_Item.style.display ="none";
		Border_Top_Item.style.display ="none";
		Border_Bottom_Item.style.display ="none";
		Title_Item.style.display ="none";
	}
}

Border_Width = 1;
function Highlight_Item(Target_Item, Title)
{	
	Item_Position = Find_Position(Target_Item);
	Item_Size = Find_Size(Target_Item, 1	);
	
	Border_Left_Item = $('Item_Border_Left');
	
	// If the border and title don't exist yet, generate them.
	if (!Border_Left_Item)
	{		
		Border_Left_Item = document.createElement("div");
		Border_Left_Item.id = "Item_Border_Left";
		Border_Left_Item.className = "Item_Border";	
		$('Body').appendChild(Border_Left_Item);

	
		Border_Right_Item = document.createElement("div");
		Border_Right_Item.id = "Item_Border_Right";
		Border_Right_Item.className = "Item_Border";	
		$('Body').appendChild(Border_Right_Item);
	
		Border_Top_Item = document.createElement("div");
		Border_Top_Item.id = "Item_Border_Top";
		Border_Top_Item.className = "Item_Border";	
		$('Body').appendChild(Border_Top_Item);
	
		Border_Bottom_Item = document.createElement("div");
		Border_Bottom_Item.id = "Item_Border_Bottom";
		Border_Bottom_Item.className = "Item_Border";	
		$('Body').appendChild(Border_Bottom_Item);
	
		Title_Item = document.createElement("div");
		Title_Item.className = "Item_Title";
		Title_Item.id = "Item_Title";
		
		// Yay!
		Title_Item_Title_Item  = document.createElement("div");
		Title_Item_Title_Item.id = "Item_Title_Title";
		
		Pointer_Item = document.createElement("div");
		Pointer_Item.className = "Item_Pointer";
		Pointer_Item.id = "Item_Pointer";		

		$('Body').appendChild(Title_Item);
		Title_Item.appendChild(Title_Item_Title_Item);
		Title_Item.appendChild(Pointer_Item);
	}
	
//	if (!$Item_Position[1])
//		$Item_Position[1] = 15;
//	$Item_Position[1] = Math.max($Item_Position[1], 15);
	
	// Position border 
	Border_Left_Item.style.top = Item_Position[1] + "px";
	Border_Left_Item.style.left = Item_Position[0] - Border_Width  +"px";
	Border_Left_Item.style.height = Item_Size[1] + "px";
	Border_Left_Item.style.width = Border_Width + "px";
	Border_Left_Item.style.display = "block";
	
	Border_Right_Item.style.top = Item_Position[1] + "px";
	Border_Right_Item.style.left = Item_Position[0] + Item_Size[0] +"px";
	Border_Right_Item.style.height = Item_Size[1] + "px";
	Border_Right_Item.style.width = Border_Width + "px";
	Border_Right_Item.style.display = "block";

	Border_Top_Item.style.top = Item_Position[1]  - Border_Width + "px";
	Border_Top_Item.style.left = Item_Position[0]  - Border_Width +  "px";
	Border_Top_Item.style.height = Border_Width + "px";
	Border_Top_Item.style.width =  (Item_Size[0]  + Border_Width * 2) + "px";
	Border_Top_Item.style.display = "block";

	Border_Bottom_Item.style.top = Item_Position[1]  + Item_Size[1] + "px";
	Border_Bottom_Item.style.left = Item_Position[0] - Border_Width +"px";
	Border_Bottom_Item.style.height = Border_Width + "px";
	Border_Bottom_Item.style.width =  (Item_Size[0]  + Border_Width * 2) + "px";
	Border_Bottom_Item.style.display = "block";
	
	// Position title and set inner text
	Title_Item.style.top = Math.max(5, (Item_Position[1]  - 30)) + "px";
	Title_Item.style.left = Math.max(5, (Item_Position[0] - 20))+ "px";
	Title_Item.style.display = "block";
	$('Item_Title_Title').innerHTML = Title;
}


// TODO: This was clearly a test hack.  Clean up.
function Copy_From_Design_And_Save(Action_Namespace, Property_Name)
{
	// Copy html from design div into hidden input
	$(Action_Namespace + "_Property_" + Property_Name).value =  $(Action_Namespace + "_Property_" + Property_Name + "_Design").contentWindow.document.body.innerHTML; 	
	
	//Wtf copy into edit item too
	Edit_Item.innerHTML =  $(Action_Namespace + "_Property_" + Property_Name + "_Design").contentWindow.document.body.innerHTML; 	
	
	// Submit edit action
	Execute_Action(Action_Namespace);
}

function Scope_Visible_Properties(Namespace)
{
	var Simple_Types = new Array("Boolean", "Data", "Date", "DateTime", "Number", "Password", "String", "Time");
	if (Simple_Types.indexOf($(Namespace + "_Value_Type").value) < 0)
	{
		// Complex types	
		$(Namespace + "_Multiple_Row").style.opacity = "1";
		$(Namespace + "_Inline_Row").style.opacity = "1";
		Ramp_Value({"Command": "$(Namespace + \"_Multiple_Row\").style.opacity = VALUE / 10", "Start_Value": 3, "End_Value": 10});
		Ramp_Value({"Command": "$(Namespace + \"_Inline_Row\").style.opacity = VALUE / 10", "Start_Value": 3, "End_Value": 10});
		
		$(Namespace + "_Multiple").enabled = true;
		$(Namespace + "_Inline").enabled = true;
		
		if ($(Namespace + "_Multiple").checked)
		{
			//Multiple	
			$(Namespace + "_Bidirectional_Row").style.opacity = "1";
			$(Namespace + "_Commutative_Row").style.opacity = "1";
			
			$(Namespace + "_Bidirectional").enabled = true;
			$(Namespace + "_Commutative").enabled = true;
		}
		else
		{
			//Single
			$(Namespace + "_Bidirectional_Row").style.opacity = ".3";
			$(Namespace + "_Commutative_Row").style.opacity = ".3";

			$(Namespace + "_Bidirectional").checked = false;
			$(Namespace + "_Commutative").checked = false;
			
			$(Namespace + "_Bidirectional").enabled = false;
			$(Namespace + "_Commutative").enabled = false;
		}
	}
	else
	{
		//Simple types
		$(Namespace + "_Multiple_Row").style.opacity = ".3";
		$(Namespace + "_Inline_Row").style.opacity = ".3";
		$(Namespace + "_Bidirectional_Row").style.opacity = ".3";
		$(Namespace + "_Commutative_Row").style.opacity = ".3";
		
		$(Namespace + "_Commutative_Row").style.height = "0px";

		$(Namespace + "_Multiple").checked = false;
		$(Namespace + "_Inline").checked = true;
		$(Namespace + "_Bidirectional").enabled = false;
		$(Namespace + "_Commutative").enabled = false;
		
		$(Namespace + "_Multiple").enabled = false;
		$(Namespace + "_Inline").enabled = false;
		$(Namespace + "_Bidirectional").enabled = false;
		$(Namespace + "_Commutative").enabled = false;
	}

	console.log($(Namespace + "_Value_Type").value);
	console.log($(Namespace + "_Self_Type").value);
	console.log($(Namespace + "_Multiple").value);
}

/* Menus : None of this shit is used yet.*/

var Menus = new Array;
var Keep_Menus = new Array;
var Lightbox_Clicked = false;
var Windows = new Array;
function Handle_Click(e)
{
	if (!e) var e = window.event;

	// For all the registered menus	
	for (var i = 0; i < Menus.length; i++)
	{
		var Found = false;
		for (var j = 0; j < Keep_Menus.length; j++)
		{
			// For each menu to keep
			var Current_Keep = Keep_Menus[j];
			while (Current_Keep)
			{
				console.log("Current_Keep: " + Current_Keep);
				
				// If I found this registered menu in the either the list of menus to keep or one of the parents of the list of menus to keep, then don't do anything.
				if (Current_Keep.Current == Menus[i].Current)
					Found = true;
				Current_Keep = Current_Keep.Parent;
			}
		}
		if (!Found)
		{
			// Otherwise fade this menu a bit.
			var Menu_Object = $(Menus[i].Current);
			Menu_Object.style.display ="none";
		}
	}
	
	Keep_Menus.clear();
}

function Register_Menu(ID, Parent_ID)
{
	// Create a new menu object and link it to the specified parent menu.
	var Menu_Object = new Object;
	
	Menu_Object.Current = ID;
	
	for (var i = 0; i < Menus.length; i++)
		if (Menus[i].Current == Parent_ID)
			Menu_Object.Parent = Menus[i];
	
	Menus.push(Menu_Object);
	
	console.log(Menu_Object.Current);
	if (Menu_Object.Parent)
	{
		console.log(Menu_Object.Parent.Current);
	}
}

function Register_Window(ID, URL)
{
	Windows.push({"ID": ID, "URL": URL});
}

function Keep_Menu()
{
	//Looks through all Menus for this Menu , adds it to the list of Menus to keep.
	var ID = this.id;
	for (var i = 0; i < Menus.length; i++)
		if (Menus[i].Current == ID)
			Keep_Menus.push(Menus[i]);
}


var Clear_Menus_Handled = false;
function Clear_Menus()
{
	if (!Clear_Menus_Handled)
	{
		Clear_Menus_Handled = true;
		setTimeout("Clear_Menus_Handled = false;", 5);
	
		var ID = this.id;
		Current_Level = 0;
		for (var i = 0; i < Menus.length; i++)
		{	
			if (Menus[i].Element_ID == ID)
			{
				Current_Level = Menus[i].Level;
				break;
			}
		}
		
		console.log("Clear Level for Menu (" + ID + "): " + Current_Level);
		
		for (var i = 0; i < Menus.length; i++)
		{
			if (Menus[i].Level >= Current_Level && Menus[i].Element_ID != ID)
			{
				Menu_Object = $(Menus[i].Element_ID);
				Menu_Object.style.display = "none";	
			}
		}
	}
}

function Registerr_Menu(Menu_ID, Parent_ID)
{
	Menu = new Object;
	Menu.Element_ID = Menu_ID;
//	Menu.Parent_ID = Parent_ID;
	
	console.log("Hej");
	
	for (var i = 0; i < Menus.length; i++)
	{
		console.log(Menus[i].Element_ID + " ==" + Parent_ID);
		if (Menus[i].Element_ID == Parent_ID)
		{
			Menu.Level = Menus[i].Level + 1;
			break;
		}
	}
	
	if (!Menu.Level)
		Menu.Level = 1;
		
	Menus.push(Menu);
}

var Menus = new Array();
var Last_Menu_Index = 0;
function Show_Menu(Parameters)
{
	var Attach_Object = $(Parameters["Attach_ID"]);
	var Menu_Object = $(Parameters["Menu_ID"]);
	
	Last_Menu_Index++;
	
	if (!Menu_Object)
	{
		Menu_Object = document.createElement("div");
		Menu_Object.id = Parameters["Menu_ID"];
		Menu_Object.className = "Browse_Menu";
		Menu_Object.style.backgroundColor = "white";
		Menu_Object.style.borderStyle = "solid";
		Menu_Object.style.borderWidth = "1px";
		Menu_Object.style.borderColor = "#AAAAAA";
		Menu_Object.style.borderLeftWidth = "5px";
		Menu_Object.style.borderLeftColor = "#CCCCCC";
		Menu_Object.style.position = "absolute";
		Menu_Object.style.left = "0px";
		Menu_Object.style.top = "0px";
		Menu_Object.style.overflow = "auto";
		Menu_Object.style.opacity = "0";
		Menu_Object.onclick = Clear_Menus;
		
		Menu_Object.innerHTML = "Loading...";
		$("Body").appendChild(Menu_Object);
	 
	 	Registerr_Menu(Parameters["Menu_ID"], Parameters["Parent_ID"]);
/*		
		if (Parameters["Lightbox"])
			Register_Window(Parameters["Menu_ID"], Parameters["URL"]);
		else
			Register_Menu(Parameters["Menu_ID"], Parameters["Attach_ID"]);
*/

		Fill( { "ID": Parameters["Menu_ID"], "URL": Parameters["URL"], "Response": function()
			{
				Attach({"New_Object": Menu_Object, "Parent_Object": Attach_Object, "Horizontal_Position": Parameters["Horizontal_Position"], "Vertical_Position": Parameters["Vertical_Position"], "Position": Parameters["Position"]});
			}
		});
	}
	
	Menu_Object.style.opacity = "0";
	Menu_Object.style.display = "block";
	
	if (Parameters["Lightbox"])
	{
		var Lightbox_Object = DynamicSite_Lightbox();
		Lightbox_Object.style.display = "block";
		Lightbox_Object.style.zIndex = 100 + Windows.length * 2;
		Menu_Object.style.zIndex = 100 + Windows.length * 2 + 1;
	}
	else
	{
		Menu_Object.style.zIndex = 200 + Last_Menu_Index;
	}
	
	Attach({"New_Object": Menu_Object, "Parent_Object": Attach_Object, "Horizontal_Position": Parameters["Horizontal_Position"], "Vertical_Position": Parameters["Vertical_Position"], "Position": Parameters["Position"]});
	
	Ramp_Value({"Command": "$('" + Parameters["Menu_ID"] + "').style.opacity = 'VALUE';"});
	
	Menu_Object.onclick();
}


function Attach(Parameters)
{
	var New_Object = Parameters["New_Object"];
	
	var Parent_Object = Parameters["Parent_Object"];
	if (Parent_Object)
		var Current_Position = Find_Position(Parameters["Parent_Object"]);
	
	New_Object.style.position = "absolute";
	New_Object.style.left = "0px";
	New_Object.style.top = "-5000px";
	
	console.log(New_Object.offsetHeight);
	console.log(New_Object.offsetWidth);
	
	if (Parameters["Position"] == "Center")
	{
		var Current_Position = new Array($("Body").offsetWidth / 2 - New_Object.offsetWidth / 2, $("Body").offsetHeight / 2 - New_Object.offsetHeight / 2);
	}
	else
	{
		if (Parameters["Horizontal_Position"] == "Left")
			Current_Position[0] -= New_Object.offsetWidth;
		else if (Parameters["Horizontal_Position"] == "Right")
			Current_Position[0] += Parent_Object.scrollWidth + 1;
			
		if (Parameters["Vertical_Position"] == "Top")
			Current_Position[1] -= New_Object.offsetHeight;
		else if (Parameters["Vertical_Position"] == "Bottom")
			Current_Position[1] += Parent_Object.offsetHeight + 5;
		else
			Current_Position[1] += 1;
	}
	
	// Resize if too big
	if (Current_Position[0] + New_Object.offsetWidth > $("Body").offsetWidth - 20)
		Current_Position[0] = $("Body").offsetWidth - New_Object.offsetWidth - 20;
	if (Current_Position[1] + New_Object.offsetHeight > $("Body").offsetHeight - 20)
		Current_Position[1] = $("Body").offsetHeight - New_Object.offsetHeight - 20;
	if (Current_Position[0] < 5)
	{
		Current_Position[0] = 5;
//		New_Object.style.width = ($("Body").offsetWidth - 5 - 20) + "px";
	}
	if (Current_Position[1] < 5)
	{
		Current_Position[1] = 5;
//		New_Object.style.height = ($("Body").offsetHeight - 5 - 20) + "px";
	}
	
	New_Object.style.position = "absolute";
	New_Object.style.left = Current_Position[0] + "px";
	New_Object.style.top = Current_Position[1] + "px";
}

function DynamicSite_Lightbox()
{
	var DynamicSite_Lightbox_Object = $("DynamicSite_Lightbox");
	if (!DynamicSite_Lightbox_Object)
	{
		DynamicSite_Lightbox_Object = document.createElement("div");
		DynamicSite_Lightbox_Object.id = "DynamicSite_Lightbox";
		DynamicSite_Lightbox_Object.innerHTML = "&nbsp;";
		DynamicSite_Lightbox_Object.className = "DynamicSite DynamicSite_Lightbox";
		DynamicSite_Lightbox_Object.onclick = Lightbox_Click;
		$("Body").appendChild(DynamicSite_Lightbox_Object);
	}
	return DynamicSite_Lightbox_Object;
}

function Lightbox_Click()
{
	Lightbox_Clicked = true;
}

function Loading_Window()
{
	var Loading_Window_Object = $("Loading-Window");
	if (!Loading_Window_Object)
	{
		Loading_Window_Object = document.createElement("div");
		Loading_Window.className = "DynamicSite DynamicSite_Window DynamicSite_Loading";
		Loading_Window_Object.id = "Loading-Window";
		Loading_Window_Object.innerHTML = "<h2 class=\"DynamicSite DynamicSite_WindowHeader\" style=\"padding-bottom: 10px;\"><span id=\"Loading-Text\">Loading...</span><img src=\"/Binary_Data/Loading_Image/File/Raw\" alt=\"Loading\"/></h2>";
		$("Body").appendChild(Loading_Window_Object);
	}
	return Loading_Window_Object;
}

function Share_Page(Parameters)
{
	URL = Link_URL();
	switch (Parameters["Target"])
	{
		case "del.icio.us":
			document.location = "http://del.icio.us/tristanperich?url=" + URL + "&title=" + document.title + "&v=4";
			break;
		case "Facebook":
			document.location = "http://www.facebook.com/share.php?u=" + URL + "&t=" + document.title;
			break;
		case "reddit":
			document.location = "http://reddit.com/submit?url="+encodeURIComponent(URL)+"&title="+encodeURIComponent(document.title);
			break;
		case "digg":
			document.location = "http://digg.com/submit?phase=2&url=" + URL;
			break;
		case "Email":
			Show_Menu({'URL': '/Action/Email_Link/Raw', 'Menu_ID': 'Sharing_Email_Link_Menu', 'Parent_ID': 'Toolbar_Sharing_Links_Menu', 'Attach_ID': 'Sharing_Email_Link', 'Vertical_Position': 'Bottom'});
			break;
	}
}

function Link_URL()
{
	var URL = document.location.toString();
	URL = URL.replace('#', '');
	return URL;
}

function Refresh_Sharing_Links()
{
	URL = Link_URL();
	if ($("URL_Field"))
		$("URL_Field").innerHTML = "Link: <a href=\"" + URL + "\">" + URL + "</a>";
}

function Refresh_Email_Link()
{
	URL = Link_URL();
	if ($("Email_Link-URL"))
		$("Email_Link-URL").value = URL;
}

function Toggle_Input(Prefix)
{
	if ($(Prefix + "_Input").style.display == "block")
	{
		$(Prefix + "_Input").style.opacity = ".3";
		$(Prefix + "_Value").style.display = "block";
	}
	else
	{
		$(Prefix + "_Input").style.display = "block";
		$(Prefix + "_Value").style.opacity = ".3";
	}
}

function Light_Box(Target_Element)
{
	// Lightbox around the element
	if (Target_Element)
	{
			Target_Size = Find_Size(Target_Element, 1);
			Target_Position = Find_Position(Target_Element);
			
			// Generate lightbox elements, if they don't exist.
			Lightbox_Left_Item = $('Lightbox_Left');
			Lightbox_Right_Item = $('Lightbox_Right');
			Lightbox_Top_Item = $('Lightbox_Top');
			Lightbox_Bottom_Item = $('Lightbox_Bottom');
			if (!Lightbox_Left_Item)
			{		
				Lightbox_Left_Item = document.createElement("div");
				Lightbox_Left_Item.id = "Lightbox_Left";
				Lightbox_Left_Item.className = "Lightbox";	
				$('Body').appendChild(Lightbox_Left_Item);
		
			
				Lightbox_Right_Item = document.createElement("div");
				Lightbox_Right_Item.id = "Lightbox_Right";
				Lightbox_Right_Item.className = "Lightbox";	
				$('Body').appendChild(Lightbox_Right_Item);
			
				Lightbox_Top_Item = document.createElement("div");
				Lightbox_Top_Item.id = "Lightbox_Top";
				Lightbox_Top_Item.className = "Lightbox";	
				$('Body').appendChild(Lightbox_Top_Item);
			
				Lightbox_Bottom_Item = document.createElement("div");
				Lightbox_Bottom_Item.id = "Lightbox_Bottom";
				Lightbox_Bottom_Item.className = "Lightbox";	
				$('Body').appendChild(Lightbox_Bottom_Item);
			}
			
			// Position lightbox 
			Lightbox_Left_Item.style.top = (Target_Position[1] - Border_Width) + "px";
			Lightbox_Left_Item.style.left = "0px";
			Lightbox_Left_Item.style.height = Math.max(0, (Target_Size[1] + 2 * Border_Width)) + "px";
			Lightbox_Left_Item.style.width = Math.max(0, (Target_Position[0] - Border_Width)) + "px";
			Lightbox_Left_Item.style.display = "block";
			
			Lightbox_Right_Item.style.top = (Target_Position[1] - Border_Width) + "px";
			Lightbox_Right_Item.style.left = (Target_Position[0] + Target_Size[0] + Border_Width) +"px";
			Lightbox_Right_Item.style.height = Math.max(0, (Target_Size[1] + 2 * Border_Width)) + "px";
			Lightbox_Right_Item.style.width = "100%";
			Lightbox_Right_Item.style.display = "block";
		
			Lightbox_Top_Item.style.top = "0px";
			Lightbox_Top_Item.style.left = "0px";
			Lightbox_Top_Item.style.height = Math.max(0, (Target_Position[1] - Border_Width)) + "px";
			Lightbox_Top_Item.style.width =  "100%";
			Lightbox_Top_Item.style.display = "block";
		
			Lightbox_Bottom_Item.style.top = (Target_Position[1] + Target_Size[1]  + Border_Width) + "px";
			Lightbox_Bottom_Item.style.left = "0px";
			Lightbox_Bottom_Item.style.height = "100%";
			Lightbox_Bottom_Item.style.width =  "100%";
			Lightbox_Bottom_Item.style.display = "block";
	}
	
	//  Simple lightbox
	else
	{
		var Lightbox_Item = $("Lightbox");
		if (!Lightbox_Item)
		{
			Lightbox_Item = document.createElement("div");
			Lightbox_Item.id = "Lightbox";
			Lightbox_Item.className = "Lightbox";
			Lightbox_Item.style.left = "0px";
			Lightbox_Item.style.top = "0px";
			Lightbox_Item.style.width = "100%";
			Lightbox_Item.style.height = "100%";
			Body_Item = $("Body");
			Body_Item.appendChild(Lightbox_Item);
		}
		else
		{
			Lightbox_Item.style.display = "block";
		}
		Lightbox_Item.style.zIndex = 500 + Windows.length * 2;
	}
}


/* Utility Functions */

function Find_Scroll_Position()
{
	Scroll_Left = 0;
	Scroll_Top = 0;
	if (pageXOffset)
	{
		Scroll_Left = pageXOffset;
		Scroll_Top = pageYOffset;
	}	
	else if (document.documentElement)
	{
		Scroll_Left = document.documentElement.scrollLeft;
		Scroll_Top = document.documentElement.scrollTop;
	}
	else if (document.body)
	{
		Scroll_Left = document.body.scrollLeft;
		Scroll_Top = document.body.scrollTop;
	}
	
	return [Scroll_Left, Scroll_Top];
}

function Find_Position(Current_Object)
{
	var Current_Left = 0;
	var Current_Top = 0;
	if (Current_Object.offsetParent)
	{
		do
		{
			Current_Left += Current_Object.offsetLeft;
			Current_Top += Current_Object.offsetTop;
		}
		while (Current_Object = Current_Object.offsetParent);
	}
	return [Current_Left, Current_Top];
}

// TODO: Test, make work reliably
function Find_Size(Current_Element, Levels)
{
//	console.log ("Find size (" + Current_Element.id + "): ");
	var Current_Width = 0;
	var Current_Height = 0;
	Current_Width = Current_Element.offsetWidth;
	// console.log("WID: " + Current_Width);
	Current_Height = Current_Element.offsetHeight;
	
	if (Levels > 0 && Current_Element.style.display != "block")
	{
		for (Child_Index = 0; Child_Index < Current_Element.childNodes.length; Child_Index++)
		{
			var Current_Child = Current_Element.childNodes[Child_Index];
			if (Current_Child.nodeType == 1)
			{
				var Child_Size = Find_Size(Current_Child, Levels - 1);
				// console.log(Child_Index + ":" + Child_Size[0]);
//				console.log(Current_Child.id + ")" + Current_Child.offsetTop + "..." + Current_Element.offsetTop + "..." + Child_Size[0]);
				if (Child_Size[0])
					Current_Width = Math.max(Current_Width, Current_Child.offsetLeft - Current_Element.offsetLeft + Child_Size[0]);
				if (Child_Size[1])
					Current_Height = Math.max(Current_Height, Current_Child.offsetTop - Current_Element.offsetTop + Child_Size[1]);
			}
		}
	}
//	console.log ("End Find Size");
	return [Current_Width, Current_Height];
}