/*
	$Id: GEOMapUtil.js 42264 2008-09-29 16:41:53Z twoodwark $
	$URL: http://nvdweb1/svn/CodeDB/branches/stable/webroot/images/js/geo/GEOMapUtil.js $
	Description:
*/


var baseIconSingle=new GIcon();baseIconSingle.iconSize=new GSize(13,22);baseIconSingle.iconAnchor=new GPoint(6,22);baseIconSingle.infoWindowAnchor=new GPoint(6,7);baseIconSingle.shadow=geoMapMarkerGraphicsPath+"shadow_single.png";baseIconSingle.shadowSize=new GSize(33,22);var baseIconCombo=new GIcon();baseIconCombo.iconSize=new GSize(26,45);baseIconCombo.iconAnchor=new GPoint(12,45);baseIconCombo.infoWindowAnchor=new GPoint(13,13);baseIconCombo.shadow=geoMapMarkerGraphicsPath+"shadow_combo.png";baseIconCombo.shadowSize=new GSize(66,44);var baseIconCluster=new GIcon();baseIconCluster.iconSize=new GSize(44,44);baseIconCluster.iconAnchor=new GPoint(22,22);baseIconCluster.infoWindowAnchor=new GPoint(22,22);baseIconCluster.shadow=geoMapMarkerGraphicsPath+"";baseIconCluster.shadowSize=new GSize(0,0);var baseIconHidden=new GIcon();baseIconHidden.iconSize=new GSize(2,2);baseIconHidden.iconAnchor=new GPoint(1,1);baseIconHidden.infoWindowAnchor=new GPoint(1,1);baseIconHidden.image=geoMapMarkerGraphicsPath+"transparent.gif";baseIconHidden.shadow=geoMapMarkerGraphicsPath+"transparent.gif";baseIconHidden.shadowSize=new GSize(1,1);var MAX_COMBO_COUNT=25;var MAX_CLUSTER_COUNT=50;var MAX_CLUSTER_ARTICLES_SHOW=25;var geoXMLHTTP=null;var geoInfoXMLHTTP=null;var geoMarkerProcessorFunc=null;var infoWindowPoint=null;var infoWindowContentIds=null;var infoWindowContent=null;var infoWindowMarker=null;function infoWindowClose(){infoWindowPoint=null;infoWindowMarker=null;}
function objectSort(obj1,obj2){return((obj1.date>obj2.date)?-1:((obj1.date<obj2.date)?1:0));}
var objectListItems=null;function getSortedObjectInfoHTML(objects,marker){objectListItems=new Array();for(var i=0;i<objects.length;i++){objectListItems[objectListItems.length]={date:Date.parse(objects[i].match(/[a-zA-Z]{3} [0-9]{2} [0-9]{4}/)),value:objects[i]}}
objectListItems.sort(objectSort);var html='<div class="info-window">';if(objectListItems.length>1){html+='<h2>Multiple stories in this area</h2>';if(marker.objectData.type=='cluster'){html+='<div class="zoom-in"><a href="javascript:zoomAt(new GLatLng'+marker.getLatLng()+')">Zoom in</a> to see individual listings</div>';}
html+='<div class="fixed"><ul>';for(var i=0;i<objectListItems.length;i++){html+='<li>'+objectListItems[i].value+'</li>';}
html+='</ul></div>';}else{html+=objectListItems[0].value;}
html+='</div>';return html;}
var myObjects=null;function _ajaxGEOMapObjectInfoReturn(){if(geoInfoXMLHTTP.readyState!=4){}else{if(geoInfoXMLHTTP.status!=200){marker.openInfoWindowHtml('<div class="info-window">Error loading articles; please try again later.</div>');return;}
if(infoWindowPoint){var marker=infoWindowPoint.marker;myObjects=eval(geoInfoXMLHTTP.responseText);infoWindowContent=getSortedObjectInfoHTML(myObjects,marker);marker.openInfoWindowHtml(infoWindowContent);}}}
function getObjectIds(obj){var objIds='';for(var o=0;o<obj.length;o++){objIds+=o>0?',':'';objIds+=obj[o].id;}
return objIds;}
function loadObjectInfo(){var marker=infoWindowPoint.marker;var objectIds=getObjectIds(marker.objectData.objects);if(infoWindowContentIds==objectIds){if(infoWindowContent){marker.openInfoWindowHtml(infoWindowContent);}
return;}
geoInfoXMLHTTP=createXmlHttp();var url=ajaxControlerURL+'&event=getGeoMapObjectInfo'+'&object_id='+objectIds;infoWindowContentIds=objectIds;infoWindowContent=null;geoInfoXMLHTTP.onreadystatechange=_ajaxGEOMapObjectInfoReturn;geoInfoXMLHTTP.open('GET',url,true);geoInfoXMLHTTP.setRequestHeader('Content-Type','application/x-www-form-urlencoded');geoInfoXMLHTTP.setRequestHeader("charset","UTF-8");geoInfoXMLHTTP.send(null);}
function openMarkerInfoWindow(clickedPoint){var marker=clickedPoint.marker;var infoText='<div class="info-window">';infoWindowMarker=new GMarker(marker.objectData.location,{icon:new GIcon(baseIconHidden)});;if(marker.objectData.type=='single'){infoText+='<p>Loading please wait...</p>';}else if(marker.objectData.type=='combo'||marker.objectData.groupCount<=MAX_CLUSTER_ARTICLES_SHOW){infoText+='<h2>Multiple stories in this area</h2>'+
(marker.objectData.type=='cluster'?'<div class="zoom-in"><a href="javascript:zoomAt(new GLatLng'+marker.getLatLng()+')">Zoom in</a> to see individual listings</div>':'')+'<div class="fixed">'+'<p>Loading please wait...</p>'+'</div>';}else if(marker.objectData.type=='cluster'){infoText+='<h2>Multiple stories in this area</h2>'+'<div class="zoom-in"><a href="javascript:zoomAt(new GLatLng'+marker.getLatLng()+')">Zoom in</a> to see individual listings</div>';}
infoText+='</div>';marker.openInfoWindowHtml(infoText);infoWindowPoint=clickedPoint;if(marker.objectData.type!='cluster'||marker.objectData.groupCount<=MAX_CLUSTER_ARTICLES_SHOW){loadObjectInfo();}}
function createGEOMarker(objectData){var numberedIcon=null;var markerTitle='';if(objectData.type=='single'){numberedIcon=new GIcon(baseIconSingle);numberedIcon.image=geoMapMarkerGraphicsPath+"marker_single.png";markerTitle='Single story';}else if(objectData.type=='combo'){numberedIcon=new GIcon(baseIconCombo);numberedIcon.image=geoMapMarkerGraphicsPath+"marker_combo_"+(objectData.groupCount>MAX_COMBO_COUNT?MAX_COMBO_COUNT+'p':objectData.groupCount)+".png";markerTitle='Multiple stories in single location';}else{numberedIcon=new GIcon(baseIconCluster);numberedIcon.image=geoMapMarkerGraphicsPath+"marker_cluster_"+(objectData.groupCount>MAX_CLUSTER_COUNT?MAX_CLUSTER_COUNT+'p':objectData.groupCount)+".png";markerTitle='Multiple nearby stories';}
markerOptions={icon:numberedIcon,title:markerTitle};var marker=new GMarker(objectData.location,markerOptions);marker.objectData=objectData;objectData.location.marker=marker;GEvent.addListener(marker,"click",openMarkerInfoWindow);return marker;}
function markerPointInArray(point,filteredPoints){for(var p=0;p<filteredPoints.length;p++){if(point.lat()==filteredPoints[p].location.lat()&&point.lng()==filteredPoints[p].location.lng()){return p;}}
return-1;}
function duplicateMarker(m){return{location:new GLatLng(m.location.lat(),m.location.lng()),type:m.type,objects:[{id:m.objects[0].id}],compositeCount:m.compositeCount,title:m.title};}
var ajaxReturn='';var markerPoints=new Array();function _ajaxGEOMapMarkerReturn(){if(geoXMLHTTP.readyState!=4){_setMessage('Loading markers...');}else{if(geoXMLHTTP.status!=200){_setMessage('Error');return;}
try{ajaxReturn=geoXMLHTTP.responseText;markerPoints=eval(geoXMLHTTP.responseText);}catch(Ex){}
if(geoMarkerProcessorFunc){_setMessage('');geoMarkerProcessorFunc(markerPoints);}}}
function getObjectPoints(mapCenter,mapOrigin,callBackFunc){geoXMLHTTP=createXmlHttp();var url=ajaxControlerURL+'&event=getGeoMapMarkers'+'&c='+mapCenter.lat()+','+mapCenter.lng()+'&o='+mapOrigin.lat()+','+mapOrigin.lng();geoMarkerProcessorFunc=callBackFunc;geoXMLHTTP.onreadystatechange=_ajaxGEOMapMarkerReturn;geoXMLHTTP.open('GET',url,true);geoXMLHTTP.setRequestHeader('Content-Type','application/x-www-form-urlencoded');geoXMLHTTP.setRequestHeader("charset","UTF-8");geoXMLHTTP.send(null);}
function _setMessage(msg){var msgDiv=document.getElementById('map_status_div');var msgEl=document.getElementById('map_msg');msgEl.innerHTML=msg;msgDiv.style.display='none';msgDiv.style.display=msg?'':'none';}
