// Map Object
var map = null;

// Region Layer
var georegion;

// Resort Layer
var georesort;

// Hotel Layer
var geohotel;

//	Apartment Layer
var geoapartment;

// Vill Layer
var geovilla;

// Hold Shapdata for Hotels, Villas and Apartments
var shapeData = new Array();

/*
*
* Load the virtual Earth Map location and set the location to the destination
*
*/
function GetMap(){

//	Check if the map is already loaded.
	if( map == null ){

	//	Destination code for the map used to search the database.
		var destCode = document.getElementById('destCode').value;
	
	//	Latitude of the map location
		var destLat = document.getElementById('destLat').value;
		
	//	Longitude of the location
		var destLong = document.getElementById('destLong').value;
	
	//	Zoom level to use
		var zoom = document.getElementById('zoom').value;
		
	//	Create the Map Object
		map = new VEMap('maps');
		
	//	Set the dash board controls	
		map.SetDashboardSize(VEDashboardSize.Small);
	
	//  Load the Map
		map.LoadMap(new VELatLong( destLat, destLong ), zoom, 'r', false);

	//	onclick event for the pins
		map.AttachEvent("onclick", ShapeHandler);
			
	//	load region data
		LoadRegionPins( destCode );
		
	//  load resort Data
		LoadResortPins( destCode );
		
	//	Load hotel data
		LoadHotel( destCode );
		
	//	Load villa data
		LoadVilla( destCode );
		
	//	Load apartment data
		LoadApartment( destCode );
		
	}

} 

/*
*
*
*/
function ShapeHandler(e){
	
	if (e.elementID){
		
		var shape = map.GetShapeByID(e.elementID);
		var id = shape.GetId() 
		
		if( shapeData[ id ] ){
			
			map.SetCenterAndZoom( new VELatLong( parseFloat( shapeData[ id ]['geoLat']), parseFloat(shapeData[ id ]['geoLong'] ) ), parseInt(shapeData[ id ]['geozoomlevel']) );
			
			return true;
			
		}
					
	}
	       
}
	
/*
*
*
*/
function LoadResortPins( destCode ){

	var georesorturl   = "/Map_Resort/"+destCode;
	var georesortxml   = new JKL.ParseXML( georesorturl );
	var georesortdata  = georesortxml.parse();  
	var georesortCount = 0;

	if( georesortdata.rss.channel.item != null ){
		georesortCount = georesortdata.rss.channel.item.length;
	}

	georesort = new VEShapeLayer();         
	map.AddShapeLayer( georesort );
	
	for(var i=0; i < georesortCount; ++i){
	
		geoshape = new VEShape( VEShapeType.Pushpin, new VELatLong( georesortdata['rss']['channel']['item'][i]['lat'], georesortdata['rss']['channel']['item'][i]['long'] ) );  
      	geoshape.SetTitle( georesortdata['rss']['channel']['item'][i]['title'] );         
		geoshape.SetDescription( georesortdata['rss']['channel']['item'][i]['description'] ); 
		geoshape.SetCustomIcon("<img src='/_global/images/resort.gif'/>");
		
		geoshape.SetMinZoomLevel( georesortdata['rss']['channel']['item'][i]['minzoom'] );
		geoshape.SetMaxZoomLevel( georesortdata['rss']['channel']['item'][i]['maxzoom'] );
		
		georesort.AddShape( geoshape );
		
		var s = georesort.GetShapeByIndex(i);		
		var shapeId = s.GetID();
		
		shapeData[ shapeId ] = new Array('geoshapeId',
										 'geoLat',
										 'geoLong',
										 'geotitle',
										 'geodescription',
										 'geozoomlevel',
										 'geotype',
										 'geoId' );
		
		shapeData[ shapeId ]['geoshapeId'] 	   = shapeId;
		shapeData[ shapeId ]['geoid'] 	   	   = georesortdata['rss']['channel']['item'][i]['geoid'];
		shapeData[ shapeId ]['geotype'] 	   = georesortdata['rss']['channel']['item'][i]['geotype'];
		shapeData[ shapeId ]['geoLat']  	   = georesortdata['rss']['channel']['item'][i]['lat'];
		shapeData[ shapeId ]['geoLong'] 	   = georesortdata['rss']['channel']['item'][i]['long'];
		shapeData[ shapeId ]['geotitle'] 	   = georesortdata['rss']['channel']['item'][i]['title'];
		shapeData[ shapeId ]['geodescription'] = georesortdata['rss']['channel']['item'][i]['description'];
		shapeData[ shapeId ]['geozoomlevel']   = georesortdata['rss']['channel']['item'][i]['zoomlevel'];
		
	}

}


/*
* Load region data from Map_Region
*
*/
function LoadRegionPins( destCode ){

	var georegionurl   = "/Map_Region/"+destCode;
	var georegionxml   = new JKL.ParseXML( georegionurl );
	var georegiondata  = georegionxml.parse();  
	var georegionCount = 0;
	
	georegionCount = georegiondata.rss.channel.item.length;

	georegion = new VEShapeLayer();         
	map.AddShapeLayer( georegion );
	
	for(var i=0; i < georegionCount; ++i){

		geoshape = new VEShape( VEShapeType.Pushpin, new VELatLong( georegiondata['rss']['channel']['item'][i]['lat'], georegiondata['rss']['channel']['item'][i]['long'] ) );  
      	geoshape.SetTitle( georegiondata['rss']['channel']['item'][i]['title'] );         
		geoshape.SetDescription( georegiondata['rss']['channel']['item'][i]['description'] ); 
		geoshape.SetCustomIcon("<img src='/_global/images/region.gif'/>");

		geoshape.SetMaxZoomLevel( georegiondata['rss']['channel']['item'][i]['maxzoom'] );

		georegion.AddShape( geoshape );
		
		var s = georegion.GetShapeByIndex(i);		
		var shapeId = s.GetID();

		shapeData[ shapeId ] = new Array('geoshapeId',
										 'geoLat',
										 'geoLong',
										 'geotitle',
										 'geodescription',
										 'geozoomlevel',
										 'geotype',
										 'geoId' );

		shapeData[ shapeId ]['geoshapeId'] 	   = shapeId;
		shapeData[ shapeId ]['geoid'] 	   	   = georegiondata['rss']['channel']['item'][i]['geoid'];
		shapeData[ shapeId ]['geotype'] 	   = georegiondata['rss']['channel']['item'][i]['geotype'];
		shapeData[ shapeId ]['geoLat']  	   = georegiondata['rss']['channel']['item'][i]['lat'];
		shapeData[ shapeId ]['geoLong'] 	   = georegiondata['rss']['channel']['item'][i]['long'];
		shapeData[ shapeId ]['geotitle'] 	   = georegiondata['rss']['channel']['item'][i]['title'];
		shapeData[ shapeId ]['geodescription'] = georegiondata['rss']['channel']['item'][i]['description'];
		shapeData[ shapeId ]['geozoomlevel']   = georegiondata['rss']['channel']['item'][i]['zoomlevel'];
		
	}

}

/*
*
*
*/
function LoadHotel( destCode ){
	
	var geoaccomturl 	= "/Map_Accom/"+destCode+"/H";
			
	var geoaccomxml  = new JKL.ParseXML( geoaccomturl );
	var geoaccomdata = geoaccomxml.parse();  
	var geoaccomCount = 0;
	
	if( geoaccomdata.rss.channel.item ){
		var geoaccomCount = geoaccomdata.rss.channel.item.length;
	}

	geohotel = new VEShapeLayer();         
	map.AddShapeLayer( geohotel );
	
	if( geoaccomCount != null ){
		for(var i=0; i < geoaccomCount; ++i){
			
			if( geoaccomdata['rss']['channel']['item'][i]['icon'] == '/_global/images/hotel.gif'){
			
				geoshape = new VEShape( VEShapeType.Pushpin, new VELatLong( geoaccomdata['rss']['channel']['item'][i]['lat'], geoaccomdata['rss']['channel']['item'][i]['long'] ) );  
		        geoshape.SetTitle( geoaccomdata['rss']['channel']['item'][i]['title'] );         
				geoshape.SetDescription( geoaccomdata['rss']['channel']['item'][i]['description'] ); 
				geoshape.SetCustomIcon("<img src='"+geoaccomdata['rss']['channel']['item'][i]['icon']+"'/>");
						
				geohotel.AddShape( geoshape );
				
				if( geohotel.GetShapeByIndex(i) ){					
					var s = geohotel.GetShapeByIndex(i);		
					var shapeId = s.GetID();
					
					geoshape.SetMinZoomLevel( geoaccomdata['rss']['channel']['item'][i]['minzoom'] );
					geoshape.SetMaxZoomLevel( geoaccomdata['rss']['channel']['item'][i]['maxzoom'] );
									
					shapeData[ shapeId ] = new Array('geoshapeId',
													 'geoLat',
													 'geoLong',
													 'geotitle',
													 'geodescription',
													 'geozoomlevel' );
					
					shapeData[ shapeId ]['geoshapeId'] 	   = shapeId;
					shapeData[ shapeId ]['geoLat']  	   = geoaccomdata['rss']['channel']['item'][i]['lat'];
					shapeData[ shapeId ]['geoLong'] 	   = geoaccomdata['rss']['channel']['item'][i]['long'];
					shapeData[ shapeId ]['geotitle'] 	   = geoaccomdata['rss']['channel']['item'][i]['title'];
					shapeData[ shapeId ]['geodescription'] = geoaccomdata['rss']['channel']['item'][i]['description'];
					shapeData[ shapeId ]['geozoomlevel']   = geoaccomdata['rss']['channel']['item'][i]['zoomlevel'];
				}
				
			}			
			
		}
	}
	
	return true;
	
}

function LoadVilla( destCode ){
			
	var geoaccomturl 	= "/Map_Accom/"+destCode+"/V";		
	var geoaccomxml  	= new JKL.ParseXML( geoaccomturl );
	var geoaccomdata 	= geoaccomxml.parse();  
	var geoaccomCount   = 0;

	if( geoaccomdata.rss.channel.item != null ){
		geoaccomCount = geoaccomdata.rss.channel.item.length;
	}
		
	geovilla = new VEShapeLayer();         
	map.AddShapeLayer( geovilla );
	
	if( geoaccomCount > 0 ){
		for(var i=0; i < geoaccomCount; ++i){
			
			if( geoaccomdata['rss']['channel']['item'][i]['icon'] == '/_global/images/villa.gif'){
			
				geoshape = new VEShape( VEShapeType.Pushpin, new VELatLong( geoaccomdata['rss']['channel']['item'][i]['lat'], geoaccomdata['rss']['channel']['item'][i]['long'] ) );  
		        geoshape.SetTitle( geoaccomdata['rss']['channel']['item'][i]['title'] );         
				geoshape.SetDescription( geoaccomdata['rss']['channel']['item'][i]['description'] ); 
				geoshape.SetCustomIcon("<img src='"+geoaccomdata['rss']['channel']['item'][i]['icon']+"'/>");
					
				geoshape.SetMinZoomLevel( geoaccomdata['rss']['channel']['item'][i]['minzoom'] );
				geoshape.SetMaxZoomLevel( geoaccomdata['rss']['channel']['item'][i]['maxzoom'] );
					
				geovilla.AddShape( geoshape );
				
				if( geovilla.GetShapeByIndex(i) ){	
					
					var s = geovilla.GetShapeByIndex(i);		
					var shapeId = s.GetID();
					
					shapeData[ shapeId ] = new Array('geoshapeId',
													 'geoLat',
													 'geoLong',
													 'geotitle',
													 'geodescription',
													 'geozoomlevel' );
					
					shapeData[ shapeId ]['geoshapeId'] 	   = shapeId;
					shapeData[ shapeId ]['geoLat']  	   = geoaccomdata['rss']['channel']['item'][i]['lat'];
					shapeData[ shapeId ]['geoLong'] 	   = geoaccomdata['rss']['channel']['item'][i]['long'];
					shapeData[ shapeId ]['geotitle'] 	   = geoaccomdata['rss']['channel']['item'][i]['title'];
					shapeData[ shapeId ]['geodescription'] = geoaccomdata['rss']['channel']['item'][i]['description'];
					shapeData[ shapeId ]['geozoomlevel']   = geoaccomdata['rss']['channel']['item'][i]['zoomlevel'];
					
				}
				
			}			
			
		}
	}
	
	return true;
	
}
	
function LoadApartment( destCode ){
	
	var geoaccomturl = "/Map_Accom/"+destCode+"/A";
	var geoaccomxml  = new JKL.ParseXML( geoaccomturl );
	var geoaccomdata = geoaccomxml.parse();  

	if( geoaccomdata.rss.channel.item ){
		var geoaccomCount = geoaccomdata.rss.channel.item.length;
	}

	geoapartment = new VEShapeLayer();
	         
	map.AddShapeLayer( geoapartment );
	
	if( geoaccomCount != null ){
		for(var i=0; i < geoaccomCount; ++i){
			
			if( geoaccomdata['rss']['channel']['item'][i]['icon'] == '/_global/images/apartment.gif'){
			
				geoshape = new VEShape( VEShapeType.Pushpin, new VELatLong( geoaccomdata['rss']['channel']['item'][i]['lat'], geoaccomdata['rss']['channel']['item'][i]['long'] ) );  
		        geoshape.SetTitle( geoaccomdata['rss']['channel']['item'][i]['title'] );         
				geoshape.SetDescription( geoaccomdata['rss']['channel']['item'][i]['description'] ); 
				geoshape.SetCustomIcon("<img src='"+geoaccomdata['rss']['channel']['item'][i]['icon']+"'/>");
					
				geoshape.SetMinZoomLevel( geoaccomdata['rss']['channel']['item'][i]['minzoom'] );
				geoshape.SetMaxZoomLevel( geoaccomdata['rss']['channel']['item'][i]['maxzoom'] );
					
				geoapartment.AddShape( geoshape );
				
				if( geoapartment.GetShapeByIndex(i) ){	
					
					var s = geoapartment.GetShapeByIndex(i);		
					var shapeId = s.GetID();
				
					shapeData[ shapeId ] = new Array('geoshapeId',
													 'geoLat',
													 'geoLong',
													 'geotitle',
													 'geodescription',
													 'geozoomlevel' );
					
					shapeData[ shapeId ]['geoshapeId'] 	   = shapeId;
					shapeData[ shapeId ]['geoLat']  	   = geoaccomdata['rss']['channel']['item'][i]['lat'];
					shapeData[ shapeId ]['geoLong'] 	   = geoaccomdata['rss']['channel']['item'][i]['long'];
					shapeData[ shapeId ]['geotitle'] 	   = geoaccomdata['rss']['channel']['item'][i]['title'];
					shapeData[ shapeId ]['geodescription'] = geoaccomdata['rss']['channel']['item'][i]['description'];
					shapeData[ shapeId ]['geozoomlevel']   = geoaccomdata['rss']['channel']['item'][i]['zoomlevel'];
					
				}
				
			}			
			
		}
	}
	
}
	
function validate(chk){
	
	if (chk.checked == 1){
	    
		if( chk.name == 'rhotel' ){
			geohotel.Show();
		}
		
		if( chk.name == 'rvilla' ){
			geovilla.Show();
		}
		
		if( chk.name == 'rapartment' ){
			geoapartment.Show();
		}
		
	}else{

		if( chk.name == 'rhotel' ){
			geohotel.Hide();
		}
		
		if( chk.name == 'rvilla' ){
			geovilla.Hide();
		}
		
		if( chk.name == 'rapartment' ){
			geoapartment.Hide();
		}
		
	}
	
}

