var DM_WWW_ICONS = '/DM/Icons/';
function resizeText(objectId, sCase) {
	var object = document.getElementById(objectId);
	if(object) {
		var currentSize = (object.style.fontSize != '' ? object.style.fontSize : '1em');
		currentSize = Number(String(currentSize).replace('em',''));
		if (sCase == '-') {
			if (currentSize > 0.4) {
				currentSize = Number(currentSize) - 0.2;
			}
		}
		else {
			currentSize = Number(currentSize) + 0.2;
		}
		object.style.fontSize = currentSize + 'em';
		object.style.lineHeight = 1.3 + 'em';
	}
}

function webmasterToolsDisplay(objectId) {
	var object = document.getElementById(objectId);
	if(object) {
		if (object.className == '') {
			var url = document.location;
			var title = document.title;
			object.className = 'WebmasterToolsBloc';
			object.innerHTML = '<table>'
				+ '<thead><tr><td colspan="2"><span>Faites un lien vers une page de l\'Actualit&eacute;s '
				+ 'Orange</span><img src="/DM/Icons/croix.gif" title="Fermer" '
				+ 'onclick="webmasterToolsHide(\'' + objectId + '\')"/></td></tr></thead>'
				+ '<tbody><tr><td colspan="2">Ajouter cette page &agrave; votre site internet en '
				+ 'copiant-collant les liens</td></tr></tbody>'
				+ '<tfoot>'
				+ '<tr><th>Lien permanent</th><td><input id="WTpermalien" type="text" '
				+ 'value="&lt;a href&#61;&quot;' + url + '&quot; '
				+ 'alt&#61;&quot;' + title + '&quot; title&#61;&quot;' + title
				+ '&quot;&gt;' + title + '&lt;/a&gt;" '
				+ 'onclick="this.select()" READONLY /></td></tr>'
				+ '<tr><th>BB Code</th><td><input id="WTbbcode" type="text" '
				+ 'value="[url&#61' + url + ']' + title + '[/url]" onclick="this.select()" READONLY /></td></tr>'
				+ '</tfoot>'
				+ '</table>';
		}
		else {
			if (object.style.display == '') {
				object.style.display = 'none';
			}
			else {
				object.style.display = '';
			}
		}
	}
}

function webmasterToolsHide(objectId) {
	var object = document.getElementById(objectId);
	if(object) {
		object.style.display = 'none';
	}
	
}

function  sendPageByMail(objectId) {
	var object = document.getElementById(objectId);
	if(object) {
		if (object.innerHTML == '') {
			object.innerHTML = '<div class="content" style="background:#FFF;">'
				+ '<h3>Envoyer &agrave; un ami</h3>'
				+ '<span class="epPageTitle">' + document.title + '</span>'
				+ '<h4>Les donn&eacute;es vous concernant :</h4>'
				+ '<form name="formSPA" onsubmit="return(false);>'
				+ '<input type="hidden" name="titrePage" value="' + document.title + '" />'
				+ '<div class="epLName">Votre nom :</div>'
				+ '<div class="epLNameField"><input type="text" name="lastName" /></div>'
				+ '<div class="epFName">Votre pr&eacute;nom :</div>'
				+ '<div class="epFNameField"><input type="text" name="firstName" /></div>'
				+ '<div class="epEmail">Votre e-mail :</div>'
				+ '<div class="epEmailField"><input type="text" name="emailUser" /></div>'
				+ '<div class="epComm">Commentaire :</div>'
				+ '<div class="epCommField"><textarea name="comments"></textarea></div>'
				+ '<h4>Les donn&eacute;es concernant vos amis :</h4>'
				+ '<div class="epEmail2Plus">Vous pouvez saisir plusieurs adresses mails en les '
				+ 's&eacute;parant par des points virgules. Exemple : ami1@orange.fr;ami2@orange.fr</div>'
				+ '<div class="epEmail2">E-mail :</div>'
				+ '<div class="epEmail2Field"><input type="text" name="emailFriends" /></div>'
				+ '<a href="javascript:void(0);" onclick="sendPageByMailAction(document.forms[\'formSPA\'], \'' + objectId + '\');" >'
				+ '<img src="/DM/Icons//ico_envoyer.gif" border="0" alt="envoyer" class="epBtnSend"/>&nbsp;</a>'
				+ '<a href="javascript:sendPageByMail(\'' + objectId + '\');" >'
				+ '<img src="/DM/Icons//croix.gif" border="0" alt="fermer" class="epBtnClose"/></a>'
				+ '<div class="c_sep"></div>'
				+ '</form>';
				+ '</div>';
			object.style.display = '';
		}
		else {
			if (object.style.display == '') {
				object.style.display = 'none';
			}
			else {
				object.style.display = '';
			}
		}
	}
}

function sendPageByMailAction(oForm, objectId) {
	var modele = /^[a-zA-Z0-9\.\-_]+@[a-zA-Z0-9]+(\.[a-zA-Z]{2,5}){1,2}$/i;
	if (!modele.test(oForm.emailUser.value)) {
		alert('Votre e-mail n\'est pas valide.');
		oForm.emailUser.focus();
		return(false);
	}
	var listMail = String(String(oForm.emailFriends.value).replace(',',';')).split(';');
	if (listMail.length == 0) {
		alert('Vous devez saisir au moins un e-mail valide.');
		oForm.emailFriends.focus();
		return(false);
	}
	for (var i=0 ; i < listMail.length ; i++) {
		if (!modele.test(listMail[i])) {
			alert('E-mail de destination non valide.');
			oForm.emailFriends.focus();
			return(false);
		}
	}
	// send Ajax request
	new xHttp.Request("/Inc/bridgeSendAtFriends.php",
		{
			method : 'POST',
			parameters : {
				'subject' : document.title,
				'url' : document.location,
				'lastName' : String(oForm.lastName.value),
				'firstName' : String(oForm.firstName.value),
				'emailUser' : String(oForm.emailUser.value),
				'comments' : String(oForm.comments.value),
				'emailFriends' : String(oForm.emailFriends.value)
			},
			OnError : function (xhr) {
				alert("Votre demande n'a pas pu &ecirc;tre trait&eacute;e. Veuillez recommencer plus tard.");
				sendPageByMail(objectId);
			},
			OnSuccess : function (xhr) {
				if (xhr.responseText == 'ok') {
					sendPageByMail(objectId);
				}
				else {
					alert("Votre demande n'a pas pu &ecirc;tre trait&eacute;e. Veuillez recommencer plus tard.");
					sendPageByMail(objectId);
				}
			}
		}
	);
}

var xHttp = {};

/**
 * FUNCTION : xHttp.Request
 * create an advanced ajax request
 */
xHttp.Request = function (sUrl_p, oJParam) {
	// Default values
	var sUrl = sUrl_p;
	var OnSuccess = null;
	var OnLoading = null;
	var OnError = null;
	var sMethod = 'GET';
	var sParam = "";
	var sContentType = "application/x-www-form-urlencoded";
	// Set values from parameters :
	if (oJParam.OnSuccess) {OnSuccess = oJParam.OnSuccess;}
	if (oJParam.OnLoading) {OnLoading = oJParam.OnLoading;}
	if (oJParam.OnError) {OnError = oJParam.OnError;}
	if (oJParam.method && String(oJParam.method).toUpperCase() == 'POST') {sMethod = 'POST';}
	if (oJParam.content_type) {sContentType = oJParam.content_type;}
	if (oJParam.parameters) {
		for (var KEY in oJParam.parameters) {
			sParam += (sParam==""?"":"&") + escape(KEY)
				+ "=" + escape(oJParam.parameters[KEY]);
		}
	}
	// call OnLoading function
	if (OnLoading) {
		OnLoading();
	}
	// test if url is specified
	if (sUrl == "") {
		if (OnError) {OnError({status:'404'});}
	}
	else {
		//create http request object
		var oLoader = xHttp.CreateLoader();
		oLoader.onreadystatechange = function() {
			if (oLoader.readyState == 4) {
				if (oLoader.status == 200) {
					// call back function if success
					if (OnSuccess) {OnSuccess(oLoader);}
				}
				else {
					// call back function if error
					if (OnError) {OnError(oLoader);}
				}
			}
		}
		if (sMethod == "GET") {
			// send 'get' request
			oLoader.open("get", sUrl + (sParam?"?"+sParam:""), true);
			oLoader.send(null);
		}
		else {
			// send 'post' request
			oLoader.open("post", sUrl, true);
			oLoader.setRequestHeader("Content-Type", sContentType);
			oLoader.setRequestHeader("Content-Length",sParam.length);
			oLoader.setRequestHeader("Connection", "close");
			oLoader.send(sParam);
		}
	}
} // xHttp.Request

/**
 * FUNCTION : xHttp.CreateLoader
 * return a cross browser XMLHttpRequest object
 */
xHttp.CreateLoader = function() {
// Try create XMLHttpRequest object (Mozilla, Safari,...)
	if (window.XMLHttpRequest) {
		var oXmlHttp = new XMLHttpRequest();
		if (oXmlHttp.overrideMimeType) {
			oXmlHttp.overrideMimeType('text/xml');
		}
		return (oXmlHttp);
	}

// Try to create Microsoft ActiveX
	var aMSXMLVers = new Array (
		"MSXML2.XMLHttp.5.0" ,
		"MSXML2.XMLHttp.4.0" ,
		"MSXML2.XMLHttp.3.0" ,
		"MSXML2.XMLHttp",
		"Microsoft.XMLHttp"
	);
	for (var i=0 ; i<aMSXMLVers.length ; i++) {
		try {
			var oXmlHttp = new ActiveXObject(aMSXMLVers[i]);
			return(oXmlHttp);
		}
		catch (oError) {
			;
		}
	}
	return (false);
} // xHttp.CreateLoader

function pagination_articles_dm() {
	var $this = this;
	this.articles;
	this.current_page;
	this.class_name;
	this.nb_per_page;
	this.nb_pages;
	this.objects_tag;
	this.container_id;
	this.id_pages;
	
	this.init = function( container_id, name_class, nb_per_page, objects_tag) {
		$this.container_id 	= container_id;
		$this.objects_tag 	= objects_tag;
		$this.class_name 		= name_class;
		$this.nb_per_page 	= nb_per_page;
		$this.id_pages			= "pages_"+Math.floor(Math.random()*100000);
		$this.articles			= new Array();
		$this.loadArticles();
		this.initialized = true;
	};
	
	this.loadArticles = function() {
		$$(!$this.class_name?'#'+$this.container_id+' '+$this.objects_tag:'#'+$this.container_id+' '+$this.objects_tag+'.'+$this.class_name).each(function(item,i) { $this.articles.push(item); });
		$this.nb_pages = Math.ceil( $this.articles.length / $this.nb_per_page );
		/* on ne met en place la pagination que si ça en vaut la peine */
		if( $this.articles.length > this.nb_per_page ) { $this.showPage(0); }
	};
	
	this.showPage = function( nb ) {
		$this.current_page = nb; $this.articles.each(function(item,i){item.style.display='none';});
		var max_i = eval( $this.nb_per_page * nb + $this.nb_per_page ) >= $this.articles.length ? $this.articles.length : eval( $this.nb_per_page * nb + $this.nb_per_page );
		for( var i = nb * $this.nb_per_page; i < max_i; i++ ) { $this.articles[i].style.display = ''; }
		
		if($($this.id_pages)){$($this.id_pages).remove();}
		/* affichage des liens pour changer de page */
		var div_page = new Element('div', {'id':$this.id_pages, 'styles':{'clear':'both','text-align':'center','padding-top':'10px'}});
		/* lien première page */
		var tmp = new Element('span',{'class':'chevrons gauche','events': { 'click': function() { $this.showPage(0); },'mouseover': function() { this.style.cursor='pointer'; this.style.cursor='hand'; }},'title':'Afficher la premiÃ¨re page'});
		new Element('img',{'src':DM_WWW_ICONS + 'fleche_prems.gif'}).injectInside(tmp); tmp.injectInside(div_page);
		/* lien page précédente */
		var tmp = new Element('span',{'class':'gauche','events':{'click':function() { $this.showPage($this.current_page - 1 < 0 ? 0 : $this.current_page - 1); },'mouseover': function() { this.style.cursor='pointer'; this.style.cursor='hand'; }},'title':'Afficher la page prÃ©cÃ©dente'}).setHTML("&nbsp;pr&eacute;c&eacute;dent");
		new Element('img',{'src':DM_WWW_ICONS + 'fleche_prec.gif'}).injectTop(tmp); tmp.injectInside(div_page);
		/* lien pages directes */
		if( $this.nb_pages > 10 ) {
			var end = $this.current_page + 10 > $this.nb_pages ? $this.nb_pages : $this.current_page + 10;
			var start = ( end == $this.nb_pages ) ? $this.nb_pages - 10 : $this.current_page;
		}
		else { var start = 0; var end = $this.nb_pages; }
		for( var i = start; i < end ; i++ ) {
			var tmp = new Element('span',{'class':'numbers','styles':{'color':$this.current_page == i ? '#FF6600':'#444', 'font-weight':$this.current_page == i ? '700' : '400'},'events': { 'click': function() { $this.showPage(this.i); },'mouseover': function() { this.style.cursor='pointer'; this.style.cursor='hand'; }}}).setHTML(i + 1).injectInside(div_page);
			tmp.i = i; if(i < end-1){new Element('span',{'class': 'sep'}).setHTML("|").injectInside(div_page);}
		}
		/* lien page suivante */
		var tmp = new Element('span',{'class':'droite','events': { 'click': function() { $this.showPage($this.current_page + 1 > $this.nb_pages - 1 ? $this.nb_pages - 1 : $this.current_page + 1); },'mouseover': function() { this.style.cursor='pointer'; this.style.cursor='hand'; }},'title':'Afficher la page suivante'}).setHTML("suivant&nbsp;");
		new Element('img',{'src':DM_WWW_ICONS + 'fleche_suiv.gif'}).injectInside(tmp); tmp.injectInside(div_page);
		/* lien première page */
		var tmp = new Element('span',{'class':'chevrons droite','events': { 'click': function() { $this.showPage($this.nb_pages - 1); },'mouseover': function() { this.style.cursor='pointer'; this.style.cursor='hand'; }},'title':'Afficher la derniÃ¨re page'});
		new Element('img',{'src':DM_WWW_ICONS + 'fleche_dern.gif'}).injectInside(tmp); tmp.injectInside(div_page); tmp = null;
		/* inject div contenant tous les liens */
		div_page.injectInside($this.container_id);
				
		if(this.initialized){refreshAction.run({'pathSupB':'pagination/page'+($this.current_page+1)});}
	};
} 
