﻿

var HANDLER_URL = homeUrl + "zerohourhandler.ashx";
var STATUS_IMAGE_DIR = homeUrl + "images/zerohour/locstates/";
var STATE_CHECK_INTERVAL = 30000;

var linkarr = new Array();

function GetLocationState()
{
    var data = { plain: "locstate" };
    var callback = GetLocationState_Loaded;


    SendRequest(data, callback);

}

function GetLocationState_Loaded(xml, textstatus)
{
    $("loc", xml).each(function()
    {

        try
        {

            switch ($(this).attr("state"))
            {
                case "WAITING":
                    $("#" + $(this).attr("city")).attr("src", STATUS_IMAGE_DIR + $(this).attr("city") + "_0.jpg");
                    break;

                case "AVAILABLE":
                    $("#" + $(this).attr("city")).attr("src", STATUS_IMAGE_DIR + $(this).attr("city") + "_1.jpg");

                    if (!$("#" + $(this).attr("city")).hasClass("handcursor"))
                    {
                        $("#" + $(this).attr("city")).addClass("handcursor");
                        $("#" + $(this).attr("city")).bind("click", { lnk: $(this).attr("info") }, function(event)
                        {
                            window.open(event.data.lnk, "infowin");
                        });
                    }
                    break;

                case "FOUND":
                    $("#" + $(this).attr("city")).attr("src", STATUS_IMAGE_DIR + $(this).attr("city") + "_2.jpg");

                    if (!$("#" + $(this).attr("city")).hasClass("handcursor"))
                    {
                        $("#" + $(this).attr("city")).removeClass("handcursor")
                        $("#" + $(this).attr("city")).unbind("click", { lnk: $(this).attr("info") }, function(event)
                        {
                            window.open(event.data.lnk, "infowin");
                        });
                        
                    }					
					
                    break;

                default:
                    $("#" + $(this).attr("city")).attr("src", STATUS_IMAGE_DIR + $(this).attr("city") + "_3.jpg");

                    if (!$("#" + $(this).attr("city")).hasClass("handcursor"))
                    {
                        $("#" + $(this).attr("city")).removeClass("handcursor")
                        $("#" + $(this).attr("city")).unbind("click", { lnk: $(this).attr("info") }, function(event)
                        {
                            window.open(event.data.lnk, "infowin");
                        });
                        
                    }					
					
                    break;

            }

        } catch (err)
        {
            //alert("Error: " + err.toString());
        }

    });

    try
    {
        $("code", xml).each(function()
        {
            $("#owner" + $(this).attr("cnum")).html($(this).attr("owner"));
        });

    } catch (err)
    {
        //alert("No codes yet: " + err.toString());
    }

}

function BuildOwnersTable()
{
    var tgt = $("#findersTgt");
    var tablestr = "";

    tablestr += "<table>"

    for (var i = 1; i <= 22; i++)
    {
        tablestr += "<tr>";
        tablestr += "<td class='ownerTD'>";
        tablestr += i.toString() + ". <span id='owner" + i.toString() + "'></span>";
        tablestr += "</td>";
        tablestr += "<td class='ownerTD'>";
        tablestr += (i + 22).toString() + ". <span id='owner" + (i + 22).toString() + "'></span>";
        tablestr += "</td>";
        tablestr += "<td class='ownerTD'>";
        tablestr += (i + 44).toString() + ". <span id='owner" + (i + 44).toString() + "'></span>";
        tablestr += "</td>";
        tablestr += "</tr>";
    }

    tablestr += "</table>";

    tgt.html(tablestr);
}

function SendRequest(dataobj, callback, errorcallback, datatype)
{
    if (errorcallback == null) { errorcallback = RequestError; }
    if (datatype == null) { datatype = "xml"; }

    $.ajax({
        type: "GET",
        cache: false,
        url: HANDLER_URL,
        data: dataobj,
        datatype: datatype,
        success: callback,
        error: errorcallback
    });

}

function RequestError(XMLHttpRequest, textStatus, errorThrown)
{
	//alert("An error has occurred.  Please try again.");
    //alert("init_failed: textStatus: " + textStatus + " errorThrown: " + errorThrown);

}

function Init()
{
    BuildOwnersTable();
    GetLocationState();
    setInterval("GetLocationState()", STATE_CHECK_INTERVAL);
}

$(document).ready(function() { Init(); });