/**
 * 菜单
 * 
 * @author linxs@35.cn
 */
(function($) { 
	$.fn.cavanMenu = function(o) {
		// 默认参数
		o = $.extend({
			domain: '',
			data: '',
			showall: true
		}, o || {});
		
		// 插件扩展
		return this.each(function() {
			var div = $(this);
			var menus = o.data;
			
			// 根据URL取得当前菜单所属的菜单号
			var url = "";
			var now_menu_id = 0;
			if (matches =  window.location.href.match(/http:\/\/([^\/]+)(.*\.(php|html))/)) {
				url = matches[2];
			}
			if (url != "") {
				for (i = 0; i < menus.length; i++) {
					if (url.indexOf(menus[i].url) != -1) {
						now_menu_id = i;
						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;
								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;
				}
				
				// 设置控制栏状态
				var showScrollNav = false;
				if (!o.showall) {
					if (now_menus_index > 1) {
						showScrollNav = true;
						$("#scrollUp").show()
							.unbind("click")
							.bind("click", function() {
								move_up();
							});
					} else {
						$("#scrollUp").hide();
					}
					if (now_menus_index < total_menus_indexs) {
						showScrollNav = true;
						$("#scrollDown").show()
							.unbind("click")
							.bind("click", function() {
								move_down();
							});
					} else {
						$("#scrollDown").hide();
					}
				}
				/*
				if (showScrollNav) {
					$(".scrollNav").show();
				} else {
					$(".scrollNav").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);
				
				// 绑定事件
				bind_menu();
			}
			
			// 分析链接
			function parse_href(href) {
				if (!(matches = href.match(/(.*)\.(php|html)(\?.*)?/))) {
					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 bind_menu() {
				fix_subnav($(".navhover", div));
				
				div.find("li").not($(".liImg")).each(function() {
					$(this).hover(
						function() {
							if ($(this).hasClass("navhover")) {
								return false;
							}
							
							var oldnav = $(".navhover");
							oldnav.removeClass("navhover");
							$(this).addClass("navhover");
							if (o.showall) {
								return false;
							}
							
							oldnav.each(function() {
								var subnav = $(this).children(".submenu");
								subnav.hide();
							});
							fix_subnav($(this));
						}, // Over
						function() {
							
						} // Out
					);
				});
			}
			
			// 定义子菜单位置
			function fix_subnav(li) {
				var subnav = li.children(".submenu");
				if (subnav.size() == 0) return;
				
				var pos = $(".nav").position();
				pos = parseInt(pos.left);
				if (isNaN(pos)) pos = 0;
				
				var left = Math.round(pos + li.attr("offsetLeft"));
				var length = Math.round(subnav.width());
				if (left + length > $("#Navbar").attr("offsetWidth")) {
					subnav.css("right", "0px");
				} else {
					subnav.css("left", (left - pos) + "px");
				}
				subnav.show();
			}
		});
	};
})(jQuery);