// CrimeView Server

function CVS(){}

CVS.Init = function() {
    //alert("CVS.Init");
    CVS.IFrameHeight = 500;

    WHAT.Init();
    WHEN.Init();
    WHERE.Init();
    HOW.Init();
    ANALYSIS.Init();
    CHART.Init();
    GRID.Init();
    CW.Init();
    //CVS.ResizeMap();
    UIHelper.Init();

    // Disable the Enter key
    $("form#form1").bind("keypress", function(e) { if (e.keyCode == 13) return false; });

    // Issue warning about questionable browsers
    switch (BROWSER.IsSupported()) {
        case -1:
            UIHelper.ShowDelayedStatusMessage("Warning: Your browser type is unknown and may not function correctly with this application.", "error");
            break;

        case 1:
            UIHelper.ShowDelayedStatusMessage("Please note: IE 6 will not display correctly and is not fully supported.", "warning");
            break;

        case 2:
            UIHelper.ShowDelayedStatusMessage("Warning: Your browser is extremely old and is not supported by this application.", "error");
            break;
    }

    // ArcGIS Server uses cookies
    if (!BROWSER.Cookies) UIHelper.ShowDelayedStatusMessage("CrimeView Community requires the use of browser cookies which are currently disabled.", "error");
};

CVS.ShowWhat = function()
{
    var d = new Date();
    var url = "Data.aspx?page=what&t=" + d.getTime();
	var target = document.getElementById("what-content");

    //alert("Show What = " + url);
  	_dynoLoader._loadXmlDocument(url, target, WHAT.Load, null, false);
};

CVS.ShowWhen = function()
{
    $.ajax(
    	{
    	    type: "GET",
    	    url: "Data.aspx",
    	    data: "page=whenliterals",
    	    dataType: "json",
    	    error: UIHelper.AjaxError,
    	    success: WHEN.DefineLiterals
    	});
};

CVS.ShowWhere = function()
{
    $.ajax(
    	{
    	    type: "GET",
    	    url: "Data.aspx",
    	    data: "page=whereliterals",
    	    dataType: "json",
    	    error: UIHelper.AjaxError,
    	    success: WHERE.DefineLiterals
    	});
};

CVS.ShowHow = function() {
    $.ajax(
    	{
    	    type: "GET",
    	    url: "Data.aspx",
    	    data: "page=howliterals",
    	    dataType: "json",
    	    error: UIHelper.AjaxError,
    	    success: HOW.DefineLiterals
    	});
};

CVS.ShowAnalysis = function() {
    $.ajax(
    	{
    	    type: "GET",
    	    url: "Data.aspx",
    	    data: "page=analysisliterals",
    	    dataType: "json",
    	    error: UIHelper.AjaxError,
    	    success: ANALYSIS.DefineLiterals
    	});
};

CVS.ShowCyberWatch = function()
{
	var url = "Data.aspx?page=cyberwatch";
	var target = document.getElementById("cyberwatch-tab-holder");

	_dynoLoader._loadXmlDocument(url, target, CW.Load, null, false);
};

CVS.ShowReport = function() {
    var url = "Data.aspx?page=report";
    var target = document.getElementById("report-container");

    _dynoLoader._loadXmlDocument(url, target, null, null, false);
};

CVS.BuildSummaryDisplay = function() {
    //alert("CVS.BuildSummaryDisplay = " + UIHelper.InputStyle);

    switch (UIHelper.InputStyle) {
        case "query":
            CVS.BuildQueryDisplay();
            break;

        case "analysis":
            CVS.BuildAnalysisDisplay();
            break;
    }
};

CVS.BuildQueryDisplay = function() {
    var summary = "", disableReset = true;

    summary = "<table>" + HOW.GetSummary() + "</table>";
    $("#how-summary-drop-down").html(summary);

    $("#what-where-when-summaries").show();

    if (WHERE.IsValid) {
        summary = "<table>" + WHERE.GetSummary() + "</table>";
        UIHelper.WhereQuery = WHERE.GetQuery();
        disableReset = false;
    }
    else {
        summary = "<p class=\"default\">" + UIHelper.SummaryWhereNothingSelected + "</p>";
        UIHelper.WhereQuery = "";
    }
    $("#where-summary-drop-down").html(summary);

    //alert("WHAT.SummaryDisplay = " + WHAT.SummaryDisplay);
    if (WHAT.IsValid && WHAT.SummaryDisplay != "") {
        summary = "<p>" + WHAT.SummaryDisplay + "</p>";
        UIHelper.WhatQuery = WHAT.GetQuery();
        $("#QueryLayersInUse").val(WHAT.GetLayersInUse());
        disableReset = false;
        //alert("WHEN.IsLoaded = " + WHEN.IsLoaded + ", !WHEN.IsValid = " + !WHEN.IsValid);
        if (WHEN.IsLoaded && !WHEN.IsValid) {
            //alert("WHEN.IsLoaded, !WHEN.IsValid = DISABLEING");
            UIHelper.DisableUpdateMapButton(true);
        }
        //alert("WHAT.SummaryDisplay QueryLayersInUse = " + $("#QueryLayersInUse").val());
    }
    else {
        summary = "<p class=\"default\">" + UIHelper.SummaryWhatNothingSelected + "</p>";
        UIHelper.WhatQuery = "";
        $("#QueryLayersInUse").val("");
    }
    $("#what-summary-drop-down").html(summary);

    if (WHEN.IsLoaded) {
        summary = "<table>" + WHEN.GetSummary() + "</table>";
        UIHelper.WhenQuery = WHEN.GetQuery();
        disableReset = false;
    }
    else {
        summary = "<p class=\"default\">" + UIHelper.SummaryWhenNothingSelected + "</p>";
        UIHelper.WhenQuery = "";
    }
    $("#when-summary-drop-down").html(summary);

    UIHelper.DisableResetButton(disableReset);
};

CVS.BuildAnalysisDisplay = function() {
    var summary = "";

    if (ANALYSIS.IsLoaded) {
        summary = "<table>" + ANALYSIS.GetSummary() + "</table>";
    }
    else {
        summary = "<p class=\"default\">" + UIHelper.SummaryAnalysisNothingSelected + "</p>";
    }
    $("#analysis-summary-drop-down").html(summary);
};

CVS.StartProgressBar = function() {
    UIHelper.ProcessingRequest = true;
    UIHelper.EnableUpdateButton(false);
    UIHelper.DisableResetButton(true);

    UIHelper.ProgressWidth = 0;
    $("#progress-bar-container").show();
    $("#progress-bar").css("width", UIHelper.ProgressWidth);
    UIHelper.ProgressTimer = setTimeout("CVS.SetProgressTimer()", 1000);
};

CVS.StopProgressBar = function() {
    clearTimeout(UIHelper.ProgressTimer);
    $("#progress-bar-container").hide();

    UIHelper.ProcessingRequest = false;
    UIHelper.EnableUpdateButton(true);
    UIHelper.DisableResetButton(false);
};

CVS.SetProgressTimer = function() {
    UIHelper.ProgressWidth += UIHelper.ProgressIncrement;
    if (UIHelper.ProgressWidth <= UIHelper.ProgressMax) {

        $("#progress-bar").css("width", UIHelper.ProgressWidth);
        UIHelper.ProgressTimer = setTimeout("CVS.SetProgressTimer()", 1000);

        return;
    }

    CVS.StopProgressBar();

    switch (UIHelper.InputStyle) {
        case "query":
            UIHelper.ShowStatusMessage("The query request has timed out.", "error");

            break;

        case "analysis":
            UIHelper.AnalysisExtent = "";
            clearTimeout(ANALYSIS.JobCheckTimer);

            UIHelper.ShowStatusMessage("The analysis process has timed out.", "error");

            break;
    }
};

CVS.ResizeMap = function() {
    var minMapW = 665;
    var minMapH = 360;
    var offTop = 249;
    var offRight = 17;
    var offBottom = 17;
    var offLeft = 314;
    var offInfo = 22;   // if scroll bar then 39;
    var winSize = CVS.GetClientSize();

    // IE 6 needs a little more room
    if ($.browser.msie) {
        //offInfo = 22;
        if (parseInt($.browser.version) < 6) offRight = 25;
        //} else if ($.browser.safari) {
        //    offInfo = 22;
    }

    // Set the info area width
    $("#info-content-holder").css("width", winSize.x - offInfo);

    // Set the print and report frame height
    CVS.IFrameHeight = winSize.y - 165;
    $("#report-frame").css("height", CVS.IFrameHeight);
    $("#report-frame").css("width", winSize.x - 225);

    // Calc the map size
    var mapW = winSize.x - offLeft - offRight;
    if (mapW < minMapW) mapW = minMapW;

    var mapH = winSize.y - offTop - offBottom - 6;
    if (mapH < minMapH) mapH = minMapH;

    // remove the height / width styles from the map
    if ($("#Map1")) {
        var styleArray = $("#Map1").attr("style").split(";");
        var newStyleAttributes = "";
        var styleElement = "";

        if (styleArray.length > 0) {
            for (var i = 0; i < styleArray.length; i++) {
                styleElement = styleArray[i].toString().toLowerCase();

                // if the string height / width is not found within the
                // style array index i, then concatenate the string back together
                if (styleElement.indexOf("height") == -1 && styleElement.indexOf("width") == -1) {
                    if (newStyleAttributes.length > 0) newStyleAttributes += ";";
                    newStyleAttributes += styleArray[i];
                }
            }
        }
        newStyleAttributes += ";height:" + mapH + "px;width:" + mapW + "px;";
    } else {
        alert("map=" + $("#Map1"));
        newStyleAttributes += "height:" + mapH + "px;width:" + mapW + "px;";
    }

    //alert(newStyleAttributes);        
    // reset the map's style without the in-line height / width
    $("#Map1").attr({ style: newStyleAttributes });
};

CVS.GetClientSize = function()
{
    var size = new CVS.Coord(0, 0);
    
    if(self.innerWidth)
    {
        size.x = self.innerWidth;
        size.y = self.innerHeight;
    } else if(document.documentElement && document.documentElement.clientWidth) {
        size.x = document.documentElement.clientWidth;
        size.y = document.documentElement.clientHeight;
    } else if(document.body) {
		size.x = document.body.clientWidth;
		size.y = document.body.clientHeight;
	}
    
    return size;
};

CVS.Coord = function(x, y)
{
    this.x = x;
    this.y = y;
};

/* Wire the map resize function to the windows event listener */
if (window.attachEvent)
{
    window.attachEvent("onresize", function() { CVS.ResizeMap(); }, false);  
}else{
    window.addEventListener("resize", function() { CVS.ResizeMap(); }, false);
};

/* When the DOM is ready, initialize the js libraries */
$(document).ready(CVS.Init);

