
try
{
	// create the new tool
	Tools.ToolBox.create("AddDefect");

	Tools.ToolBox.get("AddDefect").onadd = function(map, symbolId)
	{
		// when the map has loaded, update the information
		map.onloaded = function()
		{
			map.onloaded = null;
			showAddDefectDialog(map.key, symbolId, function()
			{
				map.removeSymbol(symbolId, true)
			});
		};
	};
	
	ctl00_Content_MappingControl1.oninformation = function(info)
	{
		if (info == null || info.length == 0)
			info = "To add a defect to the map, click on the defect button <img src=\"torbay.gis.web/gif/selection-toolbar.ashx\" width=\"16\" height=\"16\" /> then click on the map.";
		
		var element = $("mapInformation");
		element.innerHTML = info;
		return true; //disable default event handler
	}
}
catch (e)
{ }

function showDialog(width, height, name, callbackFunction)
{
	if (name == null || name.length == 0)
		var name = "dialog";
	
	var wnd = Windows.getWindow(name);
	
	if (wnd == null)
	{
		var wnd = new Window(name,
		{
			title: "Defect Details", 
			top: 0, left: 0,
			width: width,
			height: height,
			resizable: false,
			minimizable: false,
			maximizable: false,
			showEffect: Element.show,
			hideEffect: Element.hide
		});
		
		wnd.setContent("ctl00_Content_plcDefectDialog", true);
		wnd.onClose = callbackFunction;
	}
	
	wnd.showCenter(true);
}

function hideDefectDialog()
{
	ctl00_Content_plcDefectDialog.hide();
	Windows.close("defectDialog", null);
}

function createDefectFromDialog()
{
	var defectDropDown = $("ctl00_Content_selDefectDialogDefect");
	var defectCode = defectDropDown.options[defectDropDown.selectedIndex].value;
	
	if (defectCode == "")
	{
		alert("You must select the type of defect you are reporting.");
		return false;
	}
	
	var symbolId = $("ctl00_Content_plcDefectDialog").symbolId;
	var defectId = $("ctl00_Content_plcDefectDialog").defectId;
	var location = $("txtDefectDialogLocation").value;
	
	$("ctl00_Content_plcDefectDialog").symbolId = $("ctl00_Content_plcDefectDialog").defectId = null;
	
	if (symbolId != null)
		return StreetCare.Ajax.Highway.CreateDefect(ctl00_Content_MappingControl1.key, symbolId, defectCode, location).value;
	else
		return StreetCare.Ajax.Highway.UpdateDefect(defectId, defectCode, location).value;
}

function showAddDefectDialog(key, symbolId, callbackFunction)
{
	var symbol = StreetCare.Ajax.Highway.GetSymbol(key, symbolId).value;
	var streetName = StreetCare.Ajax.Highway.GetStreetNameForSymbolID(key, symbolId).value;
	
	if (streetName.length == 0)
		streetName = symbol.Centroid.X + ", " + symbol.Centroid.Y;
	
	$("ctl00_Content_selDefectDialogDefect").selectedIndex = 0;
	$("lblDefectDialogStreet").innerHTML = streetName;
	$("txtDefectDialogLocation").value = "";
	
	$("ctl00_Content_plcDefectDialog").symbolId = symbolId;
	
	ctl00_Content_plcDefectDialog.show();
	showDialog(400, 250, "defectDialog", function(){callbackFunction();RefreshDefects();});
}

function _updateDefectCancelButton(controlId, canCancel)
{
	if (canCancel) return;
	
	var button = $$("#" + controlId + " input");
	if (!button) return;
	
	button[0].style.display = "none";
}

function RefreshDisplayDefects()
{
	var defects = StreetCare.Ajax.Highway.GetDisplayModeDefects();
	
	ctl00_Content_repDisplayDefects.dataSource = eval(defects.value);
	ctl00_Content_repDisplayDefects.dataBind();
}

function CloseDisplayMap()
{
	ctl00_Content_plcDisplayMap.hide();
}

function ShowDisplayMap(easting, northing)
{
	ctl00_Content_DisplayMappingControl.centerMap(new Mapping.DoublePoint(easting, northing));
	ctl00_Content_plcDisplayMap.show();
	ctl00_Content_DisplayMappingControl.refresh();
}

function CancelDefect(defectNumber)
{
	if (confirm("Are you sure you wish to cancel this defect?  This will prevent us investigating any issue.\n\n(Defect: " + defectNumber + ")"))
	{
		StreetCare.Ajax.Highway.CancelDefect(defectNumber);
		RefreshDisplayDefects();
		alert("The defect has been cancelled.  It may take a few seconds to update - please refresh the page if it does not clear immediately.");
	}
}

function RefreshDefects()
{
	try
	{
		if (ctl00_Content_repDefects == null)
			return;
	}
	catch (e)
	{
		return;
	}
	
	var defects = StreetCare.Ajax.Highway.GetDisplayDefects();
	
	ctl00_Content_repDefects.dataSource = eval(defects.value);
	ctl00_Content_repDefects.dataBind();
	
	if (ctl00_Content_repDefects.dataSource == null || ctl00_Content_repDefects.dataSource.length == 0)
	{
		ctl00_Content_plcNoDefectDetails.show();
		ctl00_Content_plcDefectDetails.hide();
	}
	else
	{
		ctl00_Content_plcDefectDetails.show();
		ctl00_Content_plcNoDefectDetails.hide();
	}
}

removeDefectEffect = function(element)
{
	element = $(element);
	
	return new Effect.Parallel([
		new Effect.Scale(element, 50, {sync: true, scaleFromCenter: true, scaleContent: true, restoreAfterFinish: true}), 
		new Effect.Opacity(element, {sync: true, to: 0.0} )],
		Object.extend(
		{
			duration: 1.0, 
			
			beforeSetupInternal: function(effect)
			{
				Position.absolutize(effect.effects[0].element)
				effect.effects[0].element.focus();
			},
			
			afterFinishInternal: function(effect)
			{
				RefreshDefects();
			}
		}, arguments[1] || {})
	);
}

function EditDefect(defectId, displayId)
{
	var defect  = StreetCare.Ajax.Highway.GetDefect(defectId).value;
	
	if (defect.Street != null)
		var streetName = defect.Street.StreetDescription;
	else
		var streetName = defect.Defect.Coordinates[0].Easting + ", " + defect.Defect.Coordinates[0].Northing;

	if (defect.Defect.DefectCode.length > 0 && defect.Defect.ItemCode.length > 0 && defect.Defect.SubItemCode.length > 0)
		var defectCode = defect.Defect.ItemCode + "|" +  defect.Defect.SubItemCode + "|" + defect.Defect.DefectCode;

	var location = defect.Defect.Location;
	
	$("ctl00_Content_selDefectDialogDefect").value = defectCode;
	$("lblDefectDialogStreet").innerHTML = streetName;
	$("txtDefectDialogLocation").value = location;
	
	$("ctl00_Content_plcDefectDialog").defectId = defectId;
	
	ctl00_Content_plcDefectDialog.show();
	showDialog(400, 250, "defectDialog", RefreshDefects);
}

function RemoveDefect(defectId, displayId)
{
	if (!confirm("Are you sure you want to remove this defect?"))
		return;
	
	StreetCare.Ajax.Highway.RemoveDefect(defectId);
	ctl00_Content_MappingControl1.removeSymbol(defectId, true);
	
	try
	{
		removeDefectEffect(displayId);
	}
	catch(e)
	{
		RefreshDefects();
	}
}

function selectTab(tab)
{
	ctl00_Content_FormTabStrip.selectTabById(tab);
}

function HighlightListItem(item, selected)
{
	if (item.className.contains(" selected"))
		item.className = item.className.replace(" selected", "");
	
	item.className = item.className + (selected ? " selected" : "");
}

function FindAddressPage(street, town)
{
	var button = $("btnFindAddressPage");
	ctl00_Content_plcStreets.hide();
	ctl00_Content_plcNoStreets.hide();
	selectTab("Find");
	
	if (street == null || street.length < 2)
	{
		alert("You must enter a full or partial street name.\n(Minimum of two characters.)");
		$('txtStreetNamePage').focus();
		return;
	}
	
	Drawing.Window.setCursor("wait", button);
	button.disabled = true;
	
	var timer = new Core.Timer(100);
	timer.ontimer = function()
	{
		var result = StreetCare.Ajax.Highway.GetMatchingStreets(street, town);
		if (result.error != null)
		{
			alert(result.error.Message);
			return;
		}
		
		ctl00_Content_repStreets.dataSource = eval(result.value);
		ctl00_Content_repStreets.dataBind();
		
		if (ctl00_Content_repStreets.dataSource == null || ctl00_Content_repStreets.dataSource.length == 0)
			ctl00_Content_plcNoStreets.show();
		else
			ctl00_Content_plcStreets.show();
		
		Drawing.Window.setCursor("auto", button);
		button.disabled = false;
	};
	timer.start();
}

function FindAddressMap(street, town)
{
	ctl00_Content_plcStreetsPopup.hide();
	selectTab("Map");
	
	if (street == null || street.length < 2)
	{
		alert("You must enter a full or partial street name.\n(Minimum of two characters.)");
		$('txtStreetName').focus();
		return;
	}
	
	var result = StreetCare.Ajax.Highway.GetMatchingStreets(street, town);
	if (result.error != null)
	{
		alert(result.error.Message);
		return;
	}
	
	ctl00_Content_repStreetsPopup.dataSource = eval(result.value);
	
	if (ctl00_Content_repStreetsPopup.dataSource != null && ctl00_Content_repStreetsPopup.dataSource.length == 1)
	{
		var _street = ctl00_Content_repStreetsPopup.dataSource[0];
		ShowStreetOnMap(_street.StreetName, _street.TownName, _street.Easting, _street.Northing);
	}
	else
	{
		var _size = ctl00_Content_repStreetsPopup.dataSource.length;
		if (_size > 8) _size = 8;
		
		ctl00_Content_repStreetsPopup.control.style.height = Core.Utility.toSize(_size * 18);
		ctl00_Content_repStreetsPopup.dataBind();
		ctl00_Content_plcStreetsPopup.show();
	}
}

function ShowStreetOnMap(street, town, easting, northing)
{
	$('txtStreetName').value = street;
	$('txtTown').value = town;
	ctl00_Content_plcStreetsPopup.hide();
	
	selectTab("Map");
	
	ctl00_Content_MappingControl1.zoomUnit(150, Mapping.DistanceUnit.Meter);
	ctl00_Content_MappingControl1.centerMap(new Mapping.DoublePoint(easting, northing), true);
}

try
{
	RequiredDefectValidator = Web.UI.Validator.extend(
	{
		constructor: function()
		{
			this.base();
		},
		
		validate: function()
		{
			var defects = eval(StreetCare.Ajax.Highway.GetDisplayDefects().value);
			
			if (defects.length == 0)
				return this.validationError();
			
			return true;
		},
		
		validationError: function()
		{
			return this.base("You must report at least one defect.");
		}
	});

	new RequiredDefectValidator();
}
catch (e)
{ }

