// $Id: maps.jsm 15015 2008-12-09 01:16:56Z efoster $
// map linking

var mapsLinked = false; // global that stores whether maps are linked or not
var map_count = 2;
function showDualMaps() {
   var update1, update2;
   if ($('map1_container').style.display != 'none') {
      update1 = true;
   } else { 
      update2 = true;
   }
   

   $('map1_container').show();
   $('map2_container').show();

   $('map1_container').style.width="50%";
   $('map2_container').style.width="50%";

   $('map_link_container').show();

    map1.dpmap.showMap();

    map2.dpmap.showMap();

    if (update1) {
      map2.dpmap.updateMap( map1.dpmap.getPlace(), map1.dpmap.getIndicator());
    } else {
      map1.dpmap.updateMap( map2.dpmap.getPlace(), map2.dpmap.getIndicator());
    }   
    
    showCloseMapButton( 1 );
    showCloseMapButton( 2 );

		
    resizeMapHeight();
    if (updatePageTitle) updatePageTitle();    

}


function showCloseMapButton( n ) {
   $('google' + n + '_close_link').show();
   $('google' + n + '_dual_link').hide();
}

function showDualMapButton( n ) {
   $('google' + n + '_close_link').hide();
   $('google' + n + '_dual_link').show();
}

function closeMap( number ) {
   $('map' + number + '_container').hide();

    var other_map = number == 1? 2 : 1;
  
    var width = "100%";

    showDualMapButton( 1 );
    showDualMapButton( 2 );

    $('map' + other_map + '_container').style.width = width;
//    $('map' + other_map).style.width = width;
	
    $('map_link_container').hide();
    resizeMapHeight();
    if (updatePageTitle) updatePageTitle();    
}
function toggleMapLink() {
  mapsLinked? unlinkMaps() : linkMaps();

}


function unlinkMaps() {
  mapsLinked = false;
  map1.dpmap.unlinkMap( map2);
  map2.dpmap.unlinkMap( map1);

}

function linkMaps() {
  mapsLinked = true;
  map2.dpmap.linkMap( map1);
  map1.dpmap.linkMap( map2);

}


function showMap(number, width) { 
    width = width? width : "100%";
    $('map' + number + '_container').show();
    $('map' + number + '_container').style.width= width;
    
    eval("map" + number + ".dpmap.map.checkResize()");
   
    if (updatePageTitle) updatePageTitle();
}

function mapLinkOver() { // mouseover link button
    if(!mapsLinked) { $('map_link').src = 'http://www.indiadevelopmentindicators.org/0113/static/images/map_unlinked_ov.gif'; }
    else { $('map_link').src = 'http://www.indiadevelopmentindicators.org/0113/static/images/map_linked_ov.gif'; }
}

function mapLinkOut() { // mouseout link button
    if(!mapsLinked) { $('map_link').src = 'http://www.indiadevelopmentindicators.org/0113/static/images/map_unlinked.gif'; }
    else { $('map_link').src = 'http://www.indiadevelopmentindicators.org/0113/static/images/map_linked.gif'; }
}


function pageURI( print, query_add ) {
    var query = $H();
    if($('map1_container').visible()) {
        query['mp1z'] = map1.dpmap.getZoomLevel();
        var coords = map1.dpmap.getCenterLatLng();
        query['mp1lat'] = coords.lat;
        query['mp1lng'] = coords.lng;
        query['mp1maptype'] = map1.dpmap.getMapTypeName();

        var ind = map1.dpmap.getIndicator();
        if(typeof(ind) != 'undefined') { 
            query['mp1cid'] = ind.id; 
            query['mp1theme'] = ind.getActiveColor();
            query['mp1breaks'] = ind.getActiveBreaks();
	    query['mp1thematic'] = ind.getActiveThematicBoundary();
        }

        if (map1.dpmap.place ) { 
            query['mp1pl'] = map1.dpmap.place.id;
        }else if (typeof(whereSelectValues) != 'undefined' &&
            typeof(whereSelectValues['map1']) != 'undefined') {
            query['mp1pl'] = whereSelectValues['map1']['key'];
        }
    }

    if($('map2_container').visible()) {
        var map = !$('map1_container').visible() ? 'mp1' : 'mp2';
        query[map + 'z'] = map2.dpmap.getZoomLevel();
        var coords = map2.dpmap.getCenterLatLng();
        query[map + 'lat'] = coords.lat;
        query[map + 'lng'] = coords.lng;
        query['mp2maptype'] = map2.dpmap.getMapTypeName();
        var ind = map2.dpmap.getIndicator();
        if(typeof(ind) != 'undefined') { 
            query[map + 'cid'] = ind.id; 
            query[map + 'theme'] = ind.getActiveColor();
            query[map + 'breaks'] = ind.getActiveBreaks();
	    query[map + 'thematic'] = ind.getActiveThematicBoundary();
        }
        if (map2.dpmap.place ) { 
            query['mp2pl'] = map2.dpmap.place.id;
        }else if (typeof(whereSelectValues) != 'undefined' &&
            typeof(whereSelectValues['map2']) != 'undefined') {
            query['mp2pl'] = whereSelectValues['map2']['key'];
        }
    }

    for(key in query_add) {
	query[key] = query_add[key];
    }

    var link = '';
    if( print ) { 
        query['print'] = 1;
        link = 'http://www.indiadevelopmentindicators.org/map/print?';
    }else {
        link = 'http://www.indiadevelopmentindicators.org/map?';
    }

    return  link + query.toQueryString();
}

