/* AL */


/* 初始化导航栏 */
(function($){
	
	jQuery(function(){
		
		/* 配置参数 */
		var hover_delay_lv1 = 0;/* 一级菜单延迟触发hover效果的毫秒数 */
		var hover_delay_lv2 = 0;/* 二级菜单延迟触发hover效果的毫秒数 */
		var hover_delay_leave = 300;/* 鼠标离开菜单的延迟 */
		var mousewheel_delay = 50;/* 鼠标离开菜单的延迟 */
		
		var list_max_item = 7;
		var list_step = -20;
		var list_animate_speed = 'fast';
		
		/* 找到相关控制元件 */
		var $TMN = jQuery('.js_top_main_nav');
		var $lv1_li = $TMN.find( '.li_lv1' );
		var $lv2_li = $TMN.find( '.li_lv2' );
		var $lv3_li = $TMN.find( '.li_lv3' );
		var $lv4_li = $TMN.find( '.li_lv4' );
		
		var $list_lv3 = $TMN.find( '.list_lv3' );
		var $list_lv4 = $TMN.find( '.list_lv4' );
		
		/* 初始化一级导航栏按钮 */
		var cur_hover_obj_lv1 = null;
		$lv1_li.hover(
			function(){
				cur_hover_obj_lv1 = this;
				(function(){
					var hover_obj = cur_hover_obj_lv1;
					setTimeout(function(){
						if ( cur_hover_obj_lv1 && hover_obj == cur_hover_obj_lv1 ){
							var $this = jQuery(hover_obj);
							$this.siblings('.li_lv1').removeClass('hover').removeClass('expend');
							$this.addClass('hover');
							if ( $this.hasClass('submenu') ){
								$this.addClass('expend');
							}
						}
						draw_nav();
					},hover_delay_lv1);
				})();
				
			},
			function(){
				cur_hover_obj_lv1 = null;
			}
		 );
		

		/* 初始化二级导航栏按钮 */
		var cur_hover_obj_lv2 = null;
		$lv2_li.hover(
			function(){
				cur_hover_obj_lv2 = this;
				(function(){
					var hover_obj = cur_hover_obj_lv2;
					setTimeout(function(){
						if ( cur_hover_obj_lv2 && hover_obj == cur_hover_obj_lv2 ){
							var $this = jQuery(hover_obj);
							//$this.siblings('.li_lv2').removeClass('hover').removeClass('expend').find('.ul_lv3').hide();
							$this.siblings('.li_lv2').removeClass('hover').removeClass('expend').find('.panel_lv3').hide();
							$this.addClass('hover');
							if ( $this.hasClass('submenu') ){
								//$this.addClass('expend').find('.ul_lv3').show();
								$this.addClass('expend').find('.panel_lv3').show();
								if ( !$this.find( '.li_lv3.active' ).length ){
									$this.find( '.li_lv3' ).first().data('no_animate',true).mouseenter();
								}
							}
						}
					},hover_delay_lv2);
				})();
			},
			function(){
				var $this = jQuery(this);
				cur_hover_obj_lv2 = null;
				setTimeout(function(){
					if( !cur_hover_obj_lv2 ){
						if ( !$this.hasClass('trail') ){
							$this.removeClass('expend').find('.panel_lv3').hide();
						}
						if ( !$this.hasClass('default') ){
							$this.removeClass('active').removeClass('hover');
						}
					}
				},hover_delay_lv2);
			}
		);
		
		/* 初始化三级导航栏按钮 */
		$list_lv3.each(function(){
			var $this = jQuery(this);
			init_list_nav($this,3);
		});
		
		/* 注册三级导航栏事件 */
		$lv3_li.hover(
			function( evt_obj ){
				var $this = jQuery(this);
				//不用动画弹出
				var no_animate = $this.data('no_animate');
				$this.data('no_animate',false);
			
				var $list_lv4_crop = $this.parents('.panel_lv3').find('.list_lv4_crop');
				var $list_lv4 = $list_lv4_crop.find('.list_lv4');
				var $list_window_lv4 = $list_lv4.find('.window_lv4').empty();
				if ( $this.hasClass('submenu') ){
					$this.find('.ul_lv4').clone(true).appendTo($list_window_lv4);
					init_list_nav($list_lv4_crop,4);
					var $new_list_li = $list_window_lv4.find('.li_lv4');
					if ( !$new_list_li.filter('.active').length ){
						$new_list_li.first().mouseenter();
					}
					if ( no_animate ){
						$list_lv4_crop.css({'width':180});
					}else{
						$list_lv4_crop.stop().animate( {'width':180},'fast' );
					}
				}else{
					$list_lv4_crop.stop().animate( {'width':0},'fast' );
				}
				$this.siblings().removeClass('active').end().addClass('active');
				/* ajax load product info */
				var $a = $this.find('.tit_lv3');
				if ( $a.hasClass( 'js_ajax' ) ){
					var $content_lv5 = $this.parents('.panel_lv3').find('.content_lv5');
					//$content_lv5.load($a.attr('rel'));
					load_lv5($content_lv5,$a.attr('rel'));
				}
			},
			function(){
			}
		).click(function(){
			var $this = jQuery(this);
			var $a = $this.find('.tit_lv3');
			//if ( !$a.hasClass( 'js_ajax' ) ){
			//	return false;
			//}
		});

		/* 注册四级导航栏事件 */
		$lv4_li.hover(
			function(){
				var $this = jQuery(this);
				var $content_lv5 = $this.parents('.panel_lv3').find('.content_lv5');
				var $list_lv4 = $this.parents('.panel_lv3').find('.list_lv4');
				$this.siblings().removeClass('active').end().addClass('active');
				/* ajax load product info */
				var $a = $this.find('.tit_lv4');
				if ( $a.hasClass( 'js_ajax' ) ){
					//$content_lv5.load($a.attr('rel'));
					load_lv5($content_lv5,$a.attr('rel'));
				}
			},
			function(){
				
			}
		).click(function(){
			var $this = jQuery(this);
			var $a = $this.find('.tit_lv4');
			//if ( !$a.hasClass( 'js_ajax' ) ){
			//	return false;
			//}
		});

		
		/* 如果鼠标离开导航栏，重置导航栏。 */
		var tmn_hover_flag = false;
		$TMN.hover(
			function(){
				tmn_hover_flag = true;
			},
			function(){
				setTimeout(function(){
					if ( !tmn_hover_flag ){
						reset_tmn();
					}
				},hover_delay_leave);
				tmn_hover_flag = false;
			}
		);
		
		/* 载入页面后初始化导航栏 */
		reset_tmn();
		
		
		/* 函数定义 */
		function fix_png_lv5( $content_lv5 ){
			$content_lv5.MMJS_fix_png('.p_info .p_img img');
		}
		
		var content_lv5_cache = {};
		var delay_load = 200;
		var delay_load_order = 0;
		function load_lv5( $content_lv5,url ){
			//$content_lv5.hide();
			
			delay_load_order++;
			
			(function(){
				var cur_order = delay_load_order;
				window.setTimeout(function(){
					if ( cur_order == delay_load_order ){
						if ( typeof content_lv5_cache[url] == 'undefined' ){
							$content_lv5.empty();
							jQuery.get(url,function(html){
								content_lv5_cache[url]=html;
								$content_lv5.empty().append(content_lv5_cache[url]);
								fix_png_lv5( $content_lv5 );
							});
						}else{
							$content_lv5.empty().append(content_lv5_cache[url]);
							fix_png_lv5( $content_lv5 );
						}
					}
				},delay_load);
			})();
			
		}
		
		
		
		/* 绘制导航栏 */
		function draw_nav(){
			
			/* 设置list lv2宽度和位置 */
			$lv1_li.each(function(){
				var $this = jQuery(this);
				var $lv2_list = $this.find('.ul_lv2:visible');
				
				var box_width = $TMN.find('.tmn_box').width();
				var box_padding = 32;
				var wrap_offset = - ( parseInt($TMN.find('.ul_lv1').css('left')) - box_padding );
				var wrap_width = box_width - box_padding*2;
				var btn_width = $this.outerWidth(true);
				var btn_offset_w = $this.position().left;
				
				var $lv2_items = $lv2_list.find('> li');
				
				if ( $lv2_items.length ){
					var item_total_length = 0;
					$lv2_items.each(function(){
						var $item = jQuery(this);
						var item_length = $item.outerWidth(true);
						item_total_length += item_length;
					});
					$lv2_list.css({'width':(item_total_length+10)+'px'});
					
					var min_list_offset = wrap_offset;
					var max_list_offset = wrap_offset + wrap_width - item_total_length;
					
					var list_offset = btn_offset_w - ( item_total_length - btn_width )/2 ;
					
					list_offset = ( list_offset < min_list_offset ) ? min_list_offset : list_offset ;
					list_offset = ( list_offset > max_list_offset ) ? max_list_offset : list_offset ;
					
					$lv2_list.css({'left':list_offset+'px'});
					
				}
			});
		}
		
		draw_nav();
		
		
		/* 重置到导航栏的初始状态 */
		function reset_tmn(){
			$lv1_li.removeClass('hover').removeClass('active').removeClass('expend');
			$lv2_li.removeClass('hover').removeClass('active').removeClass('expend');
			$TMN.find('li.trail').addClass('hover').addClass('expend');
			$TMN.find('li.default').addClass('active');
		}
		
		/* 初始化list 按钮 */
		function init_list_nav( list_obj,level ){
			if ( list_obj.find('.li_lv'+level).length <= list_max_item ){
				list_obj.find('.btn_up_lv'+level+',.btn_down_lv'+level).hide();
			}else{
				
				var update_btn_status = function(cur_index){
					var cur_index = cur_index || 1;
					var item_length = list_obj.data('item_length') || list_obj.find('.li_lv'+level).length;
					if ( cur_index <= 1 ){
						list_obj.find('.btn_up_lv'+level).hide();
					}else{
						list_obj.find('.btn_up_lv'+level).show();
					}
					
					if ( cur_index >= (item_length-list_max_item+1) ){
						list_obj.find('.btn_down_lv'+level).hide();
					}else{
						list_obj.find('.btn_down_lv'+level).show();
					}
				};
				
				var $move_block = list_obj.find('.ul_lv'+level);
				
				list_obj.find('.btn_up_lv'+level).unbind( 'click' ).click(function(){
					var $this = jQuery(this);
					var cur_index = list_obj.data('cur_index') || 1;
					var item_length = list_obj.data('item_length') || list_obj.find('.li_lv'+level).length;
					cur_index --;
					cur_index = ( cur_index < 1 )? 1 : cur_index;
					update_btn_status( cur_index );
					list_obj.data('cur_index',cur_index);
					var target_offset = ( cur_index - 1 ) * list_step;
					$move_block.stop().animate( {'top':target_offset},list_animate_speed );
				});
				
				list_obj.find('.btn_down_lv'+level).unbind( 'click' ).click(function(){
					var $this = jQuery(this);
					var cur_index = list_obj.data('cur_index') || 1;
					var item_length = list_obj.data('item_length') || list_obj.find('.li_lv'+level).length;
					cur_index ++;
					cur_index = ( cur_index > (item_length-list_max_item+1) ) ? (item_length-list_max_item+1) : cur_index;
					update_btn_status(cur_index);
					list_obj.data('cur_index',cur_index);
					var target_offset = ( cur_index - 1 ) * list_step;
					
					$move_block.stop().animate( {'top':target_offset},list_animate_speed );
				});
				
				var buffer = [];
				list_obj.unbind( 'mousewheel' ).mousewheel(function(event, delta){
					buffer.push( delta );
					setTimeout(function(){
						var sum = 0;
						for( var i = 0,l = buffer.length; i<l ; i++ ){
							sum += buffer[i];
						}
						var average = ( average < 0 ) ? Math.floor(sum/buffer.length) : Math.ceil(sum/buffer.length);
						buffer = [];
						if ( average ){
							
							var $this = jQuery(this);
							var cur_index = list_obj.data('cur_index') || 1;
							var item_length = list_obj.data('item_length') || list_obj.find('.li_lv'+level).length;
							
							var next_index = cur_index - average;
							next_index = ( next_index < 1 )?1:next_index;
							next_index = ( next_index > (item_length-list_max_item+1) )? (item_length-list_max_item+1) :next_index;
							update_btn_status(next_index);
							list_obj.data('cur_index',next_index);
							var target_offset = ( next_index - 1 ) * list_step;
							$move_block.stop().animate( {'top':target_offset},list_animate_speed );
						}
					},mousewheel_delay);
					
					return false;
				});
				
				update_btn_status();
				
			}			
		}

	});
	
})(jQuery);
