﻿//
var currDemoSiteId = "";
var currDemoSiteX = 0;
var currDemoSiteY = 0;
var currDemoSiteDemoRadius = 0;
var currDemoSiteGraphic = null;
var theResizePropConnect;
var theZoomPopUpConnect;
var demoSiteBusPoints = "";
var theBusinessConnect;
var tabstripDemographicsDetails;

function clickDemographicsTab()
{
   theResizePropConnect = dojo.connect(jsMap,"onResize",function(){
		    blnResizing = false;
		    dojo.disconnect(theResizePropConnect);
		    window.setTimeout('gotoCurrentDemographics();',10);
		    });
   if(mapisvisible)
    {
    	restorePanes();
    }
   else
   {
       restorePanes();
       RepositionMap();
       ResizeMap();
   }
   selectRadTab('Map');
}

function resizeDemographicsControl()
{
    //alert('here');
    var arrSize;
	arrSize = ResizeInPane('paneLeftBody', 'bbusDemographicsDetails_demoContent', 475, 200, 0, 0);
	arrSize = ResizeInPane('paneLeftBody', 'bbusDemographicsDetails_businessContent', 475, 200, 0, 0);
	arrSize = ResizeInPane('paneLeftBody', 'sitedivBAOReport', 475, 200, 0, 0);
	arrSize = ResizeInPane('paneLeftBody', 'divDemographicsDetails', 475, 200, 0, 0);
	
}


function OnDemographicsTabCreated(sender,eventArgs)
{
    tabstripDemographicsDetails = sender;
    tabstripDemographicsDetails.OnClientTabSelected = OnDemographicsTabSelectedHandler;
}

function OnDemographicsTabSelectedHandler(sender, eventArgs)
{
    resizeDemographicsControl();
    if (eventArgs.Tab.Value.length > 0)
	{
		eval(eventArgs.Tab.Value);
	}
}

function getDemographicsTab(strTabId)
{
	var tab = tabstripDemographicsDetails.FindTabById("bbusDemographicsDetails_tabstripDemographicsDetails_"+strTabId);
	return tab;
}
function unselectDemographicsTab(strTabId)
{
	var tab = getDemographicsTab(strTabId);
	if (tab)
	{
		tab.UnSelect();
	}
}
function selectDemographicsTab(strTabId)
{
	var tab = getDemographicsTab(strTabId);
	if (tab)
	{
		tab.UnSelect();
		tab.Select();
	}
}

function clickDemoTab()
{
    turnOffMapGraphics();
    var units = document.getElementById('bbusSearchForm_siteUnits').value;
    var radius = document.getElementById('bbusDemographicsDetails_buffradius').value;
    if (units=="ENGLISH") units="miles";
    else if (units=="METRIC") units="km";
    else units=null;
    if(currDemoSiteGraphic) 
    {
        bufferPoint(currDemoSiteGraphic,radius,units);
        var propname = document.getElementById("bbusDemographicsDetails_demoPropName").value;
        GACTTrackEvent("Site Analysis","Demographics","Site - "+propname);
    }
    else
        GACTTrackEvent("Site Analysis","Demographics","Block Group");
    
}
function clickBusTab()
{
    turnOffMapGraphics();
    var units = document.getElementById('bbusSearchForm_siteUnits').value;
    var radius = document.getElementById('bbusDemographicsDetails_buffRadiusBusiness').value;
    if (units=="ENGLISH") units="miles";
    else if (units=="METRIC") units="km";
    else units=null;
    
    theBusinessConnect = dojo.connect(jsMap,"onExtentChange",function(){
        showLayer("BusyIndicator");
        window.setTimeout('plotDemoSiteBusinesses(demoSiteBusPoints)',10);
        dojo.disconnect(theBusinessConnect);
        });
        
    if(currDemoSiteGraphic) 
        bufferPoint(currDemoSiteGraphic,radius,units);
    
    var propname = document.getElementById("bbusDemographicsDetails_demoPropName").value;
    GACTTrackEvent("Site Analysis","Businesses","Site - "+propname);
}
function setDemoBuffer(newBuffer,newindex)
{
    if(newBuffer == -1) return;
    var radius = document.getElementById('bbusDemographicsDetails_buffradius');
    var radiusmenu = document.getElementById('bbusDemographicsDetails_buffradiusmenu');
    if(radius) radius.value = newBuffer;
    if(radiusmenu) radiusmenu.selectedIndex = newindex;
        
}    
function setSiteName(newName)
{
    if(newName == -1) return;
    var propName = document.getElementById('bbusDemographicsDetails_demoPropName');
    if(propName) propName.value = newName;
}    
function plotDemoSiteBusinesses(points)
{
    if (points.length > 0)
    {
        pArray = points.split(";;;;");
        arraylength = pArray.length;
        var pt_graphic;
        var pt_symbol;
        var pt_point;
        var nullLine = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_NULL,new dojo.Color([255,255,255,1]),1);
        
        var i;
        for(i in pArray)
        {
            pointsArray = pArray[i].split("||||");
            pt_point = new esri.geometry.Point(pointsArray[0], pointsArray[1], jsSR);
            pt_symbol = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE, 6, nullLine, new dojo.Color(pointsArray[2]));
            pt_graphic = new esri.Graphic(pt_point,pt_symbol);
            jsMap.graphics.add(pt_graphic);
        }
    }
    hideLayer("BusyIndicator");
}

function setSiteBusPoints(points)
{
    demoSiteBusPoints = points;
    //alert(points);
}

function gotoCurrentDemographics()
{
    jsMap.infoWindow.hide();   
    if(getDemographicsTab('Demographics'))
    {    
        unselectDemographicsTab('Demographics'); 
        selectDemographicsTab("Demographics");
    }
    else 
    {
        unselectDemographicsTab('BAO'); 
        selectDemographicsTab("BAO");
    }
    //zoomToPoint(currDemoSiteX,currDemoSiteY);
}

function gotoDemographicsDetails(strDemographicsId, x, y)
{
    
    currDemoSiteId = strDemographicsId;
	currDemoSiteX = x;
	currDemoSiteY = y;
	var radius = document.getElementById('bbusDemographicsDetails_buffradius');
    if(radius) currDemoSiteDemoRadius = radius.value
	currDemoSiteGraphic = getGraphicById(currDemoSiteId);
    
    setSiteName(currInfoWindowTitle);
    //alert('here');
	// load Demographics Details
	if (strDemographicsId != null)
	{
	    __doPostBack('bbusDemographicsDetails$updateDemographicsDetails', 'Event=updateSiteDemographicsDetails&strDemographicsId=&xVal='+ x +'&yVal='+ y);
        GACTTrack("Demographic Details");
    }
	// go to Demographics Details page
	hideRadTab('Details');
	showRadTab('DemoDetails');
	unselectRadTab('DemoDetails');
	selectRadTab('DemoDetails');
    clearBAO('site');
}

function gotoDemographicsByBlockGroup(strDemographicsId, x, y)
{
    
	currDemoSiteId = strDemographicsId;
	currDemoSiteX = x;
	currDemoSiteY = y;
	var radius = document.getElementById('bbusDemographicsDetails_buffradius');
    if(radius) currDemoSiteDemoRadius = radius.value
	currDemoSiteGraphic = null;
    //alert('here');
	// load Demographics Details
	if (strDemographicsId != null)
	{
	    __doPostBack('bbusDemographicsDetails$updateDemographicsDetails', 'Event=updateSiteDemographicsDetails&strDemographicsId='+strDemographicsId+'&xVal='+ x +'&yVal='+ y);
        GACTTrack("Demographic Details");
    }
	// go to Demographics Details page
	hideRadTab('Details');
	showRadTab('DemoDetails');
	unselectRadTab('DemoDetails');
	selectRadTab('DemoDetails');
    clearBAO('site');
}

function selectedDemographics()
{
    var idlist = "";
    for (var i=0, il=selResults.length; i<il; i++) {
        idlist = (idlist.length>0)? idlist+",":idlist;
        idlist = idlist + selResults[i].attributes.id;
    }
    
    gotoDemographicsByBlockGroup(idlist,0,0);
}

function generateDemographicProfile()
{
    clickDemoTab();
    __doPostBack("bbusDemographicsDetails$updateDemoProfile", "Event=PrepareSiteDemographics");
    GACTTrack("Demographic Details");
    var propname = document.getElementById("bbusDemographicsDetails_demoPropName").value;
    GACTTrackEvent("Site Analysis","Demographics","Site - "+propname);
}

function performDemoBusinessAnalysis()
{
    //clickBusTab();
    __doPostBack("bbusDemographicsDetails$updateBusinessAnalysis", "Event=PrepareSiteBusinessProfile");
    GACTTrack("Demographic Details");
    var propname = document.getElementById("bbusDemographicsDetails_demoPropName").value;
    GACTTrackEvent("Site Analysis","Businesses","Site - "+propname);
}

function showSiteBusAnalysis(strType,strDESC,strNAICS,strPrevSPEC)
{
    var strPostBack
    if(strType!='MAPBUSINESS')
        strPostBack = 'Event=updateSiteBusinessAnalysis'
    if(strType == 'SPECIFIC')
        strPostBack += '&SHOW=SPECIFIC&SPECIFIC='+strDESC+'&SPECIFIC_NAICS='+strNAICS
    if(strType == 'BUSINESSES')
        strPostBack += '&SHOW=BUSINESSES&BUSINESSES='+strDESC+'&BUSINESSES_NAICS='+strNAICS+strPrevSPEC
	// load BusinessAnalysis
	if (strPostBack)
	{   
	    //selectRadTab('Map');
        //window.setTimeout('selectRadTab("Demographics");__doPostBack("bbusDemographicsDetails$updateBusinessAnalysis", strPostBack);',10);
        __doPostBack("bbusDemographicsDetails$updateBusinessAnalysis", strPostBack);
        GACTTrack("Demographic Details");
    }
	// show "loading..." display

}

function showSiteBusMap(strType,strNAICS,busX,busY,strColor)
{
    //var strPostBack
    if(strType='MAPBUSINESS')
    {    
        demoSiteBusPoints = busX+"||||"+busY+"||||"+strColor;
        clickBusTab();
    }
    
        //strPostBack = 'Event=updateBusinessMap&COLOR='+strColor+'&busX='+x+'&busY='+y
    // load BusinessMap
	//if (strPostBack)
	//{   
	    
	    //selectRadTab('Map');
        //window.setTimeout('selectRadTab("Demographics");__doPostBack("bbusDemographicsDetails$updateBusinessAnalysis", strPostBack);',10);
        //__doPostBack("bbusDemographicsDetails$updateSiteBusinessAnalysis", strPostBack);
    //}
	// show "loading..." display

}

function ShowDemographicsImage(strImageName)
{
	var imgDemographicsImage = document.getElementById('bbusDemographicsDetails_imageDemographicsImage');
	if (imgDemographicsImage)
	imgDemographicsImage.src = strImageName;
}

function printDemographicsDetails(strID)
{
//    strPostBack = 'Event=printDemographicsDetails';
//    if(strID)
//        strPostBack = strPostBack + '&strDemographicsID='+strID
//    __doPostBack('', strPostBack);

    if (strID != null && strID != "")
    {
        var d = new Date();
    	var siteUnits = document.getElementById('bbusSearchForm_siteUnits').value;
        
        var printurl = 'Dialogs/print_map.aspx?t='+d.getTime()+'&type=Demographics&NoMap=true';
        printurl = printurl + '&bgid='+strID+'&site='+strID.replace(/,/g,', ')+'&units='+siteUnits;
        
        var pFrame = document.getElementById('iframePrintMap');
        pFrame.src = printurl;
        return;
    }

    strID = currDemoSiteId;
    currDemoSiteGraphic = getGraphicById(currDemoSiteId);
    pGeom = currDemoSiteGraphic.geometry;
	var mapGsvc = document.getElementById("bbusMap_mapGeometryService").value;
    if (mapGsvc == "") mapGsvc = "http://maps.binarybus.net/arcgis/rest/services/Geometry/GeometryServer";
    if(gsvc == null) gsvc = new esri.tasks.GeometryService(mapGsvc);
    var outSR = new esri.SpatialReference({ wkid: 4326});
    gsvc.project([ new esri.Graphic( pGeom ) ], outSR, buildDemographicsDetailsPrint);

}
    
function buildDemographicsDetailsPrint(features) 
{
        pt = features[0].geometry;
        
    //    strPostBack = 'Event=printDemographics';
    //    if(strID)
    //        strPostBack = strPostBack + '&strPropertyID='+strID
    //    __doPostBack('', strPostBack);
        
        var d = new Date();
    	var minx = jsMap.extent.xmin;
        var maxx = jsMap.extent.xmax;
        var miny = jsMap.extent.ymin;
        var maxy = jsMap.extent.ymax;
    
        var mapw = jsMap.width;
        var maph = jsMap.height;
   
	    var srid = jsSR.wkid;
        var mapurl, units;
        for (var j=0, jl=jsMap.layerIds.length; j<jl; j++) {
          var layer = jsMap.getLayer(jsMap.layerIds[j]);
          if (currentMapView == layer.id) {
            mapurl = layer.url;
            units = layer.units;
          }
        }
        
        var radiusDistance;
        var siteUnits = document.getElementById('bbusSearchForm_siteUnits').value;
        if (units == "esriFeet")
        {
            if (siteUnits=="ENGLISH")
                radiusDistance = currDemoSiteDemoRadius*5280;
            else 
                radiusDistance = currDemoSiteDemoRadius/0.3048*1000;
        }
        if (units == "esriMeters") 
        {
            if (siteUnits=="ENGLISH")
                radiusDistance = currDemoSiteDemoRadius*0.3048*5280;
            else
                radiusDistance = currDemoSiteDemoRadius*1000;
        }
        maxx=parseFloat(currDemoSiteX)+(radiusDistance*1.5)
        maxy=parseFloat(currDemoSiteY)+(radiusDistance*1.5)
        minx=parseFloat(currDemoSiteX)-(radiusDistance*1.5)
        miny=parseFloat(currDemoSiteY)-(radiusDistance*1.5)
        
        var mapname = currentMapView;
        var mapNames = document.getElementById("bbusMap_mapServiceMapNames").value;
        if (mapNames!= "")
        {
            var arrMapNames = mapNames.split(",");
	        var arrMapServices = document.getElementById("bbusMap_mapServices").value.split(",");
	        for (var am in arrMapServices)
	        {
	            if(arrMapServices[am]==currentMapView)
	                mapname = arrMapNames[am];
	        }
	    }
        
        var propname = document.getElementById("bbusDemographicsDetails_demoPropName").value;
        var siteUnits = document.getElementById('bbusSearchForm_siteUnits').value;
        
        var printurl = 'Dialogs/print_map.aspx?t='+d.getTime()+'&type=Demographics&MapURL='+mapurl+'&MapName='+mapname+'&MinX='+minx+'&MinY='+miny+'&MaxX='+maxx+'&MaxY='+maxy+'&MapW='+mapw+'&MapH='+maph+'&SRID='+srid;
        printurl = printurl + '&x='+currDemoSiteX+'&y='+currDemoSiteY+'&long='+pt.y.toFixed(7)+'&lat='+pt.x.toFixed(7)+'&radius='+currDemoSiteDemoRadius+'&mapradius='+radiusDistance+'&site='+propname+'&units='+siteUnits;
        
        var pFrame = document.getElementById('iframePrintMap');
        pFrame.src = printurl;
        GACTTrack("Print Demographic Details");
        GACTTrackEvent("Site Analysis","Print Demographics","Site - "+propname);
}
function DemographicsDetails_ToggleContent(divHeaderDiv)
{
	var divContentDiv = divHeaderDiv.nextSibling;
	if (divContentDiv)
		divContentDiv.style.display = (divContentDiv.style.display == 'none') ? '' : 'none';
}

function DemographicsDetails_ExpandAll(blnExpand,divID)
{
    var divSection;
    if (divID!=null) divSection = document.getElementById(divID);
    if (!divSection) divSection = document.getElementById('divDemographicsDetails_Right');
	if (!divSection)	return;
	
	var arrPanels = divSection.getElementsByTagName('table');
	for (var ix=0; ix < arrPanels.length; ix++)
	{
		if (arrPanels[ix].className == 'content')
			arrPanels[ix].style.display = (blnExpand) ? '' : 'none';
	}
}

function demoProfile_ExpandAll(blnExpand)
{
	var divSection = document.getElementById('divDemoProfile_Right');
	if (!divSection)	return;
	
	var arrPanels = divSection.getElementsByTagName('table');
	for (var ix=0; ix < arrPanels.length; ix++)
	{
		if (arrPanels[ix].className == 'content')
			arrPanels[ix].style.display = (blnExpand) ? '' : 'none';
	}
}

function businessAnalysis_ExpandAll(blnExpand)
{
	var divSection = document.getElementById('divBusinessAnalysis_Right');
	if (!divSection)	return;
	
	var arrPanels = divSection.getElementsByTagName('table');
	for (var ix=0; ix < arrPanels.length; ix++)
	{
		if (arrPanels[ix].className == 'content')
			arrPanels[ix].style.display = (blnExpand) ? '' : 'none';
	}
}

function bbusAlert(strText, strTitle, blnShow)
{
	if (!strTitle)
		strTitle = strText;

	radalert(strText, 300, 200, strTitle);
}