﻿$(document).ready(function () {
    $(".watermark").watermark("watermark");

    $(".article-block .text").click(function () {
        if ($(this).parent(".article-block").hasClass("selected")) {
            $(this).parent(".article-block").removeClass("selected");
            $(this).parent(".article-block").find(".temp-clearfix").remove();
        }
        else {
            $(this).parent(".article-block").addClass("selected");
            $(this).parent(".article-block").append("<div class='temp-clearfix' style='clear: both;'></div>");
        }
    });

    $(".premium-block .text").click(function () {
        if ($(this).parent(".premium-block").hasClass("selected")) {
            $(this).parent(".premium-block").removeClass("selected");
            $(this).parent(".premium-block").find(".temp-clearfix").remove();
        }
        else {
            $(this).parent(".premium-block").addClass("selected");
            $(this).parent(".premium-block").append("<div class='temp-clearfix' style='clear: both;'></div>");
        }
    });

    $(".lightbox").fancybox({
        'transitionIn': 'elastic',
        'transitionOut': 'elastic'
    });

    $(".galleria").galleria({
        width: 460,
        height: 330,
        autoplay: 3000,
        showImagenav: false,
        showInfo: false
    });

    $("#fade, #popup-close, .close-button").live("click", function () {
        $("#fade").hide();
        $("#popup-close").hide();
        $("#popup-block").empty().hide();

        window.location.reload();
    });

    $(".facebook-like").live("click", function () {
        $.ajax({
            url: "/underhallning/frame/facebooklike",
            method: "GET",
            context: $(this),
            success: function (data) {
                var reload = true;

                $("#fade").show();
                $("#popup-close").show();
                $("#popup-block").html(data).show();
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(textStatus + ": " + errorThrown);
            }
        });
    });

    $(".earned-premiums .premium-box .remove").click(function () {
        var points = Number($(".earned-premiums .max-points").text());

        var currentAmount = $(this).parents(".premium-box").find(".amount-selected").text();
        var pointsCost = Number($(this).parents(".premium-box").find(".points").text());

        var amountOfItems = Number($("#amount-of-items").val());

        if (isNaN(currentAmount) || !isNaN(currentAmount) && Number(currentAmount) == 0 || amountOfItems == 0)
            return;

        points += pointsCost;

        if (amountOfItems > 0)
            amountOfItems--;

        $(".earned-premiums .max-points").text(points);

        var currentAmountElement = $(this).parents(".premium-box").find(".amount-selected");

        if (!isNaN(currentAmount))
            currentAmount--;

        if (Number(currentAmount) == 0) {
            currentAmount = "";
            if (currentAmountElement.hasClass("active"))
                currentAmountElement.removeClass("active");

            $(this).removeClass("active");
        }

        $("#amount-of-items").val(amountOfItems);
        $(this).parents(".premium-box").find(".amount-selected").text(currentAmount);
        $(this).parents(".premium-box").find(".quantity").val(currentAmount);

        evaluatePremiumState();
    });

    $(".earned-premiums .premium-box .add").click(function () {
        var points = Number($(".earned-premiums .max-points").text());

        var currentAmount = $(this).parents(".premium-box").find(".amount-selected").text();
        var pointsCost = Number($(this).parents(".premium-box").find(".points").text());

        var amountOfItems = Number($("#amount-of-items").val());

        if (points < pointsCost || amountOfItems >= 3)
            return;

        points -= pointsCost;
        amountOfItems += 1;

        $(".earned-premiums .max-points").text(points);

        if (!isNaN(currentAmount))
            currentAmount++;
        else
            currentAmount = 1;

        var currentAmountElement = $(this).parents(".premium-box").find(".amount-selected");

        if (!currentAmountElement.hasClass("active"))
            currentAmountElement.addClass("active");

        var currentRemoveButton = $(this).siblings(".remove");

        if (!currentRemoveButton.hasClass("active"))
            currentRemoveButton.addClass("active");

        $("#amount-of-items").val(amountOfItems);
        currentAmountElement.text(currentAmount);

        $(this).parents(".premium-box").find(".quantity").val(currentAmount);

        evaluatePremiumState();
    });

    function evaluatePremiumState() {
        var points = Number($(".earned-premiums .max-points").text());
        var amountOfItems = Number($("#amount-of-items").val());

        $(".earned-premiums .premium-box .points").each(function () {
            if (Number($(this).text()) > points || amountOfItems >= 3) {
                $(this).parents(".premium-box").addClass("inactive");

                if ($(this).parents(".premium-box").find(".amount-selected").text().length == 0)
                    $(this).parents(".premium-box").addClass("fade");
            }
            else {
                $(this).parents(".premium-box").removeClass("inactive");
                $(this).parents(".premium-box").removeClass("fade");
            }
        });
    }

    $(".start-premiums .premium-box").click(function () {
        if ($(this).find("input:checkbox").attr('checked') == 'checked') {
            var siblings = $(this).siblings(".premium-box");

            siblings.find("input:checkbox").attr('checked', false);
            siblings.removeClass("active");

            $(this).removeClass("active");
            $(this).find("input:checkbox").attr('checked', false)
        }
        else {
            var siblings = $(this).siblings(".premium-box");

            siblings.find("input:checkbox").attr('checked', false);
            siblings.removeClass("active");

            $(this).addClass("active");
            $(this).find("input:checkbox").attr('checked', true)
        }

        return false;
    });

    $(".ajax-post").live("submit", function () {

        if ($(this).hasClass("confirm")) {
            if (!confirm($(this).attr("confirm")))
                return false;
        }

        $.ajax({
            url: $(this).attr("action"),
            method: "POST", //$(this).attr("method"),
            data: $(this).serialize(),
            context: $(this),
            beforeSend: function () {
                if ($(this).hasClass("save")) {
                    $(this).find("input[type=submit]").val($(this).attr("textwaiting"));
                    $(this).find("input[type=text]").addClass("disabled");
                }
            },
            success: function (data) {
                var refload = true;

                if ($(this).hasClass("save")) {
                    $(this).find("input[type=submit]").val($(this).attr("textdone"));
                    $(this).find("input[type=text]").removeClass("disabled");

                    reload = false;
                }

                if ($(this).hasClass("popup")) {
                    $("#fade").show();
                    $("#popup-close").show();
                    $("#popup-block").html(data).show();
                    InitTabs();

                    reload = false;
                }

                if ($(this).hasClass("remove")) {
                    $(this).parents("[entity=true]").first().remove();

                    reload = false;
                }

                if ($(this).hasClass("add")) {
                    $($(this).attr("addtarget")).append(data);

                    reload = false;
                }

                if ($(this).hasClass("update")) {
                    $($(this).attr("updatetarget")).empty().append(data);

                    reload = false;
                }

                if ($(this).hasClass("reload")) {
                    reload = true;
                }

                if (reload) {
                    window.location.reload();
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(textStatus + ": " + errorThrown);
            }
        });

        return false;
    });

    $(".tab-menu li").live("click", function () {
        var sel = $(this).data("tab");

        $(".tab-content .tab").hide();
        $(".tab-content #" + sel).show();

        $(".tab-menu li").removeClass("active");
        $(".tab-menu li[data-tab=" + sel + "]").addClass("active");

    });

    $("input.numeric").filter_input({ regex: '[0-9]' });

    if (triggerEvalPremiumState)
        evaluatePremiumState();
});

function InitTabs() {
    $(".tab-content .tab").hide();

    var defaultTab = $(".tab-content").attr("default");

    $(".tab-content #" + defaultTab).show();
    $(".tab-menu li[data-tab=" + defaultTab + "]").addClass("active");
}
