var matchs_ids = "";
var cookie_actuel = read_cookie('livescore_matchs');
save_title_page = document.title;
$(document).ready(function() {
	ids_matchs = cookie_actuel.split(';');
	for (u = 0; u <= ids_matchs.length - 1; u++) {
		var id_match = ids_matchs[u];
		if (id_match.length > 0) {
			var checkbox_ressource = $('input[name=mid_'+id_match+']');
			if (checkbox_ressource.attr('id')) {
				checkbox_ressource.attr('checked', true);
				checkbox_ressource.parent().parent().addClass('isCheckedMatch');
				livescore_check_league(id_match);
			}
		}
	}
	$('.check_livescore').removeAttr('disabled');
});
function set_cookie(nom, valeur, time_add) {
	var expDate = new Date();
	expDate.setTime(expDate.getTime() + time_add)
	document.cookie = nom + "=" + escape(valeur) + ";expires=" + expDate.toGMTString() + ";path=/";
	if (is_membre) $.post('/ajax/love_matchs.php', {ids_matchs: valeur});
}
function read_cookie(nom) {
	deb = document.cookie.indexOf(nom + "=");
	if (deb >= 0) {
		deb += nom.length + 1;
		fin = document.cookie.indexOf(";",deb);
		if (fin < 0) fin = document.cookie.length;
		return unescape(document.cookie.substring(deb,fin));
	}
	return "";
}
function livescore_title_base() {
	document.title = save_title_page;
}
function livescore_check_league(id_match) {
	var checkbox_ressource = $('input[name=mid_'+id_match+']');
	id_league = $(checkbox_ressource).attr('id');
	id_league = id_league.substr(4);
	id_league = id_league.split('_');
	id_league = id_league[0];
	total_checked_this_league = $('input[id^=lid_'+id_league+'][checked]').length;
	total_this_league = $('input[id^=lid_'+id_league+']').length;
	diff = total_this_league - total_checked_this_league;
	if (diff == 0) {
		$('input[id=league'+id_league+']').attr('checked', 'checked');
	}
}
function livescore_save(id_match, multiple) {
	document.title = "Selection sauvegardee !";
	setTimeout("livescore_title_base()", 1500);
	var checkbox_ressource = $('input[name=mid_'+id_match+']');
	var active = checkbox_ressource.attr('checked');
	if (active) {
		checkbox_ressource.parent().parent().addClass('isCheckedMatch');
		if (!multiple) set_cookie('livescore_matchs', cookie_actuel + ';'+id_match, 3600 * 24 * 5);
		cookie_actuel += ';'+id_match;
		livescore_check_league(id_match);
	} else {
		checkbox_ressource.parent().parent().removeClass('isCheckedMatch');
		cookie_actuel2 = cookie_actuel + ';';
		cookie_actuel2 = cookie_actuel2.replace(';'+id_match+';', ';');
		cookie_actuel2 = cookie_actuel2.substr(0, (cookie_actuel2.length)-1);
		if (!multiple) set_cookie('livescore_matchs', cookie_actuel2, 3600 * 24 * 5);
		cookie_actuel = cookie_actuel2;
	}
}
function livescore_save_all(id_league, is_checked_group) {
	matchs_group = $('input[id^=lid_'+id_league+']');
	for (u = 0; u <= matchs_group.length - 1; u++) {
		etat = $(matchs_group[u]).attr('checked');
		if (!etat == is_checked_group) { // Si la boxe de la league est checké il faut pour quelle soti prise en compte que la box du match soit dechecké, et inversement
			$(matchs_group[u]).attr('checked', is_checked_group ? true : false);
			id_match = $(matchs_group[u]).attr('name').substr(4);
			livescore_save(id_match, true);
		}
	}
	set_cookie('livescore_matchs', cookie_actuel, 3600 * 24 * 5);
}