


var map;
var infowindow;

function resize(){
    mapDiv = document.getElementById("map");
    headerObj=document.getElementById("headTable");
    leftNavDiv=document.getElementById("leftnav");
    var windowHeight =  document.body.clientHeight;
    var headerHeight=headerObj.clientHeight+20;
    var windowWidth = document.body.clientWidth;
    var leftNavWidth=leftNavDiv.clientWidth+20;
    mapDiv.style.height=windowHeight-headerHeight;
    mapDiv.style.width=windowWidth-leftNavWidth;
}


var idx=1;
var currentUrlName="";
var markersLoaded=false;
var checkSynchroLocked=false;
function clearHashInUrl(){
    window.top.location.hash="";
    currentUrlName="";
}
function checkWhetherChanged(){
    if(!checkSynchroLocked){
	checkSnchroLocked=true;
	cf=window.top.location.hash.replace(/#/,"");    
	if(cf!=currentUrlName && markersLoaded){	
    	    try  {
        	loadConflictDetails(cf);
		currentUrlName=cf;
	    }catch(err){
		currentUrlName="";
	    }
	} 
	checkSynchroLocked=false;
    }
    setTimeout("checkWhetherChanged()", 200);

}

function load_map() {

    latlng=new google.maps.LatLng(24,45)
    var myOptions = {
	zoom: 3,
	center: latlng,
	mapTypeId: google.maps.MapTypeId.ROADMAP,
	//mapTypeControl: true,
	//scaleControl: true

    };

    map = new google.maps.Map(document.getElementById("map"), myOptions);
    infowindow = new google.maps.InfoWindow();
    google.maps.event.addListener(infowindow, "closeclick", clearHashInUrl);   

    stylizeMap();
   
    FixBackAndBookmarking();

} 
function stylizeMap(){
    var myStyle=[ { featureType: "poi.park", 
		    elementType: "all", 
		    stylers: [  { visibility: "off" } ] 
		  },
		  { featureType: "road",
		    elementType: "all",
		    stylers: [ { visibility: "simplified"}]
		  },
		  { featureType: "road",
		    elementType: "labels",
		    stylers: [ { visibility: "off"}]
		  },
		];
    
    var styledMapOptions = {
	map: map,
	name: "Simple"
    }
    var simpleMapType = new google.maps.StyledMapType(myStyle,styledMapOptions);
    map.mapTypes.set('simple', simpleMapType);
    map.setMapTypeId('simple');
}
function unload_map(){
}

function FixBackAndBookmarking() {
    if(!document.getElementById || !document.getElementsByTagName) return;
    checkWhetherChanged();
}

function createMarker(name, latlng, severity, freshness){
//    var myIcon = new GIcon(G_DEFAULT_ICON);      
    var sz=24;
    var icon = new google.maps.MarkerImage("/images/map/"+severity+"_"+freshness+".png",

      new google.maps.Size(sz, sz),
      // The origin for this image is 0,0.
   new google.maps.Point(0,0),
     //active spot
      new google.maps.Point(sz/2, sz/2));

    var shape={coord:[0,0, sz,0, sz,sz, 0,sz, 0,0],
	       type: 'poly'};

    var marker = new google.maps.Marker({
        position: latlng,
        map: map,
        icon: icon,
        shape: shape,
        title: name
    });

    return marker;
}

   var conflictMarkerHTML=new Array();
   var conflictMarkers=new Array();
var conflictNamesArr=new Array();

function getXml(){    
    new jQuery.ajax("conflicts/index.xml", {
        success: function(y){loadXml(y);},
	type: "get",
	dataType: "xml"
    });
}

function loadDataForMarker(){
    show(this.urlName);

}

function loadXml(myDom){
    
    var cNodes=myDom.getElementsByTagName("Conflict");

    for(var i=0;i<cNodes.length;i++){
	var node=cNodes.item(i);
	var latlng = new google.maps.LatLng(node.getAttribute("latitude"),node.getAttribute("longitude"));
	var severity=node.getAttribute("severity");
	//var newsVol=node.getAttribute("newsVolume");
	var freshness=node.getAttribute("freshness");
	
	var cName=node.getElementsByTagName("name")[0].childNodes[0].nodeValue;	
	var mrkr = createMarker(cName, latlng, severity, freshness);
	mrkr.conflictId=node.getAttribute("id");
	mrkr.urlName=node.getAttribute("urlName");
	conflictMarkers[mrkr.urlName]=mrkr;
	conflictNamesArr[mrkr.urlName]=cName;

	google.maps.event.addListener(mrkr, "click", loadDataForMarker);


//	mrkr.setMap(map);
//       var conflictNames=node.getElementsByTagName("name");
       
     }

    markersLoaded=true;
   } 
// wait for the data to be loaded, then run loadConflictDetails.
/*function whenReadyLoadConflictDetails(urlName){
    if(markersLoaded){	   
	loadConflictDetails(urlName);
    }
    setTimeout("whenReadyLoadConflictDetails("+urlName+");",400);
}*/

   
function show(urlName){
    window.top.location.hash="#"+urlName;
    // this just keeps us from waiting around.
    checkWhetherChanged();
}


function showInMiniWindow(urlName,miniHTML){    
    conflictMarkerHTML[urlName]=miniHTML;
    infowindow.content=miniHTML;
    infowindow.pixelOffset=new google.maps.Size(0,10);
    infowindow.open(map,conflictMarkers[urlName]);
    //conflictMarkers[urlName].openInfoWindowHtml(conflictMarkerHTML[urlName]);
    var iw=infowindow; //map.getInfoWindow();
    iw.urlName=urlName;

}	

function loadConflictDetails(x){

    new jQuery.ajax('/mini/'+x, {
        success: function(responseText){showInMiniWindow(x,responseText);},
	type: "get",
	dataType: "html"					
    });
}
  

function flagLink(articleId, conflictId){
	alert("not doing anything now: article: "+articleId+", conflict: "+conflictId);
}


