var visiteTimer;

var clockHour = _clock.getHours();
var clockMin = _clock.getMinutes();
var clockSec = _clock.getSeconds();

function clockServer() {
	if (clockSec < 10) {
		clockSec = "0" + Math.round(clockSec);
	}
	else if(clockSec >= 60) {
		clockSec = "00";
		clockMin++;
	}

	if (clockMin < 10) {
		clockMin = "0" + Math.round(clockMin);
	}
	else if(clockMin >= 60) {
		clockMin = "00";
		clockHour++;
	}

	if (clockHour < 10)
		clockHour = "0" + Math.round(clockHour);
	else if(clockHour >= 24)
		clockHour = "00";
	
	$('#clock_server').html(clockHour + ':' + clockMin);

	clockSec++;
}

// Permet de compter le total de caractéres d'un champ textuelle
function Total_caracteres(FormName, ElementName, ValTotal_Caractere, ValMinimum_Caractere, ValMaximum_Caractere)
{
	var formulaire = document.forms[FormName].elements[ElementName];

	Texte = (ValTotal_Caractere <= 1) ? "caractère" : "caractères";

	$('#carac_texte').html('Votre message fait <font class="alerte">'+ ValTotal_Caractere +'</font> '+ Texte +'.');
	formulaire.disabled = (ValTotal_Caractere < ValMinimum_Caractere || ValTotal_Caractere > ValMaximum_Caractere);
}

// Combat : modifier la cible
function battleAlterTarget() {
	$('#cible_adversaire').hide();
	$('#cible_allie').hide();

	if($('#arme_choice').options[$('#arme_choice').attr('selectedIndex')].className == 'allie')
		$('#cible_allie').show();
	else
		$('#cible_adversaire').show();
}

// Afficher l'encart Decouvrir RunesWorld (visite guidee)
function showVisite(i) {
	for(var j = 1; j <= 5; j++)
		$("#visite_guidee" + j).hide();

	$("#visite_guidee" + i).show();
}

var diapo = 1;
function diaporama() {
	diapo++;

	if(diapo > 5)
		diapo = 1;

	showVisite(diapo);
}

// Gestion des ressources
function load_resources() {
	ajaxLoader(_rootPath + "includes/get_realtime_infos.php", "resources_infos", "user_sid=" + _userSid);
}

// Lancer le combat
function load_battle_1vs1() {
	ajaxLoader(_rootPath + "includes/get_realtime_infos.php", "1vs1", "user_sid=" + _userSid);
}

function load_battle() {
	ajaxLoader(_rootPath + "includes/get_realtime_infos.php", "clanvsclan_battle", "user_sid=" + _userSid);
}

function load_table_game() {
	ajaxLoader(_rootPath + "includes/get_realtime_infos.php", "clanvsclan_table_game", "user_sid=" + _userSid);
}

function load_clan_enemy() {
	ajaxLoader(_rootPath + "includes/get_realtime_infos.php", "clanvsclan_clan_enemy", "user_sid=" + _userSid);
}

// Parler avec un pnj
var pnjBubble = false;
function showPnjBubble(myText) {
	if(pnjBubble) {
		showPnjText(myText);
		return;
	}

	$('#bulle_pnj').fadeIn(1000, function() {
		$('#pnj_text').html(myText);
		$('#pnj_text').fadeIn(1000);
	});
	
	pnjBubble = true;
}
function showPnjText(myText) {
	$('#pnj_text').hide();
	$('#pnj_text').html(myText);
	$('#pnj_text').fadeIn(1000);
}
function hidePnjBubble() {
	if(!pnjBubble)
		return;

	$('#bulle_pnj').fadeOut(1000);
	$('#pnj_text').fadeOut(1000);
	
	pnjBubble = false;
}
function hidePnjText() {
	$('#pnj_text').fadeOut(1000);
}

// Réponse du joueur
var playerBubble = false;
function showPlayerBubble(myText) {
	$('#bulle_pnj').queue(function() {
		$('#bulle_joueur').fadeIn(2000, function() {
			$('#joueur_text').html(myText);
			$('#joueur_text').fadeIn(1000);
		});
		$(this).dequeue();
	});
	
	playerBubble = true;
}
function hidePlayerBubble() {
	if(!playerBubble)
		return;

	$('#bulle_joueur').fadeOut(1000);
	$('#joueur_text').fadeOut(1000);
	
	playerBubble = false;
}

// Ouvrir ou fermer la popup
function openPopup(content, title) {
	$('#popup').dialog("option", "title", $('#popup h2').html());
	
	if(title != null)
		$('#popup').dialog("option", "title", title);
	
	$('#popup-content').html(content);
	$('#popup').dialog("open");
}

function closePopup() {
	$('#popup').dialog("close");
}

var prettyOptions = {
	allow_resize: false,
	deeplinking: false,
	opacity: 0.8,
	show_title: true,
	social_tools: ''
};

$(document).ready(function() {
	$("a[rel^='prettyPhoto']").prettyPhoto(prettyOptions);
	
	$("#popup").dialog({
		autoOpen: false,
		buttons: {
			Ok: function() {
				$(this).dialog("close");
			}
		},
		draggable: false,
		height: 150,
		minHeight: 150,
		modal: true,
		position: ["center", 100],
		resizable: false,
		width: 500
	});
	
	// Index
	if($('#visite_guidee1').length == 1) {
		showVisite(1);
		visiteTimer = setInterval(diaporama, 6000);
	}
	// Pages d'attente combats
	else if($("#load_battle").length == 1) {
		load_battle();
		setInterval("load_battle()", 5000);
	}
	else if($("#load_table_game").length == 1) {
		load_table_game();
		setInterval("load_table_game()", 10000);
	}
	else if($("#load_clan_enemy").length == 1) {
		load_clan_enemy();
		setInterval("load_clan_enemy()", 10000);
	}
	else if($("#load_battle_1vs1").length == 1) {
		setInterval("load_battle_1vs1()", 5000);
	}
	// Combat
	else if($('#arme_choice').length == 1)
		battleAlterTarget();
	
	// Executions automatiques
	setInterval("clockServer()", 1000);
	setInterval("load_resources()", 10000);
});

jQuery.fn.outerHTML = function() {
    return $('<div>').append(this.eq(0).clone()).html();
};

