var testTimer = 0;
var emptyFieldChecking = false;
var subError = false;
var dataAjax1 = null;
var dataAjax2 = null;


function renewImgSet(imgsName,sexe) {
	if(typeof( window['withGeoLoc'] ) != "undefined" && withGeoLoc == 1){
      generateProfilWithGeoLoc(imgsName,sexe);
      return 0;
	}
	if( sexe == 0 ) sexe = 1;
	else if( sexe == 1 ) sexe = 0;
	if( ct == 2 ) sexe = 1;

	// creer le tableau aleatoire
	var temp = new Array();
	if( sexe == 2 ) {
		nm[ct][0].sort(shuffle);
		nm[ct][1].sort(shuffle);
		var tab0 = nm[ct][0].slice(0,5);
		var tab1 = nm[ct][1].slice(0,3);
		temp = tab0.concat(tab1);
	}
	else temp = nm[ct][sexe];
	temp.sort(shuffle);
	$("[name=" + imgsName +"]").each (
		function (i){
			var $this = $(this),
			info = temp[i].split(':');
			$this.attr( {href: '#'+info[0]} );
			

			$this.css("background-image", 'url(http://www.ezstatic.com' + info[2].replace( /thumb\/[0-9]+/, "thumb/100" ) + ')' );
			this.firstChild.innerHTML = info[1];
			this.firstChild.textContent = info[1];
			$this.unbind();
			$this.bind(
				'click',
				function(){
					$.ajax({
						async: false,
						type: "POST",
						url: "/home/ajx.php",
						data: 'id=' + $this.attr('href').replace(/#/i, "") + '&ct=' + ct,
						success: function(msg) {
							$("#frm_login [name=wanthim]").val($this.attr('href').replace(/#/i, ""));
							$("#register-form [name=wanthim]").val($this.attr('href').replace(/#/i, ""));
							$("#follow-profil").removeAttr("style");
							$("#follow-profil").html("");
							$("#follow-profil").html(msg);
						}
					});
					return false;
				}
				);
		}
		);
}

function chAction(formId,obj) {
	resetFollowProfil();
 var jObj = $(obj);
	if(jObj.is('input')) {
		if(!obj.checked) return;
	}
	else if(!jObj.val().length) return;
	$('#' + formId).attr('action' ,'http://' + fl[obj.value] + '/login.php' )
}
function showBase() {
	if(arguments.length == 1) $('#search-type').css('display','none');
	else {
		$('#ctDef0').css('display','none');
		$('#ctDef1').css('display','none');
		$('#ctDef2').css('display','none');
		for( var i = 0; i < arguments.length; i++ ) {
			$('#ctDef' + arguments[i]).css('display','');
		}
		$('#search-type').css('display','');
	}
}

/**
 * Retourne une image de background adaptée à la section et au sexe
 * NB : ininiche et inscriptionSexe sont globales !
 */
function switchBackground(section,niche) {
	// Si besoin, update de la variable globale
	if (typeof(niche) !== 'undefined' && niche != ininiche) ininiche = niche;
	$.ajax({
		async: true,
		type: "POST",
		url: "/home/ajx.php",
		data: 'niche=' + ininiche + '&ct=' + section + '&sex=' + inscriptionSexe + '&side=' + side,
		success: function(msg) {
			$("#content-background").css('background-image', 'url(' + msg + ')');
  }
	});
}

function resetFollowProfil()
    {
     if(typeof( window['hideWhenFollowProfil'] ) != "undefined" && hideWhenFollowProfil == 1)
        $("#features").show();
    $("#follow-profil").html("");
    $("input[name=wanthim]").val('');
    }

//******** Gère les états du form en fonction ******//
//******** de la section/sexe choisis		 ******//
function switchSection(url,section) {
    resetFollowProfil();
	// ajournement var globale
	if (ct != section) {
		ct = section;
		switchBackground(section);
		if (section == 2) renewImgSet('thumbs',2);
		else renewImgSet('thumbs',inscriptionSexe);
	}
	if(url.length) {
		$("#register-form").attr("action",url);
		setTypeRenc(section);
		return true;
	}
	else return false;
}
function switchSex(sex) {
	if (inscriptionSexe != sex) inscriptionSexe = sex;  // ajournement var globale
	if (ct == 2 && sex != 1) {
		ct = inisection;
		switchBackground(inisection);
		setTypeRenc(ct);
	}
	switch (ct) {

		case 0: // Love
			switch (sex) {
				case 0: //femme
					$("#ctDef0").show();
					$("#ctDef1").show();
					$("#ctDef2").hide();
					break;
				case 1: //homme
					$("#ctDef0").show();
					$("#ctDef1").show();
					$("#ctDef2").show();
					break;
			}
			$('#search-type').show();
			break;

		case 1: // Sexy
			switch (sex) {
				case 0: //femme
					$("#search-type").hide();
					break;
				case 1: //homme
					$("#ctDef0").show();
					$("#ctDef1").show();
					$("#ctDef2").show();
					$('#search-type').show();
					break;
				case 2: //couple
					$('#search-type').hide();
					break;
			}
			break;

		case 2: // Gay
			$('#search-type').hide();
			break;

	}
	setTypeRenc(ct);
	renewImgSet('thumbs',sex);
}
function setTypeRenc(type) {
	$("#typerenc input[type=radio]").each(
		function(){
			$(this).attr('checked','');
			$("#" + $(this).parent().attr('id') + " label").attr('style','');
		}
		);
	$("#ctDef"+type+"s").attr('checked','checked');
	$("#ctDef"+type+" label").attr('style','font-weight: bold;');
}


