var start_ind; //FIRST IMAGE ID
var end_ind;  //LAST IMAGE ID
var total_on_stage;//NUMBERS OF LABEL ON STAGE
var slide_running = false;

//AUTOPLAY FLAGS
var timeOut_running = false;

//AUTOPLAY INTERVAL
var timeOut;


//INIT THE LABEL SYSTEM

function init_labels(start,end,total){
	
	start_ind = start; 
	end_ind = end;
	total_on_stage = total;	
	
	//override the easing metod
	jQuery.easing.def = 'easeInOutQuart';
	
	//hide the extra labels
	$('#menu_partners img').each(function(){
		current_check = parseInt($(this).attr('id'));			
		if(current_check>=start_ind+5){
			$(this).attr('srcbckp',$(this).attr('src')); //backup the original image
			$(this).attr('src','/css-img/partners/empty_icon.png');
		//	$(this).attr('titlebckp',$(this).attr('title')); //backup the original image
		//	$(this).attr('title','');						
		}
	});
	
	//LISTENER 4 MOUSE OUT FROM MASK AND AUTOPLAY
	$('#menu_mask, #but_right_partners, #but_left_partners').hover(function(){ //IN
		//stop the setTimoOut
		if(timeOut_running == true){
			clearTimeout(timeOut);
			timeOut_running = false;
		}
		
		
	},
	function(){ //OUT
		//set the timeOut
		timeOut = setTimeout("autoplay()",2000);
		timeOut_running = true;
	});
	
	$('#menu_partners').hover(function(){
		$('#menu_cont').css("left",'-4px');
		
		$(this).mousemove(function(){
			if(slide_running == false){
				//GET THE TOTAL WIDTH 4 THE menu_partners CONTAINER
				var total = 0;
				$('#menu_partners img').each(function(){
					current_check = parseInt($(this).attr('id'));			
					if(current_check>=start_ind && current_check<=start_ind+4){
						total += $(this).width();
					}
				});
				total +=15;
				total += "px";
				
				$('#menu_cont').width(total);
				//$('#menu_cont').width(720);
				//$('#menu_cont').css("left",'-4px');
			}		
		});
	},
	function(){
		$('#menu_cont').css("left",'-25px');			
		$('#menu_cont').width(674);
	});
	
	//LISTENER 4 MOUSE OVER
	$('#menu_partners')	
	.mousemove(function(){
		if(slide_running == false){
			//GET THE TOTAL WIDTH 4 THE MENU CONTAINER
			var total = 0;
			$('#menu_partners img').each(function(){
				current_check = parseInt($(this).attr('id'));			
				if(current_check>=start_ind && current_check<=start_ind+4){
					total += $(this).width();
				}
			});
			total +=15;
			total += "px";
			
			$('#menu_cont').width(total);
			$('#menu_cont').css("left",'-4px');
		}				
	})
	$('#menu_cont').mouseout(function(){
		if(slide_running == false){
			$('#menu_cont').css("left",'-25px');			
			$('#menu_cont').width(674);
		}		
	});
	
	//SET THE MENU
	var labelTransform = function(labelText, optionIndex){ //scope (this) is the #menu element
		   
		var split = labelText.split('\\n');
		var rtn = "";
			for(a=0;a<split.length;a++){
				rtn += split[a]+"<br>";
			}
		return rtn;
		}
	, dockOptions =
	  { align: 'bottom' // horizontal menu, with expansion DOWN from a fixed TOP edge
	  , labels: 'bc'  // add labels
	  , size: 110
	  , duration : 200
	  , distance :140
	   ,setLabel: labelTransform // called during initialisation to transform labels
	};
	
	//BUT RIGHT SETUP
	$('#but_right_partners').unbind('click').click(function(){
		
		if(start_ind+total_on_stage < end_ind   && slide_running == false){ 	
			slide_running = true;			
			
			$('#menu_partners').jqDock('freeze');
			
			//show the new one on the right
			$('#menu_partners img[id="'+eval(start_ind+total_on_stage)+'"]').attr('src',$('#menu_partners img[id="'+eval(start_ind+total_on_stage)+'"]').attr('srcbckp')); //restore the original image
			//$('#menu_partners img[id="'+eval(start_ind+total_on_stage)+'"]').attr('title',$('#menu_partners img[id="'+eval(start_ind+total_on_stage)+'"]').attr('titlebckp')); //restore the original image
			
			$('#menu_partners').animate({
				left: '-=130px'
			},200,function(){
				//hide the old on the left
				$('#menu_partners img[id="'+eval(start_ind)+'"]').attr('srcbckp',$('#menu_partners img[id="'+eval(start_ind)+'"]').attr('src')); //backup the original image
				$('#menu_partners img[id="'+eval(start_ind)+'"]').attr('src','/css-img/partners/empty_icon.png');
				//$('#menu_partners img[id="'+eval(start_ind)+'"]').attr('titlebckp',$('#menu_partners img[id="'+eval(start_ind)+'"]').attr('title')); //backup the original image
				//$('#menu_partners img[id="'+eval(start_ind)+'"]').attr('title','');			
				
				//update the start index
				start_ind +=1;
				
				//reset the slide flag
				slide_running = false;
				
				//re-enable the menu
				$('#menu_partners').jqDock('nudge');
				
				//check buttons
				check_lr_button();				
			});
		}	
    });
	
	//BUT LEFT SETUP
	$('#but_left_partners').unbind('click').click(function(){
	
		if(start_ind>0 && slide_running == false){ 	
			slide_running = true;			
			
			$('#menu_partners').jqDock('freeze');
			
			//show the new one on the left
			$('#menu_partners img[id="'+eval(start_ind-1)+'"]').attr('src',$('#menu_partners img[id="'+eval(start_ind-1)+'"]').attr('srcbckp')); //backup the original image
			//$('#menu_partners img[id="'+eval(start_ind-1)+'"]').attr('title',$('#menu_partners img[id="'+eval(start_ind-1)+'"]').attr('titlebckp')); //backup the original image
			
			
			$('#menu_partners').animate({
				left: '+=130px'
			},200,function(){
				//hide the old on the right
				$('#menu_partners img[id="'+eval(start_ind+total_on_stage-1)+'"]').attr('srcbckp',$('#menu_partners img[id="'+eval(start_ind+total_on_stage-1)+'"]').attr('src')); //backup the original image
				$('#menu_partners img[id="'+eval(start_ind+total_on_stage-1)+'"]').attr('src','/css-img/partners/empty_icon.png');
			//	$('#menu_partners img[id="'+eval(start_ind+total_on_stage-1)+'"]').attr('titlebckp',$('#menu_partners img[id="'+eval(start_ind+total_on_stage-1)+'"]').attr('title')); //backup the original image
				//$('#menu_partners img[id="'+eval(start_ind+total_on_stage-1)+'"]').attr('title','');	
				
				//update the start index
				start_ind -=1;
				
				//reset the slide flag
				slide_running = false;
				
				//re-enable the menu_partners
				$('#menu_partners').jqDock('nudge');
				
				//check buttons
				check_lr_button();
			});			
		}	
    });	
	
	
	
	//set the dock
	$('#menu_partners').jqDock(dockOptions);
	
	//start the autoplay
	timeOut = setTimeout("autoplay()",6000);
	timeOut_running = true;
	
	
}

function check_lr_button(){
	//alert("UGO");
	if(start_ind==1){
		$('#but_left_partners').show();
	}else if(start_ind == 0){
		$('#but_left_partners').hide();
	}
	
	if(start_ind+total_on_stage==end_ind){
		$('#but_right_partners').hide();
	}else if(start_ind+total_on_stage<=end_ind-1){
		$('#but_right_partners').show();
	}
}


//AUTOPLAY FUNCTION
function autoplay ()
{
	//MOVE THE MENU TO THE RIGHT
	if(start_ind+total_on_stage < end_ind){ 	
		slide_running = true;			
		
		$('#menu_partners').jqDock('freeze');
		
		//show the new one on the right
		$('#menu_partners img[id="'+eval(start_ind+total_on_stage)+'"]').attr('src',$('#menu_partners img[id="'+eval(start_ind+total_on_stage)+'"]').attr('srcbckp')); //backup the original image
	//	$('#menu_partners img[id="'+eval(start_ind+total_on_stage)+'"]').attr('title',$('#menu_partners img[id="'+eval(start_ind+total_on_stage)+'"]').attr('titlebckp')); //backup the original image
		
		$('#menu_partners').animate({
			left: '-=130px',
			easing: 'easeInOutQuart'
		},300,function(){
			//hide the old on the left
			$('#menu_partners img[id="'+eval(start_ind)+'"]').attr('srcbckp',$('#menu_partners img[id="'+eval(start_ind)+'"]').attr('src')); //backup the original image
			$('#menu_partners img[id="'+eval(start_ind)+'"]').attr('src','/css-img/partners/empty_icon.png');
		//	$('#menu_partners img[id="'+eval(start_ind)+'"]').attr('titlebckp',$('#menu_partners img[id="'+eval(start_ind)+'"]').attr('title')); //backup the original image
		//	$('#menu_partners img[id="'+eval(start_ind)+'"]').attr('title','');			
			
			//update the start index
			start_ind +=1;
			
			//reset the slide flag
			slide_running = false;
					
			//re-enable the menu
			$('#menu_partners').jqDock('nudge');
					
			//check buttons
			check_lr_button();		
						
		});
	}else{ //END OF THE LIST REACH - REWIND
		
		slide_running = true;			
		
		$('#menu_partners').jqDock('freeze');
		
		//show all the labels
		$('#menu_partners img').each(function(){
			if($(this).attr('src') == '/css-img/partners/empty_icon.png'){  //IF A MINITURE IS HIDE -> RESTORE THE ORIGINAL SETTING
				$(this).attr('src',$(this).attr('srcbckp')); //backup the original image
				$(this).attr('title',$(this).attr('titlebckp')); //backup the original i
			}
		});
		
		//move the menu on the origin
		$('#menu_partners').animate({
			left: '0px'			
		},300,function(){
			
			start_ind = 0;
			
			//end of animation
			$('#menu_partners img').each(function(){				
				if($(this).attr('id')>=start_ind+total_on_stage){  //LABEL OUT OF THE SCENE
					$(this).attr('srcbckp',$(this).attr('src')); //backup the original image
					$(this).attr('src','/css-img/partners/empty_icon.png');
				//	$(this).attr('titlebckp',$(this).attr('title')); //backup the original image
				//	$(this).attr('title','');
				}
			});
			
			
			
			//reset the slide flag
			slide_running = false;
					
			//re-enable the menu
			$('#menu_partners').jqDock('nudge');
					
			//check buttons
			check_lr_button();					
		});
	}
	
			
	
	//Start timeOut again
	timeOut = setTimeout ("autoplay()",3000);
}
