var tabTimer;
var currentTab;
var $onglets;
var $div;

$(function(e) {
	setTabEvents();
	
	tabTimer = setInterval(nextTab, 7000);
	
	$onglets = $('#onglets');
	currentTab = $onglets.children('div:first-child');
	
	$('div.capture').click(function() {
		clearInterval(tabTimer);
	});

	if($('#envedette_container').children().size() > 1) {
		$('#envedette_container').cycle({ 
			fx:     'fade', 
			timeout: 10000,
			cleartype: 1
		 });
	 }
});

function stopTimer() {
	clearInterval(tabTimer);
}

function nextTab() {
	currentTab = currentTab.next('div');
	if(currentTab.size() == 0) {
		currentTab = $onglets.children('div:first-child');
	}
	$div = currentTab;
	changeTab(currentTab.children('a').attr('class'));

}

function setTabEvents() {
	$('#onglets a').unbind('click');
	$('div.onglet_regulier a').click(function(e1) {
	
		var tab = $(this).attr('class');
		$div = $(this).parent();
		
		clearInterval(tabTimer);
		changeTab(tab);
		
		/*if(tab == 'saison') {
			$('#saison_presentation_background').siblings().fadeOut(300).end().fadeIn(300);
			if(document.getElementById('prochainement_presentation_video').sendEvent) {
				document.getElementById('prochainement_presentation_video').sendEvent('STOP', 'true');
			}
			if(document.getElementById('danse_presentation_video').sendEvent) {
				document.getElementById('danse_presentation_video').sendEvent('STOP', 'true');
			}
			if(document.getElementById('decouverte_presentation_video').sendEvent) {
				document.getElementById('decouverte_presentation_video').sendEvent('STOP', 'true');
			}
			if(document.getElementById('videoSaison').sendEvent) {
				document.getElementById('videoSaison').sendEvent('STOP', 'true');
			}
		}
		else if(tab == 'prochainement') {
			$('#prochainement_presentation_background').siblings().fadeOut(300).end().fadeIn(300);
			if(document.getElementById('prochainement_presentation_video').sendEvent) {
				document.getElementById('prochainement_presentation_video').sendEvent('STOP', 'true');
			}
			if(document.getElementById('danse_presentation_video').sendEvent) {
				document.getElementById('danse_presentation_video').sendEvent('STOP', 'true');
			}
			if(document.getElementById('decouverte_presentation_video').sendEvent) {
				document.getElementById('decouverte_presentation_video').sendEvent('STOP', 'true');
			}
			if(document.getElementById('videoSaison').sendEvent) {
				document.getElementById('videoSaison').sendEvent('STOP', 'true');
			}
		}
		else if(tab == 'danse') {
			$('#danse_presentation_background').siblings().fadeOut(300).end().fadeIn(300);
			if(document.getElementById('prochainement_presentation_video').sendEvent) {
				document.getElementById('prochainement_presentation_video').sendEvent('STOP', 'true');
			}
			if(document.getElementById('danse_presentation_video').sendEvent) {
				document.getElementById('danse_presentation_video').sendEvent('STOP', 'true');
			}
			if(document.getElementById('decouverte_presentation_video').sendEvent) {
				document.getElementById('decouverte_presentation_video').sendEvent('STOP', 'true');
			}
			if(document.getElementById('videoSaison').sendEvent) {
				document.getElementById('videoSaison').sendEvent('STOP', 'true');
			}
		}
		else if(tab == 'decouverte') {
			$('#decouverte_presentation_background').siblings().fadeOut(300).end().fadeIn(300);
			if(document.getElementById('prochainement_presentation_video').sendEvent) {
				document.getElementById('prochainement_presentation_video').sendEvent('STOP', 'true');
			}
			if(document.getElementById('danse_presentation_video').sendEvent) {
				document.getElementById('danse_presentation_video').sendEvent('STOP', 'true');
			}
			if(document.getElementById('decouverte_presentation_video').sendEvent) {
				document.getElementById('decouverte_presentation_video').sendEvent('STOP', 'true');
			}
			if(document.getElementById('videoSaison').sendEvent) {
				document.getElementById('videoSaison').sendEvent('STOP', 'true');
			}
		}*/
		
		setTabEvents();
	});
}
// document.getElementById("danse_presentation_video").addControllerListener("PLAY", "stopTimer")
function changeTab(id_prefix) {
	$div.addClass('onglet_current').siblings().removeClass('onglet_current');

	$('#'+id_prefix+'_presentation_background').siblings().fadeOut(300).end().fadeIn(300);
	if(document.getElementById('prochainement_presentation_video').sendEvent) {
		document.getElementById('prochainement_presentation_video').sendEvent('STOP', 'true');
	}
	if(document.getElementById('danse_presentation_video').sendEvent) {
		document.getElementById('danse_presentation_video').sendEvent('STOP', 'true');
	}
	if(document.getElementById('decouverte_presentation_video').sendEvent) {
		document.getElementById('decouverte_presentation_video').sendEvent('STOP', 'true');
	}
	if(document.getElementById('saison_presentation_video').sendEvent) {
		document.getElementById('saison_presentation_video').sendEvent('STOP', 'true');
	}
}

function playerReady(obj) {
	document.getElementById(obj.id).addViewListener("PLAY", "stopTimer");
}
