/*  Office Locator - v 1.7 / 27.05.2009	*/

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

// Global vars
var locationsXML_File = 'http://www.ing.ro/ing/dms/locator-map/ing_locations/ing_locations.xml';
var servicesXML_File = 'http://www.ing.ro/ing/dms/locator-map/ing_business_services/ing_business_services.xml';

var locationsXML = null;
var servicesXML = null;

var serviceTag = [];
var serviceName = [];
var cities = [];
var counties = [];
var locations = [];
var businessLineTag = [];
var businessLineName = [];
var infoTag = [];
var infoName = [];
var info = [];
var selectedServices = [];
var activeServices = 0;

var activeLocation;
var selectedCity = -1;

var i;

var mapObject = null;
var roLatitude = 46.1439;
var roLongitude = 25.2685;
var roZoomLevel = 6;

function initOL() {
	if (locationsXML == null) {
		try { //Internet Explorer 
			locationsXML=new ActiveXObject("Microsoft.XMLDOM");
			locationsXML.async=false;
			locationsXML.load(locationsXML_File);
		}
		catch(e) {
			try { //Firefox, Mozilla, Opera, etc. 
				locationsXML=document.implementation.createDocument("","locations",null);
				locationsXML.async=false;
				locationsXML.load(locationsXML_File);
			}
			catch(e) {
				try //Google Chrome
				{
					var xmlhttp = new window.XMLHttpRequest();
					xmlhttp.open("GET",locationsXML_File,false);
					xmlhttp.send(null);
					locationsXML = xmlhttp.responseXML.documentElement;
				}
					catch(e) {
					alert(e.message);
					return;
					}
			}
		}

		
	}

	if (servicesXML == null) {	
		try { //Internet Explorer 
				servicesXML=new ActiveXObject("Microsoft.XMLDOM");
				servicesXML.async=false;
				servicesXML.load(servicesXML_File);
			}
			catch(e) {
				try { //Firefox, Mozilla, Opera, etc. 
					servicesXML=document.implementation.createDocument("","locations",null);
					servicesXML.async=false;
					servicesXML.load(servicesXML_File);
				}
				catch(e) {
					try //Google Chrome
					{
						var xmlhttp = new window.XMLHttpRequest();
						xmlhttp.open("GET",servicesXML_File,false);
						xmlhttp.send(null);
						servicesXML = xmlhttp.responseXML.documentElement;
					}
						catch(e) {
						alert(e.message);
						return;
						}
				}
			}	
			
		}
}

function setServices() {
	var serviceElements;

	serviceElements = servicesXML.getElementsByTagName("service");
	for (i=0; i<serviceElements.length; i++) {
		serviceTag[i] = serviceElements[i].getAttribute("tag");
		serviceName[i] = serviceElements[i].childNodes[0].nodeValue;
	}
}

function setBusinessLines() {
	var businessElements;

	businessElements = servicesXML.getElementsByTagName("business_line");
	for (i=0; i<businessElements.length; i++) {
		businessLineTag[i] = businessElements[i].getAttribute("tag");
		businessLineName[i] = businessElements[i].getAttribute("display");
	}
}

function putOLServices() {
	var servicesDiv, myHTML;

	myHTML = '';
	for (i=0; i<serviceName.length; i++) {
		myHTML += '<input type="checkbox" name="service_' + i + '" id="service_' + i + '" value="1" onClick="javascript:changeServices();"/>&nbsp;' + serviceName[i] + '&nbsp;&nbsp;&nbsp;';
	}

	servicesDiv = document.getElementById("ol_services");
	servicesDiv.innerHTML = myHTML;
}

function setCities() {
	var countyElements;

	cityElements = locationsXML.getElementsByTagName("city");
	for (i=0; i<cityElements.length;i++ ) {
		cities[i] = cityElements[i].getAttribute("name");
		counties[i] = cityElements[i].getAttribute("county");
	}
}

function setLocations() {
	var cityElements, thisCity, locationElements, thisElement, cityName;
	var name, address, j, k, d;
	var myElement, locationService, nrServices;
	var mapLatitude, mapLongitude, mapZoomLevel, mapDetails;

	cityElements = locationsXML.getElementsByTagName("city");
	thisCity = null;
	nrServices = serviceTag.length;

	d = 0;
	for (i=0; i<cityElements.length; i++) {
		cityName = cityElements[i].getAttribute("name");
		locationElements = cityElements[i].getElementsByTagName("location");
		locations[cityName] = new Array(locationElements.length);
		for (j=0; j<locationElements.length; j++) {	
			d = d + 1;
			thisElement = locationElements[j];
			name = thisElement.getElementsByTagName("name")[0].childNodes[0].nodeValue;
			address = thisElement.getElementsByTagName("address")[0].childNodes[0].nodeValue;
			details = '';

			// search business lines
			for (k=0; k<businessLineTag.length; k++) {
				myElemenet = thisElement.getElementsByTagName(businessLineTag[k]);
				if (myElemenet.length > 0){
					details += '<span class="business_line">' + businessLineName[k] + "</span><br/>" + myElemenet[0].getElementsByTagName("info")[0].childNodes[0].nodeValue + "<br/>";
				}
			}
			if (details != '') {
				details = '<span id="location_details_' + d + '" class="location_details">' + details + '</span>';
			}

			locationService = new Array(nrServices);

			for (k=0; k<nrServices; k++) {
				myElemenet = thisElement.getElementsByTagName(serviceTag[k]);
				if (myElemenet.length > 0){
					locationService[k] = 1;
				}
				else
					locationService[k] = 0;
			}

			// search for map details
			myElemenet = thisElement.getElementsByTagName("map");
			if (myElemenet.length > 0){
				mapLatitude = myElemenet[0].getElementsByTagName("latitude")[0].childNodes[0].nodeValue;
				mapLongitude = myElemenet[0].getElementsByTagName("longitude")[0].childNodes[0].nodeValue;
				mapZoomLevel = myElemenet[0].getElementsByTagName("zoom_level")[0].childNodes[0].nodeValue;

				mapDetails = new map_location(mapLatitude,mapLongitude,mapZoomLevel);
			}
			else {
				mapDetails = null;
			}
			
			locations[cityName][j] = new office_location(d,name,address,details,locationService,mapDetails);
		}
	}
}

function office_location(id,name,address,details, services, map) {
	this.id = id;
	this.name = name;
	this.address = address;
	this.details = details;
	this.services = services;
	this.map = map;
}

function map_location(latitude,longitude,zoom_level) {
	this.latitude = latitude;
	this.longitude = longitude;
	this.zoom_level = zoom_level;
}

function getCityLocationsHTML(cityName) {
	var myHTML;
	var nrServices, displayLocation, j;

	myHTML = '';
	for (i=0; i<locations[cityName].length; i++) {
		nrServices = selectedServices.length;
		displayLocation = 0;

		if (activeServices != 0){
			displayLocation = 1;
			for (j=0; j<nrServices; j++){
				if (selectedServices[j] == 1 && locations[cityName][i].services[j] == 0){
					displayLocation = 0;
					j = nrServices;
				}
			}
		}
		else {
			displayLocation = 1;
		}

		if (displayLocation == 1){
			if (locations[cityName][i].details != ''){
				locationDetails = '<br/>' + locations[cityName][i].details;
			}
			else {
				locationDetails = '';
			}

			myHTML += '<div id="location_' + locations[cityName][i].id +'" onClick="javascript:displayLocationDetails(' + locations[cityName][i].id + ",'" + cityName + "'," + i + ');" title="click pentru detalii"><strong>' + locations[cityName][i].name + '</strong><br/>';
			myHTML += locations[cityName][i].address + '</a>';
			myHTML += locationDetails;
			myHTML += '</div><hr/>';
		}
	}

	return myHTML;

}

function displayLocations() {
	// init services filter
	putOLServices();
	activeServices = 0;

	// display my locations
	displayMyLocations();
}

function displayMyLocations() {
	var cityId, cityName, strTmp, resultsDiv, locationsHTML;

	cityName = document.getElementById("lf_city").value;

	// validate cityName
	cityId = -2;
	cityName = cityName.trim();

	// validate empty cityName string
	if (cityName.length == 0) {
		displayAllLocations();
		// change map to country view
		mapObject.SetCenterAndZoom(new VELatLong(roLatitude, roLongitude), roZoomLevel);
		return;
	}

	for (i=0; i<cities.length; i++){
		if (cities[i].toLowerCase() == cityName.toLowerCase()){
			cityId = i;
			selectedCity = cityId;
			cityName = cities[i];
			i = cities.length;
		}
	}

	if (cityId == -2){
		alert("Nu exista nici o locatie ING in orasul introdus!");
		return;
	}

	resultsDiv = document.getElementById("ol_results");
	resultsDiv.innerHTML = '<img id="loading" src="http://ingsite3.uat.roretail.rointranet/magnoliaPublic3/dms/locator-test/loading/loading.gif"/>';
	locationsHTML = getCityLocationsHTML(cityName);
	
	resultsDiv.innerHTML = locationsHTML;

	// city map
	findCityOnMap(cityName);
}

function displayAllLocations() {
	var cityName, resultsDiv, locationsHTML;

	resultsDiv = document.getElementById("ol_results");
	resultsDiv.innerHTML = '<img id="loading" src="http://www.ing.ro/ing/dms/locator-map/loading/loading.gif"/>';

	locationsHTML = '';

	for (k=0; k<cities.length; k++){
		cityName = cities[k];		
		locationsHTML += getCityLocationsHTML(cityName);
	}
	
	resultsDiv.innerHTML = locationsHTML;
}

function displayLocationDetails(locationId,cityName,locationIndex) {
	var detailsArea, activeArea;
	elementId = 'location_details_' + locationId;
	detailsArea = document.getElementById(elementId);

	if (activeLocation != -1) {
		elementId = 'location_details_' + activeLocation;		
		activeArea = document.getElementById(elementId);

		if (activeArea != null){
			activeArea.style.display = 'none';
		}
	}

	if (detailsArea != null){
		if (activeLocation != locationId) {
			detailsArea.style.display = 'block';
			activeLocation = locationId;

			changeMap(cityName,locationIndex);
		}
		else {
			activeLocation = -1;
		}
	}
	else {
		activeLocation = -1;
	}

}

function eraseLocationsArea() {
	document.getElementById("ol_results").innerHTML = '';
}

function setSelectedServices() {
	var elementName, thisElement;
    var nrServices = 0;
	
	for (i=0 ; i<serviceTag.length; i++) {
		elementName = 'service_' + i;
		thisElement = document.getElementById(elementName);
		if (thisElement.checked == true){
			selectedServices[i] = 1;
		}
		else {
			selectedServices[i] = 0;
		}
		nrServices = nrServices + selectedServices[i];
	}

	if (nrServices != 0)
		activeServices = 1;
	else
		activeServices = 0;
}

function changeServices() {
	setSelectedServices();
	if (selectedCity == -1) {
		displayAllLocations();
	}
	else {
		displayMyLocations();
	}
}

function findCityOnMap(cityName) {
	var locationSearchString = cityName + ',' + 'Romania';
	try {
		results = mapObject.Find(null,
					  locationSearchString,
					  null,
					  null,
					  0,
					  1,
					  true,
					  true,
					  true,
					  true,
					  displayCityMap);
	}
	catch(e){
		alert(e.message);
	}
}

function displayCityMap() {
	var k;
	k++;
}

function changeMap(cityName,locationIndex) {
	thisLocation = locations[cityName][locationIndex];
	if (thisLocation.map != null){
		if (thisLocation.map.latitude != 0 && thisLocation.map.longitude != 0 && thisLocation.map.zoom_level != 0){
			mapObject.SetCenterAndZoom(new VELatLong(thisLocation.map.latitude, thisLocation.map.longitude), thisLocation.map.zoom_level);			
		}
		else {
			findCityOnMap(cityName);
		}
	}
	else {
		// mapObject.SetCenterAndZoom(new VELatLong(roLatitude, roLongitude), roZoomLevel);
		findCityOnMap(cityName);
	}
}

function putAllPushPins() {
	var k, cityName, thisLocation;
	var lMapDetails,myPin;
	
	for (k=0; k<cities.length; k++){
		cityName = cities[k];		
		
		for (i=0; i<locations[cityName].length; i++) {
			thisLocation = locations[cityName][i];			
			if (thisLocation.map != null){
				lMapDetails = new VELatLong(thisLocation.map.latitude, thisLocation.map.longitude, 0, VEAltitudeMode.RelativeToGround);
				myPin = new VEShape(VEShapeType.Pushpin, lMapDetails);
				myPin.SetTitle(thisLocation.name);
				myPin.SetDescription(thisLocation.address);
				myPin.SetCustomIcon(
				   "<span style='font-family:Arial; font-size:x-small;" +
				   "color:Black; background-color:White'>" +
				   "<img src='http://www.ing.ro/ing/dms/locator-map/ing_logo/ing_logo.png'/>ING</span>");
				mapObject.AddShape(myPin);
			}
		}
	}
}

function initLiveMap() {
	mapObject = new VEMap('liveMap');
	mapObject.LoadMap(new VELatLong(46.1439, 25.2685), 6);
}

function startOL() {
	initOL();

	// services
	setServices();
	putOLServices();
	setBusinessLines();

	// cities
	setCities();

	// locations
	setLocations();
	activeLocation = -1;
	
	setSelectedServices();

	// display all locations
	displayAllLocations();

	// autocomplete
	new Autocompleter.Local('lf_city', 'band_list', cities, {
							choices: 15,
                            partialChars: 1,
							ignoreCase: true
                        });

	// LiveMaps
	initLiveMap();
	putAllPushPins();

}