var Rococo = {};
Rococo.main_carrousel_auto = true;

function change_carousel(){
	var cc = jQuery('.main_featured_carrousel > .visible');
	var nc = cc.next();
	if(nc.length != 1) nc = jQuery(cc.parent().children().get(0));
	if(cc != nc){
		var nc_zi = nc.css('z-index');
		if(nc_zi > cc.css('z-index')){
			nc.css('z-index', cc.css('z-index'));
			cc.css('z-index',  nc_zi);
		}
		if(nc.get(0) == cc.get(0)) return true;
		var cd = cc.find('.det');
		cd.animate({right: -(cd.outerWidth())}, 400);
		nc.css({display:'block'});
		nc.css({opacity:1});
		cc.removeClass('visible');
		nc.addClass('visible');
		cc.animate({opacity:0}, 600, function(){
			jQuery(this).hide().siblings('.visible').find('.det').animate({right: 0}, 400);
		});
	}
}

jQuery(document).ready(function($){

      // Use jQuery with $(...)
      var d = $('.cc_wrap .det');
      	d.each(function(i, n){
      	if(i+1 < d.length) $(this).css({right: -($(this).outerWidth())});
      	
      	$(this).prev().hover(function(){
      		Rococo.main_carrousel_auto = false;
      	}, function(){
      		var det = jQuery(this).next();
      		setTimeout(function(){if(!det.hasClass('hover')){ Rococo.main_carrousel_auto = true;}}, 100);
      	});
      	$(this).hover(function(){
      		$(this).addClass('hover');
      	}, function(e){
      		$(this).removeClass('hover');
      		var os = $(this).offset();
      		var dm = {w:$(this).width(),h:$(this).height()};
      		if(!( e.pageX < os.left + dm.w && e.pageY > os.top && e.pageY < os.top + dm.h )){
      			Rococo.main_carrousel_auto = true;
      		}
      	});
      	
      });
      
      $('.cc_wrap').each(function(i){
      	$(this).css({'z-index' : i+1});
      });
      
      $('.main_featured_carrousel .cc_wrap:last').addClass('visible');
      $('.main_featured_carrousel .cc_wrap:last').siblings().hide();
      setInterval(function(){if(Rococo.main_carrousel_auto)change_carousel();}, 8000);
      
      
      //Featured Product Carrousels
      
      $('.feaprlist:visible').each(function(){
      	var list_width = 0;
      	
      	if($('.list .product', this).length < 1){
      		$(this).hide();
      		return;
      	}
      	
      	$('.list .product', this).each(function(){
      		list_width += $(this).outerWidth(true);
      	});
      	
      	var mr = $('.list .product:first').css('margin-right').replace(/px/, '');
      	$('.list', this).css('width', list_width);
      	
      	var mask = $('.mask', this);
      	mask.css('overflow', 'hidden');
      	var mask_width = mask.width();
      	
      	var set_count_d = list_width / mask_width;
      	var set_count_a = Math.floor(set_count_d);
      	var pagi = $('.pagi', this);
      	
      	var al = $('a:first', pagi);
      	var pral = al;
      	var p = 1;
      	for(var i = 1; i <= set_count_a; ++i){
      		p = i+1;
	      	var link = al.clone();
	      	link.html(p);
	      	link.removeClass('current');
	      	link.attr('rel', (p <= set_count_d)? i + (mr/mask_width) :  set_count_d - 1);
	      	pral.after(link);
	      	pral = link;
	    }
	    
	    
      	pagi.css('left', ($(this).width() - pagi.width() + 40) / 2);
	    
	    $('a', pagi).click(function(){
	    	$(this).parents('.feaprlist').find('.list').animate(
	    		{'left':-(mask_width * $(this).attr('rel'))}, 500
	    		);
	    	$(this).siblings('.current').removeClass('current');
	    	$(this).addClass('current');
		    return false;
	    });
	    
	    $('.prev', this).mousedown(function(){
	    	var ms = $(this).siblings('.mask');
	    	var pw = ms.width();
	    	var l = $('.list', ms);
			var w = l.width();
			var o = l.position();
			var mt = w*0.8;
			var md = pw - w;
			var cd = pw - w - o.left;
			if((o.left - pw) < 0){
				l.animate({left: 0}, mt * (o.left / md));
			}
			return false;
		});
			
		$('.next', this).mousedown(function(){
	    	var ms = $(this).siblings('.mask');
	    	var pw = ms.width();
	    	var l = $('.list', ms);
			
			var w = l.width();
			var o = l.position();
			var mt = w*0.8;
			var md = pw - w;
			var cd = pw - w - o.left;
			if((o.left - pw) > -(w)){
				l.animate({left: md}, mt * (cd / md));
			}
			return false;
		});


		$('.next, .prev', this).click(function(){ return false; }).mouseup(function(){
			var mk = $(this).siblings('.mask')
			var li = mk.find('.list').stop();
			var p = li.position();
			var pos = -(p.left) / mk.width();
			
			$(this).siblings('.pagi').find('a').each(function(){
				if($(this).attr('rel') >= pos){
			    	$(this).siblings('.current').removeClass('current');
			    	$(this).addClass('current');
			    	return false;
				}
			});
		});
	    
      });
 });