(function($){
function WPANIMATOR(root,settings){
	var wrap = root.find(settings.items);
	var tSize	=	wrap.children().length;
	if(tSize <= 0 )
	return false;
	$.extend(root,{
			 initWPWrap : function(){
			 if(settings.npInside == true){
				var holder = $(settings.holder,root);
				var nextbtn	=	$(settings.next,root);
				var prevbtn = 	$(settings.prev,root);
				prevbtn.appendTo(holder).css({"position" : "absolute", "top" : Math.ceil(holder.height() / 3), "left" : "5px", "z-index" : "1200"});
				nextbtn.appendTo(holder).css({"position" : "absolute", "top" : Math.ceil(holder.height() / 3), "right" : "10px" , "z-index" : "1200"});
				nextbtn.hide();
				prevbtn.hide();
				holder.hover(function(){
									  nextbtn.fadeTo("fast",0.80,function(){$(this).fadeIn('fast')});
									  prevbtn.fadeTo("fast",0.80,function(){$(this).fadeIn('fast')});
									  },function(){
										nextbtn.fadeOut('fast');
										prevbtn.fadeOut('fast');
									  });
			}
				 var heights = new Array();
				 if(settings.effect != 'slider'){
					 totalWidth = $(settings.holder,root).width();
					 wrap.width(totalWidth).css("position","relative");
					 i = 0;
					 wrap.children().each(function(){
												   $(this).find(".wphpw_description").data("par",i).appendTo(holder);
												   var diff = $(this).outerWidth(true) - $(this).width();												   
												   $(this).width(totalWidth - diff).css({"float" : "left"}).height(root.height());											
												   });
					 wrap.children().eq(0).show();
				 }else{
				if(settings.horizontal){
					totalWidth = $(settings.holder, root).width();					
					itemWidth	=	Math.ceil(totalWidth / settings.size);
					i = 0;
					wrap.children().each(function(){
						$(this).find(".wphpw_description").data("par",i).appendTo(holder);
						var diff = $(this).outerWidth(true) - $(this).width();
						$(this).width(itemWidth - diff);
						heights.push($(this).outerHeight(true));
						i++;
					});
					maxh = Math.max.apply( Math, heights );
					if(settings.fixHeight) { 
					$(settings.holder, root).css({"height" : wrap.children().eq(0).outerHeight(true)});
					}
			}else{
					totalHeight = $(settings.holder, root).height();	
					itemHeight	=	Math.ceil(totalHeight / settings.size);
					wrap.children().each(function(){
						$(this).find(".wphpw_description").data("par",i).appendTo(holder);
						var wdiff = $(this).outerWidth(true) - $(this).width();
						var diff = $(this).outerHeight(true) - $(this).height();
						$(this).width($(settings.holder, root).width() - wdiff).css("float","none").height(itemHeight - diff);						
					});
				}
				 }
				wrap.data("cur_item",0);
				if(settings.descriptionBar == true){

			}
			
				root.find(settings.prev).click(function(e){
						root.moveWPPrev();
						e.preventDefault();
				});
				root.find(settings.next).click(function(e){
						root.moveWPNext();
						e.preventDefault();
				});
				root.find(settings.nextPage).click(function(e){
								root.moveWPNextPage();
								e.preventDefault();
															});
				root.find(settings.prevPage).click(function(e){
								root.moveWPPrevPage();
								e.preventDefault();
															});
				var navi = root.find(settings.navi);
				if(navi && navi.children().length > 0 ){
				navis	=	0;
				navi.children().each(function(){
					$(this).find("a").attr("goto",navis).click(function(e){
						wrap.data("clicked",navis);
						root.moveWPTo($(this).attr("goto"));							
						e.preventDefault();
					 });
					navis++;
				});
		
				}
			 },
			 moveWPNext : function(){
				 cur = wrap.data("cur_item");
				 if(!cur){
				 	root.moveWPTo(1);
				 }else{
					if(settings.loop &&  (cur == tSize - 1)){
						root.moveWPTo(0);
					}else{
				 		root.moveWPTo(parseInt(cur +  1));
					}
				 }
				 return false;
			 },
			 moveWPPrev : function(){
				 cur = wrap.data("cur_item");
				 if(!cur){
				 	root.moveWPTo(0);
				 }else{
						root.moveWPTo(parseInt(cur -  1));					
				 }
				 return false;
			 },
			 moveWPNextPage : function(){
				 cur = wrap.data("cur_item");
				 cur_page = parseInt(cur + settings.size);
				 root.moveWPTo(cur_page);
			 },
			 moveWPPrevPage : function(){
				 cur = wrap.data("cur_item");
				 cur_page = parseInt(cur - settings.size);
				 root.moveWPTo(cur_page);
			 },
			 moveWPTo : function(page){
				wrap.children().removeClass(settings.activeClass);
				tSize = wrap.children().length;
				$(settings.navi, root).find(".active").removeClass("active");
				$(settings.navi, root).find("li:eq(" + page + ")").addClass("active");
				if(page < 0 ) { page = 0; }
				if(page > (tSize - settings.size)){ return; }
				wrap.removeData("cur_item").data("cur_item",page);
				var cur_item = wrap.children().eq(page);
				cur_item.addClass(settings.activeClass).attr("indez",page);
				if(!cur_item.length) { return; }
				if(settings.effect == 'fadeIn'){				
					wrap.children().hide();
					cur_item.fadeIn(settings.speed);
				}else{
				if(settings.horizontal){			
					var left = -(cur_item.outerWidth(true) * page);
					if(settings.fixHeight) { $(settings.holder, root).animate({"height" : (cur_item.outerHeight(true))},settings.speed,settings.easing); }
					wrap.animate({left: left}, settings.speed, settings.easing);
				}else{
					var top = -(cur_item.outerHeight(true) * page); // wrap.offset().top - item.offset().top;
					wrap.animate({top: top}, settings.speed, settings.easing);				
				}
				}
			}
			
			 });
	root.initWPWrap();	
}

$.fn.wpAnimator = function(options){	
var opts = {
			size: 1,
			horizontal: true,
			loop: true,
			interval: 200,
			speed: 400,
			activeClass:'active',
			hoverClass: null,			
			items: '.items',
			fixHeight : true,
			prev: '.prev',
			next: '.next',
			nextPage : '.nextPage',
			prevPage : '.prevPage',
			navi: '.navi',
			holder: '.s_content',
			easing : 'swing',
			effect : 'slider',
			descriptionBar : true,
			afterSeek : null,
			npInside : true,
			autoplay :''
		}; 
	options = $.extend(opts, options);
	this.each(function(){
					   var elem = new WPANIMATOR($(this),options);
					   });
	
	return this;
}
})(jQuery);
