﻿//
var currPropId = "";
var currPropX = 0;
var currPropY = 0;
var currPropDemoRadius = 0;
var currPropGraphic = null;
var theResizePropConnect;
var theZoomPopUpConnect;
var busPoints = "";
var theBusinessConnect;
var tabstripDemographicsDetails;

function clickDemographicsTab()
{
   theResizePropConnect = dojo.connect(jsMap,"onResize",function(){
		    gotoCurrentDemographics();
		    dojo.disconnect(theResizePropConnect);
		    });
   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', '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 unselectDemographicsTab(strTabId)
{
	var tab = tabstripDemographicsDetails.FindTabById("bbusDemographicsDetails_tabstripDemographicsDetails_"+strTabId);
	if (tab)
	{
		tab.UnSelect();
	}
}
function selectDemographicsTab(strTabId)
{
	var tab = tabstripDemographicsDetails.FindTabById("bbusDemographicsDetails_tabstripDemographicsDetails_"+strTabId);
	if (tab)
	{
		tab.UnSelect();
		tab.Select();
	}
}

function clickDemoTab()
{
    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(currPropGraphic) 
        bufferPoint(currPropGraphic,radius,units);
}
function clickBusTab()
{
    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('plotBusinesses(busPoints)',10);
        dojo.disconnect(theBusinessConnect);
        });
        
    if(currPropGraphic) 
        bufferPoint(currPropGraphic,radius,units);
    
    
}
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 plotBusinesses(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 setBusPoints(points)
{
    busPoints = points;
    //alert(points);
}

function gotoCurrentDemographics()
{
    
    jsMap.infoWindow.hide();    
    selectDemographicsTab("Demographics");
    //zoomToPoint(currPropX,currPropY);
    
}

function gotoDemographicsDetails(strDemographicsId, x, y)
{
    currPropId = strDemographicsId;
	currPropX = x;
	currPropY = y;
	var radius = document.getElementById('bbusDemographicsDetails_buffradius');
    if(radius) currPropDemoRadius = radius.value
	currPropGraphic = getGraphicById(currPropId);
    
    setSiteName(currInfoWindowTitle);
    //alert('here');
	// load Demographics Details
	if (strDemographicsId != null)
	{
	    __doPostBack('bbusDemographicsDetails$updateDemographicsDetails', 'Event=updateDemographicsDetails&strDemographicsId=&xVal='+ x +'&yVal='+ y);
        GACTTrack("Demographic Details");
    }
	// go to Demographics Details page
	unselectRadTab('Details');
	selectRadTab('Details');
}

function gotoDemographicsByBlockGroup(strDemographicsId, x, y)
{
    
	currPropId = strDemographicsId;
	currPropX = x;
	currPropY = y;
	var radius = document.getElementById('bbusDemographicsDetails_buffradius');
    if(radius) currPropDemoRadius = radius.value
	currPropGraphic = null;
    //alert('here');
	// load Demographics Details
	if (strDemographicsId != null)
	{
	    __doPostBack('bbusDemographicsDetails$updateDemographicsDetails', 'Event=updateDemographicsDetails&strDemographicsId='+strDemographicsId+'&xVal='+ x +'&yVal='+ y);
        GACTTrack("Demographic Details");
    }
	// go to Demographics Details page
	unselectRadTab('Details');
	selectRadTab('Details');
}

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=PrepareDemographics");
    GACTTrack("Demographic Details");
}

function performDemoBusinessAnalysis()
{
    clickBusTab();
    __doPostBack("bbusDemographicsDetails$updateBusinessAnalysis", "Event=PrepareBusinessProfile");
    GACTTrack("Demographic Details");
}

function showBusAnalysis(strType,strDESC,strNAICS,strPrevSPEC)
{
    var strPostBack
    if(strType!='MAPBUSINESS')
        strPostBack = 'Event=updateBusinessAnalysis'
    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 showBusMap(strType,strNAICS,busX,busY,strColor)
{
    //var strPostBack
    if(strType='MAPBUSINESS')
    {    
        busPoints = 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$updateBusinessAnalysis", 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 = currPropId;
    currPropGraphic = getGraphicById(currPropId);
    pGeom = currPropGraphic.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 = currPropDemoRadius*5280;
            else 
                radiusDistance = currPropDemoRadius/0.3048*1000;
        }
        if (units == "esriMeters") 
        {
            if (siteUnits=="ENGLISH")
                radiusDistance = currPropDemoRadius*0.3048*5280;
            else
                radiusDistance = currPropDemoRadius*1000;
        }
        maxx=parseFloat(currPropX)+(radiusDistance*1.5)
        maxy=parseFloat(currPropY)+(radiusDistance*1.5)
        minx=parseFloat(currPropX)-(radiusDistance*1.5)
        miny=parseFloat(currPropY)-(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='+currPropX+'&y='+currPropY+'&long='+pt.y.toFixed(7)+'&lat='+pt.x.toFixed(7)+'&radius='+currPropDemoRadius+'&mapradius='+radiusDistance+'&site='+propname+'&units='+siteUnits;
        
        var pFrame = document.getElementById('iframePrintMap');
        pFrame.src = printurl;
        GACTTrack("Print Demographic Details");
}
function DemographicsDetails_ToggleContent(divHeaderDiv)
{
	var divContentDiv = divHeaderDiv.nextSibling;
	if (divContentDiv)
		divContentDiv.style.display = (divContentDiv.style.display == 'none') ? '' : 'none';
}

function DemographicsDetails_ExpandAll(blnExpand)
{
	var 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);
}