var mdvJpMaps = new Array();

var storedPOIHierarchy = '';
var storedPOIDrawClass= '';

function onLoadMapHandler() {
	var maps = document.getElementsByTagName('div');

	for (var i=0; i < maps.length; i++) {
		if (maps[i].className == 'jpmap') {
			mdvJpMaps[maps[i].id]  = new MDVMapHelperResults(maps[i]);

			/* var selector2 = document.getElementById('efa_poi_layer' +i);
			if (selector2)
				selector2.checked = false; */
					
		}
	}
}

function MDVMapHelperResults(viewport) {
	this.viewport = viewport;
	this.config = mdvMapConfig.clone();
	this.applyConfig();

	this.mdvMap = null;
	this.trips = null;
	this.mdvMapTripsLoaded = false;
	
	this.poiFirstTime = true;
}

MDVMapHelperResults.prototype.hide = function() {

	if (this.viewport)
		this.viewport.style.display = 'none';
	if(this.viewport && document.getElementById(this.viewport.id + '_Hierarchy'))
		document.getElementById(this.viewport.id + '_Hierarchy').style.display = 'none';
};

MDVMapHelperResults.prototype.display = function() {

	if (this.viewport)
		this.viewport.style.display = 'block';
	if(this.viewport && document.getElementById(this.viewport.id + '_Hierarchy'))
		document.getElementById(this.viewport.id + '_Hierarchy').style.display = 'block';

	// lazy creation
	if (!this.mdvMap) {
		this.mdvMap = 		new MDVMap(this.viewport);
							//new MDVMapEFAInfoEx(this.mdvMap, stopComparator, null, getBusPointImg);
							new MDVMapEFAInfoEx(this.mdvMap, stopComparator, getMOTImage, getBusPointImg);

		this.trips =	new MDVMapEFATrips(this.mdvMap);
					  		new MDVMapNavigator(this.mdvMap);
					  		new MDVMapControl(this.mdvMap);
							
							
		this.mdvMap.events.registerEvent(MDVEvent_MAP_INITIALISED, this, this.checkLayers);
							
		this.mdvMap.execute(this.config);
	}
};


MDVMapHelperResults.prototype.checkLayers = function(id, msg, obj) {

	/* if(document.getElementById('efa_poi_layer' +this.mdvMap.name.substr(this.mdvMap.name.lastIndexOf('_')+1)).style.display!='none'){

		var zl = obj.config.getZoomLevel(obj.config.getZoomLevelIndex());
		var showPOI  = zl.get('showPOI') == 'true';
		
		var pois = this.mdvMap.getLayer('efa_poi');

		if (pois && this.poiFirstTime) {
			pois.setVisibility(false);
			this.poiFirstTime = false;
		}

		if (showPOI) {
		
			for(i=0;i< document.forms[1].nameHierarchy_origin.length; i++){
				var selector = document.forms[1].nameHierarchy_origin[i];
				selector.disabled = false;
			}
		} else {
			for(i=0;i< document.forms[1].nameHierarchy_origin.length; i++){
				var selector = document.forms[1].nameHierarchy_origin[i];
				selector.disabled = true;
			}
		}
	
	} */
};


MDVMapHelperResults.prototype.setLayerVisibility = function (obj) {

	var layer = obj.id;
	
	var markers = this.mdvMap.getLayer('efa_poi');
		if (markers) {
			//markers.setVisibility(obj.checked);
			//markers.setVisibility(true);
			
			/*if(document.getElementById('efa_poi_layer').type=='checkbox'){
				markers.setVisibility(obj.checked);
			}
			else{ */
				markers.setVisibility(true);
			/*} */
			
			this.mdvMap.update();
		}
};

MDVMapHelperResults.prototype.displayJourney = function(sessionID, requestID, tripIndex) {
	if (!this.mdvMapTripsLoaded) {
		this.trips.loadTrip(sessionID, requestID, tripIndex, false);
		this.mdvMapTripsLoaded = true;
	}
}


MDVMapHelperResults.prototype.toggle = function() {


var maps = document.getElementsByTagName('div');

	for (var i=0; i < maps.length; i++) {
		if (maps[i].className == 'jpmap') {
			mdvJpMaps[maps[i].id].hide();
		}
	}


	 if (this.viewport.style.display == 'block')
		this.hide();
	else
		this.display();
};

MDVMapHelperResults.prototype.applyConfig = function() {
	if (this.config.params.length > 0)
		return false;

	this.config.add('defaultScale', '3');
	this.config.add('tripURL', '../lite/XSLT_TRIP_REQUEST2');
	this.config.add('efaURL', '../lite/XSLT_COORD_REQUEST');
	this.config.add('serverURL', 'http://maps.journeyplanner.org/maps/tfl/tiles/map/');
	this.config.add('transparentImg', 'mdv/images/maps/transparent.gif');
	this.config.add('mapName', 'TFLV');
	this.config.add('network', 'TFL');
	this.config.add('imagePath', 'mdv/images/');
	this.config.add('xCenterReal', '531445');
	this.config.add('yCenterReal', '820940');
	this.config.add('zoomOnDoubleClick', 'true');
	this.config.add('block', '100');
	this.config.add('crosshair', 'crosshair.gif');
	this.config.add('useMagnifyGlass', 'true');
	this.config.add('cursorMove', 'mdv/images/grabbing.cur');

	this.config.add('motColour_0', '#0000FF');
	this.config.add('motColour_1', '#000099');
	this.config.add('motColour_2', '#00797A');
	this.config.add('motColour_3', '#FF0000');
	this.config.add('motColour_4', '#7AA6FF');
	this.config.add('motColour_5', '#0000FF');
	this.config.add('motColour_6', '#0000FF');
	this.config.add('motColour_7', '#0000FF');
	this.config.add('motColour_8', '#0000FF');
	this.config.add('motColour_9', '#0000FF');
	this.config.add('motColour_10', '#0000FF');
	this.config.add('motColour_11', '#0000FF');
	this.config.add('motColour_12', '#0000FF');
	this.config.add('motColour_99', '#009B00');
	this.config.add('motColour_100', '#009B00');
	this.config.add('motColour_101', '#0000FF');
	this.config.add('motColour_102', '#0000FF');
	this.config.add('motColour_103', '#0000FF');
	this.config.add('motColour_104', '#0000FF');
	this.config.add('motColour_105', '#0000FF');
	this.config.add('motColour_106', '#0000FF');
	this.config.add('motColour_107', '#000000');
	this.config.add('motColour_108', '#0000FF');

	return true;
};

// add functions to the current onload stack
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

addLoadEvent(onLoadMapHandler);
function onDocumentMouseMove (e) {
  	e = e ? e : window.event;
  	var src = e.srcElement ? e.srcElement : e.target;
  	var obj = e.srcElement ? src.parentElement : src.parentNode;

  	while (obj) {
  		if (obj.className == 'mdvLayer')
  			return true;

  		obj = e.srcElement ? obj.parentElement : obj.parentNode;
  	}

  	for (var i=1; i <= 20; i++) {
		if (document.mdvMaps && document.mdvMaps['mdvMap_mdvJpMap_' + i]) {
			document.mdvMaps['mdvMap_mdvJpMap_' + i].release(e);
			continue;
		}
	}

  	return true;
}

// Don't add bus stops
function stopComparator (pin) {

	/* if(document.getElementById('efa_poi_layer').type=='checkbox'){
		return true;
	} */

	if(pin.attrs && pin.type=='POI_POINT'){
	
		var drawClass = storedPOIDrawClass.split(':');
		var hierarchy = storedPOIHierarchy.split(':');

		for (var i=0; i < pin.attrs.length; i++) {
			var a = pin.attrs[i];
			
			if(pin.attrs.length > 3){
					
				/* if(a.name=='POI_HIERARCHY_0' && a.value== storedPOIHierarchy)
				return true; */
				
				if(a.name=='POI_HIERARCHY_0' && storedPOIHierarchy.indexOf(a.value)!= '-1' && storedPOIHierarchy.indexOf(':')!= '-1')
				{
					for (var i=0; i < hierarchy.length; i++) {
						if(a.name=='POI_HIERARCHY_0' && a.value== hierarchy[i])
							return true;
					}
					return false;
				}
				else{
					if(a.name=='POI_HIERARCHY_0' && a.value== storedPOIHierarchy)
					return true;
				}
				
			}
			else{
			
				if(a.name=='POI_DRAW_CLASS' && storedPOIDrawClass.indexOf(a.value)!= '-1' && storedPOIDrawClass.indexOf(':')!= '-1')
				{
					for (var i=0; i < drawClass.length; i++) {
						if(a.name=='POI_DRAW_CLASS' && a.value== drawClass[i])
							return true;
					}
					return false;
				}
				else{
					if(a.name=='POI_DRAW_CLASS' && a.value== storedPOIDrawClass)
					return true;
				}
			}
			
			
		}
		return false;
	}
	
	

	// no tube/dlr platforms
	if (pin.identifier && pin.identifier.toString().length == 5 && parseInt(pin.identifier) >= 0)
		return false;
	
	if (!pin.type || pin.type != 'STOP')
		return true;

	var nodeValue = '';
	var mm	= pin.attrs;
	if (mm && mm.length > 0) {

		for (var m=0; m < mm.length; m++) {
			var tmpNode = mm[m];

			if (tmpNode.name == 'STOP_MAJOR_MEANS') {
				nodeValue = tmpNode.value;
				break;
			}
		}
	}

	if (nodeValue != 3)
		return true;

	//return false;
}

function getBusPointImg(pin) {
	var imgPath = 'images/';
	
	if (this.mdvMap.config.get('imagePath'))
		imgPath = this.mdvMap.config.get('imagePath');

	if (this.letters[pin.identifier] && this.letters[pin.identifier].src) {
	  	return this.letters[pin.identifier].src;
	} else if (pin.identifier && pin.identifier.toString().indexOf('->') > -1) {
		imgPath += 'point.gif';		
		return imgPath;
	} else {
		imgPath += 'point_' + pin.identifier + '.gif';
		
		return imgPath;
	}
}



function getMOTImage(mot, type, pin){

	var imgPath = 'images/';

	if (this.mdvMap.config.get('imagePath'))
		imgPath = this.mdvMap.config.get('imagePath');

		
	/*if(document.getElementById('efa_poi_layer').type=='checkbox' && type=='POI_POINT' && pin.attrs[1].value){
		imgPath += 'poiModes/oldPoi.gif';
		return imgPath;
	} */
		
	if(type=='POI_POINT' && pin.attrs[1].value){
	
		if(pin.attrs[3] ){
	
			if(pin.attrs[2].value== 'Pubs, Bars And Inns' || pin.attrs[3].value== 'Alcoholic Drinks'){
				imgPath += 'poiModes/PoiGastroDrink.gif';
				return imgPath;
			}
			else if(pin.attrs[2].value== 'Cafes, Snack Bars And Tea Rooms' || pin.attrs[2].value== 'Internet Cafes'){
				imgPath += 'poiModes/PoiGastroCafe.gif';
				return imgPath;
			}
			if(pin.attrs[2].value== 'Taxi Services'){
				imgPath += 'poiModes/PoiPublicTransport.gif';
				return imgPath;
			}
			
			else if(pin.attrs[2].value== 'Aquaria'){
				imgPath += 'poiModes/PoiAttractionsFish.gif';
				return imgPath;
			}
			
			else if(pin.attrs[3].value== 'Botanical And Zoological' || pin.attrs[3].value=='Zoos And Animal Collections'){
				imgPath += 'poiModes/PoiAttractionsZoo.gif';
				return imgPath;
			}

			else if(pin.attrs[3].value== 'Historic Buildings Including Castles, Forts and Abbeys'){
				imgPath += 'poiModes/PoiReligion.gif';
				return imgPath;
			}
			else if(pin.attrs[3].value== 'Historic and Ceremonial Structures'){
				imgPath += 'poiModes/PoiAttractionsCross.gif';
				return imgPath;
			}
			else if(pin.attrs[3].value== 'Historical Ships'){
				imgPath += 'poiModes/PoiAttractionsHisShip.gif';
				return imgPath;
			}
			else if(pin.attrs[3].value== 'Museums And Art Galleries'){
				imgPath += 'poiModes/PoiMuseum.gif';
				return imgPath;
			}
			else if(pin.attrs[3].value== 'Bodies Of Water' || pin.attrs[3].value== 'Swimming Pools'){
				imgPath += 'poiModes/PoiWater.gif';
				return imgPath;
			}
			else if(pin.attrs[3].value== 'Country Parks'){
				imgPath += 'poiModes/PoiPark.gif';
				return imgPath;
			}
			else if(pin.attrs[3].value== 'Picnic Areas'){
				imgPath += 'poiModes/PoiAttractionPicnic.gif';
				return imgPath;
			}
			else if(pin.attrs[3].value== 'Tourist Information Centres' || pin.attrs[3].value== 'Commercial Services'){
				imgPath += 'poiModes/PoiOthers.gif';
				return imgPath;
			}
			else if(pin.attrs[3].value== 'Playground'){
				imgPath += 'poiModes/PoiAttractionsPlayground.gif';
				return imgPath;
			}
			else if(pin.attrs[3].value== 'Cinemas'){
				imgPath += 'poiModes/PoiFair.gif';
				return imgPath;
			}
			else if(pin.attrs[3].value== 'Theatres And Concert Halls'){
				imgPath += 'poiModes/PoiSportTheatres.gif';
				return imgPath;
			}
			else if(pin.attrs[3].value== 'Libraries'){
				imgPath += 'poiModes/PoiPublicLibraries.gif';
				return imgPath;
			}
			else if(pin.attrs[3].value== 'Courts'){
				imgPath += 'poiModes/PoiCourts.gif';
				return imgPath;
			}
			else if(pin.attrs[3].value== 'Cemeteries And Crematoria'){
				imgPath += 'poiModes/PoiInfraCross.gif';
				return imgPath;
			}
			else if(pin.attrs[3].value== 'Recycling Centres'){
				imgPath += 'poiModes/PoiInfraRecycling.gif';
				return imgPath;
			}
			else if(pin.attrs[2].value== 'Helipads'){
				imgPath += 'poiModes/PoiAirHeli.gif';
				return imgPath;
			}
			else if(pin.attrs[2].value== 'Air'){
				imgPath += 'poiModes/PoiAir.gif';
				return imgPath;
			}
			else if(pin.attrs[3].value== 'Sports Complex' || pin.attrs[3].value== 'Athletics Facilities' || pin.attrs[3].value== 'Sports Grounds, Stadia And Pitches' || pin.attrs[3].value== 'Entrys Emirates Stadium'){
				imgPath += 'poiModes/PoiSport.gif';
				return imgPath;
			}
			else if(pin.attrs[3].value== 'Medical Establishments' || pin.attrs[2].value== 'Medical Establishments'){
				imgPath += 'poiModes/PoiMedicine.gif';
				return imgPath;
			}
			else if(pin.attrs[3].value== 'Higher Education Colleges' || pin.attrs[3].value== 'Universities And Medical Schools'){
				imgPath += 'poiModes/PoiSchoolUniversity.gif';
				return imgPath;
			}
			else if(pin.attrs[3].value== 'Primary, Secondary And Tertiary Education' || pin.attrs[3].value== 'Recreational And Vocational Education'){
				imgPath += 'poiModes/PoiSchool.gif';
				return imgPath;
			}
			
			else if(pin.attrs[2].value== 'Youth Hostels'){
				imgPath += 'poiModes/PoiYouthHostel.gif';
				return imgPath;
			}
			else if(pin.attrs[3].value== 'Accommodation'){
				imgPath += 'poiModes/PoiHotel.gif';
				return imgPath;
			} 
			
			else if(pin.attrs[3].value== 'Attractions'){
				imgPath += 'poiModes/PoiAttractionsOthers.gif';
				return imgPath;
			}
			else if(pin.attrs[3].value== 'Public Infrastructure'){
				imgPath += 'poiModes/PoiPublicBuilding.gif';
				return imgPath;
			} 
			else if(pin.attrs[3].value== 'Retail'){
				imgPath += 'poiModes/PoiShopping.gif';
				return imgPath;
			}
			else if(pin.attrs[3].value== 'Eating And Drinking'){
				imgPath += 'poiModes/PoiGastro.gif';
				return imgPath;
			}
			else if(pin.attrs[3].value== 'Sport And Entertainment'){
				imgPath += 'poiModes/PoiSportOthers.gif';
				return imgPath;
			}
			
			
			else if(pin.attrs[4]){
				if(pin.attrs[4].value== 'Attractions'){
					imgPath += 'poiModes/PoiAttractionsOthers.gif';
					return imgPath;
				}
				else if(pin.attrs[4].value== 'Public Infrastructure'){
					imgPath += 'poiModes/PoiPublicBuilding.gif';
					return imgPath;
				} 
				else if(pin.attrs[4].value== 'Retail'){
					imgPath += 'poiModes/PoiShopping.gif';
					return imgPath;
				}
				else if(pin.attrs[4].value== 'Eating And Drinking'){
					imgPath += 'poiModes/PoiGastro.gif';
					return imgPath;
				}
				else if(pin.attrs[4].value== 'Sport And Entertainment'){
					imgPath += 'poiModes/PoiSportOthers.gif';
					return imgPath;
				}
			
			
			
			}


			else {
			
				imgPath += 'poiModes/' + pin.attrs[1].value + '.gif';
			
				return imgPath;
			}
		
		}
		else if(type=='POI_POINT' && pin.attrs[1].value){
		
			imgPath += 'poiModes/' + pin.attrs[1].value + '.gif';
			
			return imgPath;
		}
	}
	else{
		imgPath += 'mot' + mot + '.gif';		
		return imgPath;
	} 
}


function storedPOI(hierarchy, draw, value){

	if(storedPOIHierarchy!='' && value==true){
		storedPOIHierarchy=storedPOIHierarchy + ':' +hierarchy;
	}
	else if(storedPOIHierarchy!='' && value==false){
		storedPOIHierarchy=storedPOIHierarchy.replace(hierarchy, "");
	}
	else{
		storedPOIHierarchy=hierarchy;
	}
	
	if(storedPOIDrawClass!=''){
		storedPOIDrawClass=storedPOIDrawClass + ':' +draw;
	}
	else if(storedPOIDrawClass!='' && value==false){
		storedPOIDrawClass=storedPOIDrawClass.replace(draw, "");
	}
	
	else{
		storedPOIDrawClass=draw;
	}
}



document.onmouseover = onDocumentMouseMove;

function hashLocation(pos){
	this.location.hash='jpmap_'+pos;
}