/**
 * 菜单
 * 
 * @author linxs@35.cn
 */
(function($) {
    $.fn.cavanMenu = function(o) {
        // 默认参数
        o = $.extend({
            domain: '',
            data: '',
            current: 0,
            showall: true
        }, o || {});

        // 插件扩展
        return this.each(function() {
            var div = $(this);
            var menus = o.data;

            // 根据URL取得当前菜单所属的菜单号
           // var url = "";

            var now_menu_id = o.current;
            if (matches = window.location.href.match(/http:\/\/(.*\/)+(.*\.(php|html|aspx))(((([?](\w)+){1}[=]*))*((\w)+){1}([\&](\w)+[\=](\w)+)*)*/)) {
                url = matches[2];
                if (matches[4] != null && matches[4] != "")
                    url ='/'+url+ matches[4];
            }
//            if (url != "") {

//                for (i = 0; i < menus.length; i++) {
//                  //  alert("o" + i + url + '[' + menus[i].url + ']');
//                    if (url.indexOf(menus[i].url) != -1) {
//                        now_menu_id = i;
//                      //  alert("a"+i + url + '[' + menus[i].url + ']');
//                        break;
//                    }
//                    submenu = menus[i].submenu;
//                    if (submenu.length != 0) {
//                        for (j = 0; j < submenu.length; j++) {
//                            if (url.indexOf(submenu[j].url) != -1) {
//                                now_menu_id = i;
//                         //       alert("b"+i + url + '[' + menus[i].url + ']');
//                                break;
//                            }
//                        }
//                    }
//                }
//            }

            // 配置信息（每页显示菜单数，当前菜单号）
            var show_menus_num = 8;
            var now_menus_index = now_menu_id == 0 ? 1 : Math.ceil((now_menu_id + 1) / show_menus_num);
            var total_menus_indexs = Math.ceil(menus.length / show_menus_num);

            create_menu();

            // 创建菜单
            function create_menu() {
                // 设置显示菜单的区间
                var start = o.showall ? 0 : (now_menus_index - 1) * show_menus_num;
                var end = o.showall ? menus.length : now_menus_index * show_menus_num;
                if (end > menus.length) {
                    end = menus.length;
                }

                // 设置控制栏状态
                if (!o.showall) {
                    if (now_menus_index > 1) {
                        $("#scrollUp").show()
							.unbind("click")
							.bind("click", function() {
							    move_up();
							});
                    } else {
                        $("#scrollUp").hide();
                    }
                    if (now_menus_index < total_menus_indexs) {
                        $("#scrollDown").show()
							.unbind("click")
							.bind("click", function() {
							    move_down();
							});
                    } else {
                        $("#scrollDown").hide();
                    }
                }

                // 解析数据
                var domString = '<ul class="nav_inner">';
                for (i = start; i < end; i++) {
                    if (menus[i] == undefined) {
                        break;
                    }

                    hoverClass = "";
                    if (i == now_menu_id) {
                        hoverClass = "navhover";
                    }
                    domString += '<li class="' + hoverClass + '"><a href="' + parse_href(menus[i].url) + '">' + menus[i].title + '</a>';

                    submenu = menus[i].submenu;
                    if (submenu.length != 0) {
                        domString += '<div class="submenu"><div><div>';
                        for (j = 0; j < submenu.length; j++) {
                            domString += '<a href="' + parse_href(submenu[j].url) + '">' + submenu[j].title + '</a>';
                            if (j < submenu.length - 1) {
                                domString += '<span class="subline"></span>';
                            }
                        }
                        domString += '</div></div></div>';
                    }
                    domString += '</li>';
                    if (i < end - 1) {
                        domString += '<li class="liImg"></li>';
                    }
                }
                domString += '</ul>';
                div.html(domString);

                // 确定子菜单显示的位置
                var hoverDiv = $(".navhover", div);
                var subnav = hoverDiv.children(".submenu");
                var left = hoverDiv.attr("offsetLeft");
                var length = subnav.width();
                if (left + length > $("#Navbar").width()) {
                    subnav.css("right", "0px");
                } else {
                    subnav.css("left", left + "px");
                }
                subnav.fadeIn("fast");

                // 绑定事件
                navMenu();
            }

            // 发布
            function release() {
                var dom = '<div class="outPut"><div class="putOn"><div class="inPut"><div class="none">未</div></div><div class="footEdges"></div></div></div>';

                return dom;
            }

            // 分析链接
            function parse_href(href) {
                if (!(matches = href.match(/(.*)\.(php|html|aspx)(\?.*)?/))) {
                    //href += ".html";
                }

                return o.domain + href;
            }

            // 上一页
            function move_up() {
                old = now_menus_index;
                now_menus_index--;
                if (now_menus_index <= 1) {
                    now_menus_index = 1;
                }
                if (old != now_menus_index) {
                    create_menu();
                }
            }

            // 下一页
            function move_down() {
                old = now_menus_index;
                now_menus_index++;
                if (now_menus_index >= total_menus_indexs) {
                    now_menus_index = total_menus_indexs;
                }
                if (old != now_menus_index) {
                    create_menu();
                }
            }

            // 菜单移动时的子菜单显示
            function navMenu() {
                div.find("li").not($(".liImg")).each(function() {
                    $(this).hover(
						function() {
						    if ($(this).hasClass("navhover")) {
						        return false;
						    }
						    $(".navhover").each(function() {
						        var subnav = $(this).children(".submenu");
						        $(this).removeClass("navhover");
						        subnav.fadeOut("fast");
						    });
						    $(this).addClass("navhover");

						    var subnav = $(this).children(".submenu");
						    var left = $(this).attr("offsetLeft");
						    var length = subnav.width();
						    if (left + length > $("#Navbar").width()) {
						        subnav.css("right", "0px");
						    } else {
						        subnav.css("left", left + "px");
						    }
						    subnav.fadeIn("fast");
						}, // Over
						function() {

						} // Out
					);
                });
            }
        });
    };
})(jQuery);