function Ramp_Value(Parameters)
{
	// Parameters: Command, Start_Value (default: 0), End_Value (default: 1), Duration (milliseconds, default: 500), Start_Time (default: now)
	
	var d = new Date();
	var Current_Time = d.getTime();
	if (!Parameters["Duration"])
		Parameters["Duration"] = 500;
	if (!Parameters["Start_Time"])
		Parameters["Start_Time"] = Current_Time;
	if (!Parameters["Start_Value"])
		Parameters["Start_Value"] = 0;
	if (!Parameters["End_Value"])
		Parameters["End_Value"] = 1;
	
	var Ramp_Value_Recursive = function()
	{
		var d = new Date();
		var Current_Time = d.getTime();
		
		var Partial = (Current_Time - Parameters["Start_Time"]) / Parameters["Duration"];
		if (Partial > 1)
			Partial = 1;
		Partial = (1 - Math.pow((1 - Partial), 4));
		
		var Value = Parameters["Start_Value"] + (Parameters["End_Value"] - Parameters["Start_Value"]) * Partial;
		
		var Command = Parameters["Command"];
		Command = Command.replace(/VALUE/g, Value);
		try
		{
			eval(Command);
		}
		catch(Error)
		{
		}
		
		if (Partial < 1)
			setTimeout(Ramp_Value_Recursive, 0);
		else
			if (Parameters["On_Complete"])
				Parameters["On_Complete"]();
	}
	
	setTimeout(Ramp_Value_Recursive, 0);
}


function Action( Alias, Values)
{	
	// Show loading window
	ShowWindow("Loading");
		
	// Get action information
	if (Values)
		URL = "/Type/Action/" + Alias + "/Raw:" + Values;
	else
		URL = "/Type/Action/" + Alias + "/Raw";
			
	// Create window if necessary
	var WindowElement = document.getElementById(Alias + "-Window")
	if (!WindowElement)	
	{
		var SpanElement = document.createElement("span");
		
		// Make IE use "absolute" rather than default to "static"
		SpanElement.innerHTML = "<div id=\"" + Alias + "-Window"+ "\" class=\"DynamicSite DynamicSite_Window Bowery_Window\"></div>";
		
		ExternalElement = document.getElementById("ExternalHTML");
		ExternalElement.appendChild(SpanElement);	
		WindowElement = SpanElement.lastChild;
	}
	
	// Hide loading window and show action
	Response = function () {
		HideWindow("Loading");			
		ShowWindow(Alias);
	};
	
	//Fill window with a URL and response function
	Fill(WindowElement, URL, Response);		
}

var LastURL = "";
var Display_Week = null;
var Display_Day = null;
var Last_Week = null;
var Previous_Day = null;
function Watch_Address_Bar()
{
	var CurrentURL = document.location.href;
	var PoundPosition = CurrentURL.indexOf("#");
	if (PoundPosition != -1)
		CurrentURL = CurrentURL.substring(PoundPosition + 1);
	else
		CurrentURL = "Site/Default_Page";
	
	if (CurrentURL != LastURL)
	{
		// Set Lite Login shit.
		$('Lite_Login-Redirect').value = CurrentURL;
		
		LastURL = CurrentURL;
		CurrentURL = CurrentURL.replace(/\/*$/g, "");
//		alert(Previous_Day + ":" + Display_Day);
		if (Previous_Day != Display_Day)
			if (Previous_Day && CurrentURL != Previous_Day)
				Unhighlight_Day(Last_Week, Previous_Day, true);
		if (Display_Week != Last_Week)
			if (Last_Week && CurrentURL != "Week_Of_" + Last_Week)
				Unhighlight_Week(Last_Week, true);
		Last_Week = Display_Week;
		Previous_Day = Display_Day;
		
		if (CurrentURL.toLowerCase().indexOf("bookings_calendar") > -1)
		{
			$('Normal_Sidebar').style.display = "none";
			$('Bookings_Calendar_Sidebar').style.display = "block";	
		}
		else
		{
			$('Normal_Sidebar').style.display = "block";
			$('Bookings_Calendar_Sidebar').style.display = "none";			
		}
	
		// Set loading status.
		LoadingIndicator = $('Last_Path_Item-Loading');
		if (LoadingIndicator)
			LoadingIndicator.style.display = "block";	
		
		if (CurrentURL == "")
		{
			var RequestURL = "/" + "Site/Default_Page" + "/Raw";
		}
		else
		{
			var RequestURL = "/" + CurrentURL + "/Raw";
			if (CurrentURL != 'Site/Default_Page')
			{
				var TitleURL = "/" + CurrentURL + "/Title/Raw";
				Set_To_Title(TitleURL);
			}
			
			var Callout_URL = "/" + CurrentURL + "/Callout/Raw";
			Fill('Callout', Callout_URL);			
		}
		
		//Remove loading status
		Response = function () {
			Ramp_Value({"Command":"scrollTo(0,VALUE)", "Start_Value": getViewportScrollY(), "End_Value":0, "Duration":1000});
			LoadingIndicator = $('Last_Path_Item-Loading');
			if (LoadingIndicator)
				LoadingIndicator.style.display = "none";
			Hide_Arrow();
		};
		
		Fill("Last_Path_Item", RequestURL, Response);
		
	}
}










/* Calendar Functions */

Calendar_In_Drag = false;
Calendar_Drag_Begin = "";
Calendar_Drag_End = "";
Calendar_Event_ID = "";

function Calendar_Begin_Drag(e, Date_String)
{
	Date_Array = Date_String.split("_");
	Begin_Day = Date_Array[2];
	Begin_Month = Date_Array[1] - 1;
	Begin_Year = Date_Array[0];
	
	Calendar_Drag_Begin = new Date(Begin_Year, Begin_Month,Begin_Day);
	Calendar_In_Drag = true;
}


function Calendar_Continue_Drag(Date_String)
{	
	// clear month
	Calendar_Clear_Month(Calendar_Drag_Begin);
			
	// highlight some
	Date_Array = Date_String.split("_");
	End_Day = Date_Array[2];
	End_Month = Date_Array[1] - 1;
	End_Year = Date_Array[0];
	Calendar_Drag_End = new Date(End_Year, End_Month,End_Day);	
	
	Current_Date = Calendar_Drag_Begin;
	Current_Difference = (Calendar_Drag_End - Calendar_Drag_Begin) /( 1000 * 60 * 60 * 24);
	Current_Direction = Current_Difference / Math.abs(Current_Difference);
	Current_Difference = Math.abs(Current_Difference);
	for (i = 0; i <= Current_Difference; i++)
	{
		Current_Date_ID = Current_Date.getFullYear() + "_" + (Current_Date.getMonth() + 1) + "_" + Current_Date.getDate();
		Calendar_Highlight(Current_Date_ID.toString());
		Current_Date = new Date(Current_Date.getFullYear(), Current_Date.getMonth(), Current_Date.getDate() + Current_Direction);
	}

}

function Calendar_End_Drag(e, Date_String)
{
	Date_Array = Date_String.split("_");
	End_Day = Date_Array[2];
	End_Month = Date_Array[1] - 1;
	End_Year = Date_Array[0];
	Calendar_Drag_End = new Date(End_Year, End_Month,End_Day);	
	
	/* Change display state */
	Current_Date = Calendar_Drag_Begin;
	Current_Difference = (Calendar_Drag_End - Calendar_Drag_Begin) /( 1000 * 60 * 60 * 24);
	Current_Direction = Current_Difference / Math.abs(Current_Difference);
	Current_Difference = Math.abs(Current_Difference);
	for (i = 0; i <= Current_Difference; i++)
	{
		Current_Date_ID = Current_Date.getFullYear() + "_" + (Current_Date.getMonth() + 1) + "_" + Current_Date.getDate();
		$('Event_Possible_Date_Off_' + Current_Date_ID).style.display = 'none';
		$('Event_Possible_Date_On_' + Current_Date_ID).style.display = 'block';
		Current_Date = new Date(Current_Date.getFullYear(), Current_Date.getMonth(), Current_Date.getDate() + Current_Direction);		
	}	
	
	/* Submit action  */
	$('Propose_Dates_' + Calendar_Event_ID + "-Begin").value = Calendar_Drag_Begin.getFullYear() + "-" + (Calendar_Drag_Begin.getMonth() + 1) +  "-" + Calendar_Drag_Begin.getDate();
	$('Propose_Dates_' + Calendar_Event_ID + "-End").value = Calendar_Drag_End.getFullYear() + "-" + (Calendar_Drag_End.getMonth() + 1) +  "-" + Calendar_Drag_End.getDate();		
	SubmitAsynchronousAction("Propose_Dates", "Propose_Dates_" + Calendar_Event_ID, true);
	
	Calendar_Clear_Month(Calendar_Drag_End);
	Calendar_In_Drag = false;
}

function Calendar_Highlight(Date_String)
{
	Heading = $('Event_Possible_Date_' + Date_String);
	if (Heading)
	{
		Heading.style.backgroundColor = "#dddddd";
	}
}

function Calendar_Clear(Date_String)
{
	Heading = $('Event_Possible_Date_' + Date_String);
	if (Heading)
	{
		Heading.style.backgroundColor = "#eeeeee";
	}
}

function Calendar_Clear_Month(Current_Date)
{
	Current_Date = new Date(Current_Date.getFullYear(), Current_Date.getMonth(), 1);
	Current_Month = Current_Date.getMonth();
	while (Current_Date.getMonth() == Current_Month)
	{
		Current_Date_ID = Current_Date.getFullYear() + "_" + (Current_Date.getMonth() + 1) + "_" + Current_Date.getDate();		
		Calendar_Clear(Current_Date_ID.toString());		
		Current_Date = new Date(Current_Date.getFullYear(), Current_Date.getMonth(), Current_Date.getDate() + 1);
	}	
}

function Calendar_Cancel_Possible_Date(e, Namespace, Date_String)
{	
	if (!e) var e = window.event;
	
	/* Change display state */
	Current_Date_ID = Date_String;
	$('Event_Possible_Date_On_' + Current_Date_ID).style.display = 'none';
	$('Event_Possible_Date_Off_' + Current_Date_ID).style.display = 'block';
	
	/* Submit action */
	SubmitAsynchronousAction("Cancel_Date", Namespace, true);
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
	Calendar_In_Drag = false;
}

function Calendar_Hover(e, Date_String)
{
	if (Calendar_In_Drag)
		Calendar_Continue_Drag(Date_String);
	else
		Calendar_Highlight(Date_String);
}

function Calendar_Unhover(e, Date_String)
{
	if (!Calendar_In_Drag)
		Calendar_Clear(Date_String);		
}












// Page specific functions

var Arrow_Start_X = 0;
var Arrow_Start_Y = 0;
var Arrow_End_X = 0;
var Arrow_End_Y = 0;
var Arrow_Step = 0;

function Move_Arrow_To(Target_X, Target_Y)
{
	var Arrow_Element = document.getElementById("Arrow");
	Arrow_Start_X = findPosX(Arrow_Element);
	Arrow_Start_Y = findPosY(Arrow_Element);
	
	Arrow_End_X = Target_X;
	Arrow_End_Y = Target_Y;
	
	Arrow_Step = 0;
	
	Move_Arrow();
}

function Move_Arrow()
{
	Arrow_Step++;
	
	var Arrow_Element = document.getElementById("Arrow");
	Arrow_Element.style.left = (Arrow_Start_X + (Arrow_End_X - Arrow_Start_X) / 20 * Arrow_Step) + "px";
	Arrow_Element.style.top = (Arrow_Start_Y + (Arrow_End_Y - Arrow_Start_Y) / 20 * Arrow_Step) + "px";
	
	if (Arrow_Step < 20)
		setTimeout("Move_Arrow()", 10);
}

function Hide_Arrow()
{
	var Arrow_Element = document.getElementById("Arrow");
	Arrow_Element.style.top = "-200px";
}

function Create_Event_Move_To_Save()
{
	var Target_Element = document.getElementById("Create_Event_Button");
	Move_Arrow_To(findPosX(Target_Element) - 118, findPosY(Target_Element) - 10);
}

function Edit_Details_Move_To_Save()
{
	var Target_Element = document.getElementById("Edit_Details_Save_Button");
	Move_Arrow_To(findPosX(Target_Element) - 118, findPosY(Target_Element) - 10);
}

function Edit_Basic_Move_To_Save()
{
	var Target_Element = document.getElementById("Edit_Basic_Save_Button");
	Move_Arrow_To(findPosX(Target_Element) - 118, findPosY(Target_Element) - 10);
}

function Scroll_To_Arrow()
{
	Arrow_Element = $('Arrow');
	Ramp_Value({"Command":"scrollTo(0,VALUE)", "Start_Value": getViewportScrollY(), "End_Value": findPosY(Arrow_Element), "Duration":1000});
}

function Scroll_To_Element(Target_Element_ID)
{
	var Target_Element = document.getElementById(Target_Element_ID);
	// Parameters: Command, Start_Value (default: 0), End_Value (default: 1), Duration (milliseconds, default: 500), Start_Time (default: now)
	Ramp_Value({"Command":"scrollTo(0,VALUE)", "Start_Value": getViewportScrollY(), "End_Value": findPosY(Target_Element), "Duration":1000});
}

getViewportScrollY = function() {
  var scrollY = 0;
  if( document.documentElement && document.documentElement.scrollTop ) {
    scrollY = document.documentElement.scrollTop;
  }
  else if( document.body && document.body.scrollTop ) {
    scrollY = document.body.scrollTop;
  }
  else if( window.pageYOffset ) {
    scrollY = window.pageYOffset;
  }
  else if( window.scrollY ) {
    scrollY = window.scrollY;
  }
  return scrollY;
};

function Toggle_Contract(Booking_ID)
{
	if ( $('Admin_Contract_Toggle_' +Booking_ID).innerHTML == "show")
	{
		$('Admin_Contract_' +Booking_ID).style.display = "block";
		$('Admin_Contract_Toggle_' +Booking_ID).innerHTML = "hide";
	}
	else
	{
		$('Admin_Contract_' +Booking_ID).style.display = "none";
		$('Admin_Contract_Toggle_' +Booking_ID).innerHTML = "show";
	}	
}

Current_Highlight_Booking_ID = "";
function Highlight_Calendar_Booking(Booking_ID)
{
	if (Current_Highlight_Booking_ID != "")
	{
//		$('Calendar_Booking_' + Current_Highlight_Booking_ID).style.background = "none";
	}
	
	Current_Highlight_Booking_ID = Booking_ID;
//	$('Calendar_Booking_' + Booking_ID).style.backgroundColor = "lightPink";
}


// Bookings Calendar
var Suggestion_Visibilities = new Hash();
var Suggestions = new Array();

var Selected_Calendar_Event_ID = -1;

function Select_Calendar_Event(Event_ID)
{
	// Unselect selected event.
	if (Selected_Calendar_Event_ID != -1)
	{
		Sidebar_Event = $('Sidebar_Event_' + Selected_Calendar_Event_ID);
		if (Sidebar_Event)
			Sidebar_Event.style.fontWeight = "normal";
		Unhighlight_Calendar_Event(Selected_Calendar_Event_ID);
	}

	// Select this event.
	Selected_Calendar_Event_ID = Event_ID;
	Sidebar_Event = $('Sidebar_Event_' + Selected_Calendar_Event_ID);
	if (Sidebar_Event)
		Sidebar_Event.style.fontWeight = "bold";
	Highlight_Calendar_Event(Selected_Calendar_Event_ID);


}

function Highlight_Calendar_Event(Event_ID)
{
	// Steal highlight from selected item.
	if (Event_ID != Selected_Calendar_Event_ID)
		Unhighlight_Calendar_Event(Selected_Calendar_Event_ID);

	// Highlight this event.
	Calendar_Events = $$("a.Calendar_Event_" + Event_ID);
	Calendar_Events.each(function(Calendar_Event)
	{
		Calendar_Event.addClassName("Calendar_Event_Highlight");
	});

	Suggestion_Events = $$("div.Suggested_Event_" + Event_ID);
	Suggestion_Events.each(function(Suggestion_Event)
	{
		Suggestion_Event.addClassName("Calendar_Event_Highlight");
	});
}

function Unhighlight_Calendar_Event(Event_ID)
{
	// Unhighlight this event.
	Calendar_Events = $$("a.Calendar_Event_" + Event_ID);
	Calendar_Events.each(function(Calendar_Event)
	{
		Calendar_Event.removeClassName("Calendar_Event_Highlight");
	});

	Suggestion_Events = $$("div.Suggested_Event_" + Event_ID);
	Suggestion_Events.each(function(Suggestion_Event)
	{
		Suggestion_Event.removeClassName("Calendar_Event_Highlight");
	});

	// Return highlight to selected item.
	if (Event_ID != Selected_Calendar_Event_ID)
		Highlight_Calendar_Event(Selected_Calendar_Event_ID);
}


// Small action called by onchange events in a date selector in order to update a list of events for the day.
function Refresh_Single_Date_Schedule(Action_Namespace)
{
	// Build a date string.
		Month = $(Action_Namespace + "-Booking_Date-Month").value;
		Day = $(Action_Namespace + "-Booking_Date-Day").value;
		Year = $(Action_Namespace + "-Booking_Date-Year").value;

		SQL_Time = Year + "-" + Month + "-" + Day;

	// Refresh list.
		Fill("Single_Date_Schedule", "/block/single_date_schedule/raw:Single_Date=" + SQL_Time);
}

Bookings_Calendar_Start_Date = null;
Bookings_Calendar_Start_Date_URL = "";
function Refresh_Bookings_Calendar(Event_ID, Booking_ID)
{
	if (Bookings_Calendar_Start_Date != null)
		Bookings_Calendar_Start_Date_URL = ":Start_Date=" + Bookings_Calendar_Start_Date;
		
	Fill('Bookings_Calendar', '/Block/Bookings_Calendar_Block/Raw' + Bookings_Calendar_Start_Date_URL);	
	
	if (Event_ID)
	{
		Event_Booking_Window_URL = '/Event/' + Event_ID + '/Booking_Window/Raw';
		if (Booking_ID)
			Event_Booking_Window_URL += ':Booking=' + Booking_ID;
		Fill('Event_Booking-Window', Event_Booking_Window_URL);
	}
	else
	{
		HideWindow(Event_Booking, true);
	}	
}

// Called by the bookings calendar block on refresh.
function Refresh_Suggestions()
{
	// Toggle the suggestions in the calendar after the scope has loaded.
	Response = function()
	{
		Suggestions.each(function(Suggestion)
		{
			if (Suggestion_Visibilities.get(Suggestion) == true)
				Show_Suggestions_For_Event(Suggestion);
		});

		Select_Calendar_Event(Selected_Calendar_Event_ID);
	}

	// Refreshes the scope of suggestions, both in the sidebar and in the Suggestions array used in the response. 
	Fill('Bookings_Calendar_Sidebar', '/block/Bookings_Calendar_Event_Toggles/raw', Response);
}

function Hide_Suggestions_For_Event(Event_ID)
{
	$("Toggle_" + Event_ID).checked = false;
	Suggestion_Visibilities.set(Event_ID, false);
	Suggested_Events = $$("div.Suggested_Event_" + Event_ID);
	Suggested_Events.each(function(Suggested_Event)
	{
		Suggested_Event.hide();
	});
}

function Show_Suggestions_For_Event(Event_ID)
{
	$("Toggle_" + Event_ID).checked = true;
	Suggestion_Visibilities.set(Event_ID, true);
	Suggested_Events = $$("div.Suggested_Event_" + Event_ID);
	Suggested_Events.each(function(Suggested_Event)
	{
		Suggested_Event.show();
	});
}

function Show_Suggestions_For_Date(SQL_Time)
{
	// Show all the suggestions for the day.
	Suggested_Events = $$("div.Suggested_Date_" + SQL_Time);
	Suggested_Events.each(function(Suggested_Event)
	{
		Suggested_Event.show();
	});
}

function Hide_Suggestions_For_Date(SQL_Time)
{
	// Hide all the suggestions for the day.
	Suggested_Events = $$("div.Suggested_Date_" + SQL_Time);
	Suggested_Events.each(function(Suggested_Event)
	{
		Suggested_Event.hide();
	});

	// Then show the suggestions for that day that are toggled on.
	Suggestions.each(function(Suggestion)
	{
		if (Suggestion_Visibilities.get(Suggestion) == true)
		{
			Suggestion_Element = $('Suggestion_' + Suggestion + '_' + SQL_Time);
			if (Suggestion_Element)
				Suggestion_Element.show();
		}
	});
}

function Toggle_Suggestions_For_Event(Event_ID)
{
	if ( Suggestion_Visibilities.get(Event_ID) == true)
		Hide_Suggestions_For_Event(Event_ID);
	else
		Show_Suggestions_For_Event(Event_ID);
}

function Show_All_Suggestions()
{
	Suggestions.each(function(Suggestion)
	{
		Show_Suggestions_For_Event(Suggestion);
	});
}

function Hide_All_Suggestions()
{
	Suggestions.each(function(Suggestion)
	{
		Hide_Suggestions_For_Event(Suggestion);
	});
}

Last_Selected_Date = null;
function Select_Bookings_Calendar_Date(SQL_Time)
{
	if (Last_Selected_Date != null)
	{
		Date_Element = $('Calendar_Day_' + Last_Selected_Date);
		Date_Element.removeClassName("Calendar_Day_Selected");
		Hide_Suggestions_For_Date(Last_Selected_Date);
	}

	Last_Selected_Date = SQL_Time;
	Date_Element = $('Calendar_Day_' + SQL_Time);
	Date_Element.addClassName("Calendar_Day_Selected");
	Show_Suggestions_For_Date(SQL_Time);
}


