/* 
* NavIt (v.0.0.6)
* 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 (v0.0.6) 20/07/2010
* Change Log:
* Fixed bug with alternating arrow buttons
* added ability to use hover on top menu items in horizontal menu only and show buttons on children items
* streamlined some of the code to make the process faster
*
* NavIt (v0.0.5) 20/05/2010
* Change Log:
* Added Option to has alternating arrow button when showing child items
*
* 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,
            AlternatingButton: false,
            UseHoverOnTopOnly: false
        };
        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');
                });


                $(this).parent().children("ul").slideToggle('slide');
                if ($(this).children("ul").size() > 0) {

                    if (settings.IncludeDropdownArrow) {

                        dropArrow = $('<span id=\"navIt-click\" class=\'navit-down\'>&nbsp;</span>');
                        dropArrow.bind("click", function() {
                            if (settings.AlternatingButton) { AlternateButton($(this)); }

                            $(this).parent().children("ul").slideToggle('slide');


                            if (!settings.MultiSections) {
                                HideOtherItems($(this).parent().attr("id"));
                            }
                        });
                        //put hover on top level items if required, NO button shown, if UseHoverOnTopOnly == false button IS shown
                        //TODO: This need cleaning up and making so it doesnt use hoverIntent
                        if ($(this).parents("ul").size() == 1 && settings.UseHoverOnTopOnly) {
                            //hove code here
                            var config = {
                                over: function() { $(this).children('ul').slideToggle('slide'); }, // function = onMouseOver callback (REQUIRED)    
                                timeout: 300, // number = milliseconds delay before onMouseOut    
                                out: function() { $(this).children('ul').slideToggle('slide'); } // function = onMouseOut callback (REQUIRED)    
                            };

                            $(this).hoverIntent(config);
                        }
                        else {
                            if (settings.ArrowSideOnRight) {
                                dropArrow.attr('style', 'float:right;');
                                dropArrow.insertBefore($(this).children('a'));
                            } else {
                                dropArrow.attr('style', 'float:left;');
                                dropArrow.insertBefore($(this).children('a'));
                            }
                        }
                    }
                    else {
                        //hove code here
                        var config = {
                            over: function() { $(this).children('ul').slideToggle('slide'); }, // function = onMouseOver callback (REQUIRED)    
                            timeout: 300, // number = milliseconds delay before onMouseOut    
                            out: function() { $(this).children('ul').slideToggle('slide'); } // function = onMouseOut callback (REQUIRED)    
                        };

                        $(this).hoverIntent(config);
                    }
                }

                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;
                    }
                }
                if (settings.HighlightedPage != null) {
                    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;
                            }
                        }
                    }
                }
                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() {
                            if (settings.AlternatingButton) { AlternateButton($(this)); }

                            $(this).parent().children("ul").slideToggle('slide');
                            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
                    }
                }

                if (settings.HighlightedPage != null) {
                    //Highlight the current page in the navigation
                    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"));
                    }
                }

                navItemId++;
            });



            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;
        }

        function AlternateButton(btnControl) {


            btnControl.parent().parent().children("li").children("#navIt-click").each(function() {
                if ($(this).parent().attr("id") != btnControl.parent().attr("id")) {
                    if ($(this).hasClass("navit-down-alt")) {
                        $(this).removeClass("navit-down-alt");
                        $(this).addClass("navit-down");
                    }
                }
            });

            if ($(this).hasClass("navit-down")) {
                $(this).removeClass("navit-down");
                $(this).addClass("navit-down-alt");
            }
            else {
                $(this).removeClass("navit-down-alt");
                $(this).addClass("navit-down");
            }
        }
    }
})(jQuery);
