﻿var map = null;
var mapManager = new MapManager();
var start, end;

function MapManager() {
	this.MWS = null;
	this.shapeLayer = null;
	this.results = null;
	this.resultsLength = null;
	this.driveIndex = null;

	this.LoadMap = function() {
		map = new VEMap('LJSMap');
		map.SetClientToken(clienttoken);
		map.LoadMap();
		this.MWS = LJSLocator.services.MWS;
		this.shapeLayer = new VEShapeLayer();
		//this.shapeLayer.SetClusteringConfiguration(VEClusteringType.Grid);
		map.AddShapeLayer(this.shapeLayer);
		document.getElementById('MSVE_obliqueNotification').style.visibility = "hidden";
		document.getElementById('MSVE_navAction_modeCell').style.visiblity = "hidden";
		document.getElementById('MSVE_navAction_modeCell').style.display = "none";
		document.getElementById('MSVE_navAction_separator0').style.display = "none";

		if (getQueryVariable("address")) {
			document.getElementById('txtSearch').value = getQueryVariable("address");
			this.Find();
		}
	}

	this.Find = function() {
		map.DeleteAllShapes();
		map.DeleteRoute();
		document.getElementById('results').innerHTML = "";
		document.getElementById('drivingDirections').style.display = "none";
		document.getElementById('itinerary').style.display = "none";
		//VEMap.Find(what, where, findType, shapeLayer, startIndex, numberOfResults, showResults, createResults, useDefaultDisambiguation, setBestMapView, callback);
		map.Find(null, document.getElementById('txtSearch').value + ", USA", null, null, null, null, null, null, null, null, this.FindCallback);
	}

	this.FindCallback = function(veShapeLayer, veFindResult, vePlace) {
		if (vePlace != null) {
			document.getElementById('txtSearch').value = vePlace[0].Name;
			mapManager.FindNearby(vePlace[0].LatLong);
		}
	}
	this.FindNearby = function(veLatLong) {
		var filter = new Filter("LJS", true);
		var filterList = new Array();
		filterList.push(filter);

		this.MWS.FindNearby(veLatLong.Latitude, veLatLong.Longitude, 200, filterList, this.FindNearbyCallback);
	}
	this.FindNearbyCallback = function(results) {
		mapManager.results = results;
		mapManager.resultsLength = results.length;
		start = 0;
		end = 0;

		if (end + 10 <= mapManager.resultsLength) {
			end = end + 10;
			document.getElementById("imgPrevious").style.display = "block";
			document.getElementById("imgNext").style.display = "block";
		}
		else {
			end = mapManager.resultsLength;
		}

		mapManager.AddPushpinsToMap();
		mapManager.BuildResultsTable(start, end);
		document.getElementById('results').style.display = "block";
	}

	this.AddPushpinsToMap = function() {
		map.DeleteAllShapes();
		var latlongArray = new Array();
		for (var i = start; i < end; i++) {
			var center = new VELatLong(this.results[i].Latitude, this.results[i].Longitude);
			latlongArray.push(center);
			var shape = new VEShape(VEShapeType.Pushpin, center);
			shape.SetTitle(this.results[i].Address);

			var businessPhone = "";
			for (var k = 0; k < this.results[i].cssProperties.length; k++) {
				if (this.results[i].cssProperties[k].Name == "BusinessPhone") {
					businessPhone = this.results[i].cssProperties[k].Value;
				}
			}

			shape.SetDescription("<table><tr><td><img src='images/ljsMapIcon.png'/></td><td>" + this.results[i].City + ", " + this.results[i].StateProvince + ", " + this.results[i].PostalCode + " <br/>" + businessPhone + " </td></tr></table>");
			shape.SetCustomIcon("<div><img src='images/ljsMapIcon.png'/><div class='pushpinNumbers'>" + (i + 1) + "</div></div>");
			mapManager.shapeLayer.AddShape(shape);
		}
		map.SetMapView(latlongArray);
	}
	this.BuildResultsTable = function() {
		var oTbl = document.createElement("table");
		var oTbody = document.createElement("tbody");
		oTbl.style.width = "100%";
		oTbl.style.cellSpacing = "0";
		oTbl.style.borderSpacing = "0px";
		oTbl.style.borderCollapse = "collapse";

		var oTRHead = document.createElement("tr");
		for (j = 0; j < 4; j++) {
			var oTDHead = document.createElement("td");
			switch (j) {
				case 0:
					oTDHead.innerHTML = "&nbsp;";
					break;
				case 1:
					oTDHead.innerHTML = "<h3>Address</h3>";
					break;
				case 2:
					oTDHead.innerHTML = "<h3>Distance</h3>";
					break;
				case 3:
					oTDHead.innerHTML = "<h3>Store Details</h3>";
					break;
			}
			oTRHead.appendChild(oTDHead);
		}
		oTbody.appendChild(oTRHead);

		for (i = start; i < end; i++) {
			var oTR = document.createElement("tr");

			var businessPhone = "";
			var multiBrand = "";
			var multiBrandImg = "";
			var multibrandHtml = "";

			for (var k = 0; k < this.results[i].cssProperties.length; k++) {
				if (this.results[i].cssProperties[k].Name == "BusinessPhone") {
					businessPhone = this.results[i].cssProperties[k].Value;
				}
				else if (this.results[i].cssProperties[k].Name == "MultibrandConfiguration") {
					multiBrand = this.results[i].cssProperties[k].Value;
				}
			}

			switch (multiBrand) {
				case "KL":
					multiBrandImg = "images/s_KFCIcon.png";
					break;
				case "LA":
					multiBrandImg = "images/s_AwIcon.png";
					break;
				case "TL":
					multiBrandImg = "images/s_TBIcon.png";
					break;
			}

			if (multiBrand != "") {
				multibrandHtml = "<img src='" + multiBrandImg + "'></img>";
			}

			for (j = 0; j < 4; j++) {
				var oTD = document.createElement("td");

				switch (j) {
					case 0:
						oTD.innerHTML = "<div class='resultNumbers'>" + (i + 1) + "</div>";
						break;
					case 1:
						oTD.innerHTML = this.results[i].Address + "<br/>" + this.results[i].City + ", " + this.results[i].StateProvince + " " + this.results[i].PostalCode + "<br/>" + businessPhone;
						break;
					case 2:
						oTD.innerHTML = (Math.round((this.results[i].Distance * 100)) / 100) + " miles <br/>" + "<a class='ddLink' onclick='mapManager.DrivingDirections(" + i + ");'>Driving Directions</a>";
						break;
					case 3:
						oTD.innerHTML = "Multibrand: " + ((multiBrand != "") ? multibrandHtml : "N");
						break;
				}
				if (i < this.results.length - 1) {
					oTD.className = 'borderBottom';
				}
				oTR.appendChild(oTD);
			}
			oTbody.appendChild(oTR);
		}
		document.getElementById('results').innerHTML = "";

		var header = "<b>Search results from:</b><br/>" + document.getElementById('txtSearch').value + "<br/><br/>";
		document.getElementById('results').innerHTML = header;
		oTbl.appendChild(oTbody);
		document.getElementById('results').appendChild(oTbl);
	}

	this.Next = function() {
		if (end != this.resultsLength) {
			start = start + (end - start);
			if (end + 10 <= this.resultsLength) {
				end = end + 10;
			}
			else {
				end = this.resultsLength;
			}
			mapManager.BuildResultsTable();
			mapManager.AddPushpinsToMap();
		}
	}

	this.Previous = function() {
		if (start != 0) {
			if (end - start < 10) {
				end = start;
				start = end - 10;
			}
			else {
				start = start - 10;
				end = end - 10;
			}
			mapManager.BuildResultsTable();
			mapManager.AddPushpinsToMap();
		}
	}

	this.DrivingDirections = function(index) {
		document.getElementById("imgPrevious").style.display = "none";
		document.getElementById("imgNext").style.display = "none";

		for (var i = 0; i < map.GetShapeLayerCount(); i++) {
			for (var j = 0; j < map.GetShapeLayerByIndex(i).GetShapeCount(); j++) {
				if (map.GetShapeLayerByIndex(i).GetShapeByIndex(j).GetCustomIcon().indexOf("results_" + (index + 1) + ".gif") < 0) {
					map.GetShapeLayerByIndex(i).GetShapeByIndex(j).Hide();
				}
			}
		}
		this.driveIndex = index;
		var businessPhone = "";
		for (var k = 0; k < this.results[index].cssProperties.length; k++) {
			if (this.results[index].cssProperties[k].Name == "BusinessPhone") {
				businessPhone = this.results[index].cssProperties[k].Value;
			}
		}
		var destination = "";
		destination += "Long John Silver's Restaurant <br/> " + this.results[index].Address + "<br/>" + this.results[index].City + ", " + this.results[index].StateProvince + " " + this.results[index].PostalCode + "<br/>" + businessPhone;
		document.getElementById('destination').innerHTML = destination;

		document.getElementById('drivingDirections').style.display = "block";
		document.getElementById('results').style.display = "none";
	}

	this.FindDirections = function() {
		map.Find(null, document.getElementById('origin').value + ", USA", null, null, null, null, null, null, null, null, this.FindDirectionsCallback);
	}

	this.FindDirectionsCallback = function(veShapeLayer, veFindResult, vePlace) {
		if (vePlace != null) {
			document.getElementById('origin').value = vePlace[0].Name;
			mapManager.DriveIt(vePlace[0].LatLong);
		}
	}

	this.DriveIt = function(origin) {
		var destination = new VELatLong(this.results[this.driveIndex].Latitude, this.results[this.driveIndex].Longitude);
		var options = new VERouteOptions();
		options.RouteCallback = this.gotRouteCallback;
		map.DeleteAllShapes();
		map.GetDirections([origin, destination], options);
	}
	this.gotRouteCallback = function(veRoute) {
		var details = "<b>Total Distance: </b>" + (Math.round(veRoute.Distance * 100) / 100) + " miles<br/><b>Estimated Time: </b>" + Math.round(veRoute.Time / 60) + " minutes <br/><br/>";
		var disclaimer = "<br/>These maps/directions are informational only. No representation is made or warranty given as to their content, road conditions or route usability or expeditiousness. User assumes all risk of use. Long John Silver's and their suppliers assume no responsibility for any loss or delay resulting from such use.<br/>";
		var footer = "<center><br/><img style='cursor: pointer;' src='images/printDirections.png' onclick='window.print();'/></center>";
		var oTbl = document.createElement("Table");
		oTbl.style.width = "100%";
		oTbl.style.cellSpacing = "0";
		oTbl.style.borderSpacing = "0px";
		oTbl.style.borderCollapse = "collapse";


		var oTR = oTbl.insertRow(0);
		var oTD = oTR.insertCell(0);
		oTD.innerHTML = "<h3>Directions</h3>";
		oTD.className = 'borderBottom';

		oTD = oTR.insertCell(1);
		oTD.innerHTML = "<h3>Distance</h3>";
		oTD.className = 'borderBottom';

		for (var i = 0; i < veRoute.RouteLegs[0].Itinerary.Items.length; i++) {
			oTR = oTbl.insertRow(i + 1);
			oTD = oTR.insertCell(0);
			oTD.innerHTML = "<b>" + (i + 1) + " </b>" + veRoute.RouteLegs[0].Itinerary.Items[i].Text;
			oTD.className = 'borderBottom';

			oTD = oTR.insertCell(1);
			oTD.innerHTML = (Math.round(veRoute.RouteLegs[0].Itinerary.Items[i].Distance * 100) / 100) + " miles";
			oTD.className = 'borderBottom';
		}

		document.getElementById('itinerary').innerHTML = details;
		document.getElementById('itinerary').appendChild(oTbl)
		document.getElementById('itinerary').innerHTML = document.getElementById('itinerary').innerHTML + disclaimer + footer;

		document.getElementById('itinerary').style.display = "block";
	}

	/*
	this.ChooseNew = function()
	{
	document.getElementById('drivingDirections').style.display = "block";
	document.getElementById('itinerary').style.display = "none";
	document.getElementById('origin').value = "";
	map.DeleteRoute();
	}*/
}

function checkEnter(e, type) {
	var characterCode;

	if (window.event) {
		characterCode = e.keyCode;

	}
	else if (e.which) {
		characterCode = e.which;
	}

	if (characterCode == 13) {
		if (type == "globalSearch") {
			window.location = 'http://preview.mappoint.net/LJSLocator/Default.aspx?address=' + document.getElementById('quickSearch').value;
		}
		else if (type == "search") {
			mapManager.Find();
		}
		else if (type == "from") {
			mapManager.FindDirections();
		}

		return false;
	}
	else {
		return true;
	}
}

function Filter(name, value) {
	this.Name = name;
	this.FilterValue = value;
}

/**
* Parses out querystring using JavaScript
*/
function getQueryVariable(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i = 0; i < vars.length; i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return unescape(pair[1]);
		}
	}
}