var toShow = 0; //start with first one.. the best since it is unknown how many of them are
var current;
var settings = {
	timeout: '5000', // in miliseconds
	type: 'sequence' // 'sequence or 'random'
}
var myRepater;
$(function(){
	var nrOfFlipables = 0;
	var slides = [];
	$(".flipable").each(function(){
		if(nrOfFlipables==toShow){
			$(this).show();
		}else{
			$(this).hide();
		}
		slides.push($(this).attr("id"));
		nrOfFlipables++;
	});
	$(".prev").click(function(){
		var theParents	= $(this).parents();
		var theParent	= null;
		for(var i=0;theParents.length;i++){
			theParent = theParents[i];
			if($(theParent).is(".flipable")){
				var theID	= parseInt($(theParent).attr("id").split("_")[1]);
				$("#feat_"+theID).hide();
				var prevID	= (theID-1>0) ? theID - 1 : nrOfFlipables;
				$("#feat_"+prevID).show();
				clearTimeout(myRepater);
				break;
			}
		}
		return false;
	});
	$(".next").click(function(){
		var theParents	= $(this).parents();
		var theParent	= null;
		for(var i=0;theParents.length;i++){
			theParent = theParents[i];
			if($(theParent).is(".flipable")){
				var theID	= parseInt($(theParent).attr("id").split("_")[1]);
				$("#feat_"+theID).hide();
				var nextID	= (theID+1<=nrOfFlipables) ? theID + 1 : 1;
				$("#feat_"+nextID).show();
				clearTimeout(myRepater);
				break;
			}
		}
		return false;
	});
	if(slides.length>1){
		myRepater = setTimeout(function(){
			if(settings.type == 'sequence'){
				toShow = (toShow + 1 < slides.length) ? toShow + 1 : 0; // for ordered!!
			}else if(settings.type == 'random'){
				var current = 0;do{toShow = Math.floor(Math.random()*(slides.length))}while(toShow==current);
			}else{
				alert('type must either be \'sequence\' or \'random\'');
			}
			showFeat(slides, toShow, settings);
		}, settings.timeout);
	}
});
function showFeat(slides, toShow, settings){
	for(var i = 0; i < slides.length; i++){
		$("#"+slides[i]).hide();
	}
	$("#"+slides[toShow]).show();
	if(settings.type == 'sequence'){
		toShow = (toShow + 1 < slides.length) ? toShow + 1 : 0; // for ordered!!
	}else if(settings.type == 'random'){
		current = toShow;do{toShow = Math.floor(Math.random()*(slides.length))}while(toShow==current); // for random!!
	}else{
		alert('type must either be \'sequence\' or \'random\'');
	}
	myRepater = setTimeout(function(){
		 showFeat(slides, toShow, settings);
	}, settings.timeout);
}