var MINZF=1;
var MAXZF=18;
var CZF=2;
var DZF=5;
var RZF=8;
var AZF=12;
var MDZF=8;
var DMG="Destinations";
var RMG="Resorts";
var AMG="Accommodation";
var MH=19;
var MW=25;
var mv;
var pzw;
var lw;
var mtw;

var widgetContainer;
var localInfoWidget;
var localInfoDisplayed = false;

var enableResortMarkerKeyIcon;
var enableAccommMarkerKeyIcon;

var resMarker;
var accommMarker;
var resortZoomInLink;
var accommZoomInLink;

var di;
var ri;
var ai;
var em;

var infoBox;
var infoBoxSections;

var infoBoxOverlay;

var mapDispalyText;
var markerSearcher;
var selectAccommDiv;
var rfs = ['client_id', 'repository_id','name', 'url','video_source','type','image_url','street', 'town','type','pc', 'lat', 'lon'];

var xmlreqs = new Array();
var clickedMarker;

var currentlySelectedTabId='tab1';
var selectedId = '';


function loadMap(){
	if(MMIsSupportedBrowser()){
	
		mv = MMFactory.createViewer( document.getElementById('mapviewer'), MM_WORLD_MAP );
		
		mv.goToPosition( new MMLatLon( 42.3508, -71.0717 ) );
		
		mv.resize(650,500);
		mv.setAllowedZoomFactors(MINZF, MAXZF);
		
		createMarkerIcons();
		addExtraControls();
		addEventHandlers();
		
		var pan_zoom_widget = new MMPanZoomWidget ();
		mv.addWidget ( pan_zoom_widget);
		var mtw=new MMMapTypeWidget();
	    mv.addWidget(mtw);
				
		var 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';
		
		var position = new MMBox();
		localInfoWidget = new MMLocalInfoWidget (datasources,'Show me',position);
		widgetContainer = document.getElementById('widgetcontainer');
		localInfoWidget.setContainer(widgetContainer);
		
		mv.addWidget(localInfoWidget);
		mv.setOption('mousewheel:wheelup',"zoomin");
		mv.setOption('mousewheel:wheeldown',"zoomout");
		reverseDataPrefs(MM_WORLD_MAP);

				
		rmo=false;
		em=true;
		ed=false;
		enableResortMarkerKeyIcon = false;
		enableAccommMarkerKeyIcon = false;

		resMarker = document.getElementById('resmarker');
		accommMarker= document.getElementById('accommmarker');
		resortZoomInLink= document.getElementById('resortZoomInLink');
		accommZoomInLink= document.getElementById('accommZoomInLink');

		mapDispalyText='Click the "declutter" tick-box to ensure you see all markers in a destination, as some that are very close together may be hidden behind one another.<br/><br/>Use the arrow buttons on the map to move around and see more   resorts in the destination you are looking at, or click the plus and minus buttons to zoom in and out.';
		mv.setMappingAPI( 'virtualearth' );
		
		currentlySelectedTabId='tab1';
		
		mv.setZoomFactor(CZF);
		
		//createMarker();
		
	} else{
		container=document.getElementById ('mapviewer');
		container.innerHTML='<div class="error">Sorry! Your browser is not supported by the Multimap API. Please use the drop down box to select your Destination.</div>';
	}
}



function addExtraControls(){
	var controlsElement=document.getElementById('extraControls');
	var innerHTML='<div id="destinationkey">'
	+ '<ul><li id="first"><strong>Marker key</strong>:</li>'
	+ '<li id="desmarker">Destination</li>'
	+ '<li id="resmarker">Resort/Cities</li>'
	+ '<li id="accommmarker">Accommodations</li>'	
	+ '</ul>'
	+ '<div id="desInfo">Please <a id="accommZoomInLink" href="#" onclick="mv.setZoomFactor(DZF);">zoom in for resort</a> information </div>'
	+ '<div id="resInfo">Please <a id="resortZoomInLink" href="#" onclick="mv.setZoomFactor(RZF);">zoom in for accommodation</a> information</div>'
	+ '<div id="accommInfo">Please note that not all of our properties can be seen on the map just yet but these are being added daily and will be completed very soon.</div>'
	+'<div class="clear"></div></div>';
	controlsElement.innerHTML=innerHTML;
}

function createMarkerIcons(){
	di=new MMIcon('/images/browse/mmicon-destination.png');
	di.iconSize=new MMDimensions(MH,MH);
	di.iconAnchor=new MMPoint(9.5,19);
	di.groupName  = DMG;
	ri=new MMIcon('/images/browse/mmicon-resort.png');
	ri.iconSize=new MMDimensions(MH,MH);
	ri.iconAnchor=new MMPoint(9.5,19);
	ri.groupName =RMG;
	ai=new MMIcon('/images/browse/mmicon-accommodation.png');
	ai.iconSize=new MMDimensions(MH,MW);
	ai.iconAnchor=new MMPoint(9.5,25);
	ai.groupName =AMG;
	

}

function  addEventHandlers(){
	mv.addEventHandler('click', onClick);
	mv.addEventHandler ('changeZoom', onChangeZoom);
	mv.addEventHandler ('endPan', endPan);
	mv.addEventHandler ('endMouseDrag', endMouseDrag);
}

/* EVENT HANDLER functions */
function onClick(type,target,position){
	
	
	//If an accommodation marker is clicked then display its info box
	if(target instanceof MMMarkerOverlay){
		clickedMarker = target;		
		
		
		getInfoBox(target.getAttribute('itemType'),target.getAttribute('repositoryId'));
		
	}
					
}
function onChangeZoom (type,target,oldZoom,newZoom ) {
	mv.removeAllOverlays();


	if(newZoom >= 13){
		if(!localInfoDisplayed){
			widgetContainer.style.height = '100px';
			widgetContainer.style.display = 'block';
			localInfoDisplayed = true;
		}
	}
	else{
		if(localInfoDisplayed){
			widgetContainer.style.height = '10px';			
			widgetContainer.style.display = 'none';
			localInfoDisplayed = false;
		}
	
	}

	//Colour the icon for resorts on the map key if at the correct level
	if(newZoom >= DZF && !enableResortMarkerKeyIcon){
	
		document.getElementById('desInfo').style.visibility = 'hidden';
		resMarker.style.background = 'transparent url(/images/browse/resort_marker.gif) no-repeat scroll 0%';
		enableResortMarkerKeyIcon = true;
	}
	//Grey out the resort key icon if you go beklow resort level and add link
	else if(newZoom < DZF && enableResortMarkerKeyIcon){
		document.getElementById('desInfo').style.visibility= 'visible';
		resMarker.style.background = 'transparent url(/images/browse/res-grey.gif) no-repeat scroll 0%';
		enableResortMarkerKeyIcon = false;	
	}
	
	//Colour the icon for accommodations on the map key if at the correct level
	if(newZoom >= RZF && !enableAccommMarkerKeyIcon){
		document.getElementById('resInfo').style.visibility = 'hidden';
		accommMarker.style.background = 'transparent url(/images/browse/accommodation_marker.gif) no-repeat scroll 0%';	
		enableAccommMarkerKeyIcon = true;
	}	
	//Grey out the accommodation key icon if you go below accommodation level and add link
	else if(newZoom < RZF && enableAccommMarkerKeyIcon){
		document.getElementById('resInfo').style.visibility  = 'visible';
		accommMarker.style.background = 'transparent url(/images/browse/accomm-grey.gif) no-repeat scroll 0%';
		enableAccommMarkerKeyIcon = false;	
	}
	queryForMarkers();
	
	
}

function endPan(type,target,startPos,endPos,reson){
	queryForMarkers();
}

function endMouseDrag(type,target,distance){
	queryForMarkers();
}

function createMarker(){

	var geocode = new MMLatLon(37.11616,-1.83947);
	mv.setZoomFactor(AZF);
	mv.goToPosition(geocode);
	
	clickedMarker=mv.createMarker( geocode, {'label': 'test', 'icon':ai} );	
	getSearchFormHtml();	
	
}
/* End of EVENT HANDLER functions */

function moveMap(zf,type,id){
	//Populate the accommodation drop downs if necessary
	selectedId = id;
	
	mv.setZoomFactor(Number(zf));
	positionToSelectedItem(type,id);
	//mv.goToPosition(new MMLatLon(Number(lat),Number(lon)));
	



}

function positionToSelectedItem(itemType,repositoryId){
	markerSearcher = new MMSearchRequester( positionOnMap );
    
    var search = new MMSearch(); 
    
    search.return_fields = rfs;
    
    search.count=10000;
    

    search.filters = new Array();
    
	search.filters.push(new MMSearchFilter('type', 'equals', itemType));
	search.filters.push(new MMSearchFilter('repository_id', 'equals', repositoryId));
	
	
   	markerSearcher.search( search );

}

function positionOnMap(){

	// If an error code has been produced, display the explanation:
	if ( markerSearcher.error_code ) {
		
		var err = '';
		if ( markerSearcher.error_explanation ) {
		    err =  markerSearcher.error_explanation;
		} else {
		    err =  'Your request failed. Error code: ' + markerSearcher.error_code;
		}
		
	    var errorDiv = document.getElementById('errorMessage');
	    errorDiv.innerHTML=err;
		return;
	}

	if(!markerSearcher.record_sets){
		return;
	}
	
	// Loop through each record set
	for ( var count=0, l = markerSearcher.record_sets.length; count < l; count++ ) {

		// If an error was returned for the record set, display details and return:
		if ( markerSearcher.record_sets[count].error ) {
		    err =  '';
		    if ( markerSearcher.record_sets[count].error.error_explanation ) {
			err =  markerSearcher.record_sets[count].error.error_explanation;
		    } else {
			err =  'Your request failed. Error code: ' + markerSearcher.record_sets[count].error.error_code;
		    }
		    errorDiv = document.getElementById('errorMessage');
		    errorDiv.innerHTML=err;
		    return;  
		}
		// If we have a record then position it on the map
		if ( markerSearcher.record_sets[count].records ) {

		    var record = markerSearcher.record_sets[count].records[0];
		    mv.goToPosition(record.point);
		    
		    queryForMarkers();  
		}
		
	}	
	
	//getAccommsForResorts();


}

function zoomToLevel(lat,lon,zoomFactor){
	mv.setZoomFactor(zoomFactor);
	mv.goToPosition(new MMLatLon(lat,lon));
	mv.setMappingAPI( 'virtualearth' );
}




/*SEARCHING FUNCTIONS*/
function queryForMarkers(){
	//mv.removeAllOverlays();
	
	/*if(infoBoxOverlay != null){
			mv.addOverlay(infoBoxOverlay);
			infoBoxOverlay = null;
	}*/
   
    markerSearcher = MMFactory.createSearchRequester( displayResults );
    
    var search = new MMSearch(); 
    
    search.return_fields = rfs;
    
    search.count=10000;
    
    
    
    var bounds = mv.getMapBounds();
    
    var centerPoint = mv.getMapBounds().getCenter();
    search.point = centerPoint;
    search.bounding_box = mv.getMapBounds();
    
    //We want to search for markers arounds the center point. This however only will return all markers whether a record is a destination.
    //resort or accommodation therefore filters need to be applied so the correct records are returned, e.g.- destinations for destination level
    search.filters = new Array();
    var zf=mv.getZoomFactor();
        
    
    var findMarkers = false;
	
	if(zf>= CZF && zf <= 4){
		search.filters.push(new MMSearchFilter('type', 'equals', 'destination'));
		findMarkers = true;
	} 
	
	if(zf >= DZF && zf <= 7){
		search.filters.push(new MMSearchFilter('type', 'equals', 'resort'));
		
		//search.filters.push(new MMSearchFilter('parent_item_repository_id', 'equals', itemId));	
		findMarkers = true;
			
	}
	
	if(zf >= RZF && zf <= 17){
		search.filters.push(new MMSearchFilter('type', 'equals', 'accommodation'));	
		//search.filters.push(new MMSearchFilter('parent_item_repository_id', 'equals', itemId));	
	    findMarkers = true;
	}
	
	
	if(!findMarkers){
		return;
	}
	
    markerSearcher.search( search );
}



function displayResults(){
	

	// If an error code has been produced, display the explanation:
	if ( markerSearcher.error_code ) {
		var err =  '';
		if ( markerSearcher.error_explanation ) {
		    err =  markerSearcher.error_explanation;
		} else {
		    err =  'Your request failed. Error code: ' + markerSearcher.error_code;
		    
		}
	    var errorDiv = document.getElementById('errorMessage');
	    errorDiv.innerHTML=err;
		return;
	}

	if(!markerSearcher.record_sets){
		return;
	}
	
	var zf = mv.getZoomFactor();

	var iconType;
	if(zf>= CZF && zf <= 4){
		iconType= di;
	} 
	
	if(zf >= DZF && zf <= 7){
		iconType= ri;
	}
	
	if(zf >= RZF && zf <= 17){
		iconType= ai;
	}

	// Loop through each record set:
	for ( var count=0, l = markerSearcher.record_sets.length; count < l; count++ ) {
		
		//mv.removeAllOverlays();

		// If an error was returned for the record set, display details and return:
		if ( markerSearcher.record_sets[count].error ) {
		    var err =  '';
		    if ( markerSearcher.record_sets[count].error.error_explanation ) {
			err =  markerSearcher.record_sets[count].error.error_explanation;
		    } else {
			err =  'Your request failed. Error code: ' + markerSearcher.record_sets[count].error.error_code;
		    }
		    var errorDiv = document.getElementById('errorMessage');
		    errorDiv.innerHTML=err;
		    return;  
		}
		// If not, check to see if individual records have been returned:
		if ( markerSearcher.record_sets[count].records ) {
		
		    // Loop through each record in the record set, and add it to the list below the map,
		    //  and populate the infobox text: 
		           
		    for (var record_count = 0, rl = markerSearcher.record_sets[count].records.length; record_count < rl; record_count++ ) {
				var record = markerSearcher.record_sets[count].records[record_count]; 
				
				var marker=mv.createMarker( record.point, {'label': record['name'], 'icon':iconType} );	 
				marker.setAttribute('repositoryId',record['repository_id']);
				marker.setAttribute('itemType',record['type']);  
				
				if(zf == AZF  && record['repository_id'] == selectedId){
							clickedMarker = marker;		
							getInfoBox(marker.getAttribute('itemType'),marker.getAttribute('repositoryId'));
								selectedId='';
				
				}	
				//if at azf and rep id = selected accomm id
				//call function to generate info box ciontent for the accommodation
				
				
		    }
		}
	}
	
}

function getAccommsForResorts(){

	//Get the repostiroy id of the selected resort
	
	var selectResortDiv = document.getElementById('resortSelection2');
	if(!selectResortDiv){
		selectResortDiv = document.getElementById('resortSelection');
		if(!selectResortDiv){
			return;
		}
	}

	
	
	//Get the accomm drop down
	selectAccommDiv = document.getElementById('accommodationSelection');	
	
	if(!selectAccommDiv){
		return;
	}
	
	var selectValues = selectResortDiv.options[selectResortDiv.selectedIndex + 1].value.split('*');	
	
	var parentRepositoryId = selectValues[1];
	

    markerSearcher = new MMSearchRequester( populateAccommDropDowns );
    
    var search = new MMSearch(); 
    
    search.return_fields = rfs;
    
    search.count=10000;
    
    search.filters = new Array();
    

	search.filters.push(new MMSearchFilter('type', 'equals', 'accommodation'));
	search.filters.push(new MMSearchFilter('parent_item_repository_id', 'equals', parentRepositoryId));	
	
	
   	markerSearcher.search( search );

}

function populateAccommDropDowns(){

	// If an error code has been produced, display the explanation:
	if ( markerSearcher.error_code ) {
		
		var err = '';
		if ( markerSearcher.error_explanation ) {
		    err =  markerSearcher.error_explanation;
		} else {
		    err =  'Your request failed. Error code: ' + markerSearcher.error_code;
		}
		
	    var errorDiv = document.getElementById('errorMessage');
	    errorDiv.innerHTML=err;
		return;
	}

	if(!markerSearcher.record_sets){	
		return;
	}
	
	// Loop through each record set
	for ( var count=0, l = markerSearcher.record_sets.length; count < l; count++ ) {

		// If an error was returned for the record set, display details and return:
		if ( markerSearcher.record_sets[count].error ) {
		    err =  '';
		    if ( markerSearcher.record_sets[count].error.error_explanation ) {
			err =  markerSearcher.record_sets[count].error.error_explanation;
		    } else {
			err =  'Your request failed. Error code: ' + markerSearcher.record_sets[count].error.error_code;
		    }
		    errorDiv = document.getElementById('errorMessage');
		    errorDiv.innerHTML=err;
		    return;  
		}
		// If not, check to see if individual records have been returned:
		if ( markerSearcher.record_sets[count].records ) {
		    // Loop through each record in the record set, and add it to the list below the map,
		    //  and populate the infobox text: 
		    for (var record_count = 0, rl = markerSearcher.record_sets[count].records.length; record_count < rl; record_count++ ) {
				
				var record = markerSearcher.record_sets[count].records[record_count]; 				
 					selectAccommDiv.options.add(new Option(record['name'],'accommodation*' + record['repository_id'] + '*' + AZF ));		
		    }
		    queryForMarkers();
		    
		}
		
	}	


}

/* End of SEARCHING FUNCTIONS */

function getInfoBox(itemType, repositoryId){
	var zf = mv.getZoomFactor();
	var requestStr = 'itemType=' + itemType + '&repositoryId=' + repositoryId + '&zf=' + zf;
	
	
		xmlreqPOST('/mapSearch/fragments/mapSearch/infoBox.jsp',requestStr);


}

function submitSearchForm(form){
	//Pass the values entered on the form to the form handler
	

	var input = document.getElementById('formHandlerDepartureGroup');
	input.value = document.getElementById('departureAirport').value;

	input = document.getElementById('formHandlerDepartureDate');
	input.value = document.getElementById('departureDate').value;

	input = document.getElementById('formHandlerDurationGroup');
	input.value = document.getElementById('inputnights').value;


	input = document.getElementById('formHandlerBoardBasis');
	input.value = document.getElementById('inputboard').value;
	
	input = document.getElementById('formHandlerBudget');
	input.value = document.getElementById('inputbudget').value;


	input = document.getElementById('formHandlerInputchildren');
	input.value = document.getElementById('inputchildren').value;

	input = document.getElementById('formHandlerInputadults');
	input.value = document.getElementById('inputadults').value;

	input = document.getElementById('formHandlerAccommCode');
	input.value = document.getElementById('accommCode').value;
	
	
	document.getElementById('mapBasedSearchSubmitButton').click();
	//form.submit();
}



//AJAX functions

function CXMLReq(freed) { 
	this.freed = freed; 
	this.xmlhttp = false; 
	if (window.XMLHttpRequest) { 
		this.xmlhttp = new XMLHttpRequest(); 
	} else if (window.ActiveXObject) { 
		this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
	} 
} 

function xmlreqPOST(url, data) { 
	
	var pos = -1; 
	for (var i=0; i<xmlreqs.length; i++) { 
		if (xmlreqs[i].freed == 1) { 
			pos = i; 
			break; 
		} 
	} 
	
	if (pos == -1) { 
		pos = xmlreqs.length; 
		xmlreqs[pos] = new CXMLReq(1); 
	} 
	if (xmlreqs[pos].xmlhttp) { 
		xmlreqs[pos].freed = 0; 
		xmlreqs[pos].xmlhttp.open("POST",url,true); 
		xmlreqs[pos].xmlhttp.onreadystatechange = function() { 
				if (typeof(xmlhttpChange) != 'undefined') { 
					xmlhttpChange(pos); 
				} 
			} 
		xmlreqs[pos].xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
		xmlreqs[pos].xmlhttp.send(data); 
	} 
} 

function xmlhttpChange(pos) { 
	if (typeof(xmlreqs[pos]) != 'undefined' && xmlreqs[pos].freed == 0 && xmlreqs[pos].xmlhttp.readyState == 4) { 
		if (xmlreqs[pos].xmlhttp.status == 200 || xmlreqs[pos].xmlhttp.status == 304) { 
			handleResponse(xmlreqs[pos].xmlhttp.responseText);
		} else { 
			handle_error(xmlreqs[pos].xmlhttp.responseText); 
		} 
		xmlreqs[pos].freed = 1; 
	} 
}

function handleResponse(responseText) {

	var html =  responseText;
	
	infoBoxOverlay = clickedMarker.openInfoBox( html,{'min_width':50,className:'altinfobox'} ); 

}

function changeInfoBoxContent(tabName){
	clickedMarker.setInfoBoxContent(infoBoxSections[tabName],{'min_width':50,className:'altinfobox'});
	
}


function handle_error(responseText) {



	//document.getElementById('errorMessage').innerHTML='An AJAX Error occurred';	
	
	    
}

//Function that executes when the user makes a choice from the from down boxes. So when the page refreshes
//it wil know what the user chose
function updatePage(selectDivId){
	//Submit selected value to the invisible form
	 var form = document.getElementById('selectedItemForm');
	 var select = document.getElementById(selectDivId);
	 
	 
	 var selectValues = select.value.split('*');	 

	 form.selectedItemType.value = selectValues[0];
	 form.selectedItemId.value = selectValues[1];
	 
	 form.zf.value = selectValues[2];
	 
	 
	 form.itemSelected.value = 'true';
	 	 
	 form.submit();
}


function shortlistItem(itemType, repositoryId){
	
	var form = document.getElementById('shortlistForm');

	form.zoomFactor.value=mv.getZoomFactor();
	
	var currentPosition=mv.getMapBounds().getCenter();
	form.centrePositionLatitude.value=currentPosition.lat;
	form.centrePositionLongitude.value=currentPosition.lon;
	
	form.usingMap.value=true;
	
	form.sfhItemType.value = itemType;
	form.sfhRepositoryId.value = repositoryId;

	form.shortlistSubmitButton.value=repositoryId + '*' + itemType + '*' + window.location.pathname;
	form.shortlistSubmitButton.click();
}

function setSelectedItem(divName,index){

	var selectBox = document.getElementById(divName);
	selectBox.selectedIndex = Number(index);
	
}

function changeDestResortTabView(tab) {	
   /*var tabArr = new Array(5);
   tabArr[0]="tab1";
   tabArr[1]="tab2";
   tabArr[2]="tab3";
   tabArr[3]="tab4";
   tabArr[4]="tab5";
		   	
   for (i=0; i<tabArr.length; i++){
	   var current = document.getElementById(tabArr[i]);
	   var currentTab = document.getElementById(tabArr[i] + 'content');
	   if(tab == tabArr[i]){
		   current.className = tabArr[i] + ' selected';
		   currentTab.style.display = 'block';
	   }else{
				current.className = tabArr[i];
				currentTab.style.display = 'none';
	   }
   }*/
   
   var tabArr = new Array(2);
   tabArr[0]="tab1";
   tabArr[1]="tab2"; 
   
   var tabSections = new Array(2);
    tabSections[0]="Overview";
   tabSections[1]="Photos & Videos"; 
   
   var classEnding = '';
   
   if(tab == 'tab1'){
     classEnding = ' first';
   }
     
    else if (tab == 'tab2'){
    	classEnding = ' last';
    }
   
   for (i=0; i<tabArr.length; i++){
   	   var current = document.getElementById(tabArr[i]);
		var currentTab = document.getElementById(tabArr[i] + 'content');

		if(tab == tabArr[i]){
			current.className='selected' + classEnding;
			currentTab.style.display = 'block';	
			current.innerHTML = tabSections[i];
			
   
   		}else{
   		
			current.className='';
			//Make tab clickable
			current.innerHTML = '<a href="javascript:changeDestResortTabView(\'' + tabArr[i] + '\')">' + tabSections[i] + '</a>';
			currentTab.style.display = 'none';	   		
   		
   		
   		}
   	}
   

					   	
}

function changeAccommTabView(tab){
   var tabArr = new Array(5);
   tabArr[0]="tab1";
   tabArr[1]="tab2"; 
   tabArr[2]="tab3"; 
   tabArr[3]="tab4"; 
   tabArr[4]="tab5"; 
   
   
   var tabSections = new Array(5);
    tabSections[0]="Overview";
   tabSections[1]="Photos & Videos"; 
   tabSections[2]="Facilities";    
   tabSections[3]="Reviews"; 
   tabSections[4]="Check Availability"; 
   
   var selectedStyles = ['selected first', 'selected photovid','selected','selected','checkavi selected last'];
   var unSelectedStyles = ['first', 'photovid','','','checkavi last'];

   for (i=0; i<tabArr.length; i++){
	   var current = document.getElementById(tabArr[i]);
	   var currentTab = document.getElementById(tabArr[i] + 'content');
	   
	   if(current){
		   if(tab == tabArr[i]){
			   current.className = selectedStyles[i];
			   currentTab.style.display = 'block';
		   }else{
					current.className = unSelectedStyles[i];
					
					current.innerHTML = '<a href="javascript:changeAccommTabView(\'' + tabArr[i] + '\')">' + tabSections[i] + '</a>';
	
					currentTab.style.display = 'none';
		   }
	   }
   }

}

function reverseDataPrefs(maptype) {
    var prefs = MMDataResolver.getDataPreferences( maptype);
    var newprefs = [];
    // Reverse order:
    for( var i = prefs.length - 1 ; i >= 0; --i ) {
        newprefs.push(prefs[i]);
    }
    // Re-draw map:
    MMDataResolver.setDataPreferences( maptype, newprefs ); 
    mv.redrawMap();
}     

