/* 
* NavIt (v.0.0.3)
* by James Studdart (www.jamesstuddart.co.uk)
* james@studdart.co.uk
*
* Copyright (c) 2009 James Studdart
* Licensed under the GPL license. 
*
*
* NOTE: Requires jQuery framework (www.jquery.com)
* Developed for: jQuery 1.4.2
*
* 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)
* 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
        };
        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() {
                $(this).attr("id", "navIT-" + navItemId);

                $('a', this).click(function() {
                    $('ul', this).slideToggle('slide');
                });

                dropArrow = $('<span id=\"navIt-click\" class=\'navit-down\'>&nbsp;</span>');

                if (settings.ArrowSideOnRight) {
                    dropArrow.attr('style', 'float:right;');
                } else {
                    dropArrow.attr('style', 'float:left;');
                }

                dropArrow.bind("click", function() {
                    $(this).parent().children("ul").slideToggle('slide');
                    HideOtherItems($(this).parent().attr("id"));

                });

                $(this).children('ul').before(dropArrow);

                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() {
                $(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>');

                        if (settings.ArrowSideOnRight) {
                            dropArrow.attr('style', 'float:right;');
                        } else {
                            dropArrow.attr('style', 'float:left;');
                        }

                        dropArrow.bind("click", function() {
                            $(this).parent().children("ul").slideToggle('slide');
                            if (!settings.MultiSections) {
                                HideOtherItems($(this).parent().attr("id"));
                            }
                        });

                        $(this).children('ul').before(dropArrow);
                    }
                    else {

                        //Add code for hovering
                    }
                }

                navItemId++;
            });

            control.show();
        }

        function ShowDiv(pageId) {
            $(control, 'li').each(function() {
                if ($(this).attr("id") == pageId) {
                    $(this).children().show();
                    $(this).show();

                    ShowDiv($(this).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);
