var navigateur = navigator.appName;
var version = navigator.appVersion;

var ie6 = (navigateur == 'Microsoft Internet Explorer' && (version.include('MSIE 6') || version.include('MSIE 5')) ) ? true:false;

var regExpBeginning = /^\s+/;
var regExpEnd       = /\s+$/;
 
// Supprime les espaces inutiles en début et fin de la chaîne passée en paramètre.
function trim(aString) {
    if (aString!="")
    	return aString.replace(regExpBeginning, "").replace(regExpEnd, "");
    else
    	return aString;
}

function checkOnly(formId, boxChecked){

	var onClass = "checkOn";
	var offClass = "checkOff";

	var elts = $(formId).getElements();
	for(var i=0; i<5;i++) {
		if (elts[i] != $(boxChecked) && elts[i].checked) {
			elts[i].checked = false;
		}
		$('label_'+elts[i].id).className = (!elts[i].checked) ? offClass : onClass;
	}
	
	$('rechercheInput').focus();

}

function scrollToDiv(divId){
	new Effect.ScrollTo(divId, {offset: -30});
}

function togglePopupEntreprise(toShow){
	var popupList = new Array();
	var className = new Array();
	var types = new Array();

	types[0] = 'concurrents';
	types[1] = 'liens';
	types[2] = 'infosCles';
	types[3] = 'images';
	types[4] = 'diplomes';
	types[5] = 'competences';
	types[6] = 'infosSpeciales';
	
	popupList[0] = 'popupConcurrents';
	popupList[1] = 'popupLiens';
	popupList[2] = 'popupInfosCles';
	popupList[3] = 'popupImages';
	popupList[4] = 'popupDiplomes';
	popupList[5] = 'popupCompetences';
	popupList[6] = 'popupInfosSpeciales';
	className[0] = 'boutonSupprimerConcurrent';
	className[1] = 'boutonSupprimerLien';
	className[2] = 'boutonModifierAttribut';
	className[3] = 'rien';
	className[4] = 'boutonSupprimerDiplome';
	className[5] = 'rien';
	className[6] = 'boutonModifierAttributIntr';

	for(var i=0;i<popupList.length;i++){
		if(types[i] == toShow){
			if($(popupList[i])) $(popupList[i]).toggle();
			
			var toToggle = document.getElementsByClassName(className[i]);
			for(var t=0; t<toToggle.length; t++){
				$(toToggle[t]).toggle();
			}
		}
		else {
			if($(popupList[i])) $(popupList[i]).hide();
			
			var toToggle = document.getElementsByClassName(className[i]);
			for(var t=0; t<toToggle.length; t++){
				$(toToggle[t]).hide();
			}
		}
	}

	if(toShow != "images" && $('stopModifierGalerie')){
		$('stopModifierGalerie').hide();
		$('modifierGalerie').show();
	}
	
	if(toShow != "liens" && $('modif_liens_end')){
		$('modif_liens_end').hide();
		$('modif_liens').show();
	}
	
	if(toShow != "concurrents" && $('stopModifConcurrents')){
		$('stopModifConcurrents').hide();
		$('modifConcurrents').show();
	}
	
	if(toShow != "diplomes" && $('modif_diplomes_end')){
		$('modif_diplomes_end').hide();
		$('modif_diplomes').show();
	}
	
	if(toShow != "competences" && $('vote_competences_end')){
		$('vote_competences_end').hide();
		$('vote_competences').show();
	}

	if(toShow != null){
		if($('voterValeurs')) $('voterValeurs').hide();
		if($('valeursVotes')) $('valeursVotes').hide();
		if($('listeValeurs')) $('listeValeurs').hide();
		if($('valeursListe')) $('valeursListe').hide();
	}
	
}

function getLength(form){
    
    return form.value.length;
}



var Sphere3D = Class.create({
	initialize: function(options,RX,RY,angle,deltaY,deltaX,vitesse){
		this.vitesse = vitesse;
		this.deltaX = deltaX;
		this.deltaY = deltaY;
		this.angle = angle;
		this.checker = null;
		this.objects = $$(options);
		this.iL = this.objects.length;
		this.tarX = 0.001;
		this.tarY = 0.001;
		this.RX = RX;
		this.RY = RY;
		this.RZ = 30;
		this.CX = $('nav3d').offsetWidth*0.5;
		this.CY = $('nav3d').offsetHeight*0.5;
		this.CZ = 30;
		this.init();
	},
	init: function(){
		this.objects.each(function(obj, i){	
			obj.x = (2*Math.PI)*(i/this.iL);
			obj.n = 0;
			obj.observe('mousemove', this.over.bind(this));
			obj.observe('mouseout', this.overOut.bind(this));
		},this);
		this.rolling = false;
		this.overimg = false;
		$('nav3d').observe('mouseover', this.start.bind(this));
		$('nav3d').observe('mouseout', this.end.bind(this));
		$('nav3d').observe('mousemove', this.update.bind(this));
		this.process();
	},
	update: function(el){
		this.tarX = (Event.pointerX(el)-this.CX)/15000;
		this.tarY = (Event.pointerY(el)-this.CY)/15000;
	},
	start: function(){
		if (!this.rolling && !this.overimg)
		{
			this.checker = new PeriodicalExecuter(this.process.bind(this), this.vitesse);
			this.rolling=true;
		}
	},
	end: function(){
		if (this.checker!=null)
			this.checker.stop();
		this.rolling=false;
	},
	over: function(){
		if (this.rolling)
		{
			this.end();
			this.overimg=true;
		}
	},
	overOut: function(){
		if (!this.rolling)
		{
			this.overimg=false;
			this.start();
		}
	},

	process: function(){
		var s, c, j, m, newL, newT, scale;
		this.objects.each(function(img,i){
			img.x += this.tarX;
			s = Math.sin(img.x);
			c = Math.cos(img.x);
			img.n += this.tarY;
			j = Math.sin(img.n);
			m = Math.cos(img.n);
			scale = j*s*this.RZ+this.CZ;
			newL1 = c*this.RX+this.CX-scale*0.5;
			newT1 = m*s*this.RY+this.CY-scale*0.5;
			newL = newL1*Math.sin(this.angle)+newT1*Math.cos(this.angle)+this.deltaX;
			newT = -newT1*Math.sin(this.angle)+newL1*Math.cos(this.angle)+this.deltaY;
			img.setStyle({
 				 left: newL+'px',
 				 height: scale+'px',
 				 width: scale+'px',
 				 top: newT+'px',
 				 zIndex: Math.round((j*s+1)*10)
			});
			img.setOpacity(j*s+1);
		}, this);
	}
});

var Sphere3DHome = Class.create({
	initialize: function(options,RX,RY,angle,deltaY,deltaX,vitesse){
		this.vitesse = vitesse;
		this.deltaX = deltaX;
		this.deltaY = deltaY;
		this.angle = angle;
		this.checker = null;
		this.objects = $$(options);
		this.iL = this.objects.length;
		this.tarX = 0.03;
		this.tarY = 0.03;
		this.RX = RX;
		this.RY = RY;
		this.RZ = 30;
		this.CX = $('nav3d').offsetWidth*0.5;
		this.CY = $('nav3d').offsetHeight*0.5;
		this.CZ = 30;
		this.init();
	},
	init: function(){
		this.objects.each(function(obj, i){	
			obj.x = (2*Math.PI)*(i/this.iL);
			obj.n = 0;
		},this);
		this.rolling = false;
		this.overimg = false;
		$('nav3d').observe('mouseover', this.start.bind(this));
		$('nav3d').observe('mouseout', this.end.bind(this));
		$('nav3d').observe('mousemove', this.update.bind(this));
		this.process();
		this.start();
	},
	update: function(el){
		this.tarX = (Event.pointerX(el)-this.CX)/15000;
		this.tarY = (Event.pointerY(el)-this.CY)/15000;
	},
	start: function(){
		
			this.checker = new PeriodicalExecuter(this.process.bind(this), this.vitesse);
			this.rolling=true;
		
	},
	end: function(){
		if (this.checker!=null)
			this.checker.stop();
		this.rolling=false;
	},
	over: function(){
		if (this.rolling)
		{
			this.end();
			this.overimg=true;
		}
	},
	overOut: function(){
		if (!this.rolling)
		{
			this.overimg=false;
			this.start();
		}
	},

	process: function(){
		var s, c, j, m, newL, newT, scale;
		this.objects.each(function(img,i){
			img.x += this.tarX;
			s = Math.sin(img.x);
			c = Math.cos(img.x);
			img.n += this.tarY;
			j = Math.sin(img.n);
			m = Math.cos(img.n);
			scale = j*s*this.RZ+this.CZ;
			newL1 = c*this.RX+this.CX-scale*0.5;
			newT1 = m*s*this.RY+this.CY-scale*0.5;
			newL = newL1*Math.sin(this.angle)+newT1*Math.cos(this.angle)+this.deltaX;
			newT = -newT1*Math.sin(this.angle)+newL1*Math.cos(this.angle)+this.deltaY;
			img.setStyle({
 				 left: newL+'px',
 				 height: scale+'px',
 				 width: scale+'px',
 				 top: newT+'px',
 				 zIndex: Math.round((j*s+1)*10)
			});
			img.setOpacity(j*s+1);
		}, this);
	}
});

function dontShowHelp(askableId){
	new Ajax.Updater('helpBox', '/dontShowHelp.htm', {method: 'get', parameters: 'askId='+askableId});
}

function showHelp(askableId){
	new Ajax.Updater('helpBox', '/showHelp.htm', {method: 'get', parameters: 'askId='+askableId});
}

function supprElementIntranet(iId, id, restore, divId){
	if (confirm('Voulez-vous vraiment supprimer cet �l�ment de l\'intranet ?'))
		IntranetService.removeFromIntranet(iId, id, restore, function(data){
		if (data){
			if ($(divId))
				new Effect.Fade(divId);
		}
	
});

}

function richContentFormatter(content){
	content = content.escapeHTML();
	content = content.replace(/\n/g, '<br/>');
	content = content.replace('&lt;gras&gt;', '<strong>');
	content = content.replace('&lt;/gras&gt;', '</strong>');
	content = content.replace('&lt;ital&gt;', '<em>');
	content = content.replace('&lt;/ital&gt;', '</em>');
	content = content.replace('&lt;souligne&gt;', '<u>');
	content = content.replace('&lt;/souligne&gt;', '</u>');
	content = content.replace('&lt;titre&gt;', '<h3>');
	content = content.replace('&lt;/titre&gt;', '</h3>');
	content = content.replace('&lt;indent&gt;', '<blockquote>');
	content = content.replace('&lt;/indent&gt;', '</blockquote>');
	content = content.replace('&lt;puce&gt;', '<li>');
	content = content.replace('&lt;/puce&gt;', '</li>');
	content = content.replace('&lt;lien cible=\'', '<a class=\'newsLink\' href=\'');
	content = content.replace('&lt;/lien&gt;', '</a>');
	content = content.replace('&gt;', '>');
	return content;
}

function $RF(radioName){ 
  array=$$('input'); 
  var value=""; 
   
  array.each( 
    function (radio){ 
      if (radio.hasAttribute("name") && radio.readAttribute("name")==radioName) 
        if (radio.checked=="true" || radio.checked) value=radio.getValue();  
    } 
  );//End of each() 
  return value;  
} 

function bulleOver(bulle){
	$(bulle+'Top').className="bulleTopOver";
	if($(bulle+'Content').visible())
		$(bulle+'Picto').src="/images/helia/bulleDepliee.png";
	else
		$(bulle+'Picto').src="/images/helia/bullePliee.png";
}
function bulleOff(bulle){
	$(bulle+'Top').className="bulleTop";
	$(bulle+'Picto').src="/images/helia/"+bulle+"Picto.png";
}



function toggleBlind(id){
	var bulleContent = "bulleExplicationContent"+id;
	var bullePicto = "bulleExplicationPicto"+id;
	this.src='/images/helia/bullePliee.png';
	if($(bulleContent).visible()){
		$(bullePicto).src="/images/helia/bullePliee.png";
		Effect.BlindUp(bulleContent);
	}
	else{
		$(bullePicto).src="/images/helia/bulleDepliee.png";
		Effect.BlindDown(bulleContent);
	}
}

function format_date_ok(chaine) {
    var exp=new RegExp("^[0-9]{2}\/[01]{1}[0-9]\/[0-9]{4}$","g");
    return exp.test(chaine);
  }
