(function($){
	$.fn.extend({
		config: {
			pagination: false,
			showCaption: false,
			autoPlay: true,
			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'});
			}
			if(this.config.effect == 'blur'){
					for (i=0;i<imgs.length;i++){
					if(imgs[i][2] !=''){
						alert(this.config.imgPath+imgs[i][1]);
					obj.append('<div class="slid slid'+i+' slidBg'+i+'"><a href="'+imgs[i][2]+'"><img src="'+this.config.imgPath+imgs[i][1]+'"></a></div>');
					}else{
                    obj.append('<div class="slid slid'+i+' slidBg'+i+'"><img src="'+this.config.imgPath+imgs[i][1]+'"></div>');
					}
					if(this.config.pagination){
					$(".pagination").append('<li class="page page'+i+'">'+(i+1)+'</li>');
					}
				}
			}else{
				for (i=0;i<imgs.length;i++){
                    if(imgs[i][2] !=''){
					obj.append('<div class="slid slid'+i+'"><a href="'+imgs[i][2]+'"><img src="'+this.config.imgPath+imgs[i][1]+'"></a></div>');
					}
					else{
obj.append('<div class="slid slid'+i+'"><img src="'+this.config.imgPath+imgs[i][1]+'"></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 ('blur'):
					$(".slid").css({display: 'none'});			
				break;
				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");
			if((this.config.autoPlay) && (imgs.length>1)){
				this.showNext();
			}

			$('.arrow1').click(function(){
				el = ($(".offset").prev('div').length > 0) ? $(".offset").prev('div').attr('class') : $(".slid:last").attr('class');
				el = el.split(' ');
				el = el[1];
				//alert(el);				
				plgObj.showNext($("."+el));
			});
			$('.arrow2').click(function(){
				el = ($(".offset").next('div').length > 0) ? $(".offset").next('div').attr('class') : $(".slid:first").attr('class');
				el = el.split(' ');
				el = el[1];
				//alert(el);				
				plgObj.showNext($("."+el));
			});
			
		},
		bindClick: function(n){
			if(this.config.autoPlay){
				clearInterval(timeId);
			}
			plgObj.config.defaultPage = n;
			plgObj.showNext($(".slid"+n));	

		},
		showNext: function(ele){			
			this.loadAnimation(ele);
			//setInterval( "$.fn.showNext()", this.config.animationInterval );
			if(this.config.autoPlay){
				timeId = setInterval( "plgObj.loadAnimation()", this.config.animationInterval );
			}/*else{
				plgObj.loadAnimation();
			}*/
		},
		loadAnimation: function(ele){			
			//alert(plgObj.config.defaultPage);
			//$(".slid").css({display: 'none'});
			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 ('blur'):
					// Nothing
				break;
				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;		
			}
			switch (effect) {
				case ('blur'):
					curr.fadeIn(2000, function(){
						curr.addClass("offset");				
					});
					$(".offset").fadeOut(2000, function(){
						$(this).removeClass("offset");
					});
				break;
				default: 
					curr.animate(curr_aniArgs, this.config.animationSpeed, function(){
						curr.addClass("offset");				
					});
					$(".offset").animate(aniArgs, this.config.animationSpeed, function(){
						$(this).removeClass("offset");
					});	
				break;
			}
			

			$("#sliderBg").removeAttr('class');
			$("#sliderBg").addClass("bg"+plgObj.config.defaultPage);
			this.config.defaultPage++;
		}
	});
})(jQuery);

