// JavaScript Document

jQuery.jQueryRandom = 0;
jQuery.extend(jQuery.expr[":"],
{
    random: function(a, i, m, r) {
        if (i == 0) {
            jQuery.jQueryRandom = Math.floor(Math.random() * r.length);
        };
        return i == jQuery.jQueryRandom;
    }
});

var controlsLocked = false;
var currentItem = 1;
var autoRun = true;

function buildNav(){
	jQuery("#petitcuistot_slideshow li").each(function(i){
		var classes = "slideshow_nr";
		if(i==0){
			classes = "slideshow_nr selected";			
		}
		jQuery('#petitcuistot_slideshow_controls').append('<a href="#" class="'+ classes +'" id="'+ (i+1) +'"><!-- --></a>');	
	});
}

function buildtabs(){
	jQuery('#petitcuistot_slideshow_controls a').bind('click', function(){
		autoRun = false;
		var tabToShow = jQuery(this).attr('id');
		showTab(tabToShow);
		return false;
	});
	showTab(currentItem);
}

function showTab(itemnr){
	jQuery("#petitcuistot_slideshow li:eq(" + (itemnr-1) + ")").fadeIn('medium').siblings().fadeOut('medium');
	jQuery('#petitcuistot_slideshow_controls a:eq('+ (itemnr-1) +')').addClass('selected').siblings().removeClass('selected');
	currentItem = itemnr;
}

function showNextTab(){
	if(autoRun){
		var nexttab = currentItem + 1;
		if(jQuery('#petitcuistot_slideshow_controls a').length < nexttab){
			nexttab = 1;
		}
		showTab(nexttab);
	}
}

jQuery(document).ready(function(){
	if(jQuery(".editmode").text() != "editmode"){
		buildNav();
		buildtabs();
	}	
	setInterval("showNextTab()", 6500);
});
