/** Javascript par Atikae pour Warlegend Projet **/

function sleep(milliseconds) {
    var start = new Date().getTime();
        for (var i = 0; i < 1e7; i++) {
            if ((new Date().getTime() - start) > milliseconds){
                break;
        }
    }
}



function canClick(url) {
    if (window.time_over) {
        window.open(url); 
        setTimeout(function() { window.location.reload(true); }, 1000);
    }
    
    return false;
}

function classementLoadPage(type, check) {
	if (check && $("#classement-type").html()[0] == type) return false;
	$("#classement-type").html(type+" vs "+type);
	$("#classement-pvp-content").html("<img alt=\"loading\" src=\"img/ajax-loader.gif\" />");
	$("#classement-pvp-content").css("text-align", "center");
	$.getJSON("/modules_ajax/classement_pvp.php?type="+type+"&callback=?",
	function(data){
		var j = 1;
		$("#classement-pvp-content").html('');
		jQuery.each(data, function(i,item){
			var content = "";
			content += "<div class=\"classement-pvp-entity\"><span class=\"classement-content-title\">"+item["name"]+" ("+item["rating"]+")</span>";
			content += "<div class=\"classement-pvp-entity-content\"><table style=\"width: 100%\">\n<thead>\n<tr class=\"classement-pvp-table-title\">\n<th>"+j+"</th>\n<th>Nom</th>\n<th>V.H perso.</th>\n<th>Victoires</th>\n<th>Défaites</th>\n</thead>\n<tbody>\n";
			$.each(item["members"], function(key, value) {
				content += "<tr>\n<td><img alt=\"Faction du perso\" height=\"18\" width=\"18\" src=\"/images/wow/"+value["faction"]+".png\" />&nbsp;<img alt=\"Classe du perso\" src=\"/images/wow/"+value["class"]+".gif\" /></td>\n<td>"+value["name"]+"</td>\n<td>"+value["rating"]+"</td>\n<td>"+value["won"]+"</td>\n<td>"+value["lost"]+"</td>\n</tr>\n";
		    });
		    j++;
		    
		    content += "</tbody>\n</table></div>\n<br /></div>";
		    $("#classement-pvp-content").append(content);
      	});
	});
}

function refreshCounter() {
    time_escape.setTime(time_escape.getTime() + 500);
    
    if ((time_escape.getTime()/1000) > time_between_vote) {
    	window.time_over = true;
		$("#votes-bonus-head .votes-bonus img").css("background-color", '');
		$("#votes-bonus-head .votes-bonus img").css("opacity", '1');
		$("#votes-bonus-head .votes-bonus img").css("filter", 'alpha(opacity=100)');
		$("#canVote").html('Vous pouvez voter.');
		
	    $("#footer img").css("background-color", '');
		$("#footer img").css("opacity", '1');
		$("#footer img").css("filter", 'alpha(opacity=100)');
		$("#canVote2").html('');
	} else {
		$("#canVote").html('Vous pourrez voter dans <span id="canVoteSeconds" style="color: red;"></span> secondes');
		$("#canVoteSeconds").html(time_between_vote + 1 - Math.floor(time_escape.getTime()/1000));
		
		$("#canVote2").html('Vous pourrez voter dans <span id="canVoteSeconds2" style="color: red;"></span> secondes');
		$("#canVoteSeconds2").html(time_between_vote + 1 - Math.floor(time_escape.getTime()/1000));
	}
    /*
	var votes_bonus = document.getElementById('votes-bonus-head');
    if (votes_bonus != null) {
	    for (var i = 0; i != votes_bonus.childNodes.length; ++i)
	    {
		    var child = votes_bonus.childNodes[i];

            if (child.className == 'votes-bonus') {
                var child_img = child.childNodes[0];
                if (diff > time_between_vote) {
                    time_over = true;
                    
                    child_img.style.backgroundColor = '';
                    child_img.style.opacity = '1';
                    child_img.style.filter = 'alpha(opacity=1)';
                    document.getElementById('canVote').innerHTML = 'Vous pouvez voter.';
                } else {
                    document.getElementById('canVote').innerHTML = 'Vous pourrez voter dans <span id="canVoteSeconds" style="color: red;"></span> secondes';
                    document.getElementById('canVoteSeconds').innerHTML = time_between_vote + 1 - diff;
                }
            }
	    }
	}
	
	var footer_votes_bonus = document.getElementById('footer');
	for (var i = 0; i != footer_votes_bonus.childNodes.length; ++i)
	{
		var child = footer_votes_bonus.childNodes[i];

        if (child.className == 'votes-bonus') {
            var child_img = child.childNodes[0];
            if (diff > time_between_vote) {
                time_over = true;
                child_img.style.backgroundColor = '';
                child_img.style.opacity = '1';
                child_img.style.filter = 'alpha(opacity=1)';
                document.getElementById('canVote2').innerHTML = '';
            } else {
                document.getElementById('canVote2').innerHTML = 'Vous pourrez voter dans <span id="canVoteSeconds2" style="color: red;"></span> secondes';
                document.getElementById('canVoteSeconds2').innerHTML = time_between_vote + 1 - diff;
            }
        }
	}*/
}

function getForm(type) {
	if (window.contactOptions === undefined) window.contactOptions = '';
	if (type === undefined) type = 'default';
	
	$("#contact-content").html("<img alt=\"loading\" src=\"img/ajax-loader.gif\" />");
	jQuery.get('/modules_ajax/contact.php', { file: type, options: contactOptions }, function(data) {
									window.contentTextarea = undefined;
									$("#contact-content").html(data);
									if (window.contentTextarea === undefined)
										window.contentTextarea = $("#content-textarea").val();
								
									$("#content-textarea").focus(function() {
										if ($("#content-textarea").val() == window.contentTextarea)
											$("#content-textarea").val('');
									});
	
									$("#content-textarea").blur(function() {
										if ($("#content-textarea").val() === '')
											$("#content-textarea").val(window.contentTextarea);
									});
									$("#contact-form").attr("action", "?type="+type);
							  });
}

function initEventHandlers() {	

    /* Mininews */
    $(".mininews-entity-expand").click(function() {
        if ($(this).attr("src") == "img/mininews-entity-expand-on.gif")
            $(this).attr("src", "img/mininews-entity-expand-off.gif");
        else
            $(this).attr("src", "img/mininews-entity-expand-on.gif");
        $(this).parents(".mininews-entity:first").find(".mininews-entity-content").slideToggle("slow");
    });

    /* Update Select for change information form */
    if (document.getElementById('update_select') != null) {
	    document.getElementById('update_select').onchange = 
		    function() {
			    if (this.value == 'pass') {
				    document.getElementById('update_mdp').style.display = 'block';
				    document.getElementById('update_mail').style.display = 'none';
			    } else {
				    document.getElementById('update_mdp').style.display = 'none';
				    document.getElementById('update_mail').style.display = 'block';
			    }
	    }
	}	
	
	/* General boxes */
	$(".box").find(".box-header")
				.prepend('<span class="ui-icon ui-icon-minusthick ui-icon-mininews"></span>')
				.end()
			.find(".box-content");

	$(".box-header .ui-icon").click(function() {
		$(this).toggleClass("ui-icon-minusthick").toggleClass("ui-icon-plusthick");
		$(this).parents(".box:first").find(".box-content").slideToggle("slow");
	});
	
	/* Menu content */
	$(".menu-entity").find(".menu-entity-header")
				.prepend('<span class="ui-icon ui-icon-minusthick"></span>')
				.end()
			.find(".menu-entity-content");

	$(".menu-entity-header .ui-icon").click(function() {
		$(this).toggleClass("ui-icon-minusthick").toggleClass("ui-icon-plusthick");
		$(this).parents(".menu-entity:first").find(".menu-entity-content").slideToggle("slow");
	});

	if (window.location.pathname == "/classement-pvp.php") {
		$(".classement-button").click(function() { classementLoadPage($(this).attr("alt"), true); });
		classementLoadPage("2", false); // Chargement 2v2 par défaut
	}
	
	$("#content-type").change(function() {
		if ($("#content-type option:selected").val() != '-----') {
			if (window.contentTextarea !== undefined &&  window.contentTextarea != $("#content-textarea").val()) {
				if (!confirm('Changer de type de requête ?'))
					return false;
			}
			getForm($("#content-type option:selected").val());
		}
	});	
	
	if (window.location.pathname == "/contact.php" || window.location.pathname == "/support-wow-prive.htm") {
		if ($.url.param("type") != '')
			getForm($.url.param("type"));
		else
			getForm('default');
	}
	
	if (is_admin) {
		$(".column-portlet").find(".box")
			.find(".box-header")
				.addClass("ui-corner-all")
				.end()
			.find("box-content");

		$(".column-portlet").sortable({connectWith: '.column-portlet'});
		$(".column-portlet").disableSelection();
	}
	
	$("#contact-send").parents("form").submit(function() {
		if (window.contentTextarea === undefined || window.contentTextarea == $("#content-textarea").val())
			return false;
		else
			return true;
	});
	
	xOffset = 10;
	yOffset = 20;                   
	$(".honor-rank-img").hover(function(e){                                                                                        
		this.t = this.title;
		this.title = "";                                                                         
		$("body").append("<p id='tooltip'>"+ this.t +"</p>");
		$("#tooltip").css("top",(e.pageY - xOffset) + "px")	.css("left",(e.pageX + yOffset) + "px").fadeIn("fast");               
	},
	function(){
		this.title = this.t;           
		$("#tooltip").remove();
	}); 
	
	$("a.tooltip").mousemove(function(e){
		$("#tooltip").css("top",(e.pageY - xOffset) + "px").css("left",(e.pageX + yOffset) + "px");
	});        
	
	$("#add-entree-champs").click(function() {
		$("#add-contact-input").append('Champs : <input type="text" name="champs[]" /><br /> \
	                                                       Erreur : &nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="erreurs[]" class="input-error" /><br /><br />');
	});
	
	window.contentTextarea = undefined;
	if (window.contentTextarea === undefined)
		window.contentTextarea = $("#content-textarea").val();

	$("#content-textarea").focus(function() {
		if ($("#content-textarea").val() == window.contentTextarea && $.url.param("type") == "")
			$("#content-textarea").val('');
	});

	$("#content-textarea").blur(function() {
		if ($("#content-textarea").val() === ''  && $.url.param("type") == "")
			$("#content-textarea").val(window.contentTextarea);
	});
	
	$("#content-type-admin").change(function() {
		if ($("#content-type-admin").text().match('Editer')) {
			location.href = $.url.attr("path")+"?type="+$("#content-type-admin").val();
			return;
		}
	});
			
	
	setInterval('refreshCounter()', 500);
}

function addChamp(name) {
	if (name == 'dynimage')
		document.getElementById('dynimage_champ').innerHTML = document.getElementById('dynimage_champ').innerHTML + 'Src : <input type="text" name="dynimage_src[]" />  Href : <input type="text" name="dynimage_link[]" /><br />'
	else if (name == 'slider')
		document.getElementById('slider_champ').innerHTML = document.getElementById('slider_champ').innerHTML + 'Src : <input type="text" name="slider_src[]" />  Href : <input type="text" name="slider_link[]" /><br />'
}

if (window.addEventListener)
	window.addEventListener('load', initEventHandlers, false);
else
	window.attachEvent('onload', initEventHandlers);	

// Pour que tout soit lu...
// Pour que tout soit lu...
// Pour que tout soit lu...
// Pour que tout soit lu...
// Pour que tout soit lu...
// Pour que tout soit lu...
// Pour que tout soit lu...
// Pour que tout soit lu...
// Pour que tout soit lu...

