var wait = 0;
var enCours = 0;
var ad_wait = 0;
var ad_enCours = 0;
var footer_wait = 0;
var footer_enCours = 0;

function showDiv(divId)
{
	$(".js_shown").not('#'+divId).fadeOut(600 , function ()
		{
			$("#" + divId).removeClass('js_hidden').addClass("js_shown").fadeIn(600);
			$(this).removeClass('js_shown').addClass("js_hidden");
							   
		}
	);
};

function resetWait()
{
	wait = 0;
}

function resetAdWait()
{
	ad_wait = 0;
}

function resetFooterWait()
{
	footer_wait = 0;
}

function showSection(i, r)
{
	//if(r === undefined)
	if(typeof r === 'undefined')
	{
		r = true;
	}
	if(r)
	{
		wait = 1;
		setTimeout('resetWait()', 7000);
	}
    enCours = enCours + 1;
    //console.log('En cours : '+ enCours);
    var curId = $('.js_shown').attr('id');
	if(typeof curId === 'undefined')
	{
		return false;
	}
	else
	{
		if(curId != 'js_section' + i && enCours == 1)
		{
	/*			$('#' + curId).hide('slide', { direction: 'left' }, 500, function() {
				$('#js_section' + i).addClass('js_shown');
				$('#js_section' + i).removeClass('js_hidden');
				$('#js_section' + i).show('slide', { direction: 'right' }, 500);
				$('#' + curId).addClass('js_hidden');
				$('#' + curId).removeClass('js_shown');
			});*/
			var test = curId.indexOf('js_subSection');
			if(test != -1)
			{
				curId = curId.substr(13);
			}
			else
			{
			curId = curId.substr(10);
			}
			$('#js_section' + curId).fadeOut('normal', function()
				{
					$('#js_section' + i).fadeIn('normal');
					$('#js_section' + i).addClass('js_shown');
					$('#js_section' + i).removeClass('js_hidden');
					$('#js_section' + curId).removeClass('js_shown');
					$('#js_section' + curId).addClass('js_hidden');
					//$('.js_shown').attr('style', 'display: block;');
					$('.js_shown').css('display', 'block');
					enCours = 0;
				}
			);
			$('#js_subSection' + curId).fadeOut('normal', function()
				{
					$('#js_subSection' + i).fadeIn('normal');
				}
			);
		}
		else if (curId == 'js_section' + i)
		{
		   enCours = 0;
		}
	}
}

function billboard(j, speed)
{
	if(wait == 1)
	{
		setTimeout('billboard(' + j + ',' + speed + ')', 1000);
	}
	else
	{
		var newId = j + 1;
		if(!document.getElementById('js_section' + newId))
		{
			newId = 0;
		}
		showSection(newId, false);
		setTimeout('billboard(' + newId + ', ' + speed + ')', speed);
	}
}

function showAdSection(i, r)
{
	//if(r === undefined)
	if(typeof r === 'undefined')
	{
		r = true;
	}
	if(r)
	{
		ad_wait = 1;
		setTimeout('resetAdWait()', 10000);
	}
    ad_enCours = ad_enCours + 1;
    //console.log('En cours : '+ enCours);
    var curId = $('.js_ad_shown').attr('id');
	if(typeof curId === 'undefined')
	{
		return false;
	}
	else
	{
		if(curId != 'js_ad_section' + i && ad_enCours == 1)
		{
/*			$('#' + curId).hide('slide', { direction: 'left' }, 500, function() {
				$('#js_section' + i).addClass('js_shown');
				$('#js_section' + i).removeClass('js_hidden');
				$('#js_section' + i).show('slide', { direction: 'right' }, 500);
				$('#' + curId).addClass('js_hidden');
				$('#' + curId).removeClass('js_shown');
			});*/
			var ad_test = curId.indexOf('js_ad_subSection');
			if(ad_test != -1)
			{
				curId = curId.substr(16);
			}
			else
			{
				curId = curId.substr(13);
			}
			$('#js_ad_section' + curId).fadeOut('normal', function()
				{
					$('#js_ad_section' + i).fadeIn('normal');
					$('#js_ad_section' + i).addClass('js_ad_shown');
					$('#js_ad_section' + i).removeClass('js_ad_hidden');
					$('#js_ad_section' + curId).removeClass('js_ad_shown');
					$('#js_ad_section' + curId).addClass('js_ad_hidden');
					//$('.js_shown').attr('style', 'display: block;');
					$('.js_ad_shown').css('display', 'block');
					ad_enCours = 0;
				}
			);
			$('#js_ad_subSection' + curId).fadeOut('normal', function()
				{
					$('#js_ad_subSection' + i).fadeIn('normal');
				}
			);
		}
		else if (curId == 'js_ad_section' + i)
		{
		   ad_enCours = 0;
		}
	}
}

function advertisement(j, speed)
{
	if(ad_wait == 1)
	{
		setTimeout('advertisement(' + j + ',' + speed + ')', 1000);
	}
	else
	{
		var newId = j + 1;
		if(!document.getElementById('js_ad_section' + newId))
		{
			newId = 0;
		}
		showAdSection(newId, false);
		setTimeout('advertisement(' + newId + ', ' + speed + ')', speed);
	}
}

function showFooterSection(i, r)
{
	//if(r === undefined)
	if(typeof r === 'undefined')
	{
		r = true;
	}
	if(r)
	{
		footer_wait = 1;
		setTimeout('resetFooterWait()', 7000);
	}
    footer_enCours = footer_enCours + 1;
    //console.log('En cours : '+ enCours);
    var curId = $('.js_footer_shown').attr('id');
	if(curId != 'js_footer_section' + i && footer_enCours == 1) {
/*			$('#' + curId).hide('slide', { direction: 'left' }, 500, function() {
			$('#js_section' + i).addClass('js_shown');
			$('#js_section' + i).removeClass('js_hidden');
			$('#js_section' + i).show('slide', { direction: 'right' }, 500);
			$('#' + curId).addClass('js_hidden');
			$('#' + curId).removeClass('js_shown');
		});*/
		var footer_test = curId.indexOf('js_footer_subSection');
		if(footer_test != -1)
		{
			curId = curId.substr(20);
		}
		else
		{
			curId = curId.substr(17);
		}
		$('#js_footer_section' + curId).fadeOut('normal', function()
			{
				$('#js_footer_section' + i).fadeIn('normal');
				$('#js_footer_section' + i).addClass('js_footer_shown');
				$('#js_footer_section' + i).removeClass('js_footer_hidden');
				$('#js_footer_section' + curId).removeClass('js_footer_shown');
				$('#js_footer_section' + curId).addClass('js_footer_hidden');
				//$('.js_shown').attr('style', 'display: block;');
				$('.js_footer_shown').css('display', 'block');
				footer_enCours = 0;
			}
		);
		$('#js_footer_subSection' + curId).fadeOut('normal', function()
			{
				$('#js_footer_subSection' + i).fadeIn('normal');
			}
		);
	}
	else if (curId == 'js_footer_section' + i)
	{
	   footer_enCours = 0;
	}
}

function partners(j, speed)
{
	if(footer_wait == 1)
	{
		setTimeout('partners(' + j + ',' + speed + ')', 1000);
	}
	else
	{
		var newId = j + 1;
		if(!document.getElementById('js_footer_section' + newId))
		{
			newId = 0;
		}
		showFooterSection(newId, false);
		setTimeout('partners(' + newId + ', ' + speed + ')', speed);
	}
}

function starter(speed)
{
	/*var ct = 0;
	while(document.getElementById('js_section' + ct))
	{
		ct++;
	}
	var start = Math.floor(Math.random() * ct);*/
	wait = 1;
	setTimeout('resetWait()', 7000);
	billboard(0, speed);
	/* DISABLED FROM QR
	ad_wait = 1;
	setTimeout('resetAdWait()', 10000);
	advertisement(0, 10000);
	footer_wait = 1;
	setTimeout('resetFooterWait()', 7000);
	partners(0, speed);*/
}

jQuery(function($)
{
	/* afficher cacher */		
	$('.js_menu a[href^=#]').click(function()
	{				
		toggleSection(this);
				
		if(!$.browser.msie)
		{
			//update the hash
			var name =  $(this).attr('href').substring(1);
			var anchor = $('a[name="' +name + '"]');
			anchor.attr('name', name+'_temp');
			window.location.hash = $(this).attr('href');	
			anchor.attr('name', name);			
		}
		return false;				
	});
			
	/**/
	/*location change*/
	if($('.js_menu').length > 0)
	{
		$.history.init(function()
		{	
			var page = location.hash.substring(1);
			
			if(page != '' && $("a[href=#" + page + "]").length > 0)
			{           
				//$('.js_menu a[href="#' + page + '"]').click();
				toggleSection($('.js_menu a[href="#' + page + '"]'));
			}
			else
			{
				$('.collapsible:first').show();
				$('.js_menu a').removeClass('active');
				$('.js_menu a:first').addClass('active');
			}
		});
	}
	
	$(PanoramAh.init)
});

function toggleSection(obj)
{
	var idx = $('.js_menu a[href^=#]').removeClass('active').index(obj);				
	$(obj).addClass('active');
	$('.collapsible').hide();				
	$('.collapsible').eq(idx).show();
}
