var fdlrs = {
	dropMenuNav_Counties: null,
	dropMenuNav_Regions: null,
	dropMenuNav_Services: null,

	init: function() {
		this.dropMenuNav_Counties = $("menuCounties");
		this.dropMenuNav_Regions = $("menuRegions");
		this.dropMenuNav_Services = $("menuService");
		this.addEventHandlers();
	},

	addEventHandlers: function() {
		Event.observe(this.dropMenuNav_Counties, "change", function(evt) {
			this.goToURL(Event.element(evt).value);
		}.bind(this));

		Event.observe(this.dropMenuNav_Regions, "change", function(evt) {
			this.goToURL(Event.element(evt).value);
		}.bind(this));

		Event.observe(this.dropMenuNav_Services, "change", function(evt) {
			this.goToURL(Event.element(evt).value);
		}.bind(this));
	},

	goToURL: function(url) {
		window.location = url;
	},

	populateContactsMatrix: function(index, destinationID) {
		var dataRoot = "";
		var destID = $(destinationID);
		var table = new Element("table", {
			width: "100%",
			cellPadding: 0,
			cellSpacing: 0
		});

		Object.keys(contacts[index]).each(function(category) {
			dataRoot = contacts[index][category];
			var row = new Element("tr");
			var cell = new Element("td", {
				colSpan: 2,
				className: "contactsCategoryCell"
			}).update(dataRoot.friendlyName);
			row.insert({bottom: cell});
			table.insert({bottom: row});

			if (dataRoot.TBA === true) {
				var row = new Element("tr");
				var cell = new Element("td", {
						className: "contactsNameCell"
					}).update("&nbsp;");
				row.insert({bottom: cell});

				var cell = new Element("td", {
						className: "contactsEmailCell"
					}).update("TBA");
				row.insert({bottom: cell});

				table.insert({bottom: row});
			}
			else {
				dataRoot.contacts.each(function(contactRow) {
					var row = new Element("tr");
					var email = contactRow.email;

					var cell = new Element("td", {
						className: "contactsNameCell"
					}).update(contactRow.name);
					row.insert({bottom: cell});

					if (contactRow.note0.blank() === false) {
						var cellID = cell.identify();
						cell.innerHTML += " *";
						new Control.ToolTip(cell, contactRow.note0, {
								className: 'tooltip'
						});
					} else {}

					email = email.replace(/(_){3}/g, "@");
					email = email.replace(/(\|){3}/g, ".");

					var emailLink = new Element("a", {
							"href": "mailto:" + email + "?subject=FDLRS"
						}).update(email);
					var cell = new Element("td", {
						className: "contactsEmailCell"
					});
					cell.insert({bottom: emailLink});
					row.insert({bottom: cell});
					table.insert({bottom: row});
				});
			}

			row = new Element("tr");
			var cell = new Element("td", {
				colSpan: 2,
				className: "contactsRowSpacerCell"
			}).update("&nbsp;");
			row.insert({bottom: cell});
			table.insert({bottom: row});
		});

		destID.insert({bottom: table});
	}
};

document.observe("dom:loaded", function() {
	fdlrs.init();
});
