jQuery(function($){
	
	if(typeof carousel_config == 'undefined'){
		return;
	}
	
	var magentoteam_carousel = {
		itemWidth:null,
		position:0,
		per_page:3,
		active_item:2,
		items:[],
		itemsCache:[],
		itemsHtml:[],
		url:null,
		selector:'',
		proccess:false,
		init:function(selector){
			
			
			var magentoteam_carousel = this;
			
			this.itemWidth	= $(selector+' ul li').get(0).offsetWidth;
			this.items	= carousel_config.items;
			this.per_page	= carousel_config.per_page;
			this.url		= carousel_config.url;
			this.active_item = carousel_config.active;
			this.selector	= selector;
			this.itemsHtml	= carousel_config.itemsHtml;
			this.itemsPreloaded	= carousel_config.itemsPreloaded;
			this.mousedown = false;
			if(this.items.length > this.per_page){
				
				$(selector+' ul li').each(function(i){
					magentoteam_carousel.itemsCache[i] = $(this).clone(true);
				});
				
				$(selector+' ul').prepend('<li>&nbsp;</li>').css({marginLeft:'-'+this.itemWidth+'px',marginRight:'-'+this.itemWidth+'px'});
				
				$(selector+' .next-btn, '+selector+' .prev-btn').mousedown(function(){
					magentoteam_carousel.mousedown = true;
					if(magentoteam_carousel.proccess){
						return false;
					}
					
					magentoteam_carousel.proccess = true;
					
					if($(this).hasClass('prev-btn')){
						magentoteam_carousel.prev();
					}else{
						magentoteam_carousel.next();
					}
					
					return false;
				}).mouseup(function(){
					magentoteam_carousel.mousedown = false;
				});
				
			}
			return this;
		},
		next:function(){
			
			
			if(this.position > this.items.length - 1){
				this.position = 0;
			}
			
			var next_item = this.position+this.per_page;
			
			if(next_item >= this.items.length){
				next_item = (this.position+this.per_page)-this.items.length;
			}
			
			
			if(this.itemsPreloaded.length && this.itemsPreloaded[this.items[next_item]]){
				
				magentoteam_carousel.animateNext();
				magentoteam_carousel.position++;
				$(magentoteam_carousel.selector+' ul').append(this.itemsPreloaded[this.items[next_item]].clone(true));
				return true;
			}
			
			if(this.itemsCache[next_item]){
				
				magentoteam_carousel.animateNext();
				magentoteam_carousel.position++;
				$(magentoteam_carousel.selector+' ul').append(this.itemsCache[next_item].clone(true));
				return true;
			}
			
			
			var content = $(magentoteam_carousel.itemsHtml[this.items[next_item]]);
			
			magentoteam_carousel.itemsCache[next_item] = content.clone(true);
			
			content.find('img').load(function(){
				magentoteam_carousel.animateNext();
				
			})
			
			$(magentoteam_carousel.selector+' ul').append(content);
			
			magentoteam_carousel.position++;
		
			
		},
		animateNext:function(){
			
			magentoteam_carousel.hideActive();
			
			$(magentoteam_carousel.selector + ' li:first').animate({marginLeft:-magentoteam_carousel.itemWidth}, 450, magentoteam_carousel.mousedown ? 'linear' : 'swing', function(){
				$(magentoteam_carousel.selector + ' li:first').remove();
				magentoteam_carousel.showActive();

				if(magentoteam_carousel.mousedown){
					magentoteam_carousel.next();
				}else{
					magentoteam_carousel.proccess = false;
				}
			});
		},
		prev:function(){
			
			if(this.position < 1){
				this.position = this.items.length;
			}
			
			var prev_item = this.position-1;
			
			if(prev_item < 0){
				prev_item = this.items.length-1;
			}
			
			if(this.itemsPreloaded.length && this.itemsPreloaded[this.items[prev_item]]){
			
				magentoteam_carousel.position--;
				$(magentoteam_carousel.selector+' ul li:first').replaceWith(this.itemsPreloaded[this.items[prev_item]].clone(true));
				magentoteam_carousel.animatePrev();
				return true;
				
			}
			if(this.itemsCache[prev_item]){
				
				magentoteam_carousel.position--;
				$(magentoteam_carousel.selector+' ul li:first').replaceWith(this.itemsCache[prev_item].clone(true));
				magentoteam_carousel.animatePrev();
				return true;
			}
			
			var content = $(magentoteam_carousel.itemsHtml[this.items[prev_item]]);
			
			magentoteam_carousel.itemsCache[prev_item] = content.clone(true);
			
			content.find('img').load(function(){
				magentoteam_carousel.animatePrev();
			})
			
			$(magentoteam_carousel.selector+' ul li:first').replaceWith(content);
			
			magentoteam_carousel.position--;
		
		},
		animatePrev:function(){
			magentoteam_carousel.hideActive();
			
			$(magentoteam_carousel.selector + ' ul').prepend('<li style="margin-left:-'+magentoteam_carousel.itemWidth+'px">&nbsp;</li>').children('li:first').animate({marginLeft:0}, 450, magentoteam_carousel.mousedown ? 'linear' : 'swing', function(){
				$(magentoteam_carousel.selector + ' li:last').remove();
				magentoteam_carousel.showActive();
				
				if(magentoteam_carousel.mousedown){
					magentoteam_carousel.prev();
				}else{
					magentoteam_carousel.proccess = false;
				}
			});
		},
		showActive:function(callback){
			
			var info = $(magentoteam_carousel.selector + ' li').eq(this.active_item).find('div.product-info');
			info.css({display:'block'});
			
			if(typeof callback == 'function'){
				callback();
			}
			
		},
		hideActive:function(callback){
			
			var info = $(magentoteam_carousel.selector + ' li').eq(this.active_item).find('div.product-info');
			info.css({display:'none'});
			
			if(typeof callback == 'function'){
				callback();
			}
			
			
		}
	};
	
	
	magentoteam_carousel.init('div.magento-team-carousel').showActive();
	
});