﻿var _lastUpdate;
var _updateInterval = setInterval("CheckForUpdate()", 10000);

$(document).ready(function() {
    $("div#reportForm").jqm({ "modal": true }); // set up this div as a modal dialog
    LoadSightings();

    $("span.reportButton").click(function() {
        $("input#date").dateEntry('setDate', new Date());
        $("input#time").timeEntry('setTime', new Date());
        $("input#location").val("").DefaultValue("(location)");
        $("textarea#comment").val("").DefaultValue("(comment)");
        $("input#name").val("").DefaultValue("(your name)");
        $("input#link").val("").DefaultValue("(your url or email)");
        $("div#reportFormTitle").click();
    })
    $("input#report").click(function() { SubmitSighting(); });
    $("input#date").dateEntry({ maxDate: new Date() });
    $("input#time").timeEntry();
    $("input#date").DefaultValue("(date)");
    $("input#time").DefaultValue("(time)");
    $("textarea#comment").keyup(function() {
        $("textarea#comment").css("background-color", ($("textarea#comment").val().length > 200) ? "#FFCCCC" : "#FFFFFF");
    });

    // as the browser window stays open, decrease the rate of refresh to remove strain from the web server
    setTimeout("decreaseUpdateInterval(30 * 1000)", 5 * 60 * 1000); // after 5m change interval to 30sec
    setTimeout("decreaseUpdateInterval(60 * 1000)", 15 * 60 * 1000); // after 15m change interval to 60sec
    setTimeout("decreaseUpdateInterval(5 * 60 * 1000)", 30 * 60 * 1000); // after 30m change interval to 5m
    setTimeout("decreaseUpdateInterval(15 * 60 * 1000)", 60 * 60 * 1000); // after an hour change interval to 15m
});

function SubmitSighting() {
    var location = $("input#location").val();
    var date = $("input#date").val();
    var time = $("input#time").val();
    var comment = $("textarea#comment").val();
    var name = $("input#name").val();
    var link = $("input#link").val();
    var errors = false;
    if (location.substring(0, 1) == "(") {
        $("input#location").css("background-color", "#FFCCCC");
        errors = true;
    }
    if (date.substring(0, 1) == "(") {
        $("input#date").css("background-color", "#FFCCCC");
        errors = true;
    }
    if (time.substring(0, 1) == "(") {
        $("input#time").css("background-color", "#FFCCCC");
        errors = true;
    }
    if ($("textarea#comment").val().length > 200) errors = true;
    if (comment.substring(0, 1) == "(") comment = "";
    if (name.substring(0, 1) == "(") name = "";
    if (link.substring(0, 1) == "(") link = "";
    if (errors) return;
    $("input#location").css("background-color", "#FFFFFF");

    // submit form
    $.get("reportSighting.ashx", { "location": location, "date": date, "time": time, "comment": comment, "name": name, "link": link }, function(data) {
        LoadSightings();
    });

    // $("div.reportForm").fadeOut();
    $("div#reportForm").jqmHide();
}

function CheckForUpdate() {
    $.get("getLastPostingDate.ashx", function(Data) {
        if (_lastUpdate == null) _lastUpdate = new Date(Data);
        if (_lastUpdate < new Date(Data)) LoadSightings();
    });
}
function decreaseUpdateInterval(newInterval) {
    clearInterval(_updateInterval);
    if (newInterval > 0) _updateInterval = setInterval("CheckForUpdate()", newInterval);
}

function LoadSightings() {
    $("div.sightings").load("sightings.aspx", function() {
        $("a.obfuscateEmail").each(function() {
            $(this).attr("href", "mailto:" + $(this).attr("x") + "@" + $(this).attr("y"));
        });
        $(this).slideDown(1000);
        $("div.spam").click(function() {
            $(this).hide();
            $(this).prev().slideUp();
            $.get("spam.ashx", { "id": $(this).attr("entryId") });
        });
    });
}