var isIE = (navigator.appName.indexOf("Microsoft") !=-1);
var posX = 0 ;
var posY = 0 ;
var width = 0 ;
var height = 0 ;
var topScreen = 0 ;
var liveSearch = "" ;

var mouseX = 0 ;
var mouseY = 0 ;

//Initialisation du layer
function ajaxBox_init(prmBoxTitle,prmBoxText,prmBoxHeight,prmBoxWidth) {
	//attribution du titre et du texte de la box
	ajaxBox_setTitle(prmBoxTitle);
	ajaxBox_setSimpleText(prmBoxText);
	
	//dimensionnement de la box
	ajaxBox_resize(prmBoxHeight,prmBoxWidth) ;
	
	//création des liens pour le controlPanel
	document.getElementById("ajaxBoxControlPanel").firstChild.nodeValue = " x " ;
}

//afficher le layer
function ajaxBox_show() {
	document.getElementById("ajaxBoxLayer").style.display="block" ;	
}

//cacher le layer
function ajaxBox_hide() {
	document.getElementById("ajaxBoxLayer").style.display="none" ;
}

//changer le titre du layer
function ajaxBox_setTitle(prmBoxTitle) {
	document.getElementById("ajaxBoxTitle").firstChild.nodeValue = prmBoxTitle ;	
}

//changer le contenu du layer (pas de code html !)
function ajaxBox_setSimpleText(prmBoxText) {
	document.getElementById("ajaxBoxText").firstChild.nodeValue = prmBoxText ;
}

//fonction de redimensionnement du layer
function ajaxBox_resize(prmBoxHeight,prmBoxWidth) {
	document.getElementById("ajaxBox").style.height = prmBoxHeight + "px" ;
	document.getElementById("ajaxBox").style.width = prmBoxWidth + "px" ;
	document.getElementById("ajaxBoxLayer").style.height = prmBoxHeight + "px" ;
	document.getElementById("ajaxBoxLayer").style.width = prmBoxHeight + "px" ;
	height = prmBoxHeight ;
	width = prmBoxWidth ;
}

//positionnement de la boite
function ajaxBox_position(prmBoxTop,prmBoxLeft) {
	document.getElementById('ajaxBoxLayer').style.top = prmBoxTop + "px" ;
	document.getElementById('ajaxBoxLayer').style.left = prmBoxLeft + "px";
}

//déplacer le layer vers la gauche
function ajaxBox_moveToLeft(prmNbPixel) {
	document.getElementById('ajaxBoxLayer').style.left = (parseInt(document.getElementById('ajaxBoxLayer').style.left) - prmNbPixel) + "px" ;
}

//déplacer le layer vers la droite
function ajaxBox_moveToRight(prmNbPixel) {
	document.getElementById('ajaxBoxLayer').style.left = (parseInt(document.getElementById('ajaxBoxLayer').style.left) + prmNbPixel) + "px" ;
}

//déplacer le layer vers le haut
function ajaxBox_moveToTop(prmNbPixel) {
	document.getElementById('ajaxBoxLayer').style.top -= prmNbPixel ; 	
}

//déplacer le layer vers le bas
function ajaxBox_moveToBottom(prmNbPixel) {
	document.getElementById('ajaxBoxLayer').style.top += prmNbPixel ;
}

//"fermer" la boite (en fait la cacher)
function ajaxBox_close() {
	ajaxBox_hide() ;
}


//centrer le layer sur la page (en fait sur la partie de la page affichée)
function ajaxBox_center() {
  if (navigator.appName=="Microsoft Internet Explorer") {
   offsety = parseInt(document.documentElement.scrollTop);
  }
  else {
   offsety = parseInt(window.pageYOffset);
  }
  
	
	h = (parseInt(screen.height) / 2) + offsety - (height/2);
	l = (parseInt(screen.width) / 2) - (width / 2);
	ajaxBox_position(h,l) ;
}

//charge le contenu d'une page dans la box (etape 1: appel de la page)
function ajaxBox_loadPage(prmURL) {
	document.getElementById("ajaxBoxText").firstChild.nodeValue = " " ;
	var conn = new Ajax(true) ;
	conn.sendAndLoad(prmURL,"GET",ajaxBox_loadPageHandler) ;
}

//charge le contenu d'une page dans la box (etape 2: chargement du code html)
function ajaxBox_loadPageHandler(prmXmlHTTPRequest) {
	document.getElementById('ajaxBoxText').innerHTML = prmXmlHTTPRequest.responseText ;	
}

//permet d'insérer du code HTML dans le corps de la fenetre
function ajaxBox_setHTML(prmCodeHTML) {
	document.getElementById('ajaxBoxText').innerHTML = prmCodeHTML ;	
}

function suggest_countCharLeft(obj,car) {
				switch (car) {
			case 8 :
				//document.getElementById("suggest").style.visibility = 'hidden' ;
				//return true;
				break;
			case 37 :
				return true;
				break;
			case 13 :
				return true;
				break;
			case 39 :
				return true;
				break;
			case 40 :
				return true;
				break;
			case 38 :
				return true;
				break;
			case 46 :
				return true;
				break;
			case 20 :
				return true;
				break;
			case 35 :
				return true;
				break;
			case 36 :
				return true;
				break;
			case 17 :
				return true;
				break;
			case 18 :
				return true;
				break;

			case 16 :
				return true;
				break;
			default :
				break ;
		}


	document.getElementById('suggest_nbCarRestant').firstChild.nodeValue = (300 - obj.value.length) ;
	if (obj.value.length < 200 ) {
		document.getElementById('suggest_nbCarTodo').firstChild.nodeValue = "Tapez encore " + (200 - obj.value.length) + " caractères." ;
		document.getElementById('suggest_nbCarTodo').style.color= "red" ;
		document.suggest_descript.valider.disabled = true ;
	}
	else {
		document.getElementById('suggest_nbCarTodo').firstChild.nodeValue = "Votre description est suffisamment longue" ;
		document.getElementById('suggest_nbCarTodo').style.color= "green" ;
		document.suggest_descript.valider.disabled = false ;
	}
}

function theselection() {
	if (document.getSelection) sel = document.getSelection();
	else if (document.selection) {
		sel = document.selection.createRange().text;
	}
	else if (window.getSelection) sel = window.getSelection().toString;
	else return ;
	return sel ;	
}

function updateWord(prmString) {
	if (prmString.length >= 2) {
		if (liveSearch != prmString) {
			liveSearch = prmString ;
			document.popAdmin.keyword.value = prmString ;
			document.linkAnnu.keyword_annu.value = prmString;
			
			if (document.popAdmin.searchS.checked == true) {
				checked = "yes" ;
			}
			else checked = "no" ;
			
			
			conn = new Ajax(true) ;
			conn.sendAndLoad("http://www.aquadesign.be/news/admin_link_select.php?key=" + escape(prmString) + "&id_article=" + article_id + "&searchS=" + checked ,"GET",updateWordHandler) ;
		}
	}
}

function updateWordHandler(result) {
	document.getElementById('the_select').innerHTML = result.responseText ;	
	document.popAdmin.btnValider.disabled = false ;
}

function submitLink() {
	if (confirm("Etes-vous sur de vouloir valider ce lien ?")) {
		id_article = document.popAdmin.article_id.value ;
		id_lien = document.popAdmin.link_id.value ;
		if(id_lien.indexOf("daynews") == '-1') {
			if (id_lien == -1) {
				alert("Merci de sélectionner un lien dans la liste de préselection.") ;
				return false;
			}
			else {
								
				
				conn = new Ajax(true) ;
				conn.sendAndLoad("http://www.aquadesign.be/news/submitform.php?id_article=" + id_article +  "&keyword=" + escape(document.popAdmin.keyword.value) + "&lien=" + id_lien + "&libre=1" ,"GET",doSubmitLink) ;
			}
		} else {
			conn = new Ajax(true) ;
			conn.sendAndLoad("http://www.aquadesign.be/news/submitFormDayNews.php?id_article=" + id_article +  "&keyword=" + escape(document.popAdmin.keyword.value) + "&lien=" + id_lien.substring(8,id_lien.length) + "&libre=1","GET",doSubmitLink) ;	
		}
	}
}


function doSubmitLink(conn) {
	if (conn.responseText == 1) {
		alert("Le lien a bien été enregistré !") ;
	}
	else {
		alert("Le lien n'a pas pu être validé, merci de réessayer ultérieurement.") ;	
	}
}

function submitLinkAnnu() {
	if (confirm("Etes-vous sur de vouloir valider ce lien ?")) {
		id_article = document.popAdmin.article_id.value ;

			conn = new Ajax(true) ;
			conn.sendAndLoad("http://www.aquadesign.be/news/linkAnnu.php?id_article=" + id_article +  "&w=" + escape(document.linkAnnu.keyword_annu.value),"GET",doSubmitLinkAnnu) ;

	}
}


function doSubmitLinkAnnu(conn) {
	if (conn.responseText == 1) {
		alert("Le lien a bien été enregistré !") ;
	}
	else {
		alert("Le lien n'a pas pu être validé, merci de réessayer ultérieurement.") ;	
	}
}

function ereaseFromCache(key) {
	if (confirm("Etes-vous sur de vouloir supprimer cette page du cache ?\n Les modifications effectuées sur cette page seront alors visible immédiatement")) {
		window.open("/admin/deleteFromCache.php?key=" + key, "_blank","width=400,height=400") ;		
	}
	else {
		alert("Annulation de la suppression !") ;
		return false ;
	}
	return false ;
}

//visualisation des coloriages
function visu_image (url,dossier) {
	codeHTML = "<div align='center'><img src='/" + dossier + "/" + "mini2_" + url + "'/><br/><br/></div>" ;
	ajaxBox_init("Prévisualisation du coloriage",null,220,400);
	ajaxBox_setHTML(codeHTML);
	ajaxBox_center();
	ajaxBox_moveToRight(250) ;
	ajaxBox_show();	
}

var visu_timeout = 0 ;

function showPartener() {
	if (document.getElementById('d_meteo')) {
		conn = new Ajax(true) ;
		conn.sendAndLoad("/inc/partener.php","GET",showPartenerHandler) ;
	}
}

function showPartenerHandler(conn) {
	document.getElementById('partener').innerHTML = conn.responseText ;
}

function showSkyscriper() {
	if (document.getElementById('partener2')) {
		conn = new Ajax(true) ;
		conn.sendAndLoad("/inc/partener2.php","GET",showSkyscriperHandler) ;		
	}
}

function showFreeWeb() {
	if (document.getElementById('partener3')) {
		conn = new Ajax(true) ;
		conn.sendAndLoad("/inc/weblibre.php","GET",showFreeWebHandler) ;		
	}	
}

function showFreeWebHandler(conn) {
	document.getElementById('partener3').innerHTML = conn.responseText ;
}


function showSkyscriperHandler(conn) {
	document.getElementById('partener2').innerHTML = conn.responseText ;
}

function showMeteo() {
	if (document.getElementById('d_meteo')) {
		conn = new Ajax(true) ;
		conn.sendAndLoad("/_meteo.php","GET",showMeteoHandler) ;		
	}
}

function showMeteoHandler(conn) {
	document.getElementById('d_meteo').innerHTML = conn.responseText ;
}

//permet en admin de classer les news sur un terme dans les glossaires
function classerNews(prmTopic,obj, prmType) {
	var dico = obj.value ;
	var conn = new Ajax(true) ;
	if (confirm('Etes-vous sur de vouloir effectuer cette opération?')) conn.sendAndLoad('/admin/classerNews.php?t=' + prmTopic + '&value=' + dico + '&mot=' + document.popAdmin.keyword.value +'&type_news=' + prmType ,"GET",classerNewHandler) ;
}

function classerNewHandler(conn) {
	if (conn.responseText != -1) alert("Opération enregistrée avec succès !") ;
	else alert("Impossible d'effectuer l'opération!") ;	
}

// Vote pour les vidéos
function voteVideo(id,vote) {
	conn = new Ajax(true) ;
	conn.sendAndLoad("/streaming/vote.php?id=" + id + "&v=" + vote + "&" + Math.random(),"GET",voteVideoHandler) ;
}

function voteVideoHandler(conn) {
	if (conn.responseText == "1")
		alert('Votre vote a bien été pris en compte !');
	if (conn.responseText == "3")
		alert('Désolé mais vous avez déjà voté aujourd\'hui !');
	if (conn.responseText == "2")
		alert('Problème de connexion à la base de données, veuillez réessayer ultérieurement.');
}

//Verifie la longueur d'une description d'un site
function checkSizeDescription() {
	document.formu.nbcartext.value = document.formu.des.value.length + ' caractères';
}

//Verifie que le description contient bien 350 caractères
function verifSizeDes() {
	if (document.formu.des.value.length < 350)  {
		alert('Votre description doit faire au minimum 350 caractères..');
		document.formu.des.focus() ;
		return false ;
	}	
}

//Ajoute un smiley dans le textarea =)
function addSmiley(smiley) {
	if(smiley == "smile") document.formulaire.msg.value += " :-)" ;
	if(smiley == "wink") document.formulaire.msg.value += " ;-)" ;
	if(smiley == "confused") document.formulaire.msg.value += " :-/" ;
	if(smiley == "normal") document.formulaire.msg.value += " :-|" ;
	if(smiley == "lol") document.formulaire.msg.value += " LOL" ;
	if(smiley == "laugh") document.formulaire.msg.value += " :-D" ;
	if(smiley == "sad") document.formulaire.msg.value += " :(" ;
	if(smiley == "angry") document.formulaire.msg.value += " :-C" ;
	if(smiley == "cool") document.formulaire.msg.value += " 8-)" ;
	if(smiley == "surprised") document.formulaire.msg.value += " :-o" ;
	if(smiley == "cry") document.formulaire.msg.value += " ;-(" ;
	if(smiley == "eek") document.formulaire.msg.value += " 8-O" ;
	document.formulaire.msg.focus() ;
	
}

//
function verifFormComm() {
	if(document.formulaire.nom.value.length == 0) {
		alert('Veuillez indiquer votre nom ou votre pseudo.');
		document.formulaire.nom.focus() ;
		return false ;			
	}
	if(document.formulaire.msg.value.length == 0) {
		alert('Veuillez remplir le champ commentaire.');
		document.formulaire.msg.focus() ;
		return false ;	
	}
}

//Affiche le mot sélectionné 
function displaySelectedWord(prmString, id_article) {
		if ((liveSearchGautier != prmString) && (prmString != "")) {
			liveSearchGautier = prmString ;
			document.adminForm.wordToLink.value = prmString ;
			conn = new Ajax(true) ;
			if(document.adminForm.searchS.checked) searchS = "yes" ;
			else searchS = "no" ; 
			//alert(liveSearchGautier) ;
			conn.sendAndLoad("http://www.aquadesign.be/calendrier/selectLinkable.php?id_article=" + id_article +  "&w=" + escape(prmString) + "&searchS=" + searchS, "GET", doDisplaySelectWord) ;
		}
}

//Affiche le mot sélectionné a partir du input text wordTolink
function displayInputWord(id_article) {
		if (document.adminForm.wordToLink.value != "") {
			word = document.adminForm.wordToLink.value ;
			conn = new Ajax(true) ;
			if(document.adminForm.searchS.checked) searchS = "yes" ;
			else searchS = "no" 
			conn.sendAndLoad("http://www.aquadesign.be/calendrier/selectLinkable.php?id_article=" + id_article +  "&w=" + escape(word)  + "&searchS=" + searchS, "GET", doDisplaySelectWord) ;
		}
}

//Construit le select des articles liés au mot
function doDisplaySelectWord(conn) {
	if (conn.responseText != "pb_sql")	document.getElementById('articleSelected').innerHTML = conn.responseText ;
	else alert("Problème SQL.") ;
}

var liveSearchGautier = "" ;

//Valider le lien 
function validLinkWord(id_article) {
	if (confirm("Etes-vous sur de vouloir valider ce lien ?")) {
		word = document.adminForm.wordToLink.value ;
		idArticleToLink = document.adminForm.articleSel.value ;
		conn = new Ajax(true) ;
		conn.sendAndLoad("http://www.aquadesign.be/calendrier/linkWords.php?word=" + escape(word) +  "&currentArticle=" + id_article + "&idArticleToLink=" + idArticleToLink, "GET", doValidLinkWord) ;
	}
}

function doValidLinkWord(conn) {
	if (conn.responseText == "0") alert("L'expression a été correctement liée à l'article.") ;
	else alert("L'expression n'a été pas correctement liée à l'article.") ;
}

var scrollTimeOut = "" ;


function scrollMenu() {
	var menu = document.getElementById("admin_tool") ;
	if (!isIE) menu.style.top = window.pageYOffset + "px" ;
	else{
		menu.style.top = document.documentElement.scrollTop + "px" ;
	}
	scrollTimeOut = window.setTimeout("scrollMenu();",10) ;
}

function initPanel() {
	var menu = document.getElementById("admin_tool") ;

		menu.style.top = "0px" ;
		menu.style.position = "absolute" ;
		scrollTimeOut = window.setTimeout("scrollMenu();",5) ;

}

//Voter un article
function voterArticle(id_news, score) { 
	if(LireCookie(id_news) == null) {
		date=new Date;
		date.setDate(date.getDate()+1);
		EcrireCookie(id_news, "oui", date);
		conn = new Ajax(true) ;
		conn.sendAndLoad("http://www.aquadesign.be/inc/voterArticle.php?id_news=" + id_news +  "&score=" + score, "GET", voterArticleHandler) ;
	} else {
		alert("Vous ne pouvez voter qu'une fois par article !") ;	
	}
}

function voterArticleHandler(conn) {
	alert(conn.responseText) ;	
}

//Voter un article du jour
function voterArticle2(id_news, score) { 
	if(LireCookie(id_news + "dn") == null) {
		date=new Date;
		date.setDate(date.getDate()+1);
		EcrireCookie(id_news + "dn", "oui", date);
		conn = new Ajax(true) ;
		conn.sendAndLoad("http://www.aquadesign.be/inc/voterArticle2.php?id_news=" + id_news +  "&score=" + score, "GET", voterArticle2Handler) ;
	} else {
		alert("Vous ne pouvez voter qu'une fois par article !") ;	
	}
}

function voterArticle2Handler(conn) {
	alert(conn.responseText) ;	
}

function EcrireCookie(nom, valeur) {
	var argv=EcrireCookie.arguments;
	var argc=EcrireCookie.arguments.length;
	var expires=(argc > 2) ? argv[2] : null;
	var path=(argc > 3) ? argv[3] : null;
	var domain=(argc > 4) ? argv[4] : null;
	var secure=(argc > 5) ? argv[5] : false;
	document.cookie=nom+"="+escape(valeur)+
	((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
	((path==null) ? "" : ("; path="+path))+
	((domain==null) ? "" : ("; domain="+domain))+
	((secure==true) ? "; secure" : "");
}

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;
}


//Lien en js
function gotoLink(prmId) {
	window.open(prmId,"_blank","menubar=yes,toolbar=yes,resizable=yes,status=yes,dependent=yes,location=yes,scrollbars=yes") ;
}
