
mynav = '';


//////////////////////////////////////////////////////////////////////
//

$(document).ready(function()
{
	//-RECETTES OPERATION NOVEMBRE 2011 ------------------------------------
	
	$('.recette-savoir-plus').bind('click',function() {
  		var clicked_recette_id = $(this).parents().eq(2).attr('id');
   		$('.to-hide').slideUp(500, null);
   		if( $('#'+clicked_recette_id+' .to-hide').css('display') == 'none')
   		{
   			$('#'+clicked_recette_id+' .to-hide').slideDown(500, null);
   		}
  		$('#'+clicked_recette_id+' .recette-savoir-plus').toggleClass('on');
	});
	
	// affichage du current dans le sous menu "Les recettes en compétition"
	var current_cnt = $('#container').attr('class');
	current_cnt = str_replace("hasafter","",current_cnt);
	current_cnt = str_replace("hasbefore","",current_cnt);
	current_cnt = str_replace(" ","",current_cnt);
	//alert(current_cnt);
	
	$('.concours-gagnantes .tags a').attr('href', '#');
	$('.concours-gagnantes .tags a').bind('click', function(event)
	{
		event.preventDefault();
	});
	
	$('.concours-session .tags a').attr('href', '#');
	$('.concours-session .tags a').bind('click', function(event)
	{
		event.preventDefault();
	});
		
	// les 3 sessions (fluxs de recettes)
	var array_cnt_recettes_sessions = [ 'cnt-82', 'cnt-83', 'cnt-84' ];
	if (jQuery.inArray(current_cnt, array_cnt_recettes_sessions)!=-1) {
		$('.menuitem.concours-competition a').addClass('current');
	}	
	
	// les 3 sessions de recettes gagnantes
	var array_cnt_recettes_gagnantes = [ 'cnt-85', 'cnt-91', 'cnt-92' ];
	if (jQuery.inArray(current_cnt, array_cnt_recettes_gagnantes)!=-1) {
		$('.menuitem.concours-gagnantes a').addClass('current');
	}
	
	// les 3 sessions de recettes videos
	var array_cnt_recettes_videos = [ 'cnt-93', 'cnt-94', 'cnt-95' ];
	if (jQuery.inArray(current_cnt, array_cnt_recettes_videos)!=-1) {
		$('.menuitem.concours-videos a').addClass('current');
	}	

	// les amis de perte
	var array_cnt_amies = [ 'cnt-89', 'cnt-90' ];
	if (jQuery.inArray(current_cnt, array_cnt_amies)!=-1) {
		$('.menuitem.concours-amies a').addClass('current');
	}	
		
	//----------------------------------------------------------------------
	
	$('.search_btn').click(function()
	{
		search_do ();
	});
	
	//----------------------------------------------------------------------
	
	$('#menumain .menuitem').bind('mouseenter', function()
	{
		$(this).find('ul.submenu').css('display', 'block');
	});
	
	$('#menumain .menuitem').bind('mouseleave', function()
	{
		$(this).find('ul.submenu').slideUp(150, null);
		
	});
		
	//----------------------------------------------------------------------
	
	if ( $('.candidat-header').length)
	{
		$('.backtolist.top').css('margin-top', '115px').css('margin-left', '500px').fadeIn(100, null);
	}
	
	//----------------------------------------------------------------------
	
	if ( $('.defis-compteur').length)
	{		
		defis_compteur_load ('');
	}
	if ( $('.defis-compteur-v2.live').length)
	{		
		defis_compteur_v2_load ('');
	}

	if ( $('#recette-selector').length)
	{
		$('.advflux_filters select').bind('change', function()
		{
			recette_selector_load ('');
		});
		
		$('#with-video').bind('change', function()
		{
			$('.advflux_filters select:eq(0)').trigger('change');
			// recette_selector_load ('');
		});
	
		recette_selector_load ('');
	}

	if ( $('#recette-candidats-selector').length)
	{
		$('.advflux_filters select').bind('change', function()
		{
			recette_selector_load ('candidats');
		});
		
		recette_selector_load ('candidats');
	}

	if ( $('.data-34').length)
	{	
		$('.advflux_filters select').bind('change', function()
		{
			defi_selector_load ();
		});
	}
	/*
	if ( $('#defi-selector').length)
	{
		$('.advflux_filters select').bind('change', function()
		{
			defi_selector_load ('');
		});
		
		defi_selector_load ('');
	}
	*/
	//----------------------------------------------------------------------
	
	if ( $('#gamme-selector').length)
	{
		gamme_selector_load ();
	}
	
	//----------------------------------------------------------------------
	
	if ( $('.produits-home').length)
	{
		carousel_build ();
	}
	//----------------------------------------------------------------------
	
	$('.btn-print').each(function()
	{
		$(this).attr('href', "javascript:if%20(window.print%20!=%20null)%20{%20window.print();%20}%20else%20{%20alert('Votre%20navigateur%20ne%20supporte%20pas%20cette%20fonctionalit%C3%A9.');%20}");
	});
	
	//----------------------------------------------------------------------
	
	$('.mp3_play').each(function()
	{
		$(this).click(function()
		{
			mp3_listen_to($(this).attr('title'), '../'+$(this).attr('href'));
			return false;
		});
	});
	
	$('a.video_play').each(function()
	{
		$(this).click(function()
		{
			video_play($(this).attr('title'), '/'+$(this).attr('href'));
			return false;
		});
	});
	
	$('.file_download').each(function()
	{
		$(this).attr('href', 'download.php?file='+$(this).attr('href'));
	});

	//----------------------------------------------------------------------
	
	$('.desc-readmore').bind('click', function()
	{
		$(this).slideUp(500,null);
		$('.desc-more').slideDown(500,null);
	
	});

	
	if ( $('a.video_play').length && mynav != 'ismobile')
	{
		var t = $('a.video_play:eq(0)').attr('title');
		var u = $('a.video_play:eq(0)').attr('href');		
		var o = setTimeout("video_play(' "+t+"', '/"+u+"')", 3000);
	}
	

	//----------------------------------------------------------------------

	$('.reglement_opener').bind('mousedown', function(event)
	{
		legal_open($(this).attr('title'), $('.legal').html() );
		
		event.preventDefault();
	});
});

//////////////////////////////////////////////////////////////////////

function carousel_build ()
{
	$('#carousel-produits').jcarousel({
		scroll: 1,
		easing: null,
		wrap: 'circular'
		/*
		,
		initCallback: mycarousel_initCallback,
		buttonNextHTML: null,
		buttonPrevHTML: null
		*/
	});
}
//////////////////////////////////////////////////////////////////////

function defis_compteur_load ()
{
	var data_str  = 'do=defis_compteur_load';
	
	$.ajax({
		cache: false,
		type: "POST",
		url: "/templates/bridelice/js/actions/defis_compteur_load.php",
		data: data_str, 
		success: function(feedback)
		{	
			$('.defis-compteur .nb').html(feedback);
		}
	});
}

function defis_compteur_v2_load ()
{
	var data_str  = 'do=defis_compteur_v2_load';
	
	$.ajax({
		cache: false,
		type: "POST",
		url: "/templates/bridelice/js/actions/defis_compteur_load.php",
		data: data_str, 
		success: function(feedback)
		{	
			$('.defis-compteur-v2 .nb').html(feedback);
			$('.defis-compteur-v2').fadeIn(100, null);
		}
	});
}

//////////////////////////////////////////////////////////////////////

function gamme_selector_load ()
{
	var data_str  = 'do=gamme_selector_load';
	
	$.ajax({
		cache: false,
		type: "POST",
		url: "/templates/bridelice/js/actions/gamme_selector_load.php",
		data: data_str, 
		success: function(feedback)
		{	
			$('#gamme-selector').html( $('#gamme-selector').html() + feedback);
			
			$('#gamme-selector').bind ('change', function()
			{
				window.location = this.value;
			});
		}
	});
}

//////////////////////////////////////////////////////////////////////

function recette_selector_load (zone)
{
	var data_str  = 'do=recette_selector_load';
	data_str     += '&zone='+zone;
	
	var tags_nb = $('.advflux_filters select').length;
	
	for (z=0; z<tags_nb; z++)
	{
		var val = $('.advflux_filters select:eq('+z+')').val();
		data_str +=  '&tags[]='+val;
	}	
	
	if ($('#with-video').length)
	{
		if ($('#with-video').attr('checked'))
		{
			data_str +=  '&tags[]='+$('#with-video').val();
		}
	}

	$.ajax({
		cache: false,
		type: "POST",
		url: "/templates/bridelice/js/actions/recette_selector_load.php",
		data: data_str, 
		success: function(feedback)
		{				
			$('#recette-'+(zone?zone+'-':'')+'selector').html(feedback);
			
			$('#recette-'+(zone?zone+'-':'')+'selector').bind ('change', function()
			{
				window.location = this.value;
			});
		}
	});
}

//////////////////////////////////////////////////////////////////////

function defi_selector_load (zone)
{
	var data_str  = 'do=defi_selector_load';
	data_str     += '&zone='+zone;
	
	var tags_nb = $('.advflux_filters select').length;
	
	for (z=0; z<tags_nb; z++)
	{
		var val = $('.advflux_filters select:eq('+z+')').val();
		data_str +=  '&tags[]='+val;
	}
	
	$.ajax({
		cache: false,
		type: "POST",
		url: "/templates/bridelice/js/actions/defi_selector_load.php",
		data: data_str, 
		success: function(feedback)
		{	
			feedback = feedback.split('||');
			
			if (feedback[0] == 1)
			{
				window.location = feedback[1];
			}
		}
	});
}

//////////////////////////////////////////////////////////////////////

function legal_open(title, content)
{
	Shadowbox.open({
		player : 	'html',
		title:      title,
        content:    content,
        height:     700,
        width:      660
    });
    
}


//////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////

function mp3_listen_to(title, file)
{
	Shadowbox.open({
		player : 	'flv',
		title:      title,
        content:    file,
        height:     1,
        width:      400
    });
}


//////////////////////////////////////////////////////////////////////

function video_play(title, file)
{
	Shadowbox.open({
		player : 	'flv',
        content:    file ? file : ' ', // '/medias/videos/demo.flv',
        height:     480,
        width:      640
    });
    
	clearTimeout();
}

//////////////////////////////////////////////////////////////////////
 function search_do ()
{
	var data_str  = 'do=search&';
	data_str += 'search_str='+ /*str_replace(' ', '/', */$('.search_str').val() /*)*/  ;
	/*
	window.location = './search/'+ data_str;
	*/
	$.ajax({
		cache: false,
		type: "POST",
		url: "./actions/search.php",
		data: data_str, 
		success: function(feedback)
		{	
			// alert('feedback : '+ feedback);
			$('#data').html('feedback : '+ feedback);
		}
	});
}

//////////////////////////////////////////////////////////////////////
//

function witness ()
{
	alert(page_id+' : '+ $('#data').html());
}
//////////////////////////////////////////////////////////////////////
function str_replace(search, replace, subject) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // +   improved by: Philip Peterson
    // +   improved by: Simon Willison (http://simonwillison.net)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   bugfixed by: Anton Ongson
    // +      input by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    tweaked by: Onno Marsman
    // +      input by: Brett Zamir
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
    // *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
    // *     returns 2: 'hemmo, mars'
 
    var s = subject;
    var ra = r instanceof Array, sa = s instanceof Array;
    var f = [].concat(search);
    var r = [].concat(replace);
    var i = (s = [].concat(s)).length;
    var j = 0;
    
    while (j = 0, i--) {
        if (s[i]) {
            while (s[i] = (s[i]+'').split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
        }
    }
 
    return sa ? s : s[0];
}




//////////////////////////////////////////////////////////////////////
//


