$(document).ready(function() {
	
	var mouseInMainNaviForbidden = false;
	var pendingIndexBigPicture = null;
	var automaticSeek = true;

    var root = $("#menu_scroller").scrollable({
        circular: true, next: '', prev: '' 
    }).autoscroll({ autoplay: true, interval:8000 });
    
    var api = root.data("scrollable");
    
    api.onBeforeSeek(function(){ 
    	if (automaticSeek == true) {
    		$("#menu_scroller div").fadeTo("fast", 0.1);  
    	}
    }); 
    
    api.onSeek(function(){   	  	 	
		if (pendingIndexBigPicture==api.getIndex() || automaticSeek == true) {
	        var indexBigPicture = this.getIndex();
	        $('#main_navi img').each(function(index) {
	    		var src = $(this).attr("src").replace(/\d_/, indexBigPicture+'_');
	            $(this).attr("src", src);
	    	}); 
	        if (automaticSeek == true) {
	        	$("#menu_scroller div").fadeTo("fast", 1);
	        } 
		}
    }); 
    
    function doSlidingBlock() {
    	automaticSeek = false;
		mouseInMainNaviForbidden = true;
		api.pause();
		api.seekTo(pendingIndexBigPicture, 1);
    	mouseInMainNaviForbidden = false;
    	automaticSeek = true;
    }; 
    
    $("#main_navi li").mouseover(function() {
    	pendingIndexBigPicture = $("#main_navi li").index(this);
    	if (mouseInMainNaviForbidden==false && pendingIndexBigPicture == $("#main_navi li").index(this)) {
    		doSlidingBlock();
    	}
    });         	
   
    $("#main_navi ul").mouseover(function() {
    	automaticSeek = false;
    });
    
    $("#main_navi ul").mouseout(function() {
    	pendingIndexBigPicture = null;
    	api.play();
    	automaticSeek = true;
    });
    
});
