﻿var HIPNav = new Class({
    initialize: function (id) {
        isShowingNewHover = false;
        fadeSpeed = 250;
        currentPage = null;

        var fadingOut;
        var path = $(location).attr('pathname');
        var page = path.substring(path.lastIndexOf('/') + 1);
        var instance = this;

        $('ul.' + id).find('li').hover(function (e) {
            if (currentPage == null || $(this).attr('id') != currentPage.attr('id')) {
                isShowingNewHover = true;
                if (fadingOut == null || $(this).find("span")[0] != fadingOut[0]) {
                    var hovered = $(this);
                    $('ul.' + id).find('li').each(function (index, item) {
                        if (item.id.toLowerCase().contains(page.toLowerCase())) {
                            currentPage = $(this);
                            if (hovered[0] != currentPage[0]) {
                                currentPage.find('a').each(function (index, item) {
                                    $(this).removeClass('current');
                                });
                            }
                        }
                    });
                    var fadingIn = hovered.find('span');
                    hovered.children('a').addClass('current');
                    if (fadingIn.length > 0) {
                        fadingIn.css('zIndex', 9999);
                        fadingIn.fadeIn(fadeSpeed, function () {
                            isShowingNewHover = false;
                        });
                    }
                } else {
                    isShowingNewHover = false;
                }
                if (currentPage != null) {
                    currentPage.find('span').fadeOut(fadeSpeed);
                }
            } else {
                instance.fadeCurrentPageIn();
            }
        }, function (e) {
            //HACK: There is a slim area between the a and span which causes unhover to fire. Only occurs when there is a span under the a
            if (e.pageY < 38 || e.pageY > 40 || e.pageX < $(this).offset().left || e.pageX > $(this).offset().left + $(this).width()) {
                if (currentPage == null || $(this).attr('id') != currentPage.attr('id')) {
                    $(this).children('a').removeClass('current');
                    fadingOut = $(this).find("span");
                    fadingOut.css('zIndex', 5);
                    fadingOut.fadeOut(fadeSpeed, function () {
                        fadingOut = null;
                    });
                    instance.fadeCurrentPageIn();
                }
            }
        });

        $('ul.' + id).find('li').each(function (index, item) {
            if (item.id.toLowerCase().contains(page.toLowerCase())) {
                currentPage = $(this);
                currentPage.find('a').each(function (index, item) {
                    if (!item.href.contains('#')) {
                        $(this).addClass('current');
                    } else {
                        if (!$(location).attr('hash')) {
                            $(this).attr('class', 'current');
                            return false;
                        } else if (item.href.contains($(location).attr('hash'))) {
                            $(this).attr('class', 'current');
                        }
                    }
                });
                currentPage.find('span').fadeIn(fadeSpeed);
                currentPage.find('span').find('a').click(function () {
                    var selectedPage = $(this);
                    selectedPage.parent().find('a').each(function (index, item) {
                        if ($(this)[0] != selectedPage[0]) {
                            $(this).removeClass('current');
                        } else {
                            $(this).attr('class', 'current');
                        }
                    });
                    document.fireEvent("onMenuChange", { "Menu": $(this).attr('hash').replace('#', '') });
                });
            }
        });

        $(document).bind("mouseleave", function (e) {
            if (e.pageY - $(window).scrollTop() <= 1) {
                $('ul.primary').find('li').each(function (index, item) {
                    if (item.id.toLowerCase().contains(page.toLowerCase())) {
                        isShowingNewHover = false;
                        instance.fadeCurrentPageIn();
                    }
                });
            }
        });
    },

    fadeCurrentPageIn: function () {
        if (currentPage != null) {
            currentPage.find('a').each(function (index, item) {
                if (!item.href.contains('#')) {
                    $(this).addClass('current');
                } else {
                    if (!$(location).attr('hash')) {
                        $(this).attr('class', 'current');
                        return false;
                    } else if (item.href.contains($(location).attr('hash'))) {
                        $(this).attr('class', 'current');
                    }
                }
            });
            window.setTimeout(function () {
                if (!isShowingNewHover) {
                    currentPage.find('span').fadeIn(fadeSpeed);
                }
            }, 10);
        }
    }
});
