var i=false;
function move(e) {
	if(i) {
		if (navigator.appName!="Microsoft Internet Explorer") {
			$('#info-content').css('left',e.pageX + 5+"px");
			$('#info-content').css('top',e.pageY + 10+"px");
		}
		else {
			if(document.documentElement.clientWidth>0) {
				$('#info-content').css('left', 20+event.x+document.documentElement.scrollLeft+"px");
				$('#info-content').css('top',10+event.y+document.documentElement.scrollTop+"px");
			} else {
				$('#info-content').css('left',20+event.x+document.body.scrollLeft+"px");
				$('#info-content').css('top',10+event.y+document.body.scrollTop+"px");
			}
		}
	}
}

document.onmousemove=move;

showinfo = function(e) {
  	if(i==false) {
	  	$('#info-content').css('visibility','visible');
	  	$('#info-content').html($(this).attr('info'));
	  	i=true;
	}
}
hideinfo = function(e) {
  	if(i==true) {
		$('#info-content').css('visibility','hidden');
		$('#info-content').innerHTML = '';
		i=false;
	}
}

info = function(){
  $('[@info]').css('cursor','url(/images/cursor.cur)');
  $('[@info] > *').css('cursor','url(/images/cursor.cur)');
  $('[@info]').hover(showinfo, hideinfo);
}



function ajax(file, variable, method) {
	var req = null;  
	if (window.XMLHttpRequest){
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject){
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	req.open(method, file, false); 
	if(method == 'POST') {
		req.setRequestHeader('Content-length', '0'); 
		req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	}
	req.send(variable); 
	return req.responseText;
}

$(document).ready(function() {
  info();
  getTimemaj();
  $('a[rel=lightbox]').lightBox();
  
  // Tabs
  $(function () {
	  var tabContainers = $('div.tabs > div');	  
	  $('div.tabs ul.tabNavigation a').click(function () {
		  tabContainers.hide().filter(this.hash).show();
		  $('div.tabs ul.tabNavigation a').removeClass('selected');
		  $(this).addClass('selected');
		  return false;
	  }).filter(':first').click();
  });
});

function meta_look(){
	$('[class="toggle_img"][toggle_id="1"]').attr('src','http://www.lacitadellenoire.com/images/icons/small_less.gif');
	$('[toggle_id="1"]').show();
	$("#meta_toggle").fadeOut('slow');
	document.getElementById("meta_toggle").click(function(){toggle(0,1)});
}

function toggle(id, toggle_id) {
	$('[toggle][toggle_id="'+toggle_id+'"]').hide();
	$('[class="toggle_img"][toggle_id="'+toggle_id+'"]').attr('src','http://data.hordes.fr/gfx/icons/small_more.gif');
	$('[id="img_'+id+'"][toggle_id="'+toggle_id+'"]').attr('src','http://www.lacitadellenoire.com/images/icons/small_less.gif');
	$('[toggle="'+id+'"][toggle_id="'+toggle_id+'"]').show();
	$("#meta_toggle").fadeIn('slow');
	document.getElementById("meta_toggle").click(function(){meta_look()});
	$.scrollTo('[toggle="'+id+'"][toggle_id="'+toggle_id+'"]', 1000);
}

function getTimemaj() {
	
	var now = document.getElementById('value_time').innerHTML;
	var y2k = document.getElementById('value_tomorow').innerHTML;
	
	hours = (y2k - now) / 60 / 60;
	hoursRound = Math.floor(hours);
	minutes = (y2k - now) /60 - (60 * hoursRound);
	minutesRound = Math.floor(minutes);
	seconds = (y2k - now) - (60 * 60 * hoursRound) - (60 * minutesRound);
	secondsRound = Math.round(seconds);
	
	if (hoursRound < 10) hoursRound = '0' + hoursRound;
	if (minutesRound < 10) minutesRound = '0' + minutesRound;
	if (secondsRound < 10) secondsRound = '0' + secondsRound;
	
	if ((y2k - now) >= 0) {
		document.getElementById('temps').innerHTML = 'Prochaine attaque dans : '+hoursRound + 'h' + minutesRound + 'm' + secondsRound +'s';
		document.getElementById('value_time').innerHTML++;
		newtime = window.setTimeout("getTimemaj();", 1000);
	} else {
		document.getElementById('temps').innerHTML = 'C\'est l\'heure de l\'attaque !!!' ;
	}
}

function visibilite(thingId) {
	var targetElement;
	targetElement = document.getElementById(thingId) ;
	if (targetElement.style.display == "none"){
		targetElement.style.display = "" ;
	} else {
		targetElement.style.display = "none" ;
	}
}

function display(id) {
	if($('#'+id).css('display')=='none'){
		$('#'+id).slideDown(1000, function() {$.scrollTo('#'+id, 1000);});
	}else{
		$('#'+id).slideUp(1000);
	}
}

function smiley(text)
{
	var areaId = "messagetext";
	var txtarea = document.getElementById(areaId);
	var scrollPos = txtarea.scrollTop;
	var strPos = 0;
	var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ? "ff" : (document.selection ? "ie" : false ) );
	if (br == "ie") {
		txtarea.focus();
		var range = document.selection.createRange();
		range.moveStart ('character', -txtarea.value.length);
		strPos = range.text.length;
	}
	else if (br == "ff") strPos = txtarea.selectionStart;
	var front = (txtarea.value).substring(0,strPos);
	var back = (txtarea.value).substring(strPos,txtarea.value.length);
	txtarea.value=front+text+back; strPos = strPos + text.length;
	if (br == "ie") {
		txtarea.focus();
		var range = document.selection.createRange();
		range.moveStart ('character', -txtarea.value.length);
		range.moveStart ('character', strPos);
		range.moveEnd ('character', 0); 
		range.select();
	}
	else if (br == "ff") {
		txtarea.selectionStart = strPos;
		txtarea.selectionEnd = strPos;
		txtarea.focus();
	}
	txtarea.scrollTop = scrollPos; 
} 


function smiley_forum(text) {	
	var areaId = "text_forum";
	var txtarea = document.getElementById(areaId); var scrollPos = txtarea.scrollTop; var strPos = 0; var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ? "ff" : (document.selection ? "ie" : false ) ); if (br == "ie") { txtarea.focus(); var range = document.selection.createRange(); range.moveStart ('character', -txtarea.value.length); strPos = range.text.length; } else if (br == "ff") strPos = txtarea.selectionStart; var front = (txtarea.value).substring(0,strPos); var back = (txtarea.value).substring(strPos,txtarea.value.length); txtarea.value=front+text+back; strPos = strPos + text.length; if (br == "ie") { txtarea.focus(); var range = document.selection.createRange(); range.moveStart ('character', -txtarea.value.length); range.moveStart ('character', strPos); range.moveEnd ('character', 0); range.select(); } else if (br == "ff") { txtarea.selectionStart = strPos; txtarea.selectionEnd = strPos; txtarea.focus(); } txtarea.scrollTop = scrollPos; 
}

function stripslashes( str ) { 
    return (str+'').replace(/\0/g, '0').replace(/\\([\\'"])/g, '$1');
}


function cite(user,id){
	if($('#reponse').css('display')=='none'){
		$('#text_forum').html($('#text_forum').html()+'[i][g]'+user+'[/g] a dit : [/i]\n[cite]'+$('#hide_'+id).html()+'[/cite]');
		display('reponse');
	}
	else{
		smiley_forum('[i][g]'+user+'[/g] a dit : [/i]\n[cite]'+$('#hide_'+id).html()+'[/cite]');
	}
}

// BALISES

var isMozilla = (navigator.userAgent.toLowerCase().indexOf('gecko')!=-1) ? true : false;
var regexp = new RegExp("[\r]","gi");

function bbcode(balise, id)
{
	if (isMozilla) 
	{
	// Si on est sur Mozilla

		objet = document.getElementById(id);
		texteTotal = objet.value;
		debutSelec = objet.selectionStart;
		finSelec = objet.selectionEnd;
		poscurseur = objet.scrollTop;
				
		debut = texteTotal.substring(0,debutSelec);
		milieu = texteTotal.substring(debutSelec,finSelec);
		fin = texteTotal.substring(finSelec,objet.textLength);
				
		if(balise != 'lien'){
			objet.value = debut + "[" + balise + "]" + milieu + "[/" + balise + "]" + fin;
			objet.setSelectionRange(debutSelec+balise.length+2,debutSelec+balise.length+milieu.length+2);
		}else{
			if(milieu =='') {
				var lien = prompt('URL : ','http://'); 
				var titre = prompt('Titre : ',lien);
				objet.value = debut+"[lien="+lien+"]"+titre+"[/lien]"+fin;
				objet.setSelectionRange(debutSelec+7+lien.length,debutSelec+7+lien.length+titre.length);
			}else{
				var lien = prompt('URL : ','http://'); 
				objet.value = debut + "[lien="+lien+"]" + milieu + "[/lien]" + fin;
				objet.setSelectionRange(debutSelec+7+lien.length,debutSelec+7+lien.length+milieu.length);

			}
		}
		
		objet.focus();
		objet.scrollTop = poscurseur;
	}
	else
	{
	// Si on est sur IE
		
		oField = document.getElementById('text_forum');
		var str = document.selection.createRange().text;

		if (str.length>0)
		{
		// Si on a selectionné du texte
			var sel = document.selection.createRange();
			if(balise != 'lien'){
				sel.text = "[" + balise + "]" + str + "[/" + balise + "]";
			}else{
				sel.text = "[lien=" + prompt('URL : ','http://')+"]" + str + "[/lien]";
			}
			sel.collapse();
			sel.select();
		}
		else
		{
			oField.focus(oField.caretPos);
		//	alert(oField.caretPos+"\n"+oField.value.length+"\n")
			oField.focus(oField.value.length);
			oField.caretPos = document.selection.createRange().duplicate();
			
			var bidon = "%~%";
			var orig = oField.value;
			oField.caretPos.text = bidon;
			var i = oField.value.search(bidon);
			if(selec != 'lien'){
				oField.value = orig.substr(0,i) + "[" + balise + "][/" + balise + "]" + orig.substr(i, oField.value.length);
			}else{
				oField.value = orig.substr(0,i)+"[lien="+prompt('URL : ','http://')+"]"+prompt('Titre : ','')+"[/lien]"+orig.substr(i, oField.value.length);
			}
			var r = 0;
			for(n = 0; n < i; n++)
			{if(regexp.test(oField.value.substr(n,2)) == true){r++;}};
			pos = i + 2 + selec.length - r;
			//placer(document.forms['news'].elements['newst'], pos);
			var r = oField.createTextRange();
			r.moveStart('character', pos);
			r.collapse();
			r.select();

		}
	}
}

function prev(from,to) {
	var div_from = document.getElementById(from).value;
	var div_to = document.getElementById(to);
	var reg1 = new RegExp('&', 'g');
	div_from = div_from.replace(reg1,":and:");
	content = ajax('/js/prev.php', 'prev='+div_from, 'POST');
	div_to.innerHTML = content;
}

function open_prev() {
	var mess_prev, info_prev;
	mess_prev = document.getElementById('mess_prev');
	info_prev = document.getElementById('info_prev');
	if(mess_prev.style.display=='none'){
		$('#mess_prev').fadeIn('slow');
		$('#info_prev').fadeIn('slow');
		$.scrollTo('#info_prev', 1000);
	}
}

function limit(id,nb){
	var textearea = document.getElementById(id);
	var longueur = $('#'+id).val().length;
	var reste = nb-longueur;
	$('#limit_'+id).html(reste);
	if(reste<=0){
		$('#'+id).val($('#'+id).val().substring(0,nb));
		$('#limit_'+id).html('0');
	}else{
		$('#limit_'+id).html(reste);
	}
}

var popup = $('#popup');

popup.closed = function() {$('#popup').fadeOut('slow');} // Clos le popup
popup.opened = function() {$('#popup').fadeIn('slow');} // ouvre le popup
popup.change = function(e) { // Change le contenue du popup et l'ouvre
	$('#popup_mess').innerHTML=e;
	popup.opened()
}

function envoyer(name, message) {
	if(message!=null) {
		if(confirm(message)) {
			document.forms[name].submit();
		}
	} else {
		document.forms[name].submit();
	}
}

// AJAX PAGE RECHERCHE
function chargement(id) {
	var value = '<br><br><br><center><b>Recherche en cours ... Veuillez patienter.</b><br><img src="/images/ajax-loader.gif" /></center>';
	document.getElementById(id).innerHTML = value;
}

function recherche_ame(type, value) {
	if(value == "null") {
		return false;
	}
	if(type == 'liste'){
		chargement('profil_recherche');
		document.getElementById('profil_recherche').style.display = "block";
		var return_ajax = ajax('/js/rechercher.php?type=liste_profil&id='+value,'','GET');
		document.getElementById('profil_recherche').innerHTML = return_ajax;
	}else if(type == 'recherche') {
		if(value.length>=3){
			chargement('profil_recherche');
			document.getElementById('profil_recherche').style.display = "block";
			var return_ajax = ajax('/js/rechercher.php?type=recherche_profil','pseudo='+value,'POST');
			document.getElementById('profil_recherche').innerHTML = return_ajax;
		} else {
			document.getElementById('profil_recherche').style.display = "none";
		}
	}
}

function recherche_ville(value) {
	if(value == "null") {
		return false;
	}
	if(value.length>=3){
		chargement('ville_recherche');
		document.getElementById('ville_recherche').style.display = "block";
		var return_ajax = ajax('/js/rechercher.php?type=recherche_ville','ville='+value,'POST');
		document.getElementById('ville_recherche').innerHTML = return_ajax;
	} else {
		document.getElementById('ville_recherche').style.display = "none";
	}
}

function recherche_meta(type, value) {
	if(value == "null") {
		return false;
	}
	if(type == 'liste'){
		chargement('meta_recherche');
		document.getElementById('meta_recherche').style.display = "block";
		var return_ajax = ajax('/js/rechercher.php?type=liste_meta&id='+value,'','GET');
		document.getElementById('meta_recherche').innerHTML = return_ajax;
	}else if(type == 'recherche') {
		if(value.length>=3){
			chargement('meta_recherche');
			document.getElementById('meta_recherche').style.display = "block";
			var return_ajax = ajax('/js/rechercher.php?type=recherche_meta','meta='+value,'POST');
			document.getElementById('meta_recherche').innerHTML = return_ajax;
		} else {
			document.getElementById('meta_recherche').style.display = "none";
		}
	}
}

function carte(x,y){
	$('.carte_info').hide();
	$('#carte_'+x+'_'+y).show();
	$.scrollTo('#carte_'+x+'_'+y, 1000);
}

function monkey(value) {
	chargement('dgm_admin');
	document.getElementById('dgm_admin').style.display = "block";
	var return_ajax = ajax('/js/dgm.php?id='+value,'','GET');
	document.getElementById('dgm_admin').innerHTML = return_ajax;
}

function map_scroll(e) {
	switch(e){
		case 'up'         : $('#motionmap').scrollTo({top : '-=104px'                   ,left : '-=0px'                    }  ,1000);                          break;
		case 'down'       : $('#motionmap').scrollTo({top : '+=104px'                   ,left : '-=0px'                    }  ,1000);                          break;
		case 'left'       : $('#motionmap').scrollTo({top : '-=0px'                     ,left : '-=104px'                  }  ,1000);                          break;
		case 'right'      : $('#motionmap').scrollTo({top : '-=0px'                     ,left : '+=104px'                  }  ,1000);                          break;
		case 'up_left'    : $('#motionmap').scrollTo({top : '-=104px'                   ,left : '-=104px'                  }  ,1000);                          break;
		case 'down_left'  : $('#motionmap').scrollTo({top : '+=104px'                   ,left : '-=104px'                  }  ,1000);                          break;
		case 'up_right'   : $('#motionmap').scrollTo({top : '-=104px'                   ,left : '+=104px'                  }  ,1000);                          break;
		case 'down_right' : $('#motionmap').scrollTo({top : '+=104px'                   ,left : '+=104px'                  }  ,1000);                          break;
		case 'center'     : $('#motionmap').scrollTo({top : $('#coor_center_y').val()   ,left : $('#coor_center_x').val()  }  ,1000); map_highlight();         break;
		case 'ville'      : $('#motionmap').scrollTo({top : $('#coor_ville_y').val()    ,left : $('#coor_ville_x').val()   }  ,1000); map_highlight('home');   break;
		case 'me'         : $('#motionmap').scrollTo({top : $('#coor_me_y').val()       ,left : $('#coor_me_x').val()      }  ,1000); map_highlight('me');     break;
		case 'n'          : $('#motionmap').scrollTo({top : '0'                         ,left : $('#coor_center_x').val()  }  ,1000); map_highlight('n');      break;
		case 'e'          : $('#motionmap').scrollTo({top : $('#coor_center_y').val()   ,left : $('#coor_width').val()     }  ,1000); map_highlight('e');      break;
		case 's'          : $('#motionmap').scrollTo({top : $('#coor_width').val()      ,left : $('#coor_center_x').val()  }  ,1000); map_highlight('s');      break;
		case 'o'          : $('#motionmap').scrollTo({top : $('#coor_center_y').val()   ,left : '0'                        }  ,1000); map_highlight('o');      break;
		case 'no'         : $('#motionmap').scrollTo({top : '0'                         ,left : '0'                        }  ,1000); map_highlight('no');     break;
		case 'ne'         : $('#motionmap').scrollTo({top : '0'                         ,left : $('#coor_width').val()     }  ,1000); map_highlight('ne');     break;
		case 'so'         : $('#motionmap').scrollTo({top : $('#coor_width').val()      ,left : '0'                        }  ,1000); map_highlight('so');     break;
		case 'se'         : $('#motionmap').scrollTo({top : $('#coor_width').val()      ,left : $('#coor_width').val()     }  ,1000); map_highlight('se');     break;
		default           : alert("Map_croll : mauvais parametre");                                                                                            break;
	}
}

function map_highlight(e) {
	$('.map_zone_highlight').css('display', 'none');
	if(e != null){
		if($('.highlight_'+e).length>0) {
			$('.highlight_'+e).fadeIn('medium', function() {
				$(this).fadeOut('medium',function() {
					$(this).fadeIn('medium',function() {
						$(this).fadeTo('medium',0.5);
					});
				});
			});
		} else {
			alert("Aucun élément n'a été trouvé.");
		}
	}
}