/*!yanim 20111105*/
//@plugin hoverDelay
(function($){
	$.fn.hoverDelay = function(options){
		var defaults = {
			hoverDuring: 200,
			outDuring: 200,
			hoverEvent: function(){
				$.noop();
			},
			outEvent: function(){
				$.noop();
			}
		};
		var sets = $.extend(defaults,options || {});
		var hoverTimer, outTimer;
		return $(this).each(function(){
			$(this).hover(function(){
				clearTimeout(outTimer);
				hoverTimer = setTimeout(sets.hoverEvent, sets.hoverDuring);
			},function(){
				clearTimeout(hoverTimer);
				outTimer = setTimeout(sets.outEvent, sets.outDuring);
			});
		});
	}
})(jQuery);


$('document').ready(function(){
        $('#search').click(function(){
            if($('#key').val()=='') {
                alert('请填写有效的关键字');
                return false;
            }
        })
	//tab
	//注意这里tab是div标签，如果是其它标签则改一下
	$('div.tab,div.tab2').each(function(){
		var _tabnav = $(this).find('.tabnav').length == 1 ? $(this).find('.tabnav') : $(this).children('.tab-hd');
		var _tabbd = $(this).children('.tab-bd');
		_tabnav.children('a').each(function(){
			var that = $(this);
			that.hoverDelay({
				hoverEvent: function () {
					if(that.hasClass('tabhd-cur')){
						return false;
					} else {
						var n = that.index();
						that.addClass('tabhd-cur').siblings().removeClass('tabhd-cur');
						_tabbd.children('.tab-cont').eq(n).addClass('tabbd-cur').siblings().removeClass('tabbd-cur');
					}
				}
			});
		});
	});

	(function (){
		//nav
		$('.item-ti').each(function(){
			var that = $(this);
			var sib = $(this).siblings('ul');
			$(this).click(function(){
				if(sib.hasClass('hide')){
					sib.removeClass('hide');
				} else {
					sib.addClass('hide');
				}
			});
		});
		//jselect
		var _sel = $('#J_select');
		var a = _sel.children('a:not(:first)');
		var s = _sel.children('.selected');
		s.click(function(){
			return false;
		});
		a.click(function(){
			var v = $(this).attr('value');
			s.attr('value',v).text($(this).text());
			$('#selinp').val(v);
			return false;
		});
		_sel.hoverDelay({
			hoverDuring: 100,
			outDuring: 100,
			hoverEvent: function(){
				a.css('display','block');
			},
			outEvent: function(){
				a.hide();
			}
		});

	})();

	//slide
	(function (){
		var _ul = $('#J_slide');
		var _show = 2;
		var _auto = false;
		var _speed = 600;
		var _delay = 3000;
		var _lis = _ul.children('li');
		var _left = 0;

		if( _lis.length > _show ) {
			var _w = _lis.outerWidth(true);
			var _t = Math.floor(_lis.length / _show) * _w * _show;
			_ul.css('width', _w * _lis.length + 'px').parent().after('<a id="btnprev" href="#"></a><a id="btnnext" href="#"></a>');

			if(_auto){
				var _scrolling = setInterval(autoScroll,_delay);

				_ul.hover(function(){
					clearInterval(_scrolling);
				},function(){
					_scrolling = setInterval(autoScroll,_delay);
				});
			}

			$('#btnprev').click(function(){
				if(_auto){
					clearInterval(_scrolling);
				}
				_left = getLeft();
				if(_left){
					_ul.animate({marginLeft: _left + (_w * _show) +"px"},150);
				}
				if(_auto){
					_scrolling = setInterval(autoScroll,_delay);
				}
				return false;
			});
			$('#btnnext').click(function(){
				if(_auto){
					clearInterval(_scrolling);
				}
				_left = -getLeft();
				if(_left < _t){
					_ul.animate({marginLeft: _left - (_w * _show) +"px"},150);
				}
				if(_auto){
					_scrolling = setInterval(autoScroll,_delay);
				}
				return false;
			});
		}

		function autoScroll() {
			_left = -getLeft();
			if(_left < _t){
				_ul.animate({marginLeft: _left - (_w * _show) +"px"},_speed);
			} else {
				_ul.animate({marginLeft: '0px'},_speed);

			}
		}
		function getLeft() {
			return parseInt(_ul.css('marginLeft'),10);
		}
	})();

});



