

function displayNews(dt,conflictId){
    var newsFrm=document.getElementById("nws");
    newsFrm.src="newsByConflict.php?conflictId="+conflictId+"&dt="+dt;
}
var map;

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;
}

function AjaxUrlFixer() {
    this.checkWhetherChanged(0);
}
var idx=1;
var curconf="";
var markersLoaded=false;

function checkWhetherChanged(){
    cf=window.top.location.hash.replace(/#/,"");    
    if(cf!=curconf && markersLoaded){	
    	try  {
        	loadConflictDetails(cf);
		curconf=cf;
	}catch(err){
		curconf="";
	}

    }
    setTimeout("checkWhetherChanged()", 200);

}

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

function load() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
	//map.enableGoogleBar();
        //map.setCenter(new GLatLng(-1.735574,29.201660), 3);
	map.setCenter(new GLatLng(24,45),3);
	map.addMapType(G_PHYSICAL_MAP);
	var ctrl=new GLargeMapControl();
	map.addControl(ctrl);
	ctrl=new GMapTypeControl();
	map.addControl(ctrl);
	ctrl=new GScaleControl();
	map.addControl(ctrl);
	FixBackAndBookmarking();
      }
    }

function createIcon(severity, newsVol, freshness){
      var myIcon = new GIcon(G_DEFAULT_ICON);
      var swSize=severity;
      var nwSize=newsVol;
      myIcon.image="../images/icons/"+swSize+"_"+nwSize+"_"+freshness+".png";	
      var w=width[swSize][nwSize];
      var h=height[swSize][nwSize];
      myIcon.iconSize=new GSize(w,h);
      var imageMap=[0,0, w,0, w,h, 0,h, 0,0];
      myIcon.imageMap=imageMap;
      myIcon.shadow="../images/icons/shadows/"+swSize+"_"+nwSize+"-shadow.png";	
      var shW=shWidth[swSize][nwSize];
      var shH=shHeight[swSize][nwSize];
      myIcon.shadowSize=new GSize(shW, shH);
      myIcon.iconAnchor = new GPoint(w/2,h);
      myIcon.infoWindowAnchor = new GPoint(w/2,h/2);
      return myIcon;
}

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

   function getXml(){
      
     GDownloadUrl("xml/getListXml.php", loadXml);

   }

   function loadDataForMarker(){
       loadConflictDetails(this.conflictId);
       this.tooltip.hide();
   }
 function showMaximizedTab(){
	var maxInfoUrl="conflictOnMap.php?conflictId="+this.conflictId+"&tabSel="+this.getSelectedTab();
	var maxContentDiv=this.maxContentDiv;
/*	GDownloadUrl(maxInfoUrl,function(data){
		maxContentDiv.innerHTML=data;
		});
*/
       maxContentDiv.innerHTML="<iframe height=400 class='maxInfoIF' src='"+maxInfoUrl+"'></iframe>";
   }

   function loadXml(xmlText){

     var myDom = GXml.parse(xmlText);
     var cNodes=myDom.getElementsByTagName("Conflict");
     for(var i=0;i<cNodes.length;i++){
       var node=cNodes.item(i);
       var latlng = new
   GLatLng(node.getAttribute("latitude"),node.getAttribute("longitude"));
       var severity=node.getAttribute("severity");
       var newsVol=node.getAttribute("newsVolume");
       var freshness=node.getAttribute("freshness");
       var myIcon = createIcon(severity,newsVol,freshness);
       var mrkrOptions=new Object();
       mrkrOptions.icon=myIcon;
       

       var mrkr = new GMarker(latlng, mrkrOptions);

       var cName=node.getElementsByTagName("name")[0].childNodes[0].nodeValue;
       // var tEl=document.createElement("span");
       //tEl.setAttribute("class","tooltip");
       //tEl.appendChild(document.createTextNode(cName));
       var ttip=new Tooltip(mrkr, cName,4);
       mrkr.tooltip=ttip;

       GEvent.addListener(mrkr, "click", loadDataForMarker);
       GEvent.addListener(mrkr,"mouseover", function(){ this.tooltip.show();});
       GEvent.addListener(mrkr,"mouseout", function() { this.tooltip.hide();})
      // GEvent.addListener(mrkr, "infowindowopen", function() {alert("hello");});
       map.addOverlay(mrkr);
       map.addOverlay(ttip);
       mrkr.conflictId=node.getAttribute("id");
       
       var conflictNames=node.getElementsByTagName("name");
       //       var wpUrl=node.getElementsByTagName("wikipediaUrl");
       var htabs=new Array();
     //  htabs[0]=new  GInfoWindowTab("Overview",conflictNames[0].firstChild.nodeValue+'<br><a target="_blank" href="'+wpUrl[0].firstChild.nodeValue+'">'+wpUrl[0].firstChild.nodeValue+'</a>');

      // htabs[1]=new GInfoWindowTab("News","");
      // htabs[2]=new GInfoWindowTab("Blogs","");
       conflictMarkerHtabs[mrkr.conflictId]=htabs;
       conflictMarkers[mrkr.conflictId]=mrkr;
       conflictNamesArr[mrkr.conflictId]=cName;
    //   mrkr.bindInfoWindowTabsHtml(htabs);
       
     }
	markersLoaded=true;
   } 
// wait for the data to be loaded, then run loadConflictDetails.
   function whenReadyLoadConflictDetails(conflictId){
       try{	  
       	if(markersLoaded()){
	   loadConflictDetails(conflictId);
	   }
       } catch(err) {
       }
   setTimeout("whenReadyLoadConflictDetails("+conflictId+");",400);

   }

   
function prepMini(conflictId){
    window.top.location.hash="#"+conflictId;
}
function prepMax(conflictId){
}

function showInMiniWindow(conflictId,confName,longName,desc, 
			  articlesLinkText){

    conflictMarkerHtabs[conflictId][0]="<span class=ticlass>"+confName+"</span><br><a href=conflict.php?conflictId="+conflictId+">In detail...</a><br><br>"+articlesLinkText+'<span class=reslnk></span>';
    /* conflictMarkerHtabs[conflictId][1]=new GInfoWindowTab("Overview","<span class=ticlass>"+confName+'</span>'+"<br><span class=summary>"+longName+"</span>"+"<br><div class=fulldet>"+desc+'</div> <br><a class=reslnk href="conflict.php?conflictId='+conflictId+'">More Info</a><br><img src="../images/space.gif" width=275 height=2> ');*/
var maxContentDiv=document.createElement('div');
	maxContentDiv.innerHTML='Loading...';
	conflictMarkers[conflictId].openInfoWindowHtml(conflictMarkerHtabs[conflictId][0]);
	//,{maxContent:maxContentDiv,maxTitle:confName});
	var iw=map.getInfoWindow();
	//GEvent.addListener(iw,"maximizeclick",showMaximizedTab);
	//iw.maxContentDiv=maxContentDiv;
	iw.conflictId=conflictId;
}
function showInMaxWindow(conflictId,confName,longName,desc,
			 articlesLinkText){
var hasInnerText =
(document.getElementsByTagName("body")[0].innerText != undefined) ? true : false;
if(hasInnerText){   
    document.getElementById("cfLongName").innerText=longName;
   document.getElementById("cfSummary").innerText=desc;
   //   document.getElementById("wpLink").href=wpUrl;
   document.getElementById("newsArticles").innerHTML=articlesLinkText;
} else {
    document.getElementById("cfLongName").textContent=longName;
   document.getElementById("cfSummary").textContent=desc;
   //   document.getElementById("wpLink").href=wpUrl;
   document.getElementById("newsArticles").innerHTML=articlesLinkText;

}


}
function parseXmlDetails(conflictId,xmlText, prepFunc, showFunc){
   if(prepFunc){
     prepFunc(conflictId);
   }

    curconf=conflictId;
 
    var myDom = GXml.parse(xmlText);
    var cNodes=myDom.getElementsByTagName("Conflict");
    var node=cNodes.item(0);
    var conflictNames=node.getElementsByTagName("name");
    //    var wpUrlN=node.getElementsByTagName("wikipediaUrl");
    var longNameNodes=node.getElementsByTagName("longName");
    var descNodes=node.getElementsByTagName("description");
    var longName="";
    if(longNameNodes.length>0){
	longName=longNameNodes[0].firstChild.nodeValue;
	}
    var desc="";
    if(descNodes.length>0){
	desc=descNodes[0].firstChild.nodeValue;
    }
    var confName=conflictNames[0].firstChild.nodeValue;
//    conflictMarkerHtabs[conflictId]=new Array();
//    var wpUrl=wpUrlN[0].firstChild.nodeValue;
    
    var articlesNode=node.getElementsByTagName("Articles").item(0);
    var articles=articlesNode.getElementsByTagName("Article");
    var articlesLinkText="<span class='artTitle'>";
    if(articles.length==0) articlesLinkText="<i>No recent news articles</i>";
    for(var i=0;i<articles.length;i++){
	var article=articles.item(i);
	var articleId=article.getAttribute("id");
	var articleSource=article.getElementsByTagName("source").item(0).firstChild.nodeValue;
      var articleTitle=article.getElementsByTagName("title").item(0).firstChild.nodeValue;
      var articleUrl=article.getElementsByTagName("url").item(0).firstChild.nodeValue;
	var pubDt=article.getElementsByTagName("pubDate").item(0).firstChild.nodeValue;
	//'<a href="javascript:flagLink('+articleId+','+conflictId+')"><img src="../images/icons/del.png" border=0 height=5 width=5></a>
	articlesLinkText=articlesLinkText+' <span class=artDt>'+pubDt+'</span> <a class=artTitle target="_blank" href="r.php?a='+articleId+'&c='+conflictId+'&u='+escape(articleUrl)+'">'+articleTitle+'</a> <span class=artSrc>'+articleSource+'</span> <br>';
    }
	articlesLinkText=articlesLinkText+'</span>';
	
	
	/* BLOGS COMMENTED OUT 
    var blogPostsNode=node.getElementsByTagName("BlogPosts").item(0);
    var posts=blogPostsNode.getElementsByTagName("Post");
    var blogLinksText="";
    if(posts.length==0) blogLinksText="<i>No recent blog posts</i>";
    for(var i=0;i<posts.length;i++){
      var blogPost=posts.item(i);
      var blogName=blogPost.getElementsByTagName("blog").item(0).firstChild.nodeValue;
      var postTitle=blogPost.getElementsByTagName("title").item(0).firstChild.nodeValue;
      var postUrl=blogPost.getElementsByTagName("url").item(0).firstChild.nodeValue;
      var pubDt=blogPost.getElementsByTagName("pubDate").item(0).firstChild.nodeValue;
      blogLinksText=blogLinksText+'<a href="javascript:flagLink('+articleId+','+conflictId+')"><img src="../images/icons/del.png" border=0 height=5 width=5></a> <span class=artDt>'+pubDt+'</span> <a class=artTitle target="_blank" href="'+postUrl+'">'+postTitle+'</a> <span class=artSrc>'+blogName+'</span> <br>';
      //alert("Post: "+postTitle);  
  }
    conflictMarkerHtabs[conflictId][2]=new
   GInfoWindowTab("Blogs",blogLinksText+'<span class=reslnk><br><a href="javascript:map.getInfoWindow().maximize()">More Posts</a></span>');
 END OF BLOG COMMENT */
    //alert("links: "+blogLinksText);
	showFunc(conflictId,confName,longName,desc,articlesLinkText);
}

	

   function loadConflictDetails(x){
       GDownloadUrl("xml/getConflictDetailsXml.php?conflictId="+x,function(y){parseXmlDetails(x,y,prepMini, showInMiniWindow);});
   }
   function loadConflictDetailsMax(x){
       GDownloadUrl("xml/getConflictDetailsXml.php?max=100&conflictId="+x,function(y){parseXmlDetails(x,y,prepMax, showInMaxWindow);});
   }

var geocoder = new GClientGeocoder();

function searchResultOperator(response){
//	alert(response.Status.code);
// borrowed from verveeath.com - thanks guys!
	if (response.Status.code == 200 && response.Placemark.length == 1) {
		place = response.Placemark[0];
		lat = place.Point.coordinates[1];
		lng = place.Point.coordinates[0];
		acuracy = place.AddressDetails.Accuracy==3?5:place.AddressDetails.Accuracy;
		zoom = 4 + acuracy;
		zoom = (zoom>5)?5:zoom;
		map.setCenter(new GLatLng(lat, lng), zoom);
	}
}

function searchLocation(userInput){
	geocoder.getLocations(userInput, searchResultOperator);
}

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

function centerOnConflict(conflictId, lat,lng){
	map.setCenter(new GLatLng(lat, lng));
	loadConflictDetails(conflictId);
}

