﻿/* ripan.js */

var Ripan = function () {

    var galleryTimer = 0;

    bindEvents = function () {

        /* sub navigation - dropdown */
        $("#nav li").each(function (i, item) {

            if ($(item).hasClass("dropdown")) {

                $("a:first", item).click(function () {

                    if ($(item).hasClass("active")) {
                        $("ul", item).hide();
                        $(item).removeClass("active");
                    }
                    else {
                        contractSubNavigation();
                        clearActiveNavItems();

                        $("ul", item).fadeIn(200);
                        $(item).addClass("active");
                    }
                    return false;
                });

            }
            else {
                $("a:first", item).click(function () {
                    clearActiveNavItems();
                    $(this).addClass("active");
                });
            }
        });

        // set first item as active
        if ($("#gallery").length != 0) {
            $("#gallery li:first").addClass("active");
            $("#galleryPager li:first").addClass("active");
        }

        // automatic gallery switcher /         
        if ($("#gallery li").length > 1) {
            galleryTimer = setTimeout(function () {
                showArticle(1);
            }, 5000);
        }

        // gallery navigation
        $("#galleryPager a").click(function () {
            var index = $(this).attr("rel");
            showArticle(index);

            return false;
        });

    };

    // display next gallery item
    showArticle = function (nextIndex) {

        var currentIndex = $("#galleryPager li.active a").attr("rel");

        $("#galleryPager li a[rel=" + currentIndex + "]").parent().removeClass("active");
        $("#galleryPager li a[rel=" + nextIndex + "]").parent().addClass("active");

        var upcomingIndex = nextIndex + 1;

        if (upcomingIndex >= $("#gallery li").length)
            upcomingIndex = 0;

        $("#gallery li").eq(currentIndex).hide().removeClass("active");

        var next = $("#gallery li").eq(nextIndex);
        var upcoming = $("#gallery li").eq(upcomingIndex);

        next.show().addClass("active");

        if (galleryTimer > 0)
            clearInterval(galleryTimer);

        galleryTimer = setTimeout(function () {
            nextIndex++;

            if (nextIndex >= $("#gallery li").length)
                nextIndex = 0;

            showArticle(nextIndex);
        }, 5000);

    }

    clearActiveNavItems = function () {
        $("#nav > li").each(function (i, item) {
            $(item).removeClass("active");
            $(item).removeClass("selected");
        });
    }

    contractSubNavigation = function () {
        $("#nav>li").each(function (i, item) {
            if ($(item).hasClass("dropdown") && $(item).hasClass("active")) {
                //var h = $("#header").height();
                $("ul", item).hide();
                //h = h - $("ul", item).height();
                //$("#header").height(h);
            }
        });
    }

    return {
        init: function () {
            bindEvents();

            // border-radius support in ie
            // messes up positioning in IE when added to navigation elements...
            DD_roundies.addRule('#wrapper', '9px 9px');

            // set coming up on media container
            if ($("#gallery").length != 0) {
                var next = $("#gallery li:first").next();
                var title = $(".article-content h2", next).text();
                $(".coming-up a", "#gallery li:first").html("<span>Coming up:</span> " + title);
            }

            // show footer on content pages
            if ($("#startPageWrapper").length == 0) {
                $("#footerContainer").show();
            }

            // hide gallery wrapper if empty           
            if ($("#galleryWrapper").has("img").length == 0)
                $("#galleryWrapper").hide();
        }
    };
} ();


$(document).ready(function () {
	Ripan.init();
});
