$(document).ready(function(){
	transform_stage();
	get_carousel();	
	$(".emploistages").click(function() {
		window.location.href ='http://ocgs.unilim.fr:8080/carrefour/carrefour/espace_emplois/recherche.html';
	});
});

	function transform_stage() {
		$("#debug").find('div').addClass('slide'); 
		$("#debug").find('div').addClass('emploistages'); 
		
		$('<div class="title_carousel"><img src="/squelettes/images/home/stage_home.png"></div>').insertAfter($("span.intitule"));
		$('<a href="http://ocgs.unilim.fr:8080/carrefour/carrefour/espace_emplois/recherche.html" class="linkall"></a>').insertAfter($("span.description"));
			
		$(".carousel").append($("#debug").html());
		$("#debug").remove();
	}
	
function get_carousel() {
	var item=0;
	item=$(".slide").length;
	width_slides=item*210;
	nombremax=item/3;
	nombremax=Math.ceil(nombremax);
	nombremax=nombremax-1;
	var inner_pager="";
	for (i=0; i<=nombremax; i++) {
		inner_pager += '<div class="page" id="page_'+i+'"></div>';
	}
	
	jQuery("#pager").html(inner_pager);
	
	width_pager=jQuery(".page").length*40;
	jQuery('#pager').css('width',width_pager);
	
	jQuery('#slide .carousel').css('width',width_slides);
	manage_pager(0);
	
	jQuery('.page').click(function() {
		active = jQuery(this).attr('id').substr("page_".length);
		manage_pager(active); 
		the_margin=slide_calculate_margin(active);
		slides_goto(the_margin);
	});
	jQuery('#slide_right').click(function() {
		if(jQuery(this).hasClass('actif')) {
			active=slides_get_page_active(); 
			active++;
			manage_pager(active);
			the_margin=slide_calculate_margin(active);
			slides_slideto(the_margin);
		}
	});
	jQuery('#slide_left').click(function() {
		if(jQuery(this).hasClass('actif')) {
			active=slides_get_page_active(); 
			active=active-1;
			manage_pager(active);
			the_margin=slide_calculate_margin(active);
			slides_slideto(the_margin);
		}
	});
	slides_set_timer();
	jQuery("#slides").hover(function() {
		slides_clear_timer();	
	}, function() {
		slides_set_timer();	
	});
}
function slide_calculate_margin(page) {
	the_margin=0-(page*jQuery('#slide').width());
	return the_margin;
}
function slides_set_timer() {
	timer = setInterval("slides_auto_change()",15000);
}
function slides_clear_timer() {
	clearInterval(timer);
}
function slides_auto_change() {
	active = slides_get_page_active();
	active++;
	if(active==(nombremax+1)) active=0;
	manage_pager(active);
	the_margin=slide_calculate_margin(active);
	slides_slideto(the_margin);
}
function slides_slideto(the_margin) {
		jQuery(".carousel").animate({"marginLeft":the_margin}, 500);	
}
function slides_goto(the_margin) {
	jQuery('.carousel').animate({"opacity" : 0}, 100, 
		function() {jQuery('.carousel').animate({"marginLeft" : the_margin}, 100, 
			function() {jQuery('.carousel').animate({"opacity" : 1}, 500)  
			}
		)}
	);
}
function slides_get_page_active() {
	the_page=jQuery("#pager .actif").attr('id').substr("page_".length);
	return the_page;	
}
function manage_pager(the_page) {
	jQuery(".page").removeClass('actif');
	jQuery("#page_"+the_page).addClass('actif');
	slides_manage_ends();
}
function slides_manage_ends() {
	active = slides_get_page_active();
	if(active==0) {
		jQuery('#slide_left').addClass('inactif').removeClass('actif'); 
	}
	else {
		jQuery('#slide_left').addClass('actif').removeClass('inactif'); 
	}
	if(active==nombremax) {
		jQuery('#slide_right').addClass('inactif').removeClass('actif'); 
	}
	else {
		jQuery('#slide_right').addClass('actif').removeClass('inactif'); 
	}
	if(active!=0 && active!=nombremax) {
		jQuery('#slide_left').addClass('actif').removeClass('inactif'); 
		jQuery('#slide_right').addClass('actif').removeClass('inactif'); 
	}
}

