(function($){
	$.fn.extend({
		config: {
			pagination: false,
			showCaption: false,
			defaultPage: 1
		},
		enSlider: function(options){
			$.extend(true, this.config, options);
			obj = $(this);	
			plgObj = this;
			this.layout(obj, this.config.image);
		},
		layout: function(obj, imgs){
			obj.css({width: this.config.sliderWidth+'px', height: this.config.sliderHieght+'px'});
			if(this.config.showCaption){
				obj.append('<div class="Caption"></div>');
			}
			if(this.config.pagination){
				obj.append('<div class="paginationOpacity"></div><ul class="pagination"></ul>');
				$(".paginationOpacity").css({width: this.config.sliderWidth+'px'});
			}
			for (i=0;i<imgs.length;i++){
				obj.append('<div class="slid slid'+i+'"><a href="'+imgs[i][2]+'"><img src="'+this.config.imgPath+imgs[i][1]+'"></a></div>');
				if(this.config.pagination){
					$(".pagination").append('<li class="page page'+i+'">'+(i+1)+'</li>');
				}
			}
			
			/*$(".page").each(function(n){
				$(this).click(function(){
					clearInterval(timeId);
					plgObj.config.defaultPage = n;
					plgObj.showNext($(".slid"+n));					
				});
			});*/
			effect = this.config.effect;
			switch (effect) {
				case ('left'):
					$(".slid").css({left: this.config.sliderWidth+'px'});					
				break;
				case ('right'):
					leftpos = this.config.sliderWidth - (this.config.sliderWidth*2);
					$(".slid").css({left: leftpos+'px'});
				break;	
				case ('top'):	
					toppos = this.config.sliderHieght - (this.config.sliderHieght*2);
					$(".slid").css({top: toppos+'px'});
				break;
				case ('bottom'):	
					$(".slid").css({top: this.config.sliderHieght+'px'});
				break;		
			}

			$(".slid:first").addClass("offset").css({left: '0px', top: '0px'});
			$(".page:first").addClass("active");
			this.showNext();
			
		},
		bindClick: function(n){
			clearInterval(timeId);
			plgObj.config.defaultPage = n;
			plgObj.showNext($(".slid"+n));	

		},
		showNext: function(ele){			
			this.loadAnimation(ele);
			//setInterval( "$.fn.showNext()", this.config.animationInterval );
			timeId = setInterval( "plgObj.loadAnimation()", this.config.animationInterval );
		},
		loadAnimation: function(ele){
			if(this.config.defaultPage == this.config.image.length) {
				this.config.defaultPage = 0;
			}	
			page = this.config.defaultPage;

			if(this.config.showCaption){
				$(".Caption").html(plgObj.config.image[page][0]);
			}
			$(".page").each(function(n){
				$(this).bind('click', function(){plgObj.bindClick(n)});
				$(this).removeClass("active")
			});
			$(".page"+page).addClass("active").unbind('click');

			
			if(ele){
				curr = ele;
			}else{
				curr = ($(".offset").next('div').length > 0) ? $(".offset").next('div') : $(".slid:first");

			}

			curr_aniArgs = {};
			aniArgs = {};		

			effect = this.config.effect;
			switch (effect) {
				case ('left'):
					curr_aniArgs['left'] = 0; 
					aniArgs['left'] = this.config.sliderWidth; 					
				break;
				case ('right'):
					curr_aniArgs['left'] = 0; 
					aniArgs['left'] = this.config.sliderWidth - (this.config.sliderWidth*2);
				break;	
				case ('top'):	
					curr_aniArgs['top'] = 0; 
					aniArgs['top'] = this.config.sliderHieght - (this.config.sliderHieght*2);
				break;
				case ('bottom'):	
					curr_aniArgs['top'] = 0; 
					aniArgs['top'] = this.config.sliderHieght;
				break;		
			}
			
			curr.animate(curr_aniArgs, this.config.animationSpeed, function(){
				curr.addClass("offset");				
			});
			$(".offset").animate(aniArgs, this.config.animationSpeed, function(){
				$(this).removeClass("offset");
			});	
			this.config.defaultPage++;
		}
	});
})(jQuery);

