/**
 *	This is the javascript file responsible for displaying Multimaps on the saerch results tab.
 *  version: 1.0 
 *  author: vishal.jassal
 **/

var mapviewer; 
var bounds;
var checkingBounds = false;
var local_info_widget;
var container;

/**
 *	Function to display Multimap on the search results Location&Map tab.
 **/
function displayMultiMap(id, resortLatitude, resortLongitude, accommodationLatitude, accommodationLongitude) {

	var multiMapDiv = id+"_MULTI_MAP";
	mapviewer = new MultimapViewer(document.getElementById(multiMapDiv));
	
	// Displaying map resort level (default zoom factor = 8)
	mapviewer.goToPosition(new MMLocation(new MMLatLon(resortLatitude, resortLongitude), 8));
	mapviewer.addEventHandler('moveMap', checkBounds);
	
	// fix the map
	bounds = new MMBounds(new MMLatLon(resortLatitude, resortLongitude), new MMLatLon(resortLatitude, resortLongitude));
	
	// Adding marker at accommodation level
	addMarker(accommodationLatitude, accommodationLongitude);
	
	// Adding Zoom slider
	addZoomSlider();
	
	// Populate default zoom factors
	populateZoomFactors (id);
	var localInfoDiv = id+"_localInfoWidget";
	container = document.getElementById(localInfoDiv);
	container.innerHTML = '';
	
	//Display Local information widget / POIs
	addLocalInfoWidget(undefined);
}

/**
 *	Function to add Local Information Widget (POIs) outside the multimap.
 **/
function addLocalInfoWidget(header) {
    var datasources, position;
    datasources = 'mm.poi.global.general.airport,mm.poi.global.general.railwaystation,mm.poi.global.general.parking,mm.poi.global.general.shopping,mm.poi.global.general.museum,mm.poi.global.general.golfcourse,mm.poi.global.general.park,mm.poi.global.general.zoo';
    position = new MMBox();
        
    // Add the Local Info Widget:
    local_info_widget  = new MMLocalInfoWidget (datasources, header, position);
    
    // Set container for placing widget outside map viewer:
    local_info_widget.setContainer(container);
    
    mapviewer.addWidget(local_info_widget);
	return false;
}

/**
 *	Function to display Zoom slider on the Multimap.
 **/
var zoom_widget;
function addZoomSlider() {
    // Add the Zoom Widget:
    mapviewer.removeWidget(zoom_widget);
    zoom_widget = new MMPanZoomWidget ();
	mapviewer.addWidget(zoom_widget);
}

/**
 *	Function to populate allowed zoom factors.
 **/
function populateZoomFactors (id) {
    // Populate the zoom factors drop-down
	var temp1= id+"_zfs";
	
    var zfs = document.getElementById( temp1 );
    while( zfs.options.length > 0 )
    zfs.removeChild( zfs.options[0] );

    var factors = mapviewer.setAllowedZoomFactors (8, 18);
	// var factors = mapviewer.getAvailableZoomFactors();
    var zf = mapviewer.getZoomFactor();
     
	
    for( var i = 0; i < factors.length; ++i ) {
        var option = document.createElement( 'option' );

        option.value = factors[i];
        option.appendChild( document.createTextNode(factors[i]));
        if (factors[i] == zf) {
            option.selected = true;
        }
        zfs.appendChild( option );
    }
    mapviewer.addEventHandler ( 'changeZoom', onChangeZoom );
	//return false;
}

function onChangeZoom ( type, target, old_zoom, new_zoom ) {
     // Make sure the drop-down updates when the zoom factor is changed
    var zfs = document.getElementById( 'zfs' );
    for( var i = 0, l = zfs.options.length; i < l; ++i ) {

        if( zfs.options[i].value == new_zoom ) {
        zfs.selectedIndex = i;
        }
    } 
}

/**
 *	Function called when user selected a particular zoom factor.
 **/
function setZoomFactor(zf) {
	mapviewer.setZoomFactor(Number(zf));
	return false;	
}

/**
 *	Function to display accommodation marker on the Multimap.
 **/
function addMarker(accommodationLatitude, accommodationLongitude) {
	mapviewer.removeAllOverlays(); 
    
    // For customized resort icon
    var icon = new MMIcon('/images/browse/mmicon-accommodation.png');
    icon.iconSize = new MMDimensions(19, 25);
    icon.iconAnchor = new MMPoint(16, 16);
    
    var marker = mapviewer.createMarker( new MMLocation(new MMLatLon(accommodationLatitude, accommodationLongitude)), {'label': 'Accommodation marker', 'icon': icon,'inert':true} );
    var el = document.createElement('h6');
    el.appendChild (document.createTextNode ('Accommodation marker'));
    marker.setInfoBoxContent(el);
}

/**
 *	Function to check bounds for fixing the Multimap.
 **/
function checkBounds() {
    var mapcentre = mapviewer.getCurrentPosition();
    var out = false;
    var sw = bounds.getSouthWest();
    var ne = bounds.getNorthEast();
    if (mapcentre.lon > ne.lon)     {
        out = true;
        mapcentre.lon = ne.lon;
    } else if(mapcentre.lon < sw.lon)  {
        out = true;
        mapcentre.lon = sw.lon;
    }
    if(mapcentre.lat > ne.lat) {
        out = true;
        mapcentre.lat = ne.lat;
    } else if(mapcentre.lat < sw.lat) {
        out = true;
        mapcentre.lat = sw.lat;
    }
    if(out) {
        if(checkingBounds) {
        // Don't get stuck in a recursive loop
        } else {
            var smoothPan = mapviewer.getOption('smoothpan');
            mapviewer.setOption('smoothpan', false);
            checkingBounds = true;
            mapviewer.goToPosition(mapcentre);
            checkingBounds = false;
            mapviewer.setOption('smoothpan', smoothPan);
        }
    }
}
