﻿// JScript File

var GlobalThematicMapPK = 0;
var TrendGraphDefaultHasRun = false; //only run on first callback
var varChartBedrooms = '2';
var varChartMonths = 12;
var varChartType = 'Line';
var currentAggregateType;


function showTrendGraph(e, aggregateType, thematicMapPK, title) {
    if (typeof (processingRequest) != 'undefined') {
        if (processingRequest) {
            return;
        }
    }
    currentAggregateType = aggregateType;

    var select = $('.ChartMonths');
    if (aggregateType != "1000001") {
        //select.removeOption('6');
        //select.removeOption('12');
    }
    else if (aggregateType == "1000001") {
        //select.addOption('6', '6 Months');
        //select.addOption('12', '12 Months');
    }

    var beds = GlobalLookup.getValues("CurrentBedSelection");

    $("#ctl00_ContentPlaceHolder1_IndexTrendGraphControl1_ChartBedRooms").val(beds);
    $("#ctl00_ContentPlaceHolder1_PropertyTrendGraphControl1_ChartBedRooms").val(beds);

    GlobalThematicMapPK = thematicMapPK;
    TrendClickChange(aggregateType, thematicMapPK);
    var trendGraphTitle = document.getElementById('ChartTitleDiv');
    trendGraphTitle.innerText = title;
    var objGraphContainer = document.getElementById('TrendGraphBox');
    ShowElement2('TrendGraphBox');
    ShowElement2('TrendGraph_BottomTable');

    var objMouseCoords = new MouseCoords();
    captureMouseCoords(e, objMouseCoords);
    trendMouseX = objMouseCoords.X;
    trendMouseY = objMouseCoords.Y;
    var b = $('body');
    var graphContainer = $('#TrendGraphBox');

    var w = 1000;

    if ((trendMouseY + graphContainer.height() + 50) > b.height()) {
        trendMouseY = b.height() - graphContainer.height() - 75;
    }

    if (window.location.pathname.indexOf("PropertySearch.aspx")>=0) {
        if (trendMouseX == 0) {
            objGraphContainer.style.top = 850 + 'px';
            objGraphContainer.style.left = 0 + 'px';
        }
        else {
            objGraphContainer.style.top = (trendMouseY - 50) + 'px';
            objGraphContainer.style.left = (trendMouseX + 50) + 'px';
        }
    }
    else {
        if (trendMouseX == 0) {
            objGraphContainer.style.top = 850 + 'px';
            objGraphContainer.style.left = 0 + 'px';
        }
        else {
            objGraphContainer.style.top = (trendMouseY - 50) + 'px';
            objGraphContainer.style.left = (trendMouseX - 500) + 'px';
        } 
    }

}


function showTrendGraphRight(e, aggregateType, thematicMapPK, title) {
    if (typeof (processingRequest) != 'undefined') {
        if (processingRequest) {
            return;
        }
    }
    currentAggregateType = aggregateType;

    var select = $('.ChartMonths');
    if (aggregateType != "1000001") {
        select.removeOption('3');
        select.removeOption('6');
        select.removeOption('12');
    }
    else if (aggregateType == "1000001") {
        select.addOption('3', '3 Months');
        select.addOption('6', '6 Months');
        select.addOption('12', '12 Months');
    }


    GlobalThematicMapPK = thematicMapPK;
    TrendClickChange(aggregateType, thematicMapPK);
    var trendGraphTitle = document.getElementById('ChartTitleDiv');
    trendGraphTitle.innerText = title;
    var objGraphContainer = document.getElementById('TrendGraphBox');
    ShowElement2('TrendGraphBox');
    ShowElement2('TrendGraph_BottomTable');

    //create objects to store Mouse Coordinates
    var objMouseCoords = new MouseCoords();
    captureMouseCoords(e, objMouseCoords);
    trendMouseX = objMouseCoords.X;
    trendMouseY = objMouseCoords.Y;
    var w = 1000;

    if (trendMouseX == 0) {
        objGraphContainer.style.top = 850 + 'px';
        objGraphContainer.style.right = 0 + 'px';
    }
    else {
        objGraphContainer.style.top = (trendMouseY - 50) + 'px';
        objGraphContainer.style.right = (trendMouseX - 500) + 'px';
    }
}

function hidePropertyTrendGraph() {
    HideElement2('TrendGraphBox');
    HideElement2('TrendGraph_BottomTable');
}


function GetFormattedVALY(SER, VALY, VALX) {
    var NumericVALY = 0;
    if (!isNaN(VALY))
        NumericVALY = parseFloat(VALY);

    var FORMATVALY = NumericVALY;

    if (SER.indexOf('Average Rent') >= 0)
        FORMATVALY = '$' + formatDecimal(NumericVALY, false);
    else if (SER.indexOf('Vacancy') >= 0)
        FORMATVALY = NumericVALY.toFixed(1) + '%';
    else if (SER.indexOf('Rent Sq/Ft') >= 0)
        FORMATVALY = '$' + formatDecimal(NumericVALY, true);
    else if (SER.indexOf('Units') >= 0)
        FORMATVALY = formatDecimal(NumericVALY, false);

    return FORMATVALY;
}

function GetTrendGraphLegend(SER, VALY, VALX, PropCount) {
    var NumericVALY = 0;
    if (!isNaN(VALY))
        NumericVALY = parseFloat(VALY);

    var FORMATVALY = NumericVALY;
    var UnitType = 'Cost:';

    if (SER.indexOf('Average Rent') >= 0) {
        FORMATVALY = '$' + formatDecimal(NumericVALY, false);
        UnitType = 'Average Rent';
    }
    else if (SER.indexOf('Vacancy') >= 0) {
        FORMATVALY = NumericVALY.toFixed(1) + '%';
        UnitType = 'Vacancy';
    }
    else if (SER.indexOf('Rent Sq/Ft') >= 0) {
        FORMATVALY = '$' + formatDecimal(NumericVALY, true);
        UnitType = 'Rent/Sq ft';
    }
    else if (SER.indexOf('Units') >= 0) {
        FORMATVALY = formatDecimal(NumericVALY, false);
        UnitType = 'Units Surveyed';
    }

    //return '<table border=0 cellpadding=2><tr><td>' + UnitType + ' Index: ' + FORMATVALY + '</td><td style="padding-left:50px;" align=right>Date:</td><td>' + VALX + '</td></tr></table>';
    return '<table border=0 cellpadding=2><tr><td class="GraphLegendLabel">Date:</td><td class="GraphLegendValue">' + VALX + '</td></tr><tr><td class="GraphLegendLabel">' + UnitType + ':</td><td class="GraphLegendValue">' + FORMATVALY + '</td></tr><tr><td class="GraphLegendLabel">Property Count:</td><td class="GraphLegendValue">' + PropCount + '</td></tr></table>';

}


function TrendGraphDefault() {
    try {
        var i = 0;
        while (i < document.getElementById('ChartBedRooms').options.length) {
            if (document.getElementById('ChartBedRooms').options[i].value == varChartBedrooms) {
                document.getElementById('ChartBedRooms').options[i].selected = true;
            }
            i++;
        }

        i = 0;
        while (i < document.getElementById('ChartMonths').options.length) {
            if (document.getElementById('ChartMonths').options[i].value == varChartMonths) {
                document.getElementById('ChartMonths').options[i].selected = true;
            }
            i++;
        }

        i = 0;
        while (i < document.getElementById('ChartType').options.length) {
            if (document.getElementById('ChartType').options[i].value == varChartType) {
                document.getElementById('ChartType').options[i].selected = true;
            }
            i++;
        }
    }
    catch (ex) { }
}


//////////////////////////////////////////////////////////////////////////////
////////////////// THEMATIC MAP INDEX TREND GRAPH RELATED  ///////////////////
//////////////////////////////////////////////////////////////////////////////

function UpdateIndexTrendGraphLegend(SER, VALY, VALX) {
    document.getElementById('IndexTrendGraph_DisplayDiv').innerHTML = GetTrendGraphLegend(SER, VALY, VALX);
    IndexTrendGraph_ItemHover(GetFormattedVALY(SER, VALY, VALX));
}

// Loading data message
function IndexTrendGraph_LoadDataMsg() {
    document.getElementById('graph_container').innerHTML = '<BR><BR><BR><BR><BR><BR>Loading...<img class="ImgSpinner" src=\"Images/spinner.gif\" alt=\".\" align=\"middle\">';
    document.getElementById('TrendGraph_BottomTable').style.visibility = 'hidden';

}

function OnIndexTrendGraphChangeCallback(s) {
    document.getElementById("graph_container").innerHTML = s;
    document.getElementById('TrendGraph_BottomTable').style.visibility = 'visible';
    document.getElementById('IndexTrendGraph_DisplayDiv').innerHTML = '';
    if (!TrendGraphDefaultHasRun) {
        TrendGraphDefault();
        TrendGraphDefaultHasRun = true;
    }

}

// hover event
function IndexTrendGraph_ItemHover(value) {
    var hiddenDiv = document.getElementById('IndexTrendGraph_HiddenValue');
    if (hiddenDiv != null) {

        ShowElement('IndexTrendGraph_HiddenValue');
        hiddenDiv.innerHTML = value;
    }
    else {
        HideElement('IndexTrendGraph_HiddenValue');
    }
}

//////////////////////////////////////////////////////////////////////////////      
////////////////// PROPERTY SEARCH TREND GRAPH RELATED  //////////////////////
//////////////////////////////////////////////////////////////////////////////

// Temporary variables to hold mouse x-y pos.s
var PropertyTrendGraph_mouseX = 0
var PropertyTrendGraph_mouseY = 0

function UpdatePropertyTrendGraphLegend(SER, VALY, VALX, PropCount) {

    ShowElement('GraphInstructions');
    document.getElementById('PropertyTrendGraph_DisplayDiv').innerHTML = GetTrendGraphLegend(SER, VALY, VALX, PropCount);
    IndexTrendGraph_ItemHover(GetFormattedVALY(SER, VALY, VALX));
}

// Tied to onmousemove, if the Hover div is showing then move it with the mouse
function PropertyTrendChangeXY(e) {
    //create objects to store Mouse Coordinates
    var objMouseCoords = new MouseCoords();
    captureMouseCoords(e, objMouseCoords);
    PropertyTrendGraph_mouseX = objMouseCoords.X;
    PropertyTrendGraph_mouseY = objMouseCoords.Y;

    var d = document;

    // get divs that move
    var IndexTrendGraph_HiddenValue = d.getElementById ? d.getElementById('IndexTrendGraph_HiddenValue') : d.all ? d.all['IndexTrendGraph_HiddenValue'] : d.layers['IndexTrendGraph_HiddenValue'];

    if (IndexTrendGraph_HiddenValue) {
        IndexTrendGraph_HiddenValue.style.left = PropertyTrendGraph_mouseX + 10 + "px";
        IndexTrendGraph_HiddenValue.style.top = PropertyTrendGraph_mouseY + 5 + "px";
    }
    else {
        //alert("no div");
    }

    return true;
}

// Opens a print preview page in a seperate window from which the user can print the grid
function TrendPrintButton_onclick() {
    window.open("TrendPrintPreview.aspx");
}

// Loading data message
function PropertyTrendGraph_LoadDataMsg() {
    document.getElementById('graph_container').innerHTML = '<BR><BR><BR><BR><BR><BR><center><img class="ImgSpinner" src=\"Images/spinner.gif\" alt=\".\" align=\"middle\"><center><BR><BR><BR><BR>';
    document.getElementById('TrendGraph_BottomTable').style.visibility = 'hidden';
}

function OnPropertyTrendGraphChangeCallback(s) {

    HideElement('GraphInstructions');
    document.getElementById("graph_container").innerHTML = s;
    document.getElementById('TrendGraph_BottomTable').style.visibility = 'visible';
    document.getElementById('PropertyTrendGraph_DisplayDiv').innerHTML = '<br/><p style="font-style: italic;">*Hover over graph point to show index data</p>';
    if (!TrendGraphDefaultHasRun) {
        TrendGraphDefault();
        TrendGraphDefaultHasRun = true;
    }

}

function OnPropertyTrendGraphAggChangeCallback(s) {
    OnPropertyTrendGraphChangeCallback(s);
    var select = $('.ChartMonths');
    select[0].selectedIndex = 0;



}

// hover event
function PropertyTrendGraph_ItemHover(value) {
    var hiddenDiv = document.getElementById('IndexTrendGraph_HiddenValue');
    if (hiddenDiv != null) {
        ShowElement('IndexTrendGraph_HiddenValue');
        hiddenDiv.innerHTML = value;
    }
    else {
        HideElement('IndexTrendGraph_HiddenValue');
    }
}

