// global vars
gApp = null;

// application class
var NovLocations = Class.create({
	
	initialize: function() {
		this.initGMap();
		this.initCountries();
		this.initFlashMap();
		this.ccd = new CountryDropdown($("country-list"));
		this.initFilters();
	},
	
	initFlashMap: function() {
		// remove current map if present
		var fmap = $('worldmap');
		if(fmap) {
			fmap.remove();
		}
		// create container
		var div = new Element('div', {'id':'worldmap-container'}).setStyle('display: table-cell; vertical-align: middle;');
		var lImg = new Element('img', {src:'img/loading.gif', width:32, height:32, alt:'Loading ...'}).setStyle('display: block; margin: auto;');
		if(Prototype.Browser.IE) {
			lImg.setStyle('margin-top: 108px;');
		}
		div.appendChild(lImg);
		//div.appendChild(cet('p', 'Adobe Flash required'));
		$('google-map-container').insert({before: div});		
		// run rest with a slight delay (IE fix)
		var delay = 1;
		if(Prototype.Browser.IE) {
			delay = 500;
		}
		window.setTimeout(function() {
			// create flash object
			var flashvars = {};
			flashvars.jsActive = arrayToString(this.getCC3List()).toLowerCase();
			var params = {};
			params.menu = "false";
			params.quality = "best";
			params.scale = "noscale";
			params.wmode = "transparent";
			params.bgcolor = "#ffffff";
			params.allowscriptaccess = "sameDomain";
			var attributes = {};
			attributes.id = "worldmap";
			// insert flash
			swfobject.embedSWF("flash/worldmap.swf?" + new Date().getTime(), "worldmap-container", "597", "248", "8.0.0", "flash/expressInstall.swf", flashvars, params, attributes);
		}.bind(this), delay);
	},
	
	initGMap: function() {
		this.gmap = new GMap2($("google-map-container"), {size: new GSize(597, 248)});
		this.gmap.setCenter(new GLatLng(41, -98), 1);
		this.gmap.enableScrollWheelZoom(); // enable scroll wheel zooming
		this.gmap.enableContinuousZoom();
		this.gmap.addControl(new GSmallMapControl());
		this.gmap.addControl(new GMapTypeControl());
		this.gmap.enableDoubleClickZoom();
		this.gdir = new GDirections(this.gmap, $('google-dir-container'));
		this.initStatusMsgs();
		GEvent.addListener(this.gdir, 'error', function() {
			alert('Could not fetch directions: ' + this.sms[this.gdir.getStatus().code]);
		}.bind(this)); 
		this.gmm = new MarkerManager(this.gmap);
		$(this.gmap.getContainer()).observe("DOMMouseScroll", function(e) {
			e.stop();
		});
		$(this.gmap.getContainer()).observe("mousewheel", function(e) {
			e.stop();
		});
	},
	
	initStatusMsgs: function() {
		this.sms = [];
		this.sms[G_GEO_SUCCESS]            = "Success";
		this.sms[G_GEO_BAD_REQUEST]		   = "Invalid request";
		this.sms[G_GEO_MISSING_ADDRESS]    = "Missing Address";
		this.sms[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address";
		this.sms[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address";
		this.sms[G_GEO_UNKNOWN_DIRECTIONS] = "No route";
		this.sms[G_GEO_BAD_KEY]            = "Bad Key";
		this.sms[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries";
		this.sms[G_GEO_SERVER_ERROR]       = "Server error";
		this.sms[403]                      = "Error 403";
	},
	
	initFilters: function() {
		var names = ['all', 'pharma','vnd','sandoz','ch','cv','ah','group'];
		this.filters = {'all':'bu-filter-all','pharma':'bu-filter-pharma','vnd':'bu-filter-vnd','sandoz':'bu-filter-sandoz','ch':'bu-filter-ch','cv':'bu-filter-cv','ah':'bu-filter-ah','group':'bu-filter-group'};
		this.filterValues = {'pharma':['PH', 'PR', 'BL'],'vnd':['CR', 'VD'],'sandoz':['GX', 'GN'],'ch':['CH', 'OT'],'cv':['CV'],'ah':['AH'],'group':['GR', 'CO']};
		names.each(function(n) {
			var ele = $(this.filters[n]);
			if(ele) {
				ele.observe('mouseover', function() {
					ele.setStyle('color: #FFFFFF');
				}.bind(this));
				ele.observe('mouseout', function() {
					ele.setStyle('color: #f6e4d0');
				}.bind(this));
				ele.observe('click', function() {
					this.toggleFilter(n);
				}.bind(this));
				
				if(n == 'all') {
					ele.addClassName('bu-filter-value-selected');
				}
				else {
					ele.addClassName('bu-filter-value-not-selected');
				}
				
			}
		}.bind(this));
		// default applied filters
		this.filter = [];
	},
	
	toggleFilter: function(fname) {
		if(!this.filter) {
			this.filter = [];
		}
		// all filter clicked while other filter(s) active
		if(fname == 'all') {
			if(this.filter.length > 0) {
				// deselect all (except all of course ;)
				$H(this.filters).values().each(function(ele) {
					if(ele != 'bu-filter-all') {
						$(ele).removeClassName("bu-filter-value-selected");
						$(ele).addClassName("bu-filter-value-not-selected");
					}
				}.bind(this));
				// remove all filters
				this.filter = [];
			}
		}
		else {
			var vals = this.filterValues[fname];
			var removed = false;
			if(vals) {
				vals.each(function(v) {
					if(removed || this.filter.indexOf(v) > -1) {
						removed = true;
						this.filter = this.filter.without(v);
					}
					else {
						this.filter.push(v);
					}
				}.bind(this));
				var ele = this.filters[fname];
				if(ele) {
					if(removed) {
						$(ele).removeClassName("bu-filter-value-selected");
						$(ele).addClassName("bu-filter-value-not-selected");
					}
					else {
						$(ele).removeClassName("bu-filter-value-not-selected");
						$(ele).addClassName("bu-filter-value-selected");
					}
				}
			}
		}
		// (de)select all depending on current filters
		if(this.filter.length > 0) {
			$('bu-filter-all').removeClassName("bu-filter-value-selected");
			$('bu-filter-all').addClassName("bu-filter-value-not-selected");
		}
		else {
			$('bu-filter-all').removeClassName("bu-filter-value-not-selected");
			$('bu-filter-all').addClassName("bu-filter-value-selected");			
		}
		// update country cache or flashmap
		this.initCountries();
		if(this.country) {
			this.selectCountry(this.country.code);
		}
		else {
			this.initFlashMap();
		}
	},
	
	initCountries: function() {
		this.countries = [];
		gLocations.each(function(val) {
			var cc2 = val.cc.toUpperCase();
			if(this.filter && this.filter.length > 0) {
				if(val.bu != "*" && this.filter.indexOf(val.bu) == -1) {
					return;
				}
			}
			if(this.countries.indexOf(cc2) == -1) {
				this.countries.push(cc2);
			}
		}.bind(this));
		this.updateWorldmapState();
	},
	
	getCC3List: function() {
		var result = [];
		if(this.countries) {
			gCountries.each(function(c) {
				if(this.countries.indexOf(c.code2) > -1) {
					result.push(c.code);
				}
			}.bind(this));
		}
		return result;
	},
	
	displayWorldmap: function() {
		this.hideGMap();
		var fmap = $('worldmap');
		if(fmap) {
			fmap.show();
		}
		else {
			this.initFlashMap();
		}
		$('location-title').innerHTML = '&nbsp;';
	},
	
	hideWorldmap: function() {
		var fmap = $('worldmap');
		if(fmap) {
			if(Prototype.Browser.Gecko) {
				fmap.hide();
			}
			else {
				fmap.remove();	
			}
		}
		$('back-to-world').show();
	},
	
	displayGMap: function() {
		this.hideWorldmap();
		$('google-dir-container').hide();
		$('google-map-container').show();
	},
	
	hideGMap: function() {
		this.gmap.closeInfoWindow();
		$('locations-container').hide();
		$('google-dir-container').hide();
		$('google-map-container').hide();
		$('back-to-world').hide();
		this.country = null;
	},
	
	updateWorldmapState: function() {
		if(this.countries) {
			//$('worldmap').flashSetState(false, null, true);
			//$('worldmap').flashSetState(true, this.countries, false);
		}
	},
	
	selectCountry: function(cc) {
		if(Prototype.Browser.IE) {
			this.hideWorldmap();
		}
		this.gmm.clearMarkers();
		this.gmap.clearOverlays();
		this.gmap.setCenter(new GLatLng(41, -98), 1);
		this.country = getCountry(cc);
		if(this.country) {
			this.locations = [];
			var cc2 = this.country.code2;
			if(cc2) {
				gLocations.each(function(val) {
					if(val.cc.toUpperCase() == cc2) {
						if(this.filter && this.filter.length > 0) {
							if(val.bu == "*" || this.filter.indexOf(val.bu) > -1) {
								this.locations.push(val);
							}
						}
						else {
							this.locations.push(val);
						}
					}
				}.bind(this));
				this.locations.sort(function(a, b) {
					if(a.city == b.city) {
						return 0;
					}
					var test = new Array(a.city, b.city);
					test.sort();
					if(test[0] == a.city) {
						return -1;
					}
					else {
						return 1;
					}
				}.bind(this));
			}
			$('location-title').innerHTML = 'Locations > ' + this.country.name;
		}
		this.displayLocations();
		this.placeSiteMarkers();
		this.displayGMap();
	},
	
	placeSiteMarkers: function() {
		if(this.gmm && this.locations) {
			//var SW = new GLatLng(-84.9999,-179.9999);
        	//var NE = new GLatLng(84.9999,179.9999); 
			var bounds = new GLatLngBounds();
			var markers = [];
			var cache = [];
			this.locations.each(function(loc) {
				var ll = this.getLatLng(loc);
				if(ll) {
					if(cache.indexOf(loc.latlng) == -1) {
						bounds.extend(ll);
						var m = new GMarker(ll);
						m.bindInfoWindow(this.createSiteInfoWindow(loc.latlng));
						markers.push(m);
						cache.push(loc.latlng);
					}
				}
			}.bind(this));
			if(markers.length > 0) {
					this.gmm.addMarkers(markers, 0, 17); // 11 - 17
					this.gmm.refresh();
					//this.gmap.setCenter(bounds.getCenter(), this.gmap.getBoundsZoomLevel(bounds));
			}
			// zoom to country
			var zoom = this.country.zoom ? this.country.zoom : 3;
			var point = null;
			var data = this.country.latlng.split(',');
			if(data.length == 3) {
				point = new GLatLng(data[1], data[0]);
			}
			if(Prototype.Browser.IE) {
				window.setTimeout(function() {		
					if(point) {
						this.gmap.setCenter(point, zoom);
					}
				}.bind(this), 500);
			}
			else {
				if(point) {
					this.gmap.setCenter(point, zoom);
				}
			}
		}
	},
	
	getLatLng: function(loc) {
		if(loc.latlng && loc.latlng.length > 0) {
			var data = loc.latlng.split(',');
			if(data.length == 3) {
				return new GLatLng(data[1], data[0]);
			}
		}
	},
	
	displayLocations: function() {
		if(this.locations) {
			$('locations-container').innerHTML = '';
			
			// main content holder
			var content = ce('div');
			$(content).setAttribute('id', 'location-list');
			
			// helper vars
			var bu_names = {'CR':'Vaccines & Diagnostics','VD':'Vaccines & Diagnostics','PH':'Pharma','PR':'Pharma','BL':'Pharma','CV':'Ciba Vision','CH':'OTC','OT':'OTC','GN':'Sandoz','GX':'Sandoz','AH':'Animal Health','GR':'Group','CO':'Group'};
			var locCount = 0;
			
			// get cities
			var cities = [];
			this.locations.each(function(loc) {
				if(cities.indexOf(loc.city) == -1) {
					cities.push(loc.city);
				}
			}.bind(this));
			
			cities.each(function(city, idx) {
				// create containers
				var div = ce('div');
				var content_div = ce('div');
				$(content_div).hide();
				// create header
				var city_div = cet("div", city);
				$(city_div).addClassName("location-city-header");
				$(city_div).addClassName("location-city-header-closed");
				$(city_div).addClassName((idx % 2 == 0 ? "location-city-header-even" : "location-city-header-odd"));
				$(city_div).observe('click', function() {
					$(city_div).toggleClassName('location-city-header-closed');
					$(city_div).toggleClassName('location-city-header-expanded');
					$(content_div).toggle();
				}.bind(this));
				$(city_div).observe('mouseover', function() {
					$(city_div).setStyle('color: #333333; text-decoration: none;');
				}.bind(this));
				$(city_div).observe('mouseout', function() {
					$(city_div).setStyle('color: #CC3300; text-decoration: underline;');
				}.bind(this));
				// add to container
				div.appendChild(city_div);
				// create content table
				var table = ce('table');
				var tbody = ce('tbody');
				// add location rows
				this.locations.each(function(loc) {
					if(loc.city != city) {
						return;
					}
					// BU
					row = ce("tr");
					row.appendChild(cet("td", "Business Unit:"));
					row.appendChild(ce("td", cet("strong", (bu_names[loc.bu] ? bu_names[loc.bu] : loc.bu))));
					row.appendChild(ce("td"));
					tbody.appendChild(row);
					// Address
					if(loc.addr) {
						var addr = loc.name.split("\n");
						var mail_addr = loc.addr.split("\n");
						mail_addr.each(function(a) {
							addr.push(a);
						});
						var title = false;
						addr.each(function(a) {
							row = ce("tr");
							if(!title) {
								row.appendChild(cet("td", "Address:"));
								title = true;
							}
							else {
								row.appendChild(ce("td"));
							}
							row.appendChild(cet("td", a));
							row.appendChild(ce("td"));
							tbody.appendChild(row);
						});
					}
					// Phone
					if(loc.phone) {
						row = ce("tr");
						row.appendChild(cet("td", "Phone:"));
						row.appendChild(cet("td", loc.phone));
						row.appendChild(ce("td"));
						tbody.appendChild(row);
					}
					// Fax
					if(loc.fax) {
						row = ce("tr");
						row.appendChild(cet("td", "Fax:"));
						row.appendChild(cet("td", loc.fax));
						row.appendChild(ce("td"));
						tbody.appendChild(row);
					}
					// Site URL
					if(loc.url && loc.url != '-') {
						row = ce('tr');
						row.appendChild(cet('td', 'Homepage:'));
						var href = cet('a', loc.url);
						href.setAttribute('href', loc.url, 0);
						href.setAttribute('target', '_blank');
						var td = ce('td');
						td.appendChild(href);
						row.appendChild(td);
						tbody.appendChild(row);
					}
					// Site Address
					if(loc.site) {
						var addr = loc.name.split("\n");
						var site_addr = loc.site.split("\n");
						site_addr.each(function(a) {
							addr.push(a);
						});
						var title = false;
						addr.each(function(a) {
							row = ce("tr");
							if(!title) {
								row.appendChild(cet("td", "Site Address:"));
								title = true;
							}
							else {
								row.appendChild(ce("td"));
							}
							row.appendChild(cet("td", a));
							row.appendChild(ce("td"));
							tbody.appendChild(row);
						});
					}
					// Show on map link
					var ll = this.getLatLng(loc);
					if(ll) {
						row = ce("tr");
						row.appendChild(ce("td"));
						var href = cet("a", "Show on map >");
						href.setAttribute('href', 'javascript:;', 0);
						$(href).observe('click', function() {
							this.gmap.setCenter(ll);
							this.gmap.setZoom(15);
							window.scrollTo(0, 0);
						}.bind(this));
						var td = ce("td");
						td.appendChild(href);
						row.appendChild(td);
						row.appendChild(ce("td"));
						tbody.appendChild(row);
					}
					// get directions link
					if(ll) {
						var dv = ce("div"); // directions form
						$(dv).addClassName('directions-form');
						row = ce("tr");
						row.appendChild(ce("td"));
						var href= cet("a", "Get directions >");
						href.setAttribute("href", "javascript:;", 0);
						$(href).observe("click", function() {
							$(dv).toggle();
						});
						var td = ce("td");
						td.appendChild(href);
						row.appendChild(td);
						
						// close row
						row.appendChild(ce("td"));
						tbody.appendChild(row);
						
						// create new row
						row = ce("tr");
						row.appendChild(ce("td"));
						
						var span = cet("span", "From:");
						dv.appendChild(span);
						var input = ce("input");
						input.setAttribute("type", "text", 0);
						$(input).observe('keypress', function(e) {
							if(e.keyCode == Event.KEY_RETURN) {
								this.getDirections(input.value, loc);
							}
						}.bindAsEventListener(this));
						dv.appendChild(input);
						href = ce("a");
						href.setAttribute("href", "javascript:;", 0);
						$(href).observe('click', function(e) {
							if(Prototype.Browser.IE) {
								window.setTimeout(function() {
									this.getDirections(input.value, loc);
								}.bind(this), 500);
							}
							else {
								this.getDirections(input.value, loc);
							}
						}.bindAsEventListener(this));
						var img = ce("img");
						img.setAttribute("src", "img/button-go.gif", 0);
						img.setAttribute("width", "29", 0);
						img.setAttribute("height", "18", 0);
						img.setAttribute("alt", "Go", 0);
						href.appendChild(img);
						dv.appendChild(href);
						td = ce("td");
						$(dv).hide();
						td.appendChild(dv);
						row.appendChild(td);
						
						// close row
						row.appendChild(ce("td"));
						
						tbody.appendChild(row);
					}
					// spacer row
					var row = ce("tr");
					var td = cet("td", String.fromCharCode(160));
					td.setAttribute("colspan", "3", 0);
					$(td).setStyle('background: transparent url(http://www.novartis.com/images/_common/h-divider.gif) repeat-x scroll left center;');
					row.appendChild(td);
					tbody.appendChild(row);
					// increment location count
					locCount += 1;
				}.bind(this));
				// add city content
				table.appendChild(tbody);
				content_div.appendChild(table);
				div.appendChild(content_div);
				content.appendChild(div);
			}.bind(this));
			// display message if no locations rendered
			if(locCount == 0) {
				var msg_div = ce('div');
				var msg = (this.filter && (this.filter.length == 0 || this.filter.length == 7) ? 'There are no Novartis offices in ' + this.country.name : 'No matching Novartis offices found in ' + this.country.name + ' for the selected filter settings');
				msg_div.appendChild(cet('p', msg));
				content.appendChild(msg_div);
			}
			// add complete content to locations container and show
			$('locations-container').appendChild(content);
			$('locations-container').show();
		}
	},
	
	createSiteInfoWindow: function(latlng) {
		if(this.locations) {
			var names = [];
			var headers = [];
			var lLoc = null;
			this.locations.each(function(loc) {
				if(loc.latlng == latlng) {
					if(names.indexOf(loc.name) == -1) {
						names.push(loc.name);
						if(loc.url && loc.url != '-') {
							var href = cet('a', loc.name.replace("\n", " "));
							href.setAttribute('href', loc.url, 0);
							href.setAttribute('target', '_blank');
							headers.push(ce("p", href));
						}
						else {
							headers.push(ce("p", cet("strong", loc.name.replace("\n", " "))));
						}
					}
					lLoc = loc;
				}
			}.bind(this));
			names = null;
			var div = ce("div");
			$(div).addClassName("site-info-window");
			// add site names
			headers.each(function(h) {
				div.appendChild(h);
			}.bind(this));
			// add address
			var addr = lLoc.site.split("\n");
			addr.each(function(a) {
				div.appendChild(cet("p", a));
			}.bind(this));
			// add directions form
			var dv = ce("div");
			$(dv).addClassName('directions-form');
			var ph = cet("span", String.fromCharCode(160));
			
			var input = ce("input");
			input.setAttribute("type", "text", 0);
			input.setAttribute("value", "From", 0);
			$(input).observe('keypress', function(e) {
				if(e.keyCode == Event.KEY_RETURN) {
					this.gmap.closeInfoWindow();
					this.getDirections(input.value, lLoc);
				}
			}.bindAsEventListener(this));
			$(input).observe('focus', function(e) {
				input.setAttribute("value", "", 0);
			}.bindAsEventListener(this));
			$(input).observe('blur', function(e) {
				if(input.value == "") {
					input.setAttribute("value", "From", 0);	
				}
			}.bindAsEventListener(this));			
			dv.appendChild(input);
			href = ce("a");
			href.setAttribute("href", "javascript:;", 0);
			$(href).observe('click', function(e) {
				this.gmap.closeInfoWindow();
				if(Prototype.Browser.IE) {
					window.setTimeout(function() {
						this.getDirections(input.value, lLoc);
					}.bind(this), 500);
				}
				else {
					this.getDirections(input.value, lLoc);
				}
			}.bindAsEventListener(this));
			var img = ce("img");
			img.setAttribute("src", "img/button-go.gif", 0);
			img.setAttribute("width", "29", 0);
			img.setAttribute("height", "18", 0);
			img.setAttribute("alt", "Go", 0);
			href.appendChild(img);
			dv.appendChild(document.createTextNode(String.fromCharCode(160)));
			dv.appendChild(href);
			var p = ce("p", ph);
			p.appendChild(dv);
			div.appendChild(p);
			$(dv).hide();
			
			var href = cet("a", "Get directions >");
			href.setAttribute("href", "javascript:;", 0);
			$(href).observe("click", function() {
				$(dv).toggle();
				$(ph).toggle();
			});
			//div.appendChild(ce("p", document.createTextNode(String.fromCharCode(160))));
			div.appendChild(ce("p", href));
						
			return div;
		}
		return ce('div');
	},
	
	getDirections: function(from, to) {
		if(this.gdir) {
			this.gdir.clear();
			var ll = this.getLatLng(to);
			var to_str = to.name.replace("\n", " ") + "@" + ll.lat() + "," + ll.lng();
			var str = "from: " + from + " to: " + to_str;
			this.gdir.load(str);
			$('google-dir-container').show();
			window.scrollTo(0, 0);
		}
	},
	
	countryList: function(letter) {
		var result = [];
		if(this.countries) {
			gCountries.each(function(c) {
				if(this.countries.indexOf(c.code2) > -1 && c.name && c.name.substring(0, 1).toUpperCase() == letter.toUpperCase()) {
					result.push(c);
				}
			}.bind(this));
		}
		return result;
	}
});

var CountryDropdown = Class.create({
	initialize: function(parent) {
		this.parent = $(parent);
		this.mop = false;
		this.mom = false;
		this.parent.observe("mouseover", function() {
			this.mop = true;
		}.bindAsEventListener(this));
		this.parent.observe("mouseout", function() {
			this.mop = false;
			window.setTimeout(function() {
				if(!this.mom && !this.mop) {
					this.hide();
				}
			}.bind(this), 500);
		}.bindAsEventListener(this));
		this.createMenu();
		this.hookLetters();
	},
	
	hookLetters: function() {
		var eles = $$("span.country-list-letter");
		eles.each(function(e) {
			e.observe("mouseover", function() {
				this.mop = true;
				e.setStyle('background-color: #E34D14');
				if(this.cLetter) {
						this.cLetter.setStyle('background-color: #D7C3B2');
				}
				this.cLetter = e;
				this.show(e);
			}.bindAsEventListener(this));
		}.bind(this));
	},
	
	createMenu: function() {
		this.menu = $(ce('div'));
		this.menu.addClassName('dsel');
		this.menu.observe("mouseover", function() {
			this.mom = true;
		}.bindAsEventListener(this));
		this.menu.observe("mouseout", function() {
			this.mom = false;
			window.setTimeout(function() {
				if(!this.mom && !this.mop) {
					if(this.cLetter) {
						this.cLetter.setStyle('background-color: #D7C3B2');
					}
					this.hide();
				}
			}.bind(this), 500);
		}.bindAsEventListener(this));
		this.menu.hide();
		document.body.appendChild(this.menu);
	},
	
	select: function(c) {
		this.hide();
		gApp.selectCountry(c.code);
	},
	
	show: function(e) {
		this.populateMenu(e.innerHTML);
		this.menu.setStyle({top: (this.getTop(e) + this.parent.offsetHeight - 3) + 'px', left: (this.getLeft(e) - 28) + 'px', position: 'absolute'});
		this.menu.show();
	},
	
	populateMenu: function(letter) {
		var ul = ce('ul');
		this.menu.innerHTML = "";
		var countries = gApp.countryList(letter);
		countries.each(function(c) {
			var li = ce("li");
			var span = cet('span', c.name);
			$(li).observe('mouseover', function() {
				li.setStyle('background-color: #917c69');
				span.setStyle('color: #FFFFFF');
			}.bind(this));
			$(li).observe('mouseout', function() {
				li.setStyle('background-color: #f3ead8');
				span.setStyle('color: #715541');
			}.bind(this));
			$(li).observe('click', function() {
				this.select(c);
			}.bind(this));
			/*
			var href = cet('a', c.name);
			href.setAttribute('href', 'javascript:;', 0);
			$(href).observe('click', function() {
				this.select(c);
			}.bindAsEventListener(this));
			*/
			li.appendChild(span);
			ul.appendChild(li);
		}.bind(this));
		this.menu.appendChild(ul);
	},
	
	hide: function() {
		this.menu.hide();
	},
	
	getLeft: function(e) {
		var offset = 0;
		var element = e;
		while(element) {
			offset += element.offsetLeft;
			element = element.offsetParent;
		}
		return offset;
	},
	
	getTop: function(e) {
	    var offset = 0;
	    var element = e;
	    while (element) {
			offset += element.offsetTop;
			element = element.offsetParent;
	    }
	    return offset;	
	}
});

// on load event
Event.observe(window, 'load', function() {
	gApp = new NovLocations();
});

// on unload event
Event.observe(window, 'unload', function() {
	GUnload();
});

// country clicked event
function countryClicked(cc) {
	if(gApp) {
		gApp.selectCountry(cc);
	}
}

// create element
function ce(ele, child) {
	var obj = document.createElement(ele);
	if(child) {
		obj.appendChild(child);
	}
	return obj;
}

// create element with text node
function cet(ele, txt) {
	var obj = ce(ele);
	obj.appendChild(document.createTextNode(txt));
	return obj;
}

// array serializer
function arrayToString(arr) {
	var result = '';
	if(arr && arr.length) {
		arr.each(function(val, idx) {
			result += val;
			if(idx != arr.length-1) {
				result += ',';
			}
		}.bind(this));
	}
	return result;
}