/*****************/
/* FONCTIONS.JS */
/***************/

//contient des fonctions globales pour l'ensemble des scripts

if(langGlobal == "fr"){
  var msg_im_sent = "Votre message a été envoyé";
  var msg_im_empt = "Votre message ne peut pas être vide";

}else{
  var msg_im_sent = "Your message has been sent";
  var msg_im_empt = "Your message can't be empty";

}



function ajaxreq(url){
	if(window.XMLHttpRequest) // FIREFOX
		xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // IE
		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else
		return(false);
	
	xhr_object.open("GET", url, false);
	xhr_object.send(null);
	if(xhr_object.readyState == 4) 
		return(xhr_object.responseText);
	else 
		return(false);
}

function switchLangTo(langTo){
	res = ajaxreq(hejPathGlobal+'includes/ajax_lang.php?lang='+escape(langTo));
	if(res){
		window.location.reload();
	}else{
		alert("Error");
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style;
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}  	

function fadeOut(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
			for(i = opacStart; i >= opacEnd; i--) {
					setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
					timer++;
			}
	} else if(opacStart < opacEnd) {
			for(i = opacStart; i <= opacEnd; i++)
					{
					setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
					timer++;
			}
	}

	setTimeout("document.getElementById('" + id +"').style.display='none'",(timer*speed));
}

function fadeIn(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
			for(i = opacStart; i >= opacEnd; i--) {
					setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
					timer++;
			}
	} else if(opacStart < opacEnd) {
			for(i = opacStart; i <= opacEnd; i++)
					{
					setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
					timer++;
			}
	}

	//setTimeout("document.getElementById('" + id +"').style.display='block'",(timer*speed));
	document.getElementById(id).style.display='block';
}

function opacitySimple(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}
 
function getPageSize(){

	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}





function close_im_box() {
	var div_main = window.document.getElementById("imHejieForm");	
	var div_ok = window.document.getElementById("imHejieConfirm");	
	var div_err = window.document.getElementById("imHejieErr");	
	var text_im=window.document.getElementById("im_text");	
	window.document.getElementById("imHejiePopup").style.display="none";
	div_main.style.display="block";
	div_ok.style.display="none";
	div_err.style.display="none";
	text_im.value="";
	
}
function open_im_box(pseudo,id_user) {
	window.document.getElementById("imPseudo").innerHTML=pseudo;	
	window.document.getElementById("imHejiePopup").style.display="block";
	window.document.getElementById("im_id").value=id_user;
}

function validate_im() {
	var div_loading = window.document.getElementById("imHejieProb");
	var div_main = window.document.getElementById("imHejieForm");	
	
	text_im=window.document.getElementById("im_text");
	id_user=window.document.getElementById("im_id").value;
	if(text_im.value == "") {
		window.document.getElementById("prob_im").innerHTML=msg_im_empt;
		window.document.getElementById("prob_im").style.display="block";
		return false;
	} else {
		window.document.getElementById("prob_im").style.display="none";
	}

	div_main.style.display="none";
	div_loading.style.display="block";
	/*a proteger*/
	text_to_send=text_im.value;
	imRequestServerAsynch('includes/ajax_send_im.php?user='+id_user+'&message='+text_to_send);
}

function imRequestServerAsynch(fichier){
	var div_loading = window.document.getElementById("imHejieProb");	
	var div_confirm = window.document.getElementById("imHejieConfirm");
	var div_error = window.document.getElementById("imHejieErr");
	
			
	if(window.XMLHttpRequest) // FIREFOX
		xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // IE
		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else
		return(false);
	xhr_object.open("GET", fichier, true);
	
	
	xhr_object.onreadystatechange = function() {
			if(xhr_object.readyState == 4) {
				//alert(xhr_object.responseText);
				if(xhr_object.responseText=="OK") {
					
					div_loading.style.display="none";
					div_confirm.style.display="block";				
				} else {
					div_loading.style.display="none";
					div_error.style.display="block";						
				}
			} else { 
				return(false);
			}	
	}
	xhr_object.send(null);
	
}

// *  SEARCH  * /

function check_search(){
	var mysearch = window.document.getElementById("q");	
	if(mysearch.value == "") {
		return false;
	} else {
		return true;
	}
}

function highlightSearchForm() {
	//alert("toto");
	$("#idsearchform").addClass("highLighted");
}

function unHighlightSearchForm () {
	$("#idsearchform").removeClass("highLighted");
}


///////////////
// weborama //

String.prototype.linkify = function() {
	return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {
		var l="<a href='"+m+"' target='_blank'>"+m+"</a>";
		//l.target="_blank";
		//return m.link(m);
		return l;
	});
};
 
  function relative_time(time_value) {
	  var values = time_value.split(" ");
	  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
	  var parsed_date = Date.parse(time_value);
	  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
	  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
	  delta = delta + (relative_to.getTimezoneOffset() * 60);
	  
	  var r = '';
	  if (delta < 60) {
	    r = 'a minute ago';
	  } else if(delta < 120) {
	    r = 'couple of minutes ago';
	  } else if(delta < (45*60)) {
	    r = (parseInt(delta / 60)).toString() + ' minutes ago';
	  } else if(delta < (90*60)) {
	    r = 'an hour ago';
	  } else if(delta < (24*60*60)) {
	    r = '' + (parseInt(delta / 3600)).toString() + ' hours ago';
	  } else if(delta < (48*60*60)) {
	    r = '1 day ago';
	  } else {
	    r = (parseInt(delta / 86400)).toString() + ' days ago';
	  }
	  
	  return r;
}

function twitter_callback (){
	return true;
}


