﻿$(document).ready(function () {
    $("#nav-menu").TopMenu();

    if ($("#slide-show").length > 0) {
        $("#slide-show").slidershow();
    }

    $("#searchbox").SearchBox();

    $(".announce").Annoucement();

    if ($("#shop-holder").length > 0) {
        $("#shop-holder").ShopFunctions();
    }

    if ($("#hp-features .hp-feature .info-box").length > 0) {
        var dta = $("#hp-features .hp-feature .info-box");
        dta.css("padding-bottom", "0px");
        var maxheight = 0;
        dta.each(function () {
            var h = $(this).outerHeight(true);
            if (h > maxheight) {
                maxheight = h;
            }
        });

        if (maxheight > 0) {
            dta.css("height", maxheight);
        }
    }

    if ($("#lgDets").length > 0) {
        addModalBackground();

        $("#lgDets").css("margin-left", "-185px");
        $("#lgDets").css("margin-top", "-185px");

        $(".LogIn").click(function (e) {
            e.preventDefault();
            OpenModal();
            setTimeout(function () {
                $("#lgDets").fadeTo(250, "0.99");
            }, 300);
        });

        $("#lgDets .cancel").click(function (e) {
            e.preventDefault();
            $("#lgDets").fadeOut(250, function () {
                CloseModal();
            });

        });
    }

    $(".shop-list .item .shopOption").click(function (e) {
        e.preventDefault();
        addModalBackground();
        OpenModal();

        if ($("body #modal-OptionPopup").length == 0) {
            var div = $("<div id=\"modal-OptionPopup\"/>");
            div.css("margin-left", "-320px");

            var a = $("<a href=\"#\" class=\"close\">Close</a>");
            a.bind("click", function (e) {
                e.preventDefault();
                $("#modal-OptionPopup").hide();
                CloseModal();
            });
            div.append(a);
            div.append($("<h3>Is This Compatible With My Bird Scarer?</h3>"));
            div.append($("<div id=\"OptionPopupDetails\"/>"));
            div.append($("<img class=\"image\" src=\"img/compatible.png\"/>"));

            $("body").append(div);
        }

        $.post("__dta/optns.ashx", { id: $(this).attr("id") }, function (txt) {
            $("#modal-OptionPopup #OptionPopupDetails").html($(txt));
        });



        setTimeout(function () {
            $("#modal-OptionPopup").show();
        }, 300);

    });

});

$.fn.Annoucement = function () {
    return this.each(function () {
        var el = $(this),
            a = $("<a href=\"#\">close</a>");

        el.prepend(a);

        el.css("background-color", "#ffff66").animate({ "background-color": "#f7f7f7" }, 1200);

        a.bind("click", function (e) {
            e.preventDefault();
            el.remove();
        });
    });
}

function addModalBackground() {
    if ($("body #modal-background").length == 0) {
        $("body").append($("<div id=\"modal-background\"/>"));
        // get the window size
        bgResize();
        // if the user changes the window size, then the BG must change
        $(window).bind("resize.backgroundScale", function () {
            bgResize();
        })
    }
}

// open function
function OpenModal() {
    $("body #modal-background").fadeTo(250, 0.6);
};
// close function
function CloseModal() {
    $("body #modal-background").fadeOut(250);
};

// gte the BG size
function bgResize() {

    var bg = $("body #modal-background"),
            _docHeight = 0,
            _windowHeight = 0,
            theHeight = 0;

    _docHeight = document.height || document.body.offsetHeight;
    _windowHeight = document.documentElement.clientHeight
    if (_docHeight > _windowHeight) {
        theHeight = _docHeight;
    }
    else {
        theHeight = _windowHeight;
    }
    bg.css("height", (theHeight + 15) + "px");
}
