function Set_Cookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}


function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function Delete_Cookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
	

function openShareFTCByMail(){
	
	var rel = "shareFTC.php"; 
	
	$("#sharePopUpContent").load(rel, function(){ $("#sharePopUp").fadeIn(); }); 
	
	$("#close_btn2").click(function(){
			$("#sharePopUp").fadeOut();
	});
}

function openShareByMail(id_collection){
	
	var rel = "shareColl.php?id_collection="+id_collection; 
	
	$("#newsPopUpContent").load(rel, function(){ $("#newsPopUp").fadeIn(); }); 
	
	$("#close_btn").click(function(){
			$("#newsPopUp").fadeOut();
	});
}

function openShareNewsByMail(id_news){
	
	var rel = "shareNews.php?id_news="+id_news; 
	
	$("#sharePopUpContent").load(rel, function(){ $("#sharePopUp").fadeIn(); }); 
	
	$("#close_btn2").click(function(){
			$("#sharePopUp").fadeOut();
	});
}

function openShareAppliByMail(id_news){
	
	var rel = "shareAppli.php?id_appli="+id_news; 
	
	$("#sharePopUpContent").load(rel, function(){ $("#sharePopUp").fadeIn(); }); 
	
	$("#close_btn2").click(function(){
			$("#sharePopUp").fadeOut();
	});
}

function closeShareByMail(){ $("#newsPopUp").fadeOut(); alert(document.getElementById('newsPopUp').style.display ); }

// ENLEVE LE FOCUS SUR LES LIENS
function blurLinks(){
	lnks = document.getElementsByTagName('a');
	for (i = 0; i < lnks.length; i++){
		lnks[i].onfocus = new Function("if(this.blur) this.blur()");
	}
	lnks2 = document.getElementsByTagName('area');
	for (j = 0; j < lnks2.length; j++){
		lnks2[j].onfocus = new Function("if(this.blur) this.blur()");
	}
}

// LANCE UNE FONCTION AU CHARGEMENT DE LA PAGE
function addOnloadEvent(fnc){
  if ( typeof window.addEventListener != "undefined" )
    window.addEventListener( "load", fnc, false );
  else if ( typeof window.attachEvent != "undefined" ) {
    window.attachEvent( "onload", fnc );
  }
  else {
    if ( window.onload != null ) {
      var oldOnload = window.onload;
      window.onload = function ( e ) {
        oldOnload( e );
        window[fnc]();
      };
    }
    else
      window.onload = fnc;
  }
}



function do_swapImgRestore() { //v3.0
  var i,x,a=document.do_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function do_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.do_p) d.do_p=new Array();
    var i,j=d.do_p.length,a=do_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.do_p[j]=new Image; d.do_p[j++].src=a[i];}}
}

function do_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=do_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function do_swapImage() { //v3.0
  var i,j=0,x,a=do_swapImage.arguments; document.do_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=do_findObj(a[i]))!=null){document.do_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function cache(objet){
	document.getElementById(objet).style.display='none';
}
function montre(objet){
	document.getElementById(objet).style.display='';
}




function verif_mail(champ) {
	var syntaxe_mail = new RegExp("^[a-zA-Z0-9]{1}[a-zA-Z0-9\-\._]*@[\-a-zA-Z0-9\._]+[\.]{1}[a-zA-Z]{2,4}$", "");
	if(!syntaxe_mail.test(champ)) {
		return false;
	}
	else
	{
	return true;
	}
}

function submit_contact(token)
{
    if(token == "cancel")
    {
        var name = "";
        var firstname = "";
        var email = "";
        var country = "";
        var comment = "";
        $("#contact").load("_ajax_contact.php", { 'send[]': [token, name, firstname, email, country, comment] });
    }
    else if(token == "submit")
    {
        var name = $('input:text[name=name]').val();
        var firstname = $('input:text[name=firstname]').val();
        var email = $('input:text[name=email]').val();
        var country = $('#select_country').val();
        var comment = $('#comment').val();
        $("#contact").load("_ajax_contact.php", { 'send[]': [token, name, firstname, email, country, comment] });
    }
    else if(token == "sent")
    {
        var name = "";
        var firstname = "";
        var email = "";
        var country = "";
        var comment = "";
        $("#contact").load("_ajax_contact.php", { 'send[]': [token, name, firstname, email, country, comment] });
    }
}

//ajouts Julien
var ok = "#FFFFFF";
var nok = "#F5D3E7";

function verif_remplissage_champ(champ) {
	if($('#'+champ).val() == "" ) {
		$('#'+champ).css('background', nok);
		return false;
	}else { 
		$('#'+champ).css('background', ok);
		return true;
	}
}

function verif_check(champ)
{
	if(!$('#'+champ).attr('checked'))
	{
		$('#'+champ+' + label').css('color', 'red');
		return false;
	}else{
		$('#'+champ+' + label').css('color', 'white');
		return true;
	}
}

function verif_inscr()
{
	var retour 	= true;
	if(!verif_remplissage_champ('first_name'))
		retour	= false;
	if(!verif_remplissage_champ('name'))
		retour	= false;
	if(!verif_remplissage_champ('email'))
		retour	= false;
	if(!verif_remplissage_champ('adress'))
		retour	= false;
	if(!verif_remplissage_champ('zip_code'))
		retour	= false;
	if(!verif_remplissage_champ('city'))
		retour	= false;
	if(!verif_mail($('#email').val()))
	{
		$('#email').css('background', nok);
		retour	= false;
	}
	if(!verif_check('chk_rules'))
		retour	= false;

	if(retour)
	{
		return true;
	}else{
		return false;
	}
}

var chrono_d	= 0;
var chrono_s	= "00";
var chrono_m	= "00";
var timer		= "";
var c2			= "15";
var timer2		= "";
function chrono()
{
	chrono_d++;
	if(chrono_d > 9)
	{
		chrono_s++;
		chrono_d = 0;
		if(chrono_s < 10)
		{
			chrono_s = "0"+chrono_s;
		}
	}
	if(chrono_s > 59)
	{
		chrono_s = chrono_s - 60;
		if(chrono_s < 10)
		{
			chrono_s = "0"+chrono_s;
		}
		chrono_m++;
		if(chrono_m < 10)
		{
			chrono_m = "0"+chrono_m;
		}
	}
	$('#timer').val(chrono_m+":"+chrono_s);
}

function chrono2()
{
	c2--
	if(c2 < 0)
	{
		c2 = 15;
	}
	if(c2 < 10)
	{
		c2 = "0"+c2;
	}
	$('#ready').val(c2);
}

function resetT2()
{
	$('#div_ready_ju').hide();
	clearInterval(timer2);
	c2 = "15";
	$('#ready').val(c2);
}

function loadQ(id_q, lang, code_pays)
{
	resetT2();
	$('.timer div').show();
	$.ajax({
		type: "POST",
		url: "../find_the_code/_ajax_quiz.php",
		data: "action=loadQ&id_q="+id_q,
		success: function(retour){
			//alert(retour);
			$('#le_td').html(retour);
			pageTracker = _gat._getTracker('UA-17161831-2');
			pageTracker._trackPageview("/find_the_code/question.php?lang="+lang+"&pays="+code_pays+"&id="+id_q);
			timer	= setInterval('chrono()',100);
		}
	});
}

function checkRep(id_q, id_rep, lang, code_pays)
{
	$.ajax({
		type: "POST",
		url: "../find_the_code/_ajax_quiz.php",
		data: "action=checkRep&id_rep="+id_rep+"&id_q="+id_q,
		success: function(retour){
			ret	= retour.split("***");
			$('.answer').removeAttr('onclick');
			//$('.one_rep').css('background', '#CCCCCC');
			$('#left_q').html(ret[1]);
			if(ret[0] == "ok")
			{
				$('#r'+id_rep).attr('class', 'answer_good');
				$('#left_q').attr('class', 'question_good');
			}else{
				$('#r'+id_rep).attr('class', 'answer_bad');
				$('#left_q').attr('class', 'question_bad');
				chrono_s = (chrono_s*1)+30;
				//chrono();
			}
			$('#left_q').fadeIn('fast');
			if(id_q < 6)
			{
				$('#div_ready_ju').fadeIn('fast');
				timer2	= setInterval('chrono2()',1000);
				setTimeout("loadQ("+(id_q+1)+", '"+lang+"', '"+code_pays+"')", 15000);
			}else{
				$('#div_ready_ju').css('background', 'url("images/ftc/endofthegame-'+lang+'.gif") no-repeat scroll right top');
				$('#ready').attr('class', 'input_timer2 lafin');
				//$('#div_ready_ju').attr('class', 'div_ready_ju_final');
				$('#div_ready_ju').fadeIn('fast');
				timer2	= setInterval('chrono2()',1000);
				setTimeout("stop_quiz('"+lang+"', '"+code_pays+"')", 15000);
			}
			clearInterval(timer);
		}
	});
}

function stop_quiz(lang, code_pays)
{
	$('#info_comp').fadeOut('fast');
	$.ajax({
		type: "POST",
		url: "../find_the_code/_ajax_quiz.php",
		data: "action=stop_quiz",
		success: function(retour){
			//$('#le_td').html('<div id="main_ftc">the end > '+retour+'</div>');
			window.location.href = "final.php?lang="+lang+"&pays="+code_pays;
		}
	});
}




$(document).ready(function() {
  
  $("#bicplayboy #maxi").click(function() {
    $("#bicplayboy #bg1").hide();
    $("#bicplayboy #bg2").show();
    function mycarousel_initCallback(carousel) {
      jQuery('.mycarousel-next').bind('click', function() { carousel.next(); return false; });
      jQuery('.mycarousel-prev').bind('click', function() { carousel.prev(); return false; });
    };
    $('#bicplayboy #all_maxi').jcarousel({
      wrap: 'circular',
      scroll: 1,
      visible: 5,
      initCallback: mycarousel_initCallback,
      buttonNextHTML: null,
      buttonPrevHTML: null,
    });
  });
  
  $("#bicplayboy #mini").click(function() {
    $("#bicplayboy #bg1").hide();
    $("#bicplayboy #bg3").show();
    function mycarousel_initCallback(carousel) {
      jQuery('.mycarousel-next').bind('click', function() { carousel.next(); return false; });
      jQuery('.mycarousel-prev').bind('click', function() { carousel.prev(); return false; });
    };
    $('#bicplayboy #all_mini').jcarousel({
      wrap: 'circular',
      scroll: 1,
      visible: 5,
      initCallback: mycarousel_initCallback,
      buttonNextHTML: null,
      buttonPrevHTML: null,
    });
  });
  
  $("#bicplayboy #electronic").click(function() {
    $("#bicplayboy #bg1").hide();
    $("#bicplayboy #bg4").show();
    function mycarousel_initCallback(carousel) {
      jQuery('.mycarousel-next').bind('click', function() { carousel.next(); return false; });
      jQuery('.mycarousel-prev').bind('click', function() { carousel.prev(); return false; });
    };
    $('#bicplayboy #all_electronic').jcarousel({
      wrap: 'circular',
      scroll: 1,
      visible: 5,
      initCallback: mycarousel_initCallback,
      buttonNextHTML: null,
      buttonPrevHTML: null,
    });
  });
  
  $("#bicplayboy #case").click(function() {
    $("#bicplayboy #bg1").hide();
    $("#bicplayboy #bg5").show();
    function mycarousel_initCallback(carousel) {
      jQuery('.mycarousel-next').bind('click', function() { carousel.next(); return false; });
      jQuery('.mycarousel-prev').bind('click', function() { carousel.prev(); return false; });
    };
    $('#bicplayboy #all_case').jcarousel({
      wrap: 'circular',
      scroll: 1,
      visible: 5,
      initCallback: mycarousel_initCallback,
      buttonNextHTML: null,
      buttonPrevHTML: null,
    });
  });
  
  jQuery('#bicplayboy .carousels li img').live('click', function() {
    var pic = $(this).attr('src').substr(-5,1);
    var lpic = $(this).attr('src').length - 5;
    var pic1 = $(this).attr('src').substr(0, lpic) + 1 + '.png';
    var pic2 = $(this).attr('src').substr(0, lpic) + 2 + '.png';
    if (pic == 2)
      $(this).attr('src', pic1);
    else
      $(this).attr('src', pic2);
  });
  
  $("#bicplayboy .back").click(function() {
    // $("#bicplayboy #bg1").show();
    // $("#bicplayboy #bg2").hide();
    // $("#bicplayboy #bg3").hide();
    // $("#bicplayboy #bg4").hide();
    // $("#bicplayboy #bg5").hide();
    $(location).attr('href','');
  });
  
});
