var movie;
var selectedOffice;
//var officeFormDescriptionEditor;
var officeImageSourceRoot = "/graphics/locations/officeimages/";
var selectedOffices;
var selectedRegionID;
var throbber;

$(document).ready(page_Load);

window.$name = function(name)
{
	if (navigator.appName.toLowerCase().indexOf("microsoft") != -1)
	{
		return window[name];
	}
	else
	{
		return document[name];
	}
}

function page_Load()
{
	//location.hash = "0";
	
	movie = $name("locationsFlash");
	
	$("#locationTitleContainer").hide();
	
	//$("#backToMapContainer").bind("click", backToMapContainer_click);
	Throbber_Ready();
}

function Map_Loaded()
{
	movie.setFrontEndView();
}

function Throbber_Ready()
{
	hideShowLoading("hide");
	
	//throbber = $name("locationsLoading");
}


function callResetMap()
{
	$("#locationTitleContainer").hide();
	
	movie.resetMap();
}



/*
function formatCompleteOfficeDescription(office)
{
	
	$("#officeInfoCompleteImage").attr("src", officeImageSourceRoot + office.image);
	
	var address = "<strong>"+ office.name +"</strong>";
	if(office.address1.length > 0)
	{
		address += "<br>" + office.address1;
	}
	if(office.address2.length > 0)
	{
		address += "<br>" + office.address2;
	}
	if(office.city.length > 0)
	{
		address += "<br>" + office.city;
	}
	if(office.state.length > 0)
	{
		address += ", " + office.state;
	}
	if(office.zip.toString().length > 0)
	{
		address += " " + office.zip;
	}
	if(office.phone.toString().length > 0)
	{
		address += "<br>phone: " + office.phone;
	}
	if(office.fax.toString().length > 0)
	{
		address += "<br>fax: " + office.fax;
	}
	
	$("#officeInfoCompleteAddress").html(address);
	$("#officeInfoCompleteDescription").html(office.description);
	
	
	$("#officeInfoBriefTableContainer").html(office.description);
}
*/

/*
function formatSelectedOffices()
{
	// clear divs
	var leftDiv = $("#briefLeftContainer");
	var rightDiv = $("#briefRightContainer");
	leftDiv.empty();
	rightDiv.empty();
	
	// loop through offices
	for(var i=0; i < selectedOffices.length; i++)
	{
		var office = selectedOffices[i];
		
		var officeContainer = $(document.createElement("div"));
		// office container styles
		officeContainer.addClass("briefContainer");
		officeContainer.css(
			{
				"position": "relative",
				"height": 70,
				"line-height": "12px",
				"margin-bottom": 10,
				"margin-right": 5,
				"cursor": "pointer"
			}
		);
		officeContainer.bind("click", null, briefContainer_click);
		officeContainer.bind("mouseenter", null, briefContainer_mouseenter);
		officeContainer.bind("mouseleave", null, briefContainer_mouseleave);
		// office container custom attributes
		officeContainer.attr("OfficeID", office.officeid);
		officeContainer.attr("HasDescription", $.trim(office.description).length > 0)
		if(officeContainer.attr("HasDescription") == "false")
		{
			officeContainer.css("cursor", "");
		}
		
		var officeImage = $(document.createElement("div"));
		// office image styles
		officeImage.css(
			{
				"height": 70,
				"float": "left",
				"padding-right": 4
			}
		);
		var officeImageImg = $(document.createElement("img"));
		officeImageImg.attr("src", officeImageSourceRoot + office.image);
		if(office.image == "")
		{
			officeImageImg.hide();
		}
		else
		{
			officeImageImg.show();
		}
		officeImageImg.css(
			{
				"width": 64,
				"height": 44
			}
		);
		officeImageImg.appendTo(officeImage);
		
		officeImage.appendTo(officeContainer);
		
		var officeAddress = $(document.createElement("div"));
		// office address styles
		
		var address = "<strong>"+ office.name +"</strong>";
		if(office.address1.length > 0)
		{
			address += "<br>" + office.address1;
		}
		if(office.address2.length > 0)
		{
			address += "<br>" + office.address2;
		}
		if(office.city.length > 0)
		{
			address += "<br>" + office.city;
		}
		if(office.state.length > 0)
		{
			address += ", " + office.state;
		}
		if(office.zip.toString().length > 0)
		{
			address += " " + office.zip;
		}
		if(office.phone.toString().length > 0)
		{
			address += "<br>phone: " + office.phone;
		}
		if(office.fax.toString().length > 0)
		{
			address += "<br>fax: " + office.fax;
		}
		
		officeAddress.append(address);
		
		officeAddress.appendTo(officeContainer);
		
		if(i % 2 == 0)
		{
			officeContainer.appendTo(leftDiv);
		}
		else
		{
			officeContainer.appendTo(rightDiv);
		}
	}
}
*/



function hideShowLoading(type)
{
	if(type == "hide")
	{
		$("#loadingContainer").hide();
	}
	else
	{
		$("#loadingContainer").show();
	}
}


////////////

function hideShowMainLocationsMapContainer(type)
{
	if(type == "hide")
	{
		$("#locationsContainer").css("height", 0);
		$("#resetMapLocationTitleLoadingContainer").hide();
		$("#officeInfoBriefTableContainer").hide();
	}
	else
	{
		$("#locationsContainer").css("height", 480);
		$("#resetMapLocationTitleLoadingContainer").show();
		$("#officeInfoBriefTableContainer").show();
	}
}



/*
function backToMapContainer_click(e)
{
	//location.hash = "0";
	
	$("#officeInfoBriefContainer").show();
	$("#officeInfoCompleteContainer").hide();
	$("#landingPageContentContainer").hide();
	
	hideShowMainLocationsMapContainer("show");
}
*/

/*
function briefContainer_click(e)
{
	if($(this).attr("HasDescription") == "false")
	{
		return;
	}
	
	hideShowLoading("show");
	
	var officeID = $(this).attr("OfficeID");
	var result = function(res)
	{
		//location.hash = officeID;
		

		// hide map and map functionality pieces
		hideShowMainLocationsMapContainer("hide");
		$("#officeInfoBriefContainer").hide();
		$("#landingPageContentContainer").hide();
		
		$("#officeInfoCompleteContainer").show();
		
		formatCompleteOfficeDescription(res[0]);
		hideShowLoading("hide");
	}
	
	var proxy = new locationsProxy();
	proxy.setCallbackHandler(result);
	
	proxy.getOfficeData(officeID);
}
*/

function briefContainer_mouseenter(e)
{
	if($(this).attr("HasDescription") == "false")
	{
		return;
	}
	
	$(this).css("background-color", "#e4e4e4");
}

function briefContainer_mouseleave(e)
{
	$(this).css("background", "none");
}


function frontEnd_loadRegion_FromAS(id)
{
	selectedRegionID = id;
}


function regionData_Load(id)
{
	var proxy = new locationsProxy();
	var proxy_Result = function(results)
	{
		var result = results[0];
		$("#officeInfoBriefTableContainer").html(result.description);
	}
	var proxy_Error = function(txt, code)
	{
		alert("arg! " + txt + "  " + code);
	}
	
	proxy.setCallbackHandler(proxy_Result);
	proxy.setErrorHandler(proxy_Error);
	proxy.getRegionData( id );
}

function zoom_Complete_FromAS(zoomState)
{
	if(zoomState == "in")
	{
		// set/show region label
		var regionName = movie.getCurrentRegionName();
		var locTitleSrc = "/graphics/locations/locationLabels/" + regionName.replace(/ /g, "") + ".gif";
		$("#locationTitle").attr("src", locTitleSrc);
		$("#locationTitleContainer").show();
		
		var result = function(res)
		{
			$("#officeInfoBriefContainer").show();
			//$("#officeInfoCompleteContainer").hide();
			$("#landingPageContentContainer").hide();
			
			selectedOffices = res;
			//formatSelectedOffices();
			regionData_Load( movie.getCurrentRegionID() );
			hideShowLoading("hide");
		}
		
		var proxy = new locationsProxy();
		proxy.setCallbackHandler(result);
		
		hideShowLoading("show");
		proxy.getAllOfficesInRegion(selectedRegionID);
	}
	else if(zoomState == "out")
	{
		//$("#officeInfoCompleteContainer").hide();
		$("#landingPageContentContainer").show();
		$("#officeInfoBriefTableContainer").html("");
	}
	
	$("#officeInfoBriefContainer").hide();
}

function Flash_Trace(txt)
{
	alert(txt);
}

