/* CHARGEMENT DE LA PAGE
--------------------------------------------------------------------------------------------------------------------------------------*/

var ie6 = (!window.XMLHttpRequest);

$(function(){
	$('#mainContent').removeClass('noJS');
	
	// Header menu
	if(!ie6){
		createMenu();
		
		$('.page-item-5, .page-item-6', '#menu').bind('mouseenter', function(){
			$('.content', '#menu').hide();
			$(this).find('.content').show()
			$(this).addClass('on').find('span').addClass('selected')
			$('#menu > li > a').stop().animate({marginTop:'13px'}, 300);
		}).bind('mouseleave', function(){
			var link = $(this);
			$('#menu > li > a').stop().animate({marginTop:'78px'}, 300, function(){link.find('.content').hide()});
			$(this).removeClass('on');
			if(!$(this).find('span').hasClass('active')) $(this).find('span').removeClass('selected');
		});
	}
	
	// Inputs focus
	$('#s').focus(function(){
		if($(this).attr('value') == this.defaultValue) $(this).attr('value', '');
	}).blur(function(){
		if($.trim(this.value) == '') this.value = (this.defaultValue ? this.defaultValue : '');
	});
	
	// Produit
	if($('#sidebarProd')[0]) initProduitContent();
	
	// Contact
	if($('#cformsform')[0]) initContact();
	
	// Blank DL links
	$('#telechargement a').attr('target', '_blank');
	
	// News nav IE6
	$('#newsNav a').parent().css('display', 'block');
	
	// Tracking downloads
	$('#telechargement .btnDL').click(function(){
		pageTracker._trackPageview("/telechargements"+href.substr(href.lastIndexOf('/')));
	});
});

/* FONCTIONS
--------------------------------------------------------------------------------------------------------------------------------------*/

// Création du menu JS
function createMenu(){
	var elems = $('.page-item-5, .page-item-6', '#menu');
	var nbElems = elems.length;
	var ssElems = 0;
	var nbSSElems = 0;
	var inner = '';
	var classe = 0;
	var content = 0;
	
	for(var i=0; i<nbElems; i++){
		inner = '';
		content = $('<div class="content"></div>');
		ssElems = $(elems[i]).find('li');
		nbSSElems = ssElems.length;
		var k = 0;
		
		for(var j=0; j<nbSSElems; j++){
			classe = $(ssElems[j]).attr('class');
			$(ssElems[j]).find('a').addClass(classe);
			if(j%3 == 0){
				k++;
				if(inner) content.append(inner);
				inner = $('<div class="ssMenu ssMenu'+k+'"></div>');
			}
			inner.append($(ssElems[j]).html());
		}
		content.append(inner);
		$(elems[i]).append(content).find('ul').remove();
	}
	
	// Active elem
	$('.current_page_item', '#menu').find('span').addClass('selected active');
}

function initProduitContent(){

	var cInstance = null;
	cInitCallback = function(c){
		cInstance = c;
	};

	// Check the hash
	var hash = document.location.hash;
	if(hash){
		var link = $('#sidebarProd a[href="'+hash+'"]');
		
		$('#sidebarProd span').removeClass('selected');
		link.find('span').addClass('selected');
		
		$('.open, #presentation', '#boxFull').removeClass('open').hide();
		$('#prodContent .introContent').html($(hash + ' .introFull').html());
		
		$(hash).addClass('open').show();
	}else{
		$('#prodContent .introContent').html($('#presentation .introFull').html());
	}
	
	// Check Menu item
	//if(!$('#donnees_techniques')[0]) $('#sidebarProd .tech').parent().hide();
	//if(!$('#descriptif_CCTP')[0]) $('#sidebarProd .desc').parent().hide();
	//if(!$('#telechargement')[0]) $('#sidebarProd .dl').parent().hide();
	
	$('#sidebarProd a').click(function(){
		$('#sidebarProd span').removeClass('selected');
		$(this).find('span').addClass('selected');
		
		var link = $(this);
		$('#prodContent').slideUp(500, function(){

			$('.prodBox', '#boxFull').removeClass('open').hide();
			$('#prodContent .introContent').html($(link.attr('href') + ' .introFull').html());
			$(link.attr('href')).addClass('open').show();
			$(this).slideDown(500);
			if (link.attr('href')=='#phototheque') {cInstance.reload();}

		});
		
		window.location.hash = link.attr('href');
		
		return false;
	});
	
	$('.carrousel_photos').jcarousel({
		initCallback: cInitCallback,
		scroll: 1,
		visible: 4
	});
	
	function formatTitle(title, currentArray, currentIndex, currentOpts) {
		aTitle = title.split(" || ");
		sLegend = aTitle[0];
		sDescription = aTitle[1];
		if (sDescription != undefined) sDescription = '<br>' + sDescription;
		else sDescription = '';
		return '<div id="fancybox-title-over"><strong>' + sLegend + '</strong>' + sDescription + '</div>';
	}
	
	$("#phototheque .phototheque_contenu a").fancybox({
		'titleShow'			: true,
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'titleFormat'		: formatTitle,
		'titlePosition'		: 'over'
	});
	



	//cInstance.reload();

}

function initContact(){
	$('#msgAffaire').appendTo('#li--15items');
	
	// Statut
	$('#li--7').hide();
	$('#cf_field_6').change(function(){
		if($(this).find('option[selected]').attr('value') == 'pro') $('#li--7').show();
		else $('#li--7').hide();
	});
	
	// Chargé d'affaire
	$('#cf_field_15-1').click(function(){
		$('#msgAffaire').show();
	});
	$('#cf_field_15-2').click(function(){
		$('#msgAffaire').hide();
	});
	
	$('<h4>'+$('#li--16').text()+'</h4>').prependTo('#li--16items');
	$('<h4>'+$('#li--17').text()+'</h4>').prependTo('#li--17items');
	$('#li--16, #li--17').remove();
	$('<li class="titreDoc">Demande de documentation :</li>').insertAfter('#li--15items');
}

function trackGame(){
	pageTracker._trackPageview("/home/jeu");
}
