﻿

$(document).ready(function () {

    //Set default enter button
    $(".enter-submit").live("keypress", function (e) {
        if ($(this).parents(".enter-form").find(".submit-button").length <= 0) return true;

        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {

            $(this).parents(".enter-form").find(".submit-button").click();
            return false;
        } else {
            return true;
        }
    });

    //Set Message Modal popup 
    $("#dialog").dialog({
        autoOpen: false,
        title: 'Message',
        height: 120,
        resizable: false
    })

    $("#closeDialog").click(function () {
        $("#dialog").dialog('close');
        return false;
    })

    //$('#topnav a[href="<%=Request.RawUrl %>"]').addClass("current");
    $("#websitesearch .textbox").watermark("Search", { useNative: false });

    $("#websitesearch a").click(function () {
        window.location = "/search-results?keywords=" + $("#websitesearch #searchText").val();
    });

    /* Top Menu Slider */
    $("#topmenu li").not(".sub li").hover(function () {

        $(this).children(".sub").removeClass("mouseleft");

        if ($(this).children(".sub").hasClass("opened") == false) {
            $(this).children("ul").slideDown({ duration: 1000, easing: 'easeOutBounce', complete: function () {
                $(this).delay(200).addClass("opened");
                if ($(this).hasClass("mouseleft")) {
                    $(this).fadeOut();
                    $(this).removeClass("opened");
                }
            }
            });
        }
    }, function () {

        $(this).children(".sub").addClass("mouseleft");

        if ($(this).children(".sub").hasClass("opened")) {
            $(this).children(".sub").slideUp({ duration: 1000, complete: function () {
                $(this).delay(200).removeClass("opened");
            }
            });
        }
    });

    //Left/Right Always Visible.

    var offset = $("#col1").offset();
    var topPadding = 15;

    if ($(window).scrollTop() > offset.top) {
        if ((($("#body").height() - $("#col1").height()) + $("#body").offset().top) > $(window).scrollTop()) {
            $("#col1").stop().animate({
                marginTop: $(window).scrollTop() - offset.top + topPadding
            });
        }

    } else {
        $("#col1").stop().animate({
            marginTop: 0
        });
    };
    $(window).scroll(function () {
        if ($(window).scrollTop() > offset.top) {
            if ((($("#body").height() - $("#col1").height()) + $("#body").offset().top) > $(window).scrollTop()) {
                $("#col1").stop().animate({
                    marginTop: $(window).scrollTop() - offset.top + topPadding
                });
            }

        } else {
            $("#col1").stop().animate({
                marginTop: 0
            });
        };
    });

    //Gallery Menu
    $(".gallery li").not(".sub li").hover(function () {

        $(this).children(".sub").removeClass("mouseleft");

        if ($(this).children(".sub").hasClass("opened") == false) {
            $(this).children("ul").slideDown({ duration: 1000, easing: 'easeOutBounce', complete: function () {
                $(this).delay(200).addClass("opened");
                if ($(this).hasClass("mouseleft")) {
                    $(this).fadeOut();
                    $(this).removeClass("opened");
                }
            }
            });
        }
    }, function () {

        $(this).children(".sub").addClass("mouseleft");

        if ($(this).children(".sub").hasClass("opened")) {
            $(this).children(".sub").slideUp({ duration: 1000, complete: function () {
                $(this).delay(200).removeClass("opened");
            }
            });
        }
    });


    //Show your support counter
    $(".supportbutton a").click(function () {

        $.ajax({
            type: "POST",
            url: "/services/generic.asmx/IncreaseSupportCount",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {

                var json = $.parseJSON(msg.d);

                var total = json.currentCount;

                if (total > 0) {

                    if (total >= $("#supportCountMinimum").val()) {

                        if ($(".supportcount").is(":hidden")) {
                            $(".supportcount").fadeIn();
                        }

                        if ($(".supportcount .numbers").is(":hidden")) {
                            $(".supportcount .numbers").show();
                            $(".supportcount p strong").html("PEOPLE HAVE SHOWN THEIR SUPPORT");
                        }

                        $(".supportcount .numbers span:eq(4)").removeClass();
                        $(".supportcount .numbers span:eq(4)").addClass(json.slot5Class).addClass("last");


                        if ($(".supportcount .numbers span:eq(3)").hasClass(json.slot4Class) == false) {
                            $(".supportcount .numbers span:eq(3)").removeClass();
                            $(".supportcount .numbers span:eq(3)").addClass(json.slot4Class);
                        }

                        if ($(".supportcount .numbers span:eq(2)").hasClass(json.slot3Class) == false) {
                            $(".supportcount .numbers span:eq(2)").removeClass();
                            $(".supportcount .numbers span:eq(2)").addClass(json.slot3Class);
                        }

                        if ($(".supportcount .numbers span:eq(1)").hasClass(json.slot2Class) == false) {
                            $(".supportcount .numbers span:eq(1)").removeClass();
                            $(".supportcount .numbers span:eq(1)").addClass(json.slot2Class);
                        }

                        if ($(".supportcount .numbers span:eq(0)").hasClass(json.slot1Class) == false) {
                            $(".supportcount .numbers span:eq(0)").removeClass();
                            $(".supportcount .numbers span:eq(0)").addClass(json.slot1Class);
                        }

                    }
                    else {
                        $(".supportcount .numbers").hide();
                        $(".supportcount p strong").html("THANK YOU FOR SHOWING YOUR SUPPORT!");
                        $(".supportcount").fadeIn();
                    }

                }
                else {
                    $("#dialog p").html("Support counter failed! please contact technical support.");
                    $("#dialog").dialog('open');
                }
            },
            error: function (msg) {
                $("#dialog p").html(msg.d);
                $("#dialog").dialog('open');
            }
        });

        return false;

    });

    //retrospective
    $('#retrospective').cycle({
        fx: 'scrollLeft',
        speed: 2000,
        timeout: 3000,
        pager: '#retropager'
    });

    //Show your support counter
    $("a.pledgeCounter").click(function () {

        $.ajax({
            type: "POST",
            url: "/services/generic.asmx/IncreasePledgeCount",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {

                var json = $.parseJSON(msg.d);

                var total = json.currentCount;

                if (total > 0) {
                    $("#pledgeimpove p strong span").html(total);
                }
                else {
                    $("#dialog p").html("Pledge counter failed! please contact technical support.");
                    $("#dialog").dialog('open');
                }
            },
            error: function (msg) {
                $("#dialog p").html(msg.d);
                $("#dialog").dialog('open');
            }
        });

        return false;

    });

    //Font size

    $("#textsize a").click(function () {

        if ($(this).hasClass("small")) {
            $("body").css("font-size", "12px");
        }
        else if ($(this).hasClass("normal")) {
            $("body").css("font-size", "13px");
        } else {
            $("body").css("font-size", "14px");
        }

        return false;
    });

});

$("#retronav a.play").live("click", function () {
    $('#retrospective').cycle('resume');
    $(this).removeClass("play");
    $(this).addClass("pause");
    return false;
});

$("#retronav a.pause").live("click", function () {
    $('#retrospective').cycle('pause');
    $(this).removeClass("pause");
    $(this).addClass("play");
    return false;
});

function copytoclipboard(obj) {

    obj.focus();
    obj.select();
    //alert("hello21");

}
