﻿$(document).ready(function () {
    var getParameterByName, catParam, deptParam;
    getParameterByName = function getParameterByName(name) {
        name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regexS = "[\\?&]" + name + "=([^&#]*)";
        var regex = new RegExp(regexS);
        var results = regex.exec(window.location.href);
        if (results == null)
            return "";
        else
            return decodeURIComponent(results[1].replace(/\+/g, " "));
    }
    if (window.location.pathname === "/news") {
        $('.news_events ul > p').css('margin-top', '32px');
        $('#resetSelection').css('margin-top', '29px');
    }
    if (window.location.pathname === "/events") {
        $($('.label')[1]).css('padding-top', '20px');
        $('#resetSelection').css('margin-top', '29px');
    }
    if (window.location.pathname === "/giving" || window.location.pathname === "/news" || window.location.pathname === "/events") {
        $($('.label')[1]).css('padding-top', '20px');
        if (window.location.getParameter) {
            catParam = unescape(window.location.getParameter('cat'));
            deptParam = unescape(window.location.getParameter('dept'));
        } else {
            catParam = getParameterByName('cat');
            deptParam = getParameterByName('dept');
        }
        if (catParam === "" || catParam === "ALL") {
            $($('.label')[0].nextElementSibling.children[0]).addClass('selected');
        }
        if (deptParam === "" || deptParam === "ALL") {
            $($('.label')[1].nextElementSibling.children[0]).addClass('selected');
        }
        if (catParam !== "" && catParam !== "ALL") {
            jQuery('[data-val="' + catParam + '"]').addClass('selected');
            $($('.label')[0].nextElementSibling.children[0]).removeClass('selected');
        }
        if (deptParam !== "" && deptParam !== "ALL") {
            jQuery('[data-val="' + deptParam + '"]').addClass('selected');
            $($('.label')[1].nextElementSibling.children[0]).removeClass('selected');
        }
    }
    $("div.dropdown").hide();
    $(".num").keypress(function (e) { return numbersOnly(e); });
    $(".phone").keypress(function (e) { return phoneOnly(e); });
    $(".fp").keypress(function (e) { return floatpt(e); });
    $(".url").keypress(function (e) { return URL(e); });

    var pageName = $('meta[name=pageName]').attr("content");
    var pageType = $('meta[name=pageType]').attr("content");
    switch (pageName) {
        case "high-school-programs":
            $("#highschool_link").addClass("current");
            break;
        case "Alumni":
            $("#alumni_link").addClass("current");
            break;
        case "students":
            $("#students_link").addClass("current");
            break;

        case "parents":
            $("#parents_link").addClass("current");
            break;
    }

    $("li.work_tile").live("click", function (e) {
        e.preventDefault();
        var id = $("#pagecontext .reference").val();
        var idx = $("ul.worklist li").index($(this));
        dbAJAX("popslideshow.ajax", "?id=" + id + '&idx=' + idx + salt(), popSlides);
    });

    $("a.work_tile").live("click", function (e) {
        e.preventDefault();
        var id = $("#pagecontext .reference").val();
        var idx = $("ul.student_work li").index($(this).parent());
        dbAJAX("popstudentslideshow.ajax", "?id=" + id + '&idx=' + idx + salt(), popSlides);
    });

    $("#modal, .closeme").live("click", function () {
        $("#popover").hide();
        $("#modal").hide();

    });
    $("#slidenext").live("click", function () {

        var idx = $("#popover .popinner li").index($("li.current"));
        var ll = $("#popover .popinner li").length - 1;
        idx += 1;
        if (idx > ll) { idx = 0; }
        $("#popover .popinner li").removeClass("current");
        $("#popover .popinner li").eq(idx).addClass("current");
        ll += 1;
        idx += 1;
        fixSlide(idx + '/' + ll);

    });

    $("#slideprevious").live("click", function () {

        var idx = $("#popover .popinner li").index($("li.current"));
        var ll = $("#popover .popinner li").length - 1;
        idx -= 1;
        if (idx < 0) { idx = ll; }
        $("#popover .popinner li").removeClass("current");
        $("#popover .popinner li").eq(idx).addClass("current");
        ll += 1;
        idx += 1;
        fixSlide(idx + '/' + ll);

    });

    $("ul.filter li>a").live("click", function (e) {
        e.preventDefault();
        var action = $(this).attr("href");
        alert(window.location.hostname + window.location.pathname);
    });

    var dropIt;
    var closeIt;
    $("#navigation li").hover(function () {
        clearTimeout(closeIt);
        var thisThing = $(this);
        var showDropdown = function () { thisThing.children("div.dropdown").slideDown(200); };
        dropIt = setTimeout(showDropdown, 80);
        $(this).siblings("li").children("div.dropdown").slideUp();
    }, function () {
        clearTimeout(dropIt);
        var thisThing = $(this);
        var hideDropdown = function () { thisThing.children("div.dropdown").slideUp(); };
        closeIt = setTimeout(hideDropdown, 500);
    });

    //AJAX Form Handler
    $("form.formwrapper").submit(function (e) {
        e.preventDefault();
        $("input[example], textarea[example]").each(function () {
            if ($(this).val() == $(this).attr('example')) {
                $(this).val('');
            }
        })
        var f = $(this)
        var serializedForm = $(f).serialize();
        var action = $(f).attr("action");
        if (serializedForm.length > 0 && action.length > 0) { serializedForm += "&sendto=" + action; }
        $.post("formsprocessor.aspx", serializedForm, function (data) {
            var ra = data.split("||");
            if (ra[0] == "OK") {
                switch (ra[2].toUpperCase()) {
                    case "SUCCESSREDIRECT":
                        if (ra[1].toLowerCase() == "refresh") {
                            window.location.href = window.location.href;
                        } else {
                            window.location.href = ra[1];
                        }
                        break;

                    case "SUCCESSFORM":
                        //set $(f).id to the new forms entity
                        $(f).html(ra[1]);
                        break;

                    default:
                        $(f).html(ra[1]);
                        break;
                }
            } else if (ra[0] == "HASERRORS") {
                $(f).html(ra[1]);
                $("#email-error").fadeIn(120);
                $("#emailID").focus();
                timeout = window.setTimeout(closeEmailError, 4000);
            } else {
                //uhoh :( internal errors or something??
            }
            init();
        });
    });


    $("form.formwrapperzzzzzz").submit(function (e) {
        e.preventDefault();
        var f = $(this)
        var serializedForm = $(f).serialize();
        var action = $(f).attr("action");
        if (serializedForm.length > 0 && action.length > 0) { serializedForm += "&sendto=" + action; }
        $.post("formsprocessor.aspx", serializedForm, function (data) {
            var ra = data.split("||");
            if ((ra[0] == "OK") || (ra[0] == "HASERRORS")) {
                //There is no default action
                if ((ra[2].charAt(0) == "@") && (ra[0] == "OK")) {
                    window.location.href = ra[2].substr(1) + "?" + ra[1];
                } else {
                    $(f).html(ra[2]);
                }
            }
        });
    });

    //BUGTracker
    $("#reportBug").live("click", function () {
        serializedForm = "location=" + urlencode(window.location.href);
        var action = "BugReporter.aspx?action=show";
        $.post(action, serializedForm, showBugForm);
    });

    $("form#bugReporter .submit").live("click", function () {
        var serializedForm = $("#bugReporter").serialize();
        var action = "BugReporter.aspx?action=submit";
        $.post(action, serializedForm, showBugForm);
        $("form#bugReporter, #bugMask").remove();
    });

    $("form#bugReporter #levelID option").live("click", function () {
        $("form#bugReporter #levelID").attr("style", $(this).attr("style"));
    });

    $("form#bugReporter .bugClose").live("click", function () {
        $("form#bugReporter, #bugMask").remove();
    });

    /* Focus and Exit Form Fields  */
    $("input[example]").each(function (index) {
        var ex = $(this).attr("example");
        if (ex != 'undefined') {
            $(this).val(ex);
            $(this).addClass("example");
        }
    });

    $("input").focus(function () {
        var ex = $(this).attr("example");
        if (ex == $(this).val()) {
            $(this).val("");
            $(this).removeClass("example");
        }
    });
    $("input").blur(function () {
        if ($(this).val() == "") {
            $(this).val($(this).attr("example"));
            $(this).addClass("example");
        }
    });

});

//Functions

function popSlides(r) {

    var ra = r.split("||");
    if (ra[0] == "OK") {
        $("#modal").show();
        $("#popover").html(ra[1]);
        var idx = parseInt(ra[2])
        $("ul.poplist li").eq(idx).addClass("current");
        $("#popover").show();
        var ll = $("#popover .popinner li").length;
        idx += 1;
        fixSlide(idx + '/' + ll);

        $("#popover .descriptionblock").bind({
            click: function (e) {
                e.preventDefault();
                if ($(this).parent().parent().find('.description div p').html().length < 1) { return; };
                $(this).parent().parent().children(".description").toggleClass("active");
                $("#popover span.plusopen").toggleClass("active");
            }
        });

    }
}

function fixSlide(pagination) {
    $("#popover").centerInClient();
    var h = ($("#popover .popinner").height() / 2) - 2;
    $("#popover .controls").css("top", h + "px");
    $("#popover .pagination").html(pagination);
}

//Bug Reporting
function showBugForm(r) {
    $("form#bugReporter, #bugMask").remove();
    $("body").append(r);

    $("form#bugReporter #levelID option").css({ "color": "white","width":"100%"});
    $("form#bugReporter #levelID option:eq(0)").css({"color": "black", "background-color": "white"});
    $("form#bugReporter #levelID option:eq(1)").css("background-color", "red");
    $("form#bugReporter #levelID option:eq(2)").css("background-color", "orange");
    $("form#bugReporter #levelID option:eq(3)").css("background-color", "gold");
    $("form#bugReporter #levelID option:eq(4)").css("background-color", "blue");
    $("form#bugReporter #levelID option:eq(5)").css("background-color", "green");
    $("form#bugReporter #levelID").attr("style", $("form#bugReporter #levelID option:selected").attr("style"));
}
