// CrimeView Server

function CVS(){}

CVS.Init = function() {
    //alert("CVS.Init");
    CVS.IFrameHeight = 500;

    WHAT.Init();
    WHEN.Init();
    WHERE.Init();
    HOW.Init();
    CHART.Init();
    GRID.Init();
    CW.Init();
    //CVS.ResizeMap();
    UIHelper.Init();

    // Disable the Enter key
    $("#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 is extremely old and may not function correctly with this application.", "error");
            break;

        //        case 1:   
        //            UIHelper.ShowDelayedStatusMessage("Please note: The browser has been switched to IE 7 compatibility mode.", "warning");   
        //            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.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() {
    var summary = "", disableReset = true;

    //alert("CVS.BuildSummaryDisplay");
    //alert("WHERE.SummaryDisplay = " + WHERE.SummaryDisplay);

    summary = "<table>" + HOW.GetSummary() + "</table>";
    $("#how-summary-drop-down").html(summary);

    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.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
    var styleArray = $("#Map1").attr("style").split(";");
    var newStyleAttributes = "";
    var styleElement = "";

    if (styleArray != null && 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;";

    //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);

