function locationMenu (selection) {
	this.selected = -1;
	this.total = 0;
	this.locations = null;
	
	this.initLocationsArray(selection);
	this.addFocusListener();
	this.addKeyListener();
	this.addMoveListener();
}

locationMenu.prototype.initLocationsArray = function(selection) {
	var c = 0;
	this.locations = new Array();
	for (var i = 0; i < selection.length; i++) {
		if (((i + 1) % 3) == 0) {
			for (var j = 0; j < selection[i].length; j++) {
				if (((j + 2) % 3) == 0) {
					this.locations[c] = new Object();
					this.locations[c]['city_ID'] = selection[i][j - 1];
					this.locations[c]['city_name'] = selection[i][j];
					this.locations[c]['country_ID'] = selection[i - 2];
					this.locations[c]['country_name'] = selection[i - 1];
					c++;
				}
			}
		}
	}
	this.locations.sort(this.sortByCityName);
}

locationMenu.prototype.addFocusListener = function() {
	var menu = this;
	$("#location").focus(function () {
		menu.setLocationEnabled();
	});
}
	
locationMenu.prototype.addMoveListener = function() {
	var menu = this;
	$("#location").keydown(function(event) {
		var key = event.charCode ? event.charCode : event.keyCode ? event.keyCode : 0;
		if (($("#location").attr("value")) && (menu.total)) {
			switch(key) {
				case 38: 
				case 37:
					menu.moveUp(); 
					break;
				case 40:
				case 39:
					 menu.moveDown(); 
					 break; 
				
				case 13:
					var value = $("#location").attr("value");
					value = value.replace(/^\s+|\s+$/g, '');
					value = value.replace(/  /g, '');
					if ((menu.selected > -1) || (value.length < 2)) {
						event.preventDefault();
						if (menu.selected > -1) {
							menu.selectOption($("#selectedMatch").text().split(", "));
						}
					}
				break;
				case 27: menu.clearLocation(); break;
				default: break;
			}
		}
	});
}

locationMenu.prototype.addKeyListener = function() {
	var menu = this;
	$("#location").keyup(function () {
		var value = $("#location").attr("value");
		if (value && value.length > 2) {
			if( $('#matches').length ) {
			} else {
				$("body").append('<div id="matches"></div>');				
			}
			$("#matches").empty();
			menu.printOptions(value);
			if (menu.total > 0) {
				//$("#matches").css("top", $("#location").css("top"));
			//	var myOffset =  parseInt($("#location").offset().top );
			//	$("#matches").css("top", (myOffset - 115));
				$("#matches").css("position","absolute");
				var locTop = $("#location").offset().top;
				var locHeight = $("#location").height( );
				var topPos = parseInt(locTop ) + parseInt(locHeight) + 4 ;
				var leftPos  = parseInt ($("#location").offset().left)  ;
				if(jQuery.browser.msie == true && jQuery.browser.version.substring(0,1)== '6') {
					$("#matches").css("left", leftPos + parseInt($("#location").css("width")));
					$("#matches").css("top",  (locTop )  );
				} else {
					$("#matches").css("left", leftPos);
					$("#matches").css("top",  topPos  );
				}
				//$("#matches").css("left", 150 );
				$("#matches").slideDown("normal");
			} else {
				$("#matches").fadeOut("fast");
			}
		} else {
			$("#matches").fadeOut("normal");
		}
	});
}

locationMenu.prototype.selectOption = function(data) {
	this.setLocationDisabled();
	selectedcountry = data[2];
	selectedcity = data[3];
	city_name = data[0];
	populateCountries();
	$(".pulsub1").trigger("focus");
	this.clearLocation();
}

locationMenu.prototype.clearLocation = function(selection) {
	$("#location").attr("value", "");
	this.selected = -1;
	$("#matches").fadeOut("normal");
}

locationMenu.prototype.setLocationEnabled = function() {
	$("#location").css({backgroundColor:"", color:"", border:""});
	$("#country").css({backgroundColor:"#DDD", color:"#888", border:"1px solid #ABADB3" });
	$("#city").css({backgroundColor:"#DDD", color:"#888", border:"1px solid #ABADB3" });
}

locationMenu.prototype.setLocationDisabled = function() {
	$("#location").css({backgroundColor:"#DDD", color:"#888", border:"1px solid #ABADB3"});
	$("#country").css({backgroundColor:"", color:"", border:"" });
	$("#city").css({backgroundColor:"", color:"", border:"" });
}

locationMenu.prototype.moveUp = function() {
	if (this.selected > 0) {
		this.selected--;
	} else {
		this.selected = this.total - 1;
	}
}

locationMenu.prototype.moveDown = function() {
	if ((this.total) && (this.selected < (this.total - 1))) {
		this.selected++;
	} else {
		this.selected = 0;
	}
}

locationMenu.prototype.printOptions = function(value) {
	var pattern = RegExp("^" + value.toLowerCase(), "i");
	this.total = 0;
	
	for (var i = 0; i < this.locations.length; i++) {
		if (pattern.test(this.locations[i]['city_name'].toLowerCase())) {
			if (this.total == this.selected) {
				$("#matches").append("<div style=\"padding:2px; background-color:#CEE9F4;\" class=\"matchesOption\" id=\"selectedMatch\"><div>");
			} else {
				$("#matches").append("<div style=\"padding:2px;\" class=\"matchesOption\"><div>");
			}
			$("#matches div:last").append(this.locations[i]['city_name'] + ", <i>" + this.locations[i]['country_name'] + "</i>"
				+ "<div class=\"country_ID\" style=\"display:none;\">, " + this.locations[i]['country_ID'] + "</div>"
				+ "<div class=\"city_ID\" style=\"display:none;\">, " + this.locations[i]['city_ID'] + "</div>");
			this.total++;
		}
	}
	
	$(".matchesOption").hover(
		function () {
			$(".selectedMatch").css({backgroundColor:""});
			$(this).css({backgroundColor:"#CEE9F4"});
		},
		function () {
			$(this).css({backgroundColor:""});
		}
	);
	
	var menu = this;
	$(".matchesOption").click(function () {
		menu.selectOption($(this).text().split(", "));
	});
}

locationMenu.prototype.sortByCityName = function(a, b) {
	var x = a['city_name'].toLowerCase();
    var y = b['city_name'].toLowerCase();
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}