var protocol= (('https:' == document.location.protocol) ? 'https:' : 'http:');

$(document).ready(function () {
    if (typeof(chartServerUrl) == "undefined") {
        chartServerUrl = 'charts.altosresearch.com';
    }

	$.ajax({
        url: protocol + "//"+chartServerUrl+"/altos/app?service=listreports&rf=jsonp&apiv=1&pai=" + pai,
        dataType: 'jsonp',
        success: function(data) {
            if ( data.responseCode == 500 ||  data.resultCount == 0  ) {
                $("div#market_mapper").attr('style', 'color:white;text-align:center;vertical-align: bottom; height: 400px; width: 690px;background: url(/altos/img/no-locations.png) no-repeat center #233040;');
                $("div#market_mapper").html('<p><br/><br/>Looks like you have no locations to map.<br/><br/>This feature is only available to AltosConnect customers with Pro or Premium Subscriptions</p>');
                return;
            }
            $.each(data.list, function(i,item) {
                var rtStr  = "[SF]";
                if ( item.residenceTypeid == "200" ) {
                    rtStr = "[Condo]";

                }
                var cityStateZip = "&st=" + item.stateName + "&cid=" + item.cityId + "&zid=" + item.zipId + "&service=chart";
                var location = ""; var title = "";
                if ( item.cityWide == "true" ) {
                    location = item.cityDisplayName + ", " + item.stateName + " " + item.zipName + " " + "<br/>";
                    title = "" + item.cityDisplayName + ", " + item.stateName + "";
                } else {
                    location = item.cityDisplayName + ", " + item.stateName + " " + item.zipName + " " + "<br/>";
                    title_old = "" + item.cityDisplayName + ", " + item.stateName + " " + item.zipName + "";
                }

                $("#cityName").append("<option value=" + cityStateZip + "> " + location + "</option>");
                $("#cityName").attr("value", cityStateZip);
                var newChartURL = protocol + "//"+ chartServerUrl +"/altos/app?pai=" + pai +  cityStateZip + "&rt=" + $("#residenceTypeMenu").val() + "&ra=" + $("#rollingAverageMenu").val() + "&q=" + $("#quartileMenu").val() +  "&s=" + $("#statisticNameMenu").val() + "&ts=" + $("#timespanMenu").val() + "&sz=" + $("#sizeMenu").val() ;
                $("#altos_chart_1").attr("src", newChartURL).fadeIn();


            });
        }
    });
});
function redoChart () {
    newerChartURL = protocol + "//"+ chartServerUrl+"/altos/app?pai=" + pai + $("#cityName").val() + "&rt=" + $("#residenceTypeMenu").val() + "&ra=" + $("#rollingAverageMenu").val() + "&q=" + $("#quartileMenu").val() +  "&s=" + $("#statisticNameMenu").val() + "&ts=" + $("#timespanMenu").val() + "&sz=" + $("#sizeMenu").val() ;
    $("#press").attr('disabled', 'disabled');
    $("#spinner").html("<img src='"+ protocol+"//www.altosresearch.com/altos/img/spinner.gif'>");
    $("#altos_chart_1").fadeOut(2000, redoChart2);
}
function redoChart2 () {
    if (($("#timespanMenu")=="f")||($("#timespanMenu")=="g")||($("#timespanMenu")=="z")) {
        $("#altos_chart_1").attr("src", newerChartURL).delay(4500).fadeIn(1000,redoChart3);
    } else {
        $("#altos_chart_1").attr("src", newerChartURL).delay(750).fadeIn(1000,redoChart3);
    }
}

function redoChart3 () {
    $("#spinner").html("");
    $("#press").removeAttr('disabled');
}


