/* 
* NavIt (v.0.0.4)
* by James Studdart (www.jamesstuddart.co.uk)
* james@studdart.co.uk
*
* Copyright (c) 2009-2010 James Studdart
* Licensed under the GPL license. 
*
*
* NOTE: Requires jQuery framework (www.jquery.com)
* Developed for: jQuery 1.4.2
*
* NavIt (v.0.0.4) 11/05/2010
* Change Log:
* Added in option to over-ride the id generated by NavIT
* Added in highlighting functionality
* fixed issue with changing side on which the arrow appears
*
* NavIt (v.0.0.3) 05/04/2010
* Change Log:
* Removed dependancy on an anchor tag to create a list item in the vertical list
*
* NavIt (v.0.0.2) 01/12/2009
* Change Log:
* Fixed MOD issue for working out level of navigation
* Added in initial horizontal navigation code
*
*/
(function($) {
    $.fn.NavIt = function(options) {
        var defaults = {
            MultiSections: false,
            IncludeDropdownArrow: true,
            ArrowSideOnRight: true,
            IsVerticalNav: true,
            OverRideId: true,
            HighlightedPage: null
        };
        var settings = $.extend({}, defaults, options);
        var control = $(this);
        control.show();
        $(document).ready(function() { BuildNavigation(); });

        function BuildNavigation() {
            if (settings.IsVerticalNav) {
                BuildVerticalNavigation();
            }
            else {
                BuildHorizontalNavigation()
            }

        }
        function BuildHorizontalNavigation() {
            $(control, 'ul li').each(function() { $(this).hide(); });

            $(control).attr("style", "width:auto;float:left;position:absolute;");
            horizSlide(control);
        }

        function horizSlide(thisControl) {
            $("ul", control).hide();
            var navItemId = 0;
            $('li', control).each(function() {

                if (settings.OverRideId)
                { $(this).attr("id", "navIT-" + navItemId); }

                $('a', this).click(function() {
                    $('ul', this).slideToggle('slide');
                });

                dropArrow = $('<span id=\"navIt-click\" class=\'navit-down\'>&nbsp;</span>');

                dropArrow.bind("click", function() {
                    $(this).parent().children("ul").slideToggle('slide');
                    HideOtherItems($(this).parent().attr("id"));

                });

                if (settings.ArrowSideOnRight) {
                    dropArrow.attr('style', 'float:right;');
                    dropArrow.insertBefore($(this).children('ul'));
                } else {
                    dropArrow.attr('style', 'float:left;');
                    dropArrow.insertBefore($(this).children('a'));
                }

                if ($(this).parents("ul").size() == 1) {
                    $(this).addClass("navit-horiz-item navit-horiz-item-top");
                }
                else {
                    var mod = (($(this).parents("ul").size() - 1) % 3);
                    switch (mod) {
                        case 1:
                            $(this).addClass("navit-horiz-item navit-horiz-item-secondary");
                            break;
                        case 2:
                            $(this).addClass("navit-horiz-item navit-horiz-item-third");
                            break;
                        case 0:
                            $(this).addClass("navit-horiz-item navit-horiz-item-fourth");
                            break;
                        default:
                            break;
                    }
                }
                navItemId++;
            });

        }

        function BuildVerticalNavigation() {
            var navItemId = 0;
            //$("ul", control).hide();

            $('li', control).each(function() {

                if (settings.OverRideId)
                { $(this).attr("id", "navIT-" + navItemId); }

                if ($(this).parents("ul").size() == 1) {
                    $(this).addClass("navit-item-top navit-item");
                }
                else {
                    var mod = (($(this).parents("ul").size() - 1) % 3);
                    switch (mod) {
                        case 1:
                            $(this).addClass("navit-item-secondary navit-item");
                            break;
                        case 2:
                            $(this).addClass("navit-item-third navit-item");
                            break;
                        case 0:
                            $(this).addClass("navit-item-fourth navit-item");
                            break;
                        default:
                            break;
                    }
                }

                if ($(this).children("ul").size() > 0) {
                    if (settings.IncludeDropdownArrow) {

                        dropArrow = $('<span id=\"navIt-click\" class=\'navit-down\'>&nbsp;</span>');

                        dropArrow.bind("click", function() {
                            $(this).parent().children("ul").slideToggle('slow');
                            if (!settings.MultiSections) {
                                HideOtherItems($(this).parent().attr("id"));
                            }
                        });

                        if (settings.ArrowSideOnRight) {
                            dropArrow.attr('style', 'float:right;');
                            dropArrow.insertBefore($(this).children('ul'));
                        } else {
                            dropArrow.attr('style', 'float:left;');
                            dropArrow.insertBefore($(this).children('a'));
                        }
                    }
                    else {

                        //Add code for hovering
                    }
                }

                navItemId++;
            });

            if (settings.HighlightedPage != null) {
                //Highlight the current page in the navigation
                $('li', control).each(function() {
                    if ($(this).attr("id") == settings.HighlightedPage) {
                        if ($(this).parents("ul").size() == 1) {
                            $(this).addClass("navit-item-top-highlighted");
                        }
                        else {
                            var mod = (($(this).parents("ul").size() - 1) % 3);
                            switch (mod) {
                                case 1:
                                    $(this).addClass("navit-item-secondary-highlighted");
                                    break;
                                case 2:
                                    $(this).addClass("navit-item-third-highlighted");
                                    break;
                                case 0:
                                    $(this).addClass("navit-item-fourth-highlighted");
                                    break;
                                default:
                                    break;
                            }
                        }

                        $(this).children().show();
                        ShowDiv($(this).parent().parent().attr("id"));
                    }
                })
            }

            control.show();
        }

        function ShowDiv(pageId) {
            $('li', control).each(function() {
                if ($(this).attr("id") == pageId) {
                    $(this).children().show();
                    $(this).show();

                    ShowDiv($(this).parent().parent().attr("id"));
                }
            });
        }

        var pageArray;

        function HideOtherItems(pageId) {
            pageArray = "";
            BuildList(pageId);
            var pages = pageArray.split(',');
            $('li', control).each(function() { if (!IsSelectedPage(pages, $(this).attr("id"))) { $(this).children("ul").slideUp(); } });
        }

        function BuildList(pageId) {
            $('li', control).each(function() {
                if ($(this).attr("id") == pageId) {
                    pageArray += pageId + ",";
                    BuildList($(this).parent().parent().attr("id"));
                };
            });
        }

        function IsSelectedPage(pages, pageId) {
            for (i = 0; i <= pages.length; i++) {
                if (pageId == pages[i]) {
                    return true;
                }
            }
            return false;
        }
    }
})(jQuery);
