// JavaScript Document

$().ready(function(){
	if($('#hsearch').is('form')){
		var k='请输入关键字搜索';
		$('#hsearchkey').val(k).focus(function(){$(this).val('');}).blur(function(){$(this).val(k);});
		$('#hsearch').submit(function(frm){
			frm=$(frm);
			if($.trim($('#hsearchkey').val())==''||$.trim($('#hsearchkey').val())==k){
				$('#hsearchkey').val(k)
				return false;
			}else{
				return true;
			};//end if
		});//end
	};//end if
	if($('#flnksele').is('select')){
		$('#flnksele').change(function(){window.location=$(this).val();});
	};//
});//end dom ready

$.extend({
	brands:{
		init:function(){
			$('#brands img').hide();
			$($('#brands img')[0]).show();
			$('#brands').data('idx',0);
			setInterval(function(){
				var idx=$('#brands').data('idx');
				idx++;if(idx>=$('#brands img').length) idx=0;
				$($('#brands img')[$('#brands').data('idx')]).animate({opacity:'hide'},'fast',function(){$($('#brands img')[idx]).animate({opacity:'show'},'fast');$('#brands').data('idx',idx);});
			},2000);//
		}//
	},//
	products:{
		init:function(){
			$('#products').hover(function(){$(this).find('ul').show();},function(){$(this).find('ul').hide();});
		}//
	}//
});

(function($){
	$.fn.page=function(opts){
		opts = jQuery.extend({id:this.attr('id'),items:[],perpage:10,an:'#',current:0,pagekey:null},opts||{});
		if(opts.items.length<=opts.perpage) return;
		var perpage=opts.perpage;
			var total=opts.items.length;
			var pages=Math.floor(total/perpage);
			if(total/perpage-pages>0) pages++;
			var pagecurrent=opts.current;
			if(opts.pagekey!=null&&typeof($.cookies)=='object'&&$.cookies.get(opts.pagekey)!=null){
				pagecurrent=parseInt($.cookies.get(opts.pagekey));
				pagecurrent=isNaN(pagecurrent)?1:pagecurrent;
			}
			if(pagecurrent<1) pagecurrent=1;
			if(pagecurrent>pages) pagecurrent=pages;
			var pstart=pagecurrent-2;
			var pend=pagecurrent+2;
			if(pstart<=0){pstart=1;pend=pstart+5;};
			if(pend>=pages){pend=pages;pstart=pend-5;pstart=pstart<=0?1:pstart;};
			this.empty();var i;
			for(i=pstart;i<=pend;i++){
				if(i==pagecurrent){$(' <strong>'+i+'</strong> ').appendTo(this);}else{$(' <a href="'+opts.an+'" title="第 '+i+' 页" >'+i+'</a> ').bind('pageclick',jQuery.extend({},opts,{current:i}),function(evt){if(typeof($.cookies)=='object'){$.cookies.set(evt.data.pagekey,evt.data.current+'');};$('#'+evt.data.id).page(evt.data);}).click(function(){$(this).trigger('pageclick');}).appendTo(this);}//end if
			};//end for
			if(pstart>1) this.prepend(' <span> ... </span> ');
			if(pagecurrent>1) $(' <a href="'+opts.an+'" title="上一页">上一页</a> | ').bind('pageclick',jQuery.extend({},opts,{current:(pagecurrent-1)}),function(evt){if(typeof($.cookies)=='object'){$.cookies.set(evt.data.pagekey,evt.data.current+'');};$('#'+evt.data.id).page(evt.data);}).click(function(){$(this).trigger('pageclick');}).prependTo(this);
			if(pend<pages) this.append('<span> ... </span>');
			if(pagecurrent<pages) $(' | <a href="'+opts.an+'" title="下一页">下一页</a>  ').bind('pageclick',jQuery.extend({},opts,{current:(pagecurrent+1)}),function(evt){if(typeof($.cookies)=='object'){$.cookies.set(evt.data.pagekey,evt.data.current+'');};$('#'+evt.data.id).page(evt.data);}).click(function(){$(this).trigger('pageclick');}).appendTo(this);
			for(i=0;i<=opts.items.length-1;i++){
				if((pagecurrent-1)*perpage<=i&&i<pagecurrent*perpage) $(opts.items[i]).show();
				else $(opts.items[i]).hide();
			};//end for
	};//end page
})(jQuery);

