
function Grid(div){this.div=div;this.tolerance=0;this.views=[];this.currentView;this.sortCol=0;this.sortDesc=false;}
Grid.prototype.setMapApp=function(mapApp){this.mapApp=mapApp;}
Grid.prototype.setTolerance=function(tolerance){this.tolerance=tolerance;}
Grid.prototype.createView=function(viewName){var view=new GridView(viewName);if(!isDefined(this.currentView)){this.currentView=view;}
this.views.push(view);return view;}
Grid.prototype.sortFunction=function(a,b){var aVal=a._sortVal;var bVal=b._sortVal;var ret=0;if(aVal=="-")aVal=-100;if(bVal=="-")bVal=-100;if(isNaN(aVal)||isNaN(bVal)){if(bVal>aVal)ret=1;if(bVal<aVal)ret=-1;}else{ret=aVal-bVal;}
return-ret;}
Grid.prototype.sortFunctionRev=function(a,b){var aVal=a._sortVal;var bVal=b._sortVal;var ret=0;if(isNaN(aVal)||isNaN(bVal)){if(bVal>aVal)ret=1;if(bVal<aVal)ret=-1;}else{ret=aVal-bVal;}
return ret;}
Grid.prototype.evaluateAsValue=function(view,column,marker){var value="-1";try{value=view.functions[column](marker);if(value=="-")value=-1;var fval=parseFloat(value);if(!isNaN(fval)){return fval;}}catch(e){}
return value;}
Grid.prototype.handleColumnClick=function(column){if(this.sortCol==column){this.sortDesc=!this.sortDesc;}else{this.sortCol=column;this.sortDesc=false;this.resetSortCache();}
this.update();}
Grid.prototype.evaluateAsText=function(view,column,marker){var value="-";try{value=view.functions[column](marker);}catch(e){}
return value;}
Grid.prototype.resetSortCache=function(){for(v in this.mapApp.markers){var marker=this.mapApp.markers[v];marker._sortVal=undefined;}}
Grid.prototype.update=function(){if(logEnabled)GLog.write('updating grid: ');var htmlText="<table id='gridTable'>";var cView=this.currentView;if(!isDefined(this.noHeader)||!this.noHeader){htmlText+="<tr>";for(var i=0;i<cView.columns.length;i++){var arrow="";if(i==this.sortCol){arrow=this.sortDesc?"<img src='/images/arrowUpBlack.gif' />":"<img src='/images/arrowDownBlack.gif' />";}
htmlText=htmlText+"<th onclick='mapApp.handleGridColumnClick("+i+")'>"+arrow+cView.columns[i]+"</th>";}
htmlText+="</tr>";}
var markersInView=[];var totalMarkers=0;var useFilter=isDefined(cView.filter);for(v in this.mapApp.markers){if(this.mapApp.map.getBounds().contains(this.mapApp.markers[v].point)){var marker=this.mapApp.markers[v];if(!useFilter||cView.filter(marker)){if(!isDefined(marker._sortVal)){marker._sortVal=this.evaluateAsValue(this.currentView,this.sortCol,marker);}
markersInView.push(marker);}}
totalMarkers++;}
if(markersInView.length<3&&this.mapApp.autoZoomIn==true&&totalMarkers>8){this.mapApp.autoZoomIn=false;this.mapApp.map.zoomOut();this.update();return;}
if(logEnabled)GLog.write('found markers in view: '+markersInView.length+' / '+totalMarkers);if(this.sortDesc){markersInView.sort(this.sortFunctionRev);}else{markersInView.sort(this.sortFunction);}
if(logEnabled)GLog.write('done with sort: ');for(var i=0;i<markersInView.length;i++){var marker=markersInView[i];var cols=cView.functions;var shadeRow=(this.shadeAlternatingRows&&i%2==1)?" class='shadedRow' ":"";var popupLinkText=this.noPopupOnClick?"":"onclick='mapApp.openInfoWindow(\""+marker.id+"\");'";var onclickText="style='cursor:pointer;' "+popupLinkText+" onmouseover='mapApp.rollover(\""+marker.id+"\")' onmouseout='mapApp.rollout(\""+marker.id+"\")' ";htmlText+=this.hoverAllColumns?"<tr "+shadeRow+onclickText+">":"<tr "+shadeRow+">";for(var col=0;col<cols.length;col++){var value=this.evaluateAsText(cView,col,marker);var onclick="";if(col==0&&!this.hoverAllColumns){onclick=onclickText;}
var firstGridCol=(cView.columns[col]=="Name"||col==0)?" class='firstGridCol' ":"";htmlText=htmlText+"<td "+firstGridCol+onclick+">"+value+"</td>";}
htmlText=htmlText+"</tr>";}
if(markersInView.length==0){htmlText=htmlText+"<td>There are no points in the map view.<p>Use the slider on the left side of the map to zoom out [-] and view more.</p></td>"}
if(markersInView.length<=3)
{this.mapApp.updateStatusDiv("");}
htmlText=htmlText+"</table>";this.div.innerHTML=htmlText;}
function GridView(viewName){this.viewName=viewName;this.columns=[];this.functions=[];}
GridView.prototype.addColumn=function(colName,fnct){this.columns.push(colName);this.functions.push(fnct);}
GridView.prototype.setFilter=function(fnct){this.filter=fnct;}