﻿function ouvrirfenetre (lurl, lenom, width, height) {
	var left = Math.round( (screen.width - width) / 2 ),
		top = Math.round( (screen.height - height) / 2 );
	if (left<0) left = 0;
	if (top<0) top = 0;
	window.open(lurl, lenom, 'toolbar=0,menubar=0,resize=0,location=1,scrollbars=1,width=' + width + ',height=' + height + ',top=' + top + ',left=' + left);
}

function afficherLoadp (idCalque) {
	document.getElementById(idCalque).innerHTML = 'Chargement en cours de ta photo...<br /><br /><img src="images/ajax_load02.gif" alt="" />';	
}

ES.onAir = function () {
	_gat._getTracker("UA-19517754-1")._trackPageview("/titre.txt")
	$("#pubs_s_1, #pubs_s_2, #pubs_s_3").css("display", "none");
	$.ajax({
		type: "GET",
		url: "titre.txt",
		data: "R=" + new Date(),
		success: function (titre) {
			if (ES.onAir.titre != titre) {
				if (ES.onAir.titre != null) {
					$("#letitreencours").fadeTo(2500, 0.01, function () {
						$("#letitreencours").html(titre);
					}).fadeTo(2500, 0.9);
					ES.onAir.setPochette(true);
					ES.onAir.setVote(true);
				} else {
					$("#letitreencours").html(titre);
					ES.onAir.setPochette();
					ES.onAir.setVote();
				}
			}
			ES.onAir.titre = titre;
		}
	});
	$.ajax({
		type: "GET",
		url: "enjoyplayer/connectes_total.txt",
		data: "R=" + new Date(),
		success: function (auditeur) {
			auditeur = parseInt(auditeur);
			if (!isNaN(auditeur)) {
				if(ES.onAir.auditeur == null || auditeur > (ES.onAir.auditeur / 2)) {
					if (ES.onAir.auditeur == null) {
						var diff = Math.ceil(auditeur / 50), type = 1;
					} else {
						if (ES.onAir.auditeur < auditeur) {
							var diff = Math.ceil((auditeur - ES.onAir.auditeur) / 30), type = 2;
						} else {
							var diff = Math.ceil((ES.onAir.auditeur - auditeur) / 30), type = 3;
						}
					}
					if (diff != 0)
						ES.onAir.changeAuditeur(auditeur, ES.onAir.auditeur, diff, type);
					ES.onAir.auditeur = auditeur;
				}
			}
		}
	});
	$.ajax({
		type: "GET",
		url : "/ajax/ajax_emission.php",
		data : "action=get&R=" + new Date(),
		dataType : "json",
		success : function (data) {
			if (ES.onAir.emission != data.name) {
				if (ES.onAir.emission != null) {
					$("#emission .image img").fadeTo(2500, 0.01, function () {
						$(this).attr("src", data.image).attr("alt", data.name);
					}).fadeTo(2500, 0.9);
					$("#time").html(data.start + " - " + data.end);
					$("#next #heure").html(data.end);
					$("#next #nom-emission").html(data.after.name);
					$("#next #info-emission").html(data.after.info);
				} else {
					$("#emission .image img").attr("src", data.image).attr("alt", data.name);
					$("#time").html(data.start + " - " + data.end);
					$("#next #heure").html(data.end);
					$("#next #nom-emission").html(data.after.name);
					$("#next #info-emission").html(data.after.info);
				}
			}
			ES.onAir.emission = data.name;
		}
	});
};
$.extend(ES.onAir, {
	titre : null,
	auditeur : null,
	emission : null,
	setPochette : function (fade) {
		if (fade) {
			$("#image-titre").fadeTo(2500, 0.01, function () {
				$.ajax({
					type: "GET",
					url: "ajax/ajax_titre.php",
					data: "action=get_pochette&R=" + new Date(),
					success: function (pochette) {
						$('#image-titre').html('<img src="' + pochette + '" alt="Pochette du titre en cours" />');
					}
				});
			}).fadeTo(2500, 0.9);
		} else {
			$.ajax({
				type: "GET",
				url: "ajax/ajax_titre.php",
				data: "action=get_pochette&R=" + new Date(),
				success: function (pochette) {
					$('#image-titre').html('<img src="' + pochette + '" alt="Pochette du titre en cours" />');
				}
			});
		}
	},
	setVote : function () {
		$.ajax({
			type: "GET",
			url: "ajax/ajax_titre.php",
			data: "action=get_iunique&R=" + new Date(),
			success: function (id) {
				var val = id.split(" ");
				$('#votepour').attr("href", "javascript: vote('pourcontre', '" + val[0] + "', '" + val[1] + "', 'pour', 'smal')");
				$('#votecontre').attr("href", "javascript: vote('pourcontre', '" + val[0] + "', '" + val[1] + "', 'contre', 'smal')");
			}
		});
	},
	changeAuditeur : function (auditeur, next, diff, type) {
		var number = 0;
		if (type == 1 || type == 2) {
			if(next > auditeur || next == auditeur) {
				number = auditeur;
			} else {
				number = next;
				next = next + diff;
			}
		} else if (type == 3) {
			if (next < auditeur || next == auditeur) {
				number = auditeur;
			} else {
				number = next;
				next = next - diff;
			}
		}
		$('#stats').html('<center>' + number + ' Auditeurs</center>');
		if (number != auditeur)
			setTimeout("ES.onAir.changeAuditeur(" + auditeur + ", " + next + ", " + diff + ", " + type + ")", 100);
	}
});

$(function () {
	ES.onAir();
	setInterval(ES.onAir, 20000);
});

function vote (idCalque, cibleArtiste, cibleTitre, vote, vtype) {
	//document.getElementById(idCalque).innerHTML = 'Vote en cours...';
	$.ajax({
		type: "GET",
		url: "ajax/ajax_moteur.php",
		data: "action=titre_vote&artiste=" + cibleArtiste + "&titre=" + cibleTitre + "&vote=" + vote + "&vtype=" + vtype + "&R=" + new Date(),
		success: function (resultat) {
			//document.getElementById(idCalque).innerHTML = resultat;
			ES.alert("Alerte", resultat);
		}
	});
}

function couleur (indexcouleur, calque, couleur1, couleur2) {
	tabColoris = new Array (couleur1, couleur2);
	indexcouleur++;
	if (indexcouleur >= tabColoris.length) indexcouleur = 0;
	document.getElementById(calque).setAttribute('color', tabColoris[ indexcouleur ]);
	setTimeout('couleur("' + indexcouleur + '","' + calque + '","' + couleur1 + '","' + couleur2 + '")', 500);
}
