//
// Définir un cookie
//
function setCookie (name, value, path, domain, secure)
{
	var fin = new Date ();
	fin.setTime (fin.getTime() + (24 * 60 * 60 * 1000));

	sdjCookie = name + '=' + escape (value) + fin.toGMTString() +
		((path) ? '; path=' + path : '') +
		((domain) ? '; domain=' + domain : '') +
		((secure) ? '; secure' : '');

	document.cookie = sdjCookie;
	document.location.reload();
}


//
// Arrondir un nombre
//
function arrondir(x, n)
{
     var decalage = Math.pow(10, n);
     x *= decalage;
     x = Math.round(x);
     x /= decalage;
     return x;
}


//
// Ouvrir une fenêtre popup
//
function popup_smilies()
{
	document.getElementById('req_msg').focus();
	var width = 120;
	var height = 150;
	window.open('smilies.php?req_msg=1', 'smilies', 'alwaysRaised=yes, dependent=yes, resizable=yes, location=no, width='+width+', height='+height+', menubar=no, status=yes, scrollbars=yes, menubar=no');
}


//
// Ajouter du texte dans un textarea
//
function addText(where_, txt_)
{
	//-- Recup l'Objet
	var Obj = document.getElementById(where_);
	if (Obj)
	{
		//-- Focus sur Objet
		Obj.focus();
		if (typeof Obj.selectionStart != 'undefined')
		{
			//-- Position du curseur
			var PosDeb  = Obj.selectionStart;
			var PosFin  = Obj.selectionEnd;
			//-- Recup. des Chaines
			var Chaine  = Obj.value;
			var szAvant = Chaine.substring(0 , PosDeb);
			var szApres = Chaine.substring(PosFin, Obj.textLength);
			//-- Recup. texte selectionne
			var szSelect = Chaine.substring(PosDeb, PosFin);
			//-- Insertion du texte
			Obj.value = szAvant + txt_ + szApres;
			//-- Replace le curseur
			Obj.setSelectionRange(szAvant.length + txt_.length, szAvant.length + txt_.length);
			//-- Replace le Focus
			Obj.focus();
		}
		else // IE and consort
		{
			//-- Recup. de la selection
			var szSelect = document.selection.createRange().text;
			//-- Si du Texte est selectionne on le remplace
			if (szSelect.length > 0)
			{
				var Chaine = document.selection.createRange();
				Chaine.text = txt_ ;
				Chaine.collapse();
				Chaine.select();
			}
			else
			{
				var Chaine = Obj.value;
				var szMark = '~~';
				//-- Cree un double et insert la Mark ou est le curseur
				var szTmp = document.selection.createRange().duplicate();
				szTmp.text = szMark;
				//-- Recup. la position du curseur
				var PosDeb = Obj.value.search(szMark);
				//-- Recup. des Chaines
				var szAvant = Chaine.substring(0 , PosDeb);
				var szApres = Chaine.substring(PosDeb, Obj.textLength);
				//-- Insertion du texte
				Obj.value = szAvant + txt_ + szSelect + szApres;
				//-- Repositionne le curseur
				PosDeb += txt_.length;
				//-(*)- Supprime les retours Chariot
				PosDeb -= Get_NbrCR(szAvant);
				//-- Recup de la Chaine
				Chaine = Obj.createTextRange();
				//-- Deplace le Debut de la chaine
				Chaine.moveStart('character', PosDeb);
				//-- Deplace le curseur
				Chaine.collapse();
				Chaine.select();
			}
		}
	}
}


/*
Author: mg12
Update: 05/05/2008
Author URI: http://www.neoease.com/
*/
(function()
{
	function getId(id)
	{
		return document.getElementById(id);
	}

	function goTop(a, t)
	{
		a = a || 0.1;
		t = t || 16;

		var x1 = 0;
		var y1 = 0;
		var x2 = 0;
		var y2 = 0;
		var x3 = 0;
		var y3 = 0;

		if (document.documentElement)
		{
			x1 = document.documentElement.scrollLeft || 0;
			y1 = document.documentElement.scrollTop || 0;
		}

		if (document.body)
		{
			x2 = document.body.scrollLeft || 0;
			y2 = document.body.scrollTop || 0;
		}

		var x3 = window.scrollX || 0;
		var y3 = window.scrollY || 0;

		var x = Math.max(x1, Math.max(x2, x3));
		var y = Math.max(y1, Math.max(y2, y3));

		var speed = 1 + a;
		window.scrollTo(Math.floor(x / speed), Math.floor(y / speed));
		if (x > 0 || y > 0)
		{
			var f = 'MGJS.goTop('+a+', '+t+')';
			window.setTimeout(f, t);
		}
	}

	window['MGJS'] = {};
	window['MGJS']['getId'] = getId;
	window['MGJS']['goTop'] = goTop;

})();


//
// Ajoute un jeu aux favoris
//
function add_favorite(add_path, delete_path)
{
	$('#linkfavorite').fadeOut('normal', function() {
		$.get(add_path);
		num_favorites = num_favorites + 1;
		$('#linkfavorite').html('<a href="'+delete_path+'" onclick="del_favorite(\''+delete_path+'\', \''+add_path+'\'); return false;">Supprimer ce jeu des favoris</a>&nbsp;('+num_favorites+' fan'+(num_favorites > 1 ? 's' : '')+')');
		$('#linkfavorite').fadeIn('normal');
	});
}


//
// Supprime un jeu des favoris
//
function del_favorite(delete_path, add_path)
{
	$('#linkfavorite').fadeOut('normal', function() {
		$.get(delete_path);
		num_favorites = num_favorites - 1;
		$('#linkfavorite').html('<a href="'+add_path+'" onclick="add_favorite(\''+add_path+'\', \''+delete_path+'\'); return false;">Ajouter ce jeu aux favoris</a>&nbsp;('+num_favorites+' fan'+(num_favorites > 1 ? 's' : '')+')');
		$('#linkfavorite').fadeIn('normal');
	});
}


//
// Supprimer un jeu des favoris
// depuis la page des favoris
//
function delete_favorite(id, delete_path)
{
	$('#favorites').dialog('option', 'buttons', {
		'Supprimer des favoris': function() {
			$(this).dialog('close');
			$('#game'+id).addClass('highlight');
			$('#game'+id).fadeOut('slow', function() {
				$.get(delete_path);
			});
		},
		'Annuler': function() {
			$(this).dialog('close');
		}
	});
	$('#favorites').dialog('open');
	return false;
}


//
// Supprimer un score
//
function delete_score(id, delete_path)
{
	$('#scores').dialog('option', 'buttons', {
		'Supprimer le score': function() {
			$(this).dialog('close');
			$('#game'+id).removeClass();
			$('#game'+id).addClass('highlight');
			$('#game'+id).fadeOut('slow', function() {
				$.get(delete_path);
			});
		},
		'Annuler': function() {
			$(this).dialog('close');
		}
	});
	$('#scores').dialog('open');
	return false;
}


//
// Ajouter une note
//
function add_note()
{
	$('#stars-wrapper1').toggle();
	return false;
}


//
// Supprimer une note
//
function delete_note(delete_path)
{
	$.ajax({
		type: 'GET',
		url: delete_path,
		success: function(msg) {
			var result = msg.substring(0, 6);
			if (result == 'error:') {
				message = msg.substring(6, msg.length);
				$('#display-note').html(message);
			}
			else if (result == 'return') {
				$('#display-note').fadeOut('normal', function() {
					message = msg.substring(6, msg.length);
					$('#display-note').html(message);
					$('#display-note').fadeIn('normal');
				});
			}
		}
	});

	return false;
}


//
// Ajouter un membre aux destinataires
//
function add_user()
{
	var user = $('#jforms_messaging_message__for');
	$('#jforms_messaging_message_for').append("\n"+user.val());
	user.val('');
}


$(function() {
	// Supprimer des favoris
	$('#favorites').dialog({
		autoOpen: false,
		resizable: true,
		modal: false,
		width: 300,
		height: 150,
		title: 'Supprimer des favoris'
	});

	// Supprimer le score
	$('#scores').dialog({
		autoOpen: false,
		resizable: true,
		modal: false,
		width: 360,
		height: 160,
		title: 'Supprimer le score'
	});

	$('#RechercherKeywords').focus(function() {
		if ($(this).val() == 'Recherche')
			$(this).val('');
	});

	$('#RechercherKeywords').focusout(function() {
		if ($(this).val() == '')
			$(this).val('Recherche');
	});

	$('#stars-wrapper1').hide();
	$('#stars-wrapper1').stars({
		inputType: 'select',
		oneVoteOnly: false,
		cancelShow: false,
		captionEl: $('#stars-cap'),
		callback: function(ui, type, value) {
			var selectValue = arrondir(value, 0);
			$.ajax({
				type: 'POST',
				url: note_path,
				data: 'rating='+selectValue,
				success: function(msg) {
					var result = msg.substring(0, 6);
					if (result == 'error:') {
						message = msg.substring(6, msg.length);
						$('#stars-wrapper1').hide();
						$('#display-note').html(message);
					}
					else if (result == 'return') {
						$('#stars-wrapper1').hide();
						$('#display-note').fadeOut('normal', function() {
							message = msg.substring(6, msg.length);
							$('#display-note').html(message);
							$('#display-note').fadeIn('normal');
						});
					}
				}
			});
		}
	});

	$('#chatbox-form').hide();

	$('#zoom-out').click(function() {
		if (w > h) {
			w = w - 20;
			h = Math.round(height/width*w);
		}
		else if (w < h) {
			h = h - 20;
			w = Math.round(width/height*h);
		}
		else {
			h = h - 20;
			w = w - 20;
		}
		$('#game-object object').height(h);
		$('#game-object object').width(w);
	});

	$('#zoom-reset').click(function() {
		h = height;
		w = width;
		$('#game-object object').height(h);
		$('#game-object object').width(w);
	});

	$('#zoom-in').click(function() {
		if (w > h) {
			w = w + 40;
			h = Math.round(height/width*w);
		}
		else if (w < h) {
			h = h + 40;
			w = Math.round(width/height*h);
		}
		else {
			h = h + 40;
			w = w + 40;
		}
		$('#game-object object').height(h);
		$('#game-object object').width(w);
	});

	$('#report-link').click(function() {
		window.open($(this).attr('href'));
		return false;
	});

	$('#jforms_loto_day_date_end').datepicker({
		dateFormat: 'dd-mm-y',
		dayNames: ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi'],
		dayNamesMin: ['Di', 'Lu', 'Ma', 'Me', 'Je', 'Ve', 'Sa'],
		dayNamesShort: ['Dim', 'Lun', 'Mar', 'Mer', 'Jeu', 'Ven', 'Sam'],
		monthNames: ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre'],
		monthNamesShort: ['Jan', 'Fév', 'Mar', 'Avr', 'Mai', 'Jui', 'Juil', 'Aoû', 'Sep', 'Oct', 'Nov', 'Déc'],
		firstDay: 1,
		minDate: 0,
		showOn: 'button',
		buttonImage: basePath+'img/icons/calendar.png',
		buttonImageOnly: true,
		buttonText: 'Calendrier'
	});

	$('#jforms_loto_dayedit_date_end').datepicker({
		dateFormat: 'dd-mm-y',
		dayNames: ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi'],
		dayNamesMin: ['Di', 'Lu', 'Ma', 'Me', 'Je', 'Ve', 'Sa'],
		dayNamesShort: ['Dim', 'Lun', 'Mar', 'Mer', 'Jeu', 'Ven', 'Sam'],
		monthNames: ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre'],
		monthNamesShort: ['Jan', 'Fév', 'Mar', 'Avr', 'Mai', 'Jui', 'Juil', 'Aoû', 'Sep', 'Oct', 'Nov', 'Déc'],
		firstDay: 1,
		minDate: 0,
		showOn: 'button',
		buttonImage: basePath+'img/icons/calendar.png',
		buttonImageOnly: true,
		buttonText: 'Calendrier'
	});
});

