$(document).ready(function () {
	attacherActions();
});
//window.onload = attacherActions;
var taux=0.95; var lequel=""; var lautre=""; var valeur=0;
var chab = 0; var cres = 0; var cesq = 0; var cfor = 0;
var stop_countdown = false;
var timeleft2 = 50;

function attacherActions() {
	Start();


	if ($('#captcha_countdown').length != 0) {
		countdown();
	}

	if ($('#heure').length != 0)
		setTimeout("UR_Start()",1000);

	if ($('#submitleg').length != 0) {
		$('#submitleg').click(function() {
			return confirmSubmit("Confirmes-tu le legs de ton gang ?");
		});
	}
	if ($('#submitban').length != 0) {
		$('#submitban').click(function() {
			return confirmSubmit("Confirmes-tu le bannissement de ce membre ?");
		});
	}
	if ($('#submitdis').length != 0) {
		$('#submitdis').click(function() {
			return confirmSubmit("Confirmes-tu la dissolution de ton gang ?");
		});
	}
	if ($('#submitlevelup').length != 0) {
		$('#submitlevelup').click(function() {
			return confirmSubmit("Confirmes-tu ta montée au niveau supérieur ?");
		});
	}
	if ($('#brut').length != 0) {
		var tout = $('#brut').val();
		$('#submitretrait').click(function() {
			if ($('#brut').val() == tout)
				return confirmSubmit("Confirmes-tu le retrait de tout ton argent ?");
		});
	}
	if ($('#chkall').length != 0) {
		$('#chkall').click(function() {
			if ($(this).attr("checked"))
				$("input[name=del\\[\\]]").attr('checked', 'true');
			else
				$("input[name=del\\[\\]]").attr('checked', '');
		});
	}
	if ($('#brut').length != 0) {
		$('#brut').focus(function() {
			lequel = 'brut';
			lautre = 'net';
		});
	}
	if ($('#net').length != 0) {
		$('#net').focus(function() {
			lequel = 'net';
			lautre = 'brut';
		});
	}
	if ($('#contact').length != 0) {
		$('#contact').change(function() {
			$("input[name=new_to]").val($("input[name=new_to]").val() + " " + $(this).val());
		});
	}
	
	// Compétences
	if ($('#competences').length != 0) {

		majformcomp();

		$('#hab\\+').click(function() {
			$('#hab').val(parseInt($('#hab').val()) + 1);
			$('#xp').val(parseInt($('#xp').val()) - parseInt($('#hab').val()));
			chab++;
			majformcomp();
		});
		$('#hab-').click(function() {
			$('#xp').val(parseInt($('#xp').val()) + parseInt($('#hab').val()));
			$('#hab').val(parseInt($('#hab').val()) - 1);
			chab--;
			majformcomp();
		});

		$('#res\\+').click(function() {
			$('#res').val(parseInt($('#res').val()) + 1);
			$('#xp').val(parseInt($('#xp').val()) - parseInt($('#res').val()));
			cres++;
			majformcomp();
		});
		$('#res-').click(function() {
			$('#xp').val(parseInt($('#xp').val()) + parseInt($('#res').val()));
			$('#res').val(parseInt($('#res').val()) - 1);
			cres--;
			majformcomp();
		});
		$('#esq\\+').click(function() {
			$('#esq').val(parseInt($('#esq').val()) + 1);
			$('#xp').val(parseInt($('#xp').val()) - parseInt($('#esq').val()));
			cesq++;
			majformcomp();
		});
		$('#esq-').click(function() {
			$('#xp').val(parseInt($('#xp').val()) + parseInt($('#esq').val()));
			$('#esq').val(parseInt($('#esq').val()) - 1);
			cesq--;
			majformcomp();
		});
		$('#for\\+').click(function() {
			$('#for').val(parseInt($('#for').val()) + 1);
			$('#xp').val(parseInt($('#xp').val()) - parseInt($('#for').val()));
			cfor++;
			majformcomp();
		});
		$('#for-').click(function() {
			$('#xp').val(parseInt($('#xp').val()) + parseInt($('#for').val()));
			$('#for').val(parseInt($('#for').val()) - 1);
			cfor--;
			majformcomp();
		});
	}

}

function confirmSubmit(txt) {
	var agree = confirm(txt + "");
	if (agree)
		return true;
	return false;
}
function conv() {

}

function Start() {
	if (lequel!="") {
		if (lequel == "brut")
			valeur=Math.floor(parseFloat($('#brut').val())*taux);
		if (lequel == "net")
			valeur=Math.ceil(parseFloat($('#net').val())/taux);
		if (!isNaN(valeur))
			$('#'+lautre).val(valeur);
	}
	setTimeout("Start()",100)
}

function url(url) {
	$(location).attr('href',"http://www.champimouss.net/partenaires/out.php?ref="+url);
}

function popup_color_picker() {
  var width = 400;
  var height = 300;
  window.open('color_picker.html', 'cp', 'resizable=no, location=no, width=' +width+', height='+height+', menubar=no, status=yes, scrollbars=no, menubar=no');
}

function majformcomp() {
  if ((chab * 1) > 0) $('#hab-').attr('disabled', "");
  else $('#hab-').attr('disabled', "disabled");
  if ((cres * 1) > 0) $('#res-').attr('disabled', "");
  else $('#res-').attr('disabled', "disabled");
  if ((cesq * 1) > 0) $('#esq-').attr('disabled', "");
  else $('#esq-').attr('disabled', "disabled");
  if ((cfor * 1) > 0) $('#for-').attr('disabled', "");
  else $('#for-').attr('disabled', "disabled");
  if ($('#xp').val() < 0) {
		$('#submitxp').attr('disabled', "disabled");
		$('#hab\\+').attr('disabled', "disabled");
		$('#res\\+').attr('disabled', "disabled");
		$('#esq\\+').attr('disabled', "disabled");
		$('#for\\+').attr('disabled', "disabled");
  } else {
		$('#submitxp').attr('disabled', "");
		$('#hab\\+').attr('disabled', "");
		$('#res\\+').attr('disabled', "");
		$('#esq\\+').attr('disabled', "");
		$('#for\\+').attr('disabled', "");
    if (($('#hab').val()*1)+1 > $('#xp').val()) $('#hab\\+').attr('disabled', "disabled");
    if (($('#res').val()*1)+1 > $('#xp').val()) $('#res\\+').attr('disabled', "disabled");
    if (($('#esq').val()*1)+1 > $('#xp').val()) $('#esq\\+').attr('disabled', "disabled");
    if (($('#for').val()*1)+1 > $('#xp').val()) $('#for\\+').attr('disabled', "disabled");
  }
}

/*function countdown(timeleft, prio) {
	if ($('#captcha_countdown').length != 0) {
		if (stop_countdown && (prio == false)) {
			stop_countdown = false;
		} else {
			if (timeleft > 0) {
				$('#captcha_countdown').html("" + timeleft + "s restantes");
				setTimeout(function() {countdown(timeleft-1, false);}, 1000);
			} else {
				$('#captcha_countdown').html("/!\\ temps écoulé");
			}
		}
	}
}*/

function countdown() {
	if (timeleft2 > 0) {
		$('#captcha_countdown').html("" + timeleft2 + "s restantes");
		timeleft2 = timeleft2 - 1;
	} else {
		$('#captcha_countdown').html("/!\\ temps écoulé");
	}
	setTimeout(function() {countdown();}, 1000);
}


function UR_Start() {
	UR_Nu.setSeconds(parseInt(UR_Nu.getSeconds()) + parseInt(1));
	UR_Indhold = UR_Nu.getHours() + ":" + showFilled(UR_Nu.getMinutes()) + ":" + showFilled(UR_Nu.getSeconds());
	if ($('#heure').length != 0)
		$('#heure').html(UR_Indhold);
	setTimeout("UR_Start()",1000);
}
function showFilled(Value) {
	return (Value > 9) ? "" + Value : "0" + Value;
}

