var IN_FB_FRAME=false; /* Dans l'iframe FB ? */

function initialisation()
{
	initialiser_tooltips();

	$("form.triche").bind('submit', function(event, resubmit) {

		if (TRICHE_POSSIBLE && !resubmit)
		{
			var _this = this;

			confirm(_UTILISATION_DESTINEE,
				function () {
					$(_this).attr('action', $(_this).attr('action')+'&triche=1');
					$(_this).trigger('submit', [true]);
				},
				function() {
					$(_this).trigger('submit',[true]);
				}
			);

			return false;
		}

	});

	/*$("a.triche").bind('click', function() {

		if (TRICHE_POSSIBLE)
		{
			var _this = this;

			confirm(_UTILISATION_DESTINEE,
				function () {
					var nouv = $(_this).attr('href')+'&triche=1';
					window.location.replace(nouv);
				},
				function() {
					window.location.replace($(_this).attr('href'));
				}
			);

			return false;
		}

	});*/


}

function initialisation_png()
{
	var liste = '.png, img, #conteneur a, input, select, .image';

	if (initialisation_png.arguments.length>0 && initialisation_png.arguments[0])
		liste=initialisation_png.arguments[0];

	if (typeof(DD_belatedPNG)!="undefined") DD_belatedPNG.fix(liste);

}

function initialisation_page()
{
	initialisation();

	// Petit effet sur le menu, mais pas pour IE !!
	if (!jQuery.browser.msie)
	{
		$(".dropdown:has(li.sub)").hover(
			function() { },
			function() { $('#marker').hide(); }
		);

		$('.dropdown li:has(li)').hover(
			function() {
				$('#marker').appendTo($(this)).show();
				$('#marker').stop().css({'top':40});
			},
			function() { $('#marker').hide(); }
		);

		$('.dropdown ul li').hover(
			function() {
				topval = $(this).position()['top'];
				$('#marker').stop().show().animate({top: topval+42}, {easing: 'easeOutBack', duration:700});
			},
			function() { }
		);
	}
	else if (jQuery.browser.msie && jQuery.browser.version==8)
	{
		$(".dropdown:has(li.sub)").hover(
			function() { },
			function() { $('#marker').hide(0); }
		);

		$('.dropdown li:has(li)').hover(
			function() {
				$('#marker').appendTo($(this)).show(0);
				$('#marker').css({'top':40});
			},
			function() { $('#marker').hide(0); }
		);

		$('.dropdown ul li').hover(
			function() {
				topval = $(this).position()['top'];
				$('#marker').show(0).css({top: topval+42});
			},
			function() { }
		);
	}

	// Précharge les images hover
	preload_array(liste);

	// Fancybox landing
	$("a.fancy").fancybox({
		'zoomOpacity'			: true,
		'overlayShow'			: false,
		'zoomSpeedIn'			: 500,
		'zoomSpeedOut'			: 500,
		'hideOnContentClick'	: true,
		'centerOnScroll'		: true
	});

	// Bug chrome avec son background overridé sur les input
	var ischrome = navigator.appVersion.indexOf("Chrome")!=-1 ? true : false;
	if (ischrome) $("form").attr('autocomplete', 'off').find('select').addClass('chrome');

	// Bug FF3.0
	var isFF30	= navigator.userAgent.indexOf("Firefox/3.0")!=-1 ? true : false;
	if (isFF30)	$("#global").addClass("ff30");


	// Dans Facebook ?
	try {
		var v = top.location.toString;
	}
	catch (e) {
		IN_FB_FRAME=true;
	}


	// Aggrandi le contenu si nécessaire
	if (!IN_FB_FRAME) {
		var base = $("#global").height();
		var height = getSize().height;
		if (height>base)
		{
			var diff=height-base-24; // 24 = barre top
			$("#bg").css({height: diff});
		}
		else
		{
			$("#bg").css({height: 43}); // FOOTER
		}

		// Bug scrollbar FB
		$("html").addClass('no-overflow');

	}

}


function getSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return { 'width':myWidth, 'height':myHeight };
}


var liste = [
	"image/design/boutons/grand_h.png",
	"image/design/boutons/moyen_h.png",
	"image/design/boutons/moyen2_h.png",
	"image/design/boutons/petit_h.png",
	"image/design/boutons/moyen3_h.png",
	"image/design/btn/btn_roll.png",
	"image/design/btn/btn2_roll.png",
	"image/design/btn/btn3_roll.png",
	"image/design/combat_equipement/top_left_h.jpg",
	"image/design/combat_equipement/top_right_h.jpg",
	"image/design/combat_equipement/top_right_h.jpg",
	"image/design/menu_top/menu_top_featured_h.png",
	"image/design/menu_top/menu_top_h.png",
	"image/design/menu_top/menu_top_last_h.png"
];


function afficher_alerte_pe()
{
	$("#alerte_pe").animate({height: 'show'}, 1000);
}


function maj_infos(d)
{


	$(".ajax_monnaie").html(d.monnaie);
	$(".ajax_feez").html(d.coins);

	if (d.niveau) {
		$(".ajax_niveau").html(d.niveau);
		$(".ajax_xp").html(d.xp);
		$(".ajax_xp_total").html(d.xp_total);
		$("#xp_in").css('width', d.xp_pourc+"%");
		$("#xp_out").attr('title', _TOOLTIP_XP+"[A]"+d.xp+"/"+d.xp_total+"[/A]").attr('tooltipText', '');

		$(".ajax_pe").html(d.pe);
		$(".ajax_pe_total").html(d.pe_total);
		$("#pe_in").css('width', d.pt_pourc+"%");
		$("#pe_out").attr('title', _TOOLTIP_PE+"[A]"+d.pe+"/"+d.pe_total+"[/A]").attr('tooltipText', '');

		$(".ajax_triche").html(d.triche);
		$(".ajax_triche_total").html(d.triche_total);
		$("#triche_in").css('width', d.triche_pourc+"%");

		initialiser_tooltips($("#xp_out, #pe_out"));
	}

}


/*
 * Affiche un message
 * @param texte
 * @param titre [nom du site]
 * @param image []
 * @param sticky? [non]
 * @param time [8000]
 */
function afficher_message( texte){
	if (texte.length==0) return false;
	var titre = _ALERT;
	var image="";
	var sticky=false;
	var time = 3000;

	if (afficher_message.arguments.length>1 && afficher_message.arguments[1])	titre=afficher_message.arguments[1];
	if (afficher_message.arguments.length>2 && afficher_message.arguments[2])	image=afficher_message.arguments[2];
	if (afficher_message.arguments.length>3 && afficher_message.arguments[3])	sticky=afficher_message.arguments[3];
	if (afficher_message.arguments.length>4 && afficher_message.arguments[4])	time=afficher_message.arguments[4];

	$.gritter.add({
		title: titre,
		text: texte,
		image: image,
		sticky: sticky,
		time: time
	});
}

function initialiser_tooltips()
{
	var elem;
	if (initialiser_tooltips.arguments.length>0 && initialiser_tooltips.arguments[0])
		elem=initialiser_tooltips.arguments[0];
	else
		elem=$('.tip');

	elem.tooltip({
		delay:0,
		track: true,
		showURL: false,
		fade:true
	});
}


/* Effect slide et Fading */
jQuery.fn.slideFadeToggle = function(speed, easing, callback) {
	return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);
};

/* Effect Slide */
jQuery.fn.slideToggle = function(speed, easing, callback) {
	return this.animate({height: 'toggle'}, speed, easing, callback);
};

/* Système de tabs perso */
jQuery.fn.YotTabs = function(params)
{

	params = $.extend( {
		duree: 1000
	}, params);

	this.each(function() {
		var $t = $(this);

		// Action sur les liens
		$(">ul>li>a", this).bind('click', function() {
			var rel = $(this).attr('rel');

			if (!$(this).hasClass('select'))
			{
				$(".onglet:visible", $t).slideFadeToggle(params.duree);
				$("#"+rel).slideFadeToggle(params.duree);

				$(">ul>li>a", $t).removeClass('select');
				$(this).addClass('select');
			}

			return false;
		});


	});

	return this;
};



function delCook(nom) {
	ecrire_cookie(nom, "", -1);
}

function ecrire_cookie(nom, valeur, expires) {
  document.cookie=nom+"="+escape(valeur)+
  ((expires==null) ? "" : ("; expires="+expires));
}

function getCookieVal(offset)
{
	var endstr=document.cookie.indexOf (";", offset);
	if (endstr==-1) endstr=document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}
function LireCookie(nom)
{
	var arg=nom+"=";
	var alen=arg.length;
	var clen=document.cookie.length;
	var i=0;
	while (i<clen)
	{
		var j=i+alen;
		if (document.cookie.substring(i, j)==arg) return getCookieVal(j);
		i=document.cookie.indexOf(" ",i)+1;
		if (i==0) break;
	}
	return null;
}




function setTitle(sTitle) {
	if (document.getElementById('main_title'))
		document.getElementById('main_title').innerHTML = sTitle;
}




/*function $(id){

	return document.getElementById(id);
}*/

function file(fichier){
	if(window.XMLHttpRequest) // FIREFOX
		xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // IE
		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else
		return(false);
	xhr_object.open("GET", fichier, false);
	xhr_object.send(null);
	if(xhr_object.readyState == 4) return(xhr_object.responseText);
	else return(false);
}

function toggle(id, q){
	if(q){
		$(id).style.display = 'block';
		$(id).style.visibility = 'visible';
	} else {
		$(id).style.display = 'none';
		$(id).style.visibility = 'hidden';
	}
}

function toggleomd(id, q){
	if(q){
		$(id).style.display = 'block';
		$(id).style.visibility = 'visible';
	} else {
		$(id).style.display = 'none';
		$(id).style.visibility = 'hidden';
	}
}

function toggle2(id, id2){
	$(id2).style.display = 'block';
	$(id2).style.visibility = 'visible';
	$(id).style.display = 'none';
	$(id).style.visibility = 'hidden';
}


var _oneMinute=60; //minute unit in seconds
var _oneHour=60*60; //hour unit in seconds
var _oneDay=60*60*24; //day unit in seconds

function compte_rebour(timediff, div_id, redirect, title, jours, jour, heures, heure, et){
	//alert('ok');
	timediff--;
	if (timediff<=0 && redirect.length>0) rediriger(redirect);
	else
	{
		var j=Math.floor(timediff/_oneDay);
		var h=Math.floor((timediff-j*_oneDay)/_oneHour);
		var mn=Math.floor((timediff-j*_oneDay-h*_oneHour)/_oneMinute);
			if (mn<10) mn="0"+mn;
		var s=Math.floor((timediff-j*_oneDay-h*_oneHour-mn*_oneMinute));
			if (s<10) s="0"+s;

		var txt='';
		if (j>0) {
			txt+= j;
			if (j>1) txt+= ' '+jours;
			else	 txt+= ' '+jour;
			txt+=' '+et+' '+h+' ';
			if (h>1) txt+= ' '+heures;
			else	 txt+= ' '+heure;
		}
		else txt+= h + ':'+mn+':'+s;

		// Maj du div
		if (!document.getElementById(div_id)) return false;
		document.getElementById(div_id).innerHTML = txt;

		// Maj du titre du document
		if (title)
			document.title="Eredan-GT " + txt;

		setTimeout(function() { compte_rebour(timediff, div_id, redirect, title, jours, jour, heures, heure, et); }, 999);
	}
}


/*
 * Preload d'une liste d'images
 * @param 	Array  liste_img : liste des images
 */
function preload_array ( liste_img ) {

	var taille = liste_img.length;
	var preload = new Array(taille);

	for (var i=0; i<taille;i++) {
		preload[i] = new Image();
		preload[i].src = liste_img[i];
	}
}




function rediriger(url)
{
	window.location.replace(url);
}


function addFav() {
	if (navigator.appName != "Microsoft Internet Explorer")
		window.sidebar.addPanel(document.title, location.href, "");
	else
		window.external.AddFavorite(location.href, document.title);
}


function addEvent(elm, evType, fn, useCapture) {
	if (document.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (document.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else {
		elm['on' + evType] = fn;
	}
}


function traduire(phrase, trads)
{
	jQuery.each(trads, function(cle, val) {
		phrase = phrase.replace(new RegExp("%"+cle.toUpperCase()+"%","g"), val);
	});

	return phrase;
}


function confirm(message, callbackyes, callbackno) {
	$('#confirm').modal({
		close:false,
		position: ["40%",],
		opacity:30,
		overlayCss : { 'backgroundColor':'#000' },
		overlayId:'confirmModalOverlay',
		containerId:'confirmModalContainer',
		onShow: function (dialog) {

			dialog.data.find('.message').append(message);

			// if the user clicks "yes"
			dialog.data.find('.yes').click(function () {
				// call the callback
				if ($.isFunction(callbackyes)) {
					callbackyes.apply();
				}
				// close the dialog
				$.modal.close();
			});

			dialog.data.find('.no').click(function() {
				// call the callback
				if ($.isFunction(callbackno)) {
					callbackno.apply();
				}
				// close the dialog
				$.modal.close();
			});
		}
	});
}


function isInteger (s)
   {
      var i;

      if (isEmpty(s))
      if (isInteger.arguments.length == 1) return 0;
      else return (isInteger.arguments[1] == true);

      for (i = 0; i < s.length; i++)
      {
         var c = s.charAt(i);

         if (!isDigit(c)) return false;
      }

      return true;
   }

 function isEmpty(s)
   {
      return ((s == null) || (s.length == 0))
   }

   function isDigit (c)
   {
      return ((c >= "0") && (c <= "9"))
   }