﻿var gmarkers=[];

var Lats=new Array();
var Lngs=new Array();
var ys=new Array(); //for fiscal year, color_code
//following five fields show up in pop-up window
var ids=new Array();
var moneys=new Array(); //10 years total capital
var names=new Array(); //project name
var projaddrs=new Array();//project address
var userdepts=new Array();// user department
//Sys.Application.add_load(ApplicationLoadHandler);
function ApplicationLoadHandler()
 { 
  Sys.WebForms.PageRequestManager.getInstance().add_endRequest(drawMarkers);
 //   Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(resetURL);
   
}

function drawMarkers(){
//alert('from client');
   map.clearOverlays();
   // ===== Start with an empty GLatLngBounds object =====     
   var bounds = new GLatLngBounds();
   var count=Lats.length;
//   alert(count);
   if(count>0){
       for(i=0;i<count;i++){    
            var point =new GLatLng(Lats[i], Lngs[i]);
            addMarker(point,i);
            bounds.extend(point);
       }   
       // ===== determine the zoom level from the bounds =====
       map.setZoom(map.getBoundsZoomLevel(bounds));
       // ===== determine the centre from the bounds ======
       map.setCenter(bounds.getCenter());
   }
}

function addMarker(point,i) {       
    var marker ;
    if(ids[i].length>1){
        marker=new GMarker(point, mIcon);
    }
    else{
        var t=maxYear-ys[i];
        marker=new GMarker(point, yearColor[t]);
    }
         
    gmarkers[i]=marker;
    map.addOverlay(marker);
    //this is a simple info window with NO tab
 //   GEvent.addListener(marker, "click", function() {
//		marker.openInfoWindowHtml('<div class="infowindow">'+moneys[i]+'</div>');
//	});  
    //two tab, one for info, one for direction
      GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowTabsHtml([getFirtstTab(i), getSecondTab(i)]);
        });
        
}

function getFirtstTab(i){
    
    var html1='<div class="infowindow">';
    if (ids[i].length>1)
    {
        html1+='<b>multiple projects on this location.</b><br>';
    }
    for(j=0;j<ids[i].length;j++){
        html1 = html1+ '<a href="javascript:openDetailWindow(\''+ids[i][j]+'\')">'+names[i][j]+'</a><br>'+projaddrs[i][j];
        html1 = html1+ '<br>User Department:'+userdepts[i][j];
        html1 = html1+ '<br>Total Budget: '+moneys[i][j];
        html1 = html1+ '<br><a href="javascript:openDetailWindow(\''+ids[i][j]+'\')">more info...</a><br/><br/>';
    }
    html1= html1+ '</div>';
    return new GInfoWindowTab('Info',html1);
}
function getSecondTab(i){
    var html2= '<form id="formDirection'+i+'" action="http://maps.google.com/maps" method="get" target="_blank">' +
            '<input type="text" name="saddr" id="saddr" width="240px">' +
            '<br>' +
			'<input type="radio" name="radDirections" id="radToHere" checked>To the Project Location' +
			'<br>' +
			'<input type="radio" name="radDirections" id="radFromHere">From the Project Location' +
			'<input type="hidden" name="daddr" id="daddr" />'+
			'<br>'+
			'<input type="button" name="btnGetDirections" id="btnGetDirections" value="Get Directions" onClick="getDrivingDirection(' + i + ')">'+ 
			'</form>';           
    return new GInfoWindowTab('Direction',html2) ;
}

function getDrivingDirection(i){
    var frmDirection=document.getElementById('formDirection'+i);
    if (frmDirection.radToHere.checked)
	{
	    frmDirection.daddr.value = projaddrs[i];
	    frmDirection.submit();
    }else
    {
        frmDirection.daddr.value = frmDirection.saddr.value;
        frmDirection.saddr.value = projaddrs[i];
        frmDirection.submit();
        frmDirection.saddr.value=frmDirection.daddr.value;
    } 
       
}
function openPopup(lat,lng){
   var i=0;
   for(var i=0;i<Lats.length;i++){
      if(Lats[i]==lat && Lngs[i]==lng){
        map.setCenter(new GLatLng(lat,lng),16);
       gmarkers[i].openInfoWindowTabsHtml([getFirtstTab(i), getSecondTab(i)]);
    //    gmarkers[i].openInfoWindowHtml("ddd");
        break;
      }
   }
}

var detailWindow;
function openDetailWindow(id){
    //window.open('detail.aspx?id='+id,'detail window for project: '+id,'width=400,height=200');
    var left=window.self.screenLeft;
    var top=window.self.screenTop;
    if(detailWindow!=null){
        if(detailWindow.closed){
            detailWindow.close();
        }
    }
    detailWindow=window.open('detail.aspx?id='+id,'detailwindow','left='+left+',top='+top+',width=700,height=500,resizable=1,scrollbars=1');
    detailWindow.focus();
}

function openWindow(url){
    //window.open('detail.aspx?id='+id,'detail window for project: '+id,'width=400,height=200');
   // var left=window.self.screenLeft;
   // var top=window.self.screenTop;
    var newWindow=window.open(url);
    newWindow.focus();
}

window.onresize=onResize;

function onResize(){

  window.resizeEnd = (window.resizeEnd ==null)?(new Object()):window.resizeEnd;
  clearTimeout(window.resizeEnd);
  window.resizeEnd = setTimeout(onResizeEnd,1);
}

var minWidth=500;
var minHeight=400;
function onResizeEnd () {

	var maplayer=document.getElementById("map");
//	.offsetParent;
	var width=maplayer.offsetParent.offsetWidth-maplayer.offsetLeft-220;
	var height=maplayer.offsetParent.offsetHeight-maplayer.offsetTop-60;
	
	newWidth=Math.max(minWidth,width);
	newHeight=Math.max(minHeight,height);	
	
	maplayer.style.width=newWidth +"px";
	maplayer.style.height=newHeight +"px";
	
	map.checkResize();
	
	
}

function printResult()
{
  
	map.checkResize();
 //   
    var divP=document.getElementById("printableResult");
    
    divP.innerText=fs.length;
    window.print();

}

var helpwindow;
function openHelpWindow(myevent,name) 
{
    var top=myevent.screenY+10;
    var left=myevent.screenX+10;
   // alert(myevent.clientX+", "+myevent.clientY);
    helpwindow = window.open("help"+name+".htm","helpwindow","width=500,height=300,scrollbars=1,left="+left+",top="+top);
    helpwindow.focus();
}

function closeHelpWindow()
{
    helpwindow.close();
}