﻿$(document).ready(function () {
    $("#LoginUserName").focus(function () {
        if ($(this).val() == "USERNAME") $(this).val('');
    });
    $("#LoginUserName").blur(function () {
        if ($(this).val() == "") $(this).val("USERNAME");
    });

    $("#LoginPassword").focus(function () {
        if ($(this).val() == "******") $(this).val('');
    });
    $("#LoginPassword").blur(function () {
        if ($(this).val() == "") $(this).val("******");
    });

    $("#header-button-menu").hovermenu({
        content: $("#header-content-menu"),
        offset: -16,
        position: {
            vertical: "bottom",
            horizontal: "middle"
        }
    });

    $("#header-button-projects").hovermenu({
        content: $("#header-content-projects"),
        offset: -16,
        position: {
            vertical: "bottom",
            horizontal: "middle"
        }
    });

    $("#header-button-account").hovermenu({
        content: $("#header-content-account"),
        offset: -16,
        position: {
            vertical: "bottom",
            horizontal: "middle"
        }
    });
});

$(function () {
    // Register handler for "Content Submitted" request
    requestManager.addCallback("Content Submitted", function () {
        $("#user-menu").getUserMenu().open(function () {
            setTimeout("kidzeraOnContentSubmitted();", 1000);
        });
    });

    // Register handler for "Impression Added" request
    requestManager.addCallback("Impression Added", function (data) {
        $("#user-menu").getUserMenu().open(function () {
            $.post(APP_ROOT + "Interactions/GetTotalImpressions", { applicationId: null }, function (response) {
                if (response.Type != FAILURE) {
                    setTimeout("kidzeraOnImpressionAdded(" + response.Text + ");", 500);
                }
            });
        });
    });

    // Register handler for "Impression Removed" request
    requestManager.addCallback("Impression Removed", function (data) {
        $.post(APP_ROOT + "Interactions/GetTotalImpressions", { applicationId: null }, function (response) {
            if (response.Type != FAILURE) {
                $("#user-impressions").text(response.Text);
            }
        });
    });

    // Load Pretty Photo light box used-site wide
    getCss(APP_ROOT + "Scripts/prettyPhoto/prettyPhoto.css");
    getScript(APP_ROOT + "Scripts/jquery.prettyPhoto.js", function () {
        try {
            $("a[rel^='prettyPhoto']").prettyPhoto();
        }
        catch (e) { }
    });
});

// Destroys a message from a user stream
function kidzeraDestroyMessage(htmlId, url) {
    if (!confirm("Are you sure you want to delete this message from your stream forever?"))
        return;

    var message = $("#" + htmlId);
    if (message.css("opacity") < 1) return;

    message.fadeOut();

    if (url) {
        $.post(url, null, function (data) {
            if (data.Type == FAILURE) {
                showNotice(data.Text);
                message.show().fadeIn();
                return;
            }
        });
    }
}

function kidzeraOnContentSubmitted() {
    if (typeof (currentPointsUrl) == "undefined") {
        alert("global variable 'currentPointsUrl' is undefined");
        return;
    }

    var pointsLabel = $("#user-current-points");
    if (pointsLabel.css("opacity") < 1) return;
    pointsLabel.css("opacity", 0);

    $.getJSON(currentPointsUrl, function (result) {
        if (result.Type == FAILURE) {
            showNotice(result.Text);
            return;
        }

        var points = parseInt(result.Text);
        pointsLabel.text(points);
        pointsLabel.fadeTo(1000, 1, function () {
            $("#user-menu").delay(1000).getUserMenu().close();
        });
    });

    setTimeout("showNotice('Thanks for your submission! Check your My Stream for sweet updates.');", 1000);
}

function kidzeraOnImpressionAdded(total) {
    var impressionsLabel = $("#user-impressions");
    impressionsLabel.css("opacity", 0);
    impressionsLabel.text(total);
    impressionsLabel.fadeTo(1000, 1, function () {
        $("#user-menu").delay(1000).getUserMenu().close();
    });
}

function highlightFeedItem(target, enable) {
    if (!target) return;
    var item = $(target);

    if (enable) {
        item.addClass("highlighted");
    }
    else {
        item.removeClass("highlighted");
    }
}

function toggleFeedSize(target) {
    if (!target) return;
    var item = $(target);

    if (!target.isFull) {
        item.addClass("full");
        target.isFull = true;
    }
    else {
        item.removeClass("full");
        target.isFull = false;
    }
}

function openKidzeraSubmissionForm(selector, overAge) {
    if (overAge) {
        showMessage("<div style='font-size: 20px;'>Thanks for your interest in KidzEra! This is a place for kids, 5-18 to share their work. Feel free to vote on what you like and give the kids feedback, but please refrain from submitting your own work. Who knows, we may make a version of KidzEra for us older kids soon, but for now, just show your support, don't steal the show. Thanks!</div>", false, true);
    }
    else {
        openSubmissionForm(selector);
    }
}
