var tmout = 0;
var elements_to_fade = '.entertainment_index img, .services_index img, #carousel-brands img';

function store_initCallback(carousel) {	
	$('<span class="store-right-controll">&nbsp;</span><span class="store-left-controll">&nbsp;</span>').appendTo($('#store_label_controlls'))
	$('.store-right-controll, .store-left-controll').css({opacity:'0.1'});
    jQuery('.store-right-controll').bind('mouseenter', function() 
    {
    	$(this).css({opacity:'0.4'});
    	move_carousel(carousel,'next');        
        return false;
    });

    jQuery('.store-left-controll').bind('mouseenter', function() {
    	$(this).css({opacity:'0.4'});
       	move_carousel(carousel,'prev');
       	return false;
    });
    
    jQuery('.store-left-controll, .store-right-controll').bind('mouseleave', function() 
    {
    	$(this).css({opacity:'0.1'});
    	clearTimeout(tmout);       
        tmout=-1;
    });
};

function brands_initCallback(carousel) {
	if($("#carousel-brands li").length > 7)
	{
		$('<span class="brands-left-controll">&nbsp;</span><span class="brands-right-controll">&nbsp;</span>').appendTo($('#store_label_controlls'))
	    jQuery('.brands-right-controll').bind('mouseenter', function() 
	    {
	    	move_carousel(carousel,'next');        
	        return false;
	    });
	
	    jQuery('.brands-left-controll').bind('mouseenter', function() {
	       	move_carousel(carousel,'prev');
	       	return false;
	    });
	    
	    jQuery('.brands-left-controll, .brands-right-controll').bind('mouseleave', function() 
	    {
	    	clearTimeout(tmout);       
	        tmout=-1;
	    });
	}
};

function move_carousel(carousel,event)
{
	tmout = setTimeout(function(){move_carousel(carousel,event)},1);
	if(tmout==-1) return;
		if(event=='next') {
			carousel.next();
		}
		else {
			carousel.prev();
		}
}


$(document).ready(function() 
{	
	setTimeout(function(){
		if($("#carousel-stores li").length > 2)
		{
			$("#carousel-stores").jcarousel({
				scroll: 1,
				animation: 1400,
				easing : 'linear',
				initCallback: store_initCallback
			});
		}
		
		$("#carousel-brands").jcarousel({
			scroll: 1,
			animation: 800,
			easing : 'linear',
			initCallback: brands_initCallback
		});
		
		
		$(elements_to_fade).css({opacity: default_opacity})
		.bind('mouseenter',function(){
			$(this).css({opacity: 1})
		}).bind('mouseleave',function(){
			$(this).css({opacity: default_opacity})
		});
	},0);
	
});