/**
 * @author mpeccini
 */

//declare vars
var gDetailMap = new Array();
var gOverviewMap;
var namesListArray = new Array();
var projectSites = new Array();
var projectLocInfo = new Array();
var projectDescriptions = new Array();
var projectLat = new Array();
var projectLong = new Array();
var projectSiteCenter = new Array();
var projectSiteZoom = new Array();
var detailMarkersArray = new Array();
var ovZoomLevel;
var ovCenter;
var ovLocArrow = new Array();
var spotLight;
var winPanel;
var nav;
var tempBounds = new Array();
var tmpSW = new Array();
var tmpNE = new Array();


function BuildDetailMapWindow(prjidx){
gDetailMap  = [];
gOverviewMap;
namesListArray  = [];
projectSites  = [];
projectLocInfo = [];
projectLat = [];
projectLong = [];
projectSiteCenter = [];
projectSiteZoom  = [];
detailMarkersArray = [];
ovZoomLevel;
ovCenter;
ovLocArrow = [];
	
	//alert(prjidx)
	switch (prjidx){
	case 23:
		//Rhode Island river project variables
		ovZoomLevel = 8;
		ovCenter = new G.LatLng(41.725206,-71.496277);
		ovLocArrow[0] = new G.LatLng(41.8389,-71.3687);
		projectSites[0]= "Ten Mile River Sites";
		tmpSW[0] = new G.LatLng(41.8271,-71.3687)
		tmpNE[0] = new G.LatLng(41.8389,-71.3419)
		tempBounds[0] = new GLatLngBounds(tmpSW[0],tmpNE[0])
		projectSiteCenter[0] = tempBounds[0].getCenter();
		projectLocInfo[0] = "Omega Pond Dam|41.8389|-71.3687;Hunts Mill Dam|41.8271|-71.3457;Turner Reservoir Dam|41.8332|-71.3419";
		projectDescriptions[0] = "A denil style fish ladder will be installed to restore access for migratory fish species.|A denil style fish ladder will be installed to restore access for migratory fish species.|A denil style fish ladder will be installed to restore access for migratory fish species.";
		//--------------------------
		ovLocArrow[1] = new G.LatLng(41.4482,-71.6445);	
		projectSites[1]= "Pawcatuck River Sites";
		tmpSW[1] = new G.LatLng(41.4458,-71.6445)
		tmpNE[1] = new G.LatLng(41.4482,-71.6257)
		tempBounds[1] = new GLatLngBounds(tmpSW[1],tmpNE[1])
		projectSiteCenter[1] = tempBounds[1].getCenter();
		projectLocInfo[1]="Lower Shannock Dam|41.4482|-71.6445;Horseshoe Falls Dam|41.4475|-71.6365;Kenyon Mill Dam|41.4458|-71.6257";
	  	projectDescriptions[1] = "The dam is being removed to restore riverine function and access for migratory fish species.|A denil style fish ladder will be installed to restore access for migratory fish species.|Full or partial dam removal will be implemented to improve riverine function and access for migratory fish species.";
	  break;
	case 27:
		//New Haven and East Lyme Marsh Restoration
		ovZoomLevel = 7;
		ovCenter = new G.LatLng(41.533254,-72.696533);
		ovLocArrow[0] = new G.LatLng(41.3008,-72.2375);
		projectSites[0]= "Bride Brook Site";
		projectSiteCenter[0] = new G.LatLng(41.3008,-72.2375);
		projectSiteZoom[0] = 17;
		projectLocInfo[0] = "Bride Brook|41.3008|-72.2375";
		projectDescriptions[0] = "Bride Brook culvert will be replaced";
		//--------------------------	
		ovLocArrow[1] = new G.LatLng(41.297,-72.9507);
		projectSites[1]= "West River Site";
		projectSiteCenter[1] = new G.LatLng(41.297,-72.9507 );
		projectSiteZoom[1] = 17;
		projectLocInfo[1]="West River|41.297|-72.9507";
		projectDescriptions[1] = "The West River tide gates will be replaced";
	  
	  break;
	case 9:
		//New Haven and East Lyme Marsh Restoration
		ovZoomLevel = 7;
		ovCenter = new G.LatLng(29.117647,-80.702533);
		ovLocArrow[0] = new G.LatLng(29.417647,-81.102533);
		projectSites[0]= "North Peninsula Location";
		projectSiteCenter[0] = new G.LatLng(29.417647,-81.102533);
		projectSiteZoom[0] = 15;
		projectLocInfo[0] = "North Peninsula Location|29.417647|-81.102533";
		projectDescriptions[0] = "Dredge spoil and impoundment dikes will be removed, restoring natural tidal flow.";
		//--------------------------	
		ovLocArrow[1] = new G.LatLng(28.583014,-80.671062);
		projectSites[1]= "Merritt Island National Wildlife Refuge location";
		projectSiteCenter[1] = new G.LatLng(28.583014,-80.671062 );
		projectSiteZoom[1] = 15;
		projectLocInfo[1]="Merritt Island location|28.583014|-80.671062";
		projectDescriptions[1] = "Dredge spoil and impoundment dikes will be removed, restoring natural tidal flow.";
	  
	  break;
	case 111:
	  
	  break;
	}//end switch
	
	//process variables and build map window
	//build window with tabs
	detailmap(prjidx);
	
	//this builds the initially active tab window
	//much of the messy code on this page is due to the fact that the activate event
	//doesn't work correctly on the initial load this is needed to make sure that the initial load works correctly

	document.getElementById(projectSites[0]).style.height="100%"
	document.getElementById(projectSites[0]).style.width="100%"
	//expands the side panel if it is closed
	gDetailMap[0] = new G.Map2(document.getElementById(projectSites[0]))
	if(tempBounds[0]){
		gDetailMap[0].setCenter(projectSiteCenter[0], gDetailMap[0].getBoundsZoomLevel(tempBounds[0]));
	}else{
		gDetailMap[0].setCenter(projectSiteCenter[0], projectSiteZoom[0]);
	}	
	gDetailMap[0].enableScrollWheelZoom();
	gDetailMap[0].enableDoubleClickZoom();
	gDetailMap[0].setMapType(G_HYBRID_MAP);
	var scaleBarPos = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(30,10)); 
	var controlPosition = new GControlPosition(G_ANCHOR_TOP_RIGHT, new G.Size(2,2))
	var zoomControl = new GSmallZoomControl3D();
	gDetailMap[0].addControl(zoomControl,controlPosition);
	//gMap.addControl(new GSmallZoomControl3D,controlPosition)
	gDetailMap[0].addControl(new GScaleControl(),scaleBarPos);
	
	      // var omc = new GOverviewMapControl();
        //omc.setMapType(G_SATELLITE_MAP);
        //gDetailMap[0].addControl(omc);

	//GEvent.addListener(gDetailMap[0],"click",function(){alert('click')}); 
	//var getOverMap = overviewMap.getOverviewMap()
	//setTimeout("getOverMap.setZoom(1);",1); 
	loadProjectPointLocs(0);

}
//this builds additional tab windows upon activation

function handleActivate(tab,idx){
//alert(gDetailMap[idx])
	if (!gDetailMap[idx]){
		document.getElementById(tab.id).style.height="100%"
		document.getElementById(tab.id).style.width="100%"
		gDetailMap[idx] = new G.Map2(document.getElementById(tab.id))
		if(tempBounds[idx]){
			gDetailMap[idx].setCenter(projectSiteCenter[idx], gDetailMap[idx].getBoundsZoomLevel(tempBounds[idx]));
		}else{
			gDetailMap[idx].setCenter(projectSiteCenter[idx],projectSiteZoom[idx] );
		}	
		gDetailMap[idx].enableScrollWheelZoom();
		gDetailMap[idx].enableDoubleClickZoom();
		gDetailMap[idx].setMapType(G_HYBRID_MAP);
		var scaleBarPos = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(30,10)); 
		var controlPosition = new GControlPosition(G_ANCHOR_TOP_RIGHT, new G.Size(2,2))
		var zoomControl = new GSmallZoomControl3D();
		gDetailMap[idx].addControl(zoomControl,controlPosition);
		//gMap.addControl(new GSmallZoomControl3D,controlPosition)
		gDetailMap[idx].addControl(new GScaleControl(),scaleBarPos);	
	}
	loadProjectPointLocs(idx);
}


function loadProjectPointLocs(idx){
	//alert(namesListArray[idx])
	var latArray = new Array();
	var longArray = new Array();
	var nameArray = new Array();
	var tempArray = new Array();
	
	var tempSplit = projectLocInfo[idx].split(";")
	for (i=0;i<tempSplit.length;i++){
		var tempSplitA = tempSplit[i].split("|");
		nameArray[i]= tempSplitA[0];
		latArray[i] = tempSplitA[1];
		longArray[i] = tempSplitA[2];
	}
	namesListArray[idx]=nameArray;
	for (i=0;i<nameArray.length;i++){
		//alert(nameArray)
		if (nameArray.length==1){
			greenSQ.image = "images/green_marker_blank.png";
		}else{
			greenSQ.image = "images/smallSQGreenIcons/marker"+(i+1)+".png";
		}
		var tmpCoords = new GLatLng(latArray[i],longArray[i]);
		tempArray[i] = new GMarker(tmpCoords, {icon:greenSQ});
		eval('GEvent.addListener(tempArray["'+i+'"],"click", function(){detailPopup("'+i+'","'+idx+'")})')
		//eval('GEvent.addListener(smMarkersArray['+i+'],"mouseover", function(){projcetSmPopUp("'+i+'")})'); //end eval
		//eval('GEvent.addListener(smMarkersArray['+i+'],"mouseout", function(){closeSmPopUp()})'); //end eval
		//eval('GEvent.addListener(smMarkersArray['+i+'],"infowindowclose", function(){disableMouseOver = false})'); //end eval
		gDetailMap[idx].addOverlay(tempArray[i]);
	}
		detailMarkersArray[idx]=tempArray
		
	loadPanelList(idx)
	buildOvMap(idx);
	//nameArray = "";
}

function loadPanelList(idx){
	// this if is needed to prevent this from running the first time the window loads
	if (namesListArray.length>0){
		var listContent='';
			listContent+='<p class="detailList">'
		for (var i=0; i<namesListArray[idx].length; i++) {
			if (namesListArray[idx].length==1){
				listContent += '<a class="infoLink_detailMap" href="javascript:detailPopup('+i+','+idx+')"><img src="images/green_marker_blank.png">&nbsp;'+namesListArray[idx][i]+'</a><br>';
			}else{
				listContent += '<a class="infoLink_detailMap" href="javascript:detailPopup('+i+','+idx+')"><img src="images/smallSQGreenIcons/marker'+(i+1)+'.png">&nbsp;'+namesListArray[idx][i]+'</a><br>';
			}
		};
			listContent+='</p>'
		document.getElementById("panelContent").innerHTML=listContent
		buildOvMap(idx);
			
	}
	
}

function detailPopup(i,idx){
	var singleDescription;
	var tempSplit = new Array();
	tempSplit = projectDescriptions[idx].split("|");
	singleDescription = tempSplit[i];
	var tempName;
	tempName = '<div class="infoNameDetail">'+namesListArray[idx][i]+'</div><br><div class="infoDescDetail">'+singleDescription+'</div>'
	detailMarkersArray[idx][i].openInfoWindowHtml(tempName)
}

function buildOvMap(idx){
	//alert("overview="+gOverviewMap)
	if (!gOverviewMap){
		document.getElementById("ovMap").style.height="200px"
		document.getElementById("ovMap").style.width="200px"
		gOverviewMap = new G.Map2(document.getElementById("ovMap"))
		gOverviewMap.setCenter(ovCenter, ovZoomLevel);
		//gDetailMap[0].enableScrollWheelZoom();
		//gDetailMap[0].enableDoubleClickZoom();
		gOverviewMap.setMapType(G_PHYSICAL_MAP);
		gOverviewMap.disableDragging(); 
	}
	gOverviewMap.clearOverlays();
	var locArrow = new GMarker(ovLocArrow[idx], {icon:ArrowIcon});
	gOverviewMap.addOverlay(locArrow);
	
if(!spotLight){
	spotLight = new Ext.Spotlight({
	    easing: 'easeOut',
	    duration: .5
	});

	var t=setTimeout("spotLight.show('detailMapPanel')",200);
}

}

function detailmap(idx){
//alert(projectSites.length)
        // tabs for the center
			
        var tabs = new Ext.TabPanel({
            region: 'center',
            margins:'3 3 3 0', 
            activeTab: 0,
            defaults:{autoScroll:true},
			//this adds the first site
            items:[{
               	title: projectSites[0],
				id: projectSites[0],
				listeners: {activate: function(){loadPanelList(0)}},
				autoShow:true
            }]
        });
	
	//this loops through all additional sites and adds them as tabs	
	for (i=1;i<projectSites.length;i++){
	var indx = i;
		tabs.add({
	        id:projectSites[i],
	        title:projectSites[i],
			listeners: {activate: function(){handleActivate(this,indx)}},
			autoShow:true
		});		
	}

        // Panel for the west



        nav = new Ext.Panel({
           title:'Location Overview',
           region: 'west',
           split: true,
		   stateful:false,
           width: 200,
			id: 'detailPanel',
			html: '',
            collapsible: true,
			collapsed: false,
			floatable:false,
			autoDestroy:true,
			autoShow:true,
            margins:'3 0 3 3',
            cmargins:'3 3 3 3',
			layout: 'anchor',
//**NOTE if the anchor elements are not included in the items IE will not even load this javascript file
               items: [{ 
					height: 200,
					id: 'ovMap',
					html:  '',
                    border:false,
                    autoScroll:true,
                    anchor: ''

                },{
                    title:'Restoration Sites',
					html:  '<div id="panelContent"></div>',
                    autoScroll:true,
                    border:false,
                    anchor: ''
                }]
        });

                     

        winPanel = new Ext.Window({
            title: '&nbsp;&nbsp;'+recName[idx]+' - detailed location maps',
            closable:true,
			draggable:false,
			resizable:false,
			id: 'detailMapPanel',
			iconCls:'detailWindow',
			close: panelClose,
			stateful:false,
            width:mapFrameWidth*0.8,
            height:mapFrameHeight*0.8,
            //border:false,
            plain:true,
            layout: 'border',

            items: [nav, tabs]
        });
		//alert(winPanel)
        winPanel.show();


}

function replaceSide(){
	alert("replace");
	nav.expand();
}

function panelClose(){
	
	spotLight.hide()
	winPanel.hide()
	spotLight='';
	
gDetailMap = new Array();
gOverviewMap = undefined;
namesListArray = new Array();
projectSites = new Array();
projectLocInfo = new Array();
projectLat = new Array();
projectLong = new Array();
projectSiteCenter = new Array();
projectSiteZoom = new Array();
detailMarkersArray = new Array();
tempBounds = new Array();
winPanel.destroy();	
nav.destroy();
}



