﻿/// <reference path="jquery-1.4.1-vsdoc.js" />
/// <reference path="jquery-ui-1.8.4.custom.min.js" />

var featureCount;
var currentPosition;
var timerID;

function showNextFeature() {
    if (currentPosition < featureCount) {
        currentPosition++;
    }
    else {
        currentPosition = 1;
    }

    $("#slides li").fadeOut(1500);
    $("#slides li:nth-child(" + currentPosition + ")").fadeIn(1500);
    timerID = setTimeout("showNextFeature();", 7000);
}

function showPrevFeature() {
    if (currentPosition > 1) {
        currentPosition--;
    }
    else {
        currentPosition = featureCount;
    }

    $("#slides li").fadeOut(1500);
    $("#slides li:nth-child(" + currentPosition + ")").fadeIn(1500);
    timerID = setTimeout("showNextFeature();", 7000);
}

function clearNav() {
    $(".sub-nav").slideUp("fast");
    $(".nav > a").removeClass("selected");
    Cufon.replace('.nav > a', { hover: true });
}

$(function () {
    var navHover;
    featureCount = $("#slides li").length;
    currentPosition = 1;

    $("#main-nav").show();
    $("#slides").show();
    Cufon.replace('.nav > a', { hover: true })("h1");
    showNextFeature();


    /// Handlers for main nav

    $(".nav > a").click(function () {
        var ul = $(this).next();

        if (ul.length > 0) {
            if (ul.is(":visible")) {
                $(this).removeClass("selected")
                .next().slideUp("fast");

                return false;
            }

            $(".nav .sub-nav").slideUp("fast");
            $(".nav > a").removeClass("selected");

            $(this).addClass("selected")
            .next().slideDown("fast");

            // re-render font
            Cufon.replace('.nav > a', { hover: true });
            return false;
        }
    });

    $(".nav > a, .sub-nav").hover(
    function () {
        navHover = true;
        return false;
    },
    function () {
        navHover = false;
        return false;
    });

    $(document).click(function () {
        if (!navHover) {
            clearNav();
        }

        var list = $("#admin-menu > ul");

        if (list.is(":visible")) {
            list.fadeOut(function () {
                $("#admin-menu").animate({ width: "40px" });
            });
        }
    });

    /// Handlers for Features

    $("#feature").hover(
    function () {
        $("#feature-controls").show();
        return false;
    },
    function () {
        $("#feature-controls").hide();
        return false;
    });

    $("#next").click(function () {
        clearNav();
        clearTimeout(timerID);
        showNextFeature();
        return false;
    });

    $("#prev").click(function () {
        clearNav();
        clearTimeout(timerID);
        showPrevFeature();
        return false;
    });

    /// Handlers for content

    $(".expand").click(function () {
        $(this).toggleClass("expanded");
        $(this).next(".slide-panel").slideToggle("fast");
        return false;
    });

    $(".read-more").click(function () {
        $(this).fadeOut();
        $(this).parent().next(".slide-panel").slideDown();
        return false;
    });

    $("table.grid td").hover(
    function () {
        $(this).parent().addClass("hover");
        return false;
    },
    function () {
        $(this).parent().removeClass("hover");
        return false;
    });

    /// Handlers for contact form

    $("#tbEmail").focus(function () {
        if ($(this).val() == "" || $(this).val() == "email address") {
            $(this).val("");
        }

        return false;
    });

    $("#tbEmail").blur(function () {
        if ($(this).val() == "") {
            $(this).val("email address");
        }

        return false;
    });

    /// Handlers for Contact

    $("#contact").click(function () {
        $("#contact-bubble").show();
        $("#overlay").show();
        return false;
    });

    $("#contact-bubble .close").click(function () {
        $("#contact-bubble").hide();
        $("#overlay").hide();
        return false;
    });

    $("#content .profile").click(function () {
        $("#profile-popover").show();
        $("#overlay").show();
        return false;
    });

    $("#profile-popover .close").click(function () {
        $("#profile-popover").hide();
        $("#overlay").hide();
        return false;
    });


    /// Donate bubbles

    $("#mail-donation, #one-hundred-percent").click(function () {
        $(this).next("div.bubble").show();
        $("#overlay").show();
        return false;
    });

    $("ul.buttons .bubble .close").click(function () {
        $(this).parent().parent().hide();
        $("#overlay").hide();
        return false;
    });

    /// Admin Menu

    $("#admin-menu > .handle").click(function () {
        var list = $("#admin-menu > ul");
        var menu = $("#admin-menu");

        if (list.is(":hidden")) {
            menu.animate({ width: "+=90px" }, "normal", "swing", function () {
                list.fadeIn();
            });
        }
        else {
            list.fadeOut(function () {
                menu.animate({ width: "40px" });
            });
        }

        return false;
    });

    var scroller = $('#admin-menu');

    if (scroller.length > 0) {
        var scroller_position = scroller.offset().top;

        $(window).scroll(function () {
            var windowPosition = $(window).scrollTop();
            var destination = windowPosition;

            if (windowPosition < scroller_position) {
                destination = scroller_position;
                scroller.stop().animate({ 'top': 150 }, "normal", "swing");
            }
            else {
                scroller.stop().animate({ 'top': (destination - scroller_position) + 294 }, "normal", "swing");
            }

            return false;
        });
    }
});
