/*
// agan  2009-9-3
*/


(function($){

$.extend($.fn, {
	slider: function( options ) {

		if ( this.length < 0) {
			return false;
		}
		
		var menuID = this.attr('id');
		var con = $('#con_'+menuID);
		if( con.length != 1 ) alert( '获取 conID: con_'+menuID+' 错误!');
			
		var sliderContent = $.data(this[0], "sliderContent");
		
		if ( sliderContent ) {
			return sliderContent;
		}
		
		options.menuID = menuID;
		sliderContent = new $.slider( options , this[0]);	
		
		$.data(this[0], "sliderContent", sliderContent);
		return sliderContent;
	}
});

// constructor for slider
$.slider = function ( options , slider) {
	this.settings = $.extend( {}, $.slider.defaults, options );
	this.slider = $(slider);
	this.init();
};

$.extend($.slider, {
	defaults: {
        currentMenuClass: 'hover',        //选中的menu 的 Class
        autoSlider: false,      //是否自动滚动
		speed:5000,			//切换速度
		eventType:'mouseover'
    },
	
	prototype: {
		init: function() {
			var menuID = this.menuID = this.settings.menuID;

			if( !this.settings.currentMenuClass ) {
				this.currentMenuClass = 'hover';
			}else{
				this.currentMenuClass = this.settings.currentMenuClass;
			}
	
			this.index = 0;
			this.total = 0;
			this.currentMenu = null;
			this.currentCon = null;
			this.autoSlider = this.settings.autoSlider == true ? true : false;
			this.autoObj = null;
			
			var slider = this;
			
			$('#'+menuID+' > div').each(function(i){
				slider.total++;
				$(this).bind(slider.settings.eventType,function(){
						slider.change(i+1);
				}).css('cursor','pointer');
			});

			if( this.autoSlider == true ) {
				$('#'+menuID+' > div').each(function(i){
					$(this).bind(slider.settings.eventType,function(){
						slider.clearAuto();
					}).mouseout(function(){
						slider.setAuto();
					});
				});
				$('#con_'+menuID).mouseover(function(){
					slider.clearAuto();
				}).mouseout(function(){
					slider.setAuto();
				});
				this.setAuto();
			}
			
			
			
			
			
			this.change(1);
		},
		change:function( index ){
			var menuID = this.menuID;
			if( index < 1 || this.index == index ) return false;
			var newMenu = $('#'+menuID+' > div').eq( index - 1 );
			if( newMenu.length != 1) return false;
			if( this.currentMenu != null ) this.currentMenu.removeClass( this.currentMenuClass );
			newMenu.addClass( this.currentMenuClass );
			this.currentMenu = newMenu;
			this.index = index;
			if( this.currentCon != null ) this.currentCon.hide();
			var newCon = $('#con_'+menuID+' > div').eq( this.index - 1 );
			newCon.show();
			this.currentCon = newCon;
		},
		next : function(){
			var index = this.index;
			if( index == this.total ) {
				index = 1;
			}else{
				index++;
			}
			this.change( index );
		},
		setAuto : function(){
			if( this.autoObj != null ) return false;
			var slider = this;
			this.autoObj = window.setInterval(function(){
				   slider.next();									   
				},this.settings.speed);	
		},
		clearAuto : function(){
			window.clearInterval( this.autoObj );
			this.autoObj = null;
		}
	}
});

})(jQuery);
