// Copyright (c) 2006 SÃ©bastien Gruhier (http://xilinus.com, http://itseb.com)
// 
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// VERSION 1.1-trunk

//Adapté par Julien YVER "JOUVE" 06/2008

if(typeof Draggable == 'undefined')
  throw("widget.js requires including script.aculo.us' dragdrop.js library");

if(typeof Builder == 'undefined')
  throw("widget.js requires including script.aculo.us' builder.js library");

// Xilinus namespace
if(typeof Xilinus == 'undefined')
  Xilinus = {}
  
Builder.dump();


Xilinus.Widget = Class.create();  
Xilinus.Widget.lastId = 0;
Xilinus.Widget.remove = function(element, options) {    
  if (options && options.afterFinish)
    options.afterFinish.call();
} 

Object.extend(Xilinus.Widget.prototype, {
  initialize: function(className, id) {
    className = className || "widget";  
    this._id   = id || ("widget_" + Xilinus.Widget.lastId++);
    
    this._titleDiv   = DIV({className: className + '_title',     id: this._getId("header")},  "");
	this._editionDiv = DIV({className: className + '_edition',     id: this._getId("edition"), style:'display:none'},  "");
    this._contentDiv = DIV({className: className + '_content',   id: this._getId("content")}, "");
    this._footerDiv  = DIV({className: className + '_statusbar', id: this._getId("footer")},  "");
    
    var divHeader  = DIV({className: className + '_nw' }, this._titleDiv);
	var divEdition  = DIV({className: className + '_ew'}, this._editionDiv);
    var divContent = DIV({className: className + '_w' },  this._contentDiv); 
    var divFooter  = DIV({className: className + '_sw' }, this._footerDiv);  
	
	
	    
    this._div = DIV({className: className + (className != "widget" ? " widget" : ""), id: this._getId()}, [divHeader, divEdition, divContent, divFooter]);     
    this._div.widget = this;
        
    return this;
  },    
  
  destroy: function() {
    this._div.remove();
  },
  
  getElement: function() {
    return $(this._getId()) || $(this._div);
  },
  
  
  setTitle: function(id,title) {
    $(this._titleDiv).update("<div id='btnRouge' class='btnTitreWidget'></div><div id='upDown' style='display:none' class='upDownClass'><a href='#' class='miniBtnClass' id='mini_button' onclick='minWidget(this); return false;' title='R&eacute;duire'></a><a href='#' class='maxiBtnClass' id='maxi_button' style='display:none;' onclick='maxWidget(this); return false;' title='Agrandir'></a></div><div id='widget_title_"+id+"'>"+title+"</div>");
    return this;
  },

  getTitle: function(title) {
    return $(this._titleDiv)
  },
  
  
  
  setEdition: function() {
	  if (onglet=="actus") {
		  var contenuEdition = "<div id='zoneEdition'><div id='a0' class='cornerSamples'><div id='edit'><div id='btnFermer' onclick='miseAJour(this); return false;'><a href='#' id='validerPerso'>Valider</a></div><form name='formulaireEdition' onSubmit='return false;'><div class='colorContenu'>Options de personnalisation</div><div class='formEdition'><span class='spanEdition'>Titre du bloc</span><input type='text' id = 'titreBlocId' name='titreBloc' MAXLENGTH=55 style='vertical-align:middle;'/></div><div class='formEdition'><span>Affichage d&eacute;taill&eacute; </span><INPUT TYPE=CHECKBOX NAME='detail' checked style='vertical-align:middle;'/><span>Afficher les auteurs </span><INPUT TYPE=CHECKBOX NAME='auteurs' checked style='vertical-align:middle;'/><INPUT TYPE=HIDDEN NAME='reduire' value='0'/></div><div class='formEdition'><span>Nombre d'articles affich&eacute;s </span><SELECT id='nbId' name='nbAfficheEdition'><OPTION VALUE='3'>3</OPTION><OPTION VALUE='5'>5</OPTION><OPTION VALUE='10'>10</OPTION></SELECT></div><div class='formEdition' style='vertical-align:middle;'><span>Ambiance du bloc </span><input type='button' class='ColorChoix' style='vertical-align:middle; background-color:#768e9e;' onclick='chgfond(\"#768e9e\");'/><input type='button' class='ColorChoix' style='vertical-align:middle; background-color:#d70005;' onclick='chgfond(\"#d70005\");'/><input type='button' class='ColorChoix' style='vertical-align:middle; background-color:#47494d;' onclick='chgfond(\"#47494d\");'/><input type='button' class='ColorChoix' style='vertical-align:middle; background-color:#899639;' onclick='chgfond(\"#899639\");'/><input type='button' class='ColorChoix' style='vertical-align:middle; background-color:#9f676f;' onclick='chgfond(\"#9f676f\");'/><input type='button' class='ColorChoix' style='vertical-align:middle; background-color:#ca2e7b;' onclick='chgfond(\"#ca2e7b\");'/></div></form></div></div></div>";
	  } else if(onglet=="chronique") {
		  var contenuEdition = "<div id='zoneEdition'><div id='a0' class='cornerSamples'><div id='edit'><div id='btnFermer' onclick='miseAJour(this); return false;'><a href='#' id='validerPerso'>Valider</a></div><form name='formulaireEdition' onSubmit='return false;'><div class='colorContenu'>Options de personnalisation</div><div class='formEdition'><input type='hidden' id = 'titreBlocId' name='titreBloc' MAXLENGTH=55 style='vertical-align:middle;'/></div><div class='formEdition'><span>Affichage d&eacute;taill&eacute; </span><INPUT TYPE=CHECKBOX NAME='detail' checked style='vertical-align:middle;'/><span></span><INPUT TYPE='hidden' NAME='auteurs' checked style='vertical-align:middle;'/><INPUT TYPE=HIDDEN NAME='reduire' value='0'/></div><div class='formEdition'><span>Nombre d'articles affich&eacute;s </span><SELECT id='nbId' name='nbAfficheEdition'><OPTION VALUE='3'>3</OPTION><OPTION VALUE='5'>5</OPTION><OPTION VALUE='10'>10</OPTION></SELECT></div><div class='formEdition' style='vertical-align:middle;'><span>Ambiance du bloc </span><input type='button' class='ColorChoix' style='vertical-align:middle; background-color:#768e9e;' onclick='chgfond(\"#768e9e\");'/><input type='button' class='ColorChoix' style='vertical-align:middle; background-color:#d70005;' onclick='chgfond(\"#d70005\");'/><input type='button' class='ColorChoix' style='vertical-align:middle; background-color:#47494d;' onclick='chgfond(\"#47494d\");'/><input type='button' class='ColorChoix' style='vertical-align:middle; background-color:#899639;' onclick='chgfond(\"#899639\");'/><input type='button' class='ColorChoix' style='vertical-align:middle; background-color:#9f676f;' onclick='chgfond(\"#9f676f\");'/><input type='button' class='ColorChoix' style='vertical-align:middle; background-color:#ca2e7b;' onclick='chgfond(\"#ca2e7b\");'/></div></form></div></div></div>";
	  } else {
		  var contenuEdition = "<div id='zoneEdition'><div id='a0' class='cornerSamples'><div id='edit'><div id='btnFermer' onclick='miseAJour(this); return false;'><a href='#' id='validerPerso'>Valider</a></div><form name='formulaireEdition' onSubmit='return false;'><div class='colorContenu'>Options de personnalisation</div><div class='formEdition'><input type='hidden' id = 'titreBlocId' name='titreBloc' MAXLENGTH=55 style='vertical-align:middle;'/></div><div class='formEdition'><span>Affichage d&eacute;taill&eacute; </span><INPUT TYPE=CHECKBOX NAME='detail' checked style='vertical-align:middle;'/><span>Afficher les auteurs </span><INPUT TYPE=CHECKBOX NAME='auteurs' checked style='vertical-align:middle;'/><INPUT TYPE=HIDDEN NAME='reduire' value='0'/></div><div class='formEdition'><span>Nombre d'articles affich&eacute;s </span><SELECT id='nbId' name='nbAfficheEdition'><OPTION VALUE='3'>3</OPTION><OPTION VALUE='5'>5</OPTION><OPTION VALUE='10'>10</OPTION></SELECT></div><div class='formEdition' style='vertical-align:middle;'><span>Ambiance du bloc </span><input type='button' class='ColorChoix' style='vertical-align:middle; background-color:#768e9e;' onclick='chgfond(\"#768e9e\");'/><input type='button' class='ColorChoix' style='vertical-align:middle; background-color:#d70005;' onclick='chgfond(\"#d70005\");'/><input type='button' class='ColorChoix' style='vertical-align:middle; background-color:#47494d;' onclick='chgfond(\"#47494d\");'/><input type='button' class='ColorChoix' style='vertical-align:middle; background-color:#899639;' onclick='chgfond(\"#899639\");'/><input type='button' class='ColorChoix' style='vertical-align:middle; background-color:#9f676f;' onclick='chgfond(\"#9f676f\");'/><input type='button' class='ColorChoix' style='vertical-align:middle; background-color:#ca2e7b;' onclick='chgfond(\"#ca2e7b\");'/></div></form></div></div></div>";
	  }
	  
    $(this._editionDiv).update(contenuEdition);
    return this;
  },
  
  getEdition: function(title) {
    return $(this._editionDiv)
  },
  
  
  setFooter: function(title) {
    $(this._footerDiv).update(title);
    return this;
  },
  
  getFooter: function(title) {
    return $(this._footerDiv)
  },
  
  setContent: function(title) {
	  if (title == null || title == undefined || title == '')
		  $(this._contentDiv).update('<div class="widget_waiting">&nbsp;</div>');  
	  else
		  $(this._contentDiv).update(title);  
    return this;
  },
   
  getContent: function(title) {
    return $(this._contentDiv)
  },
  
  updateHeight: function() {       
    /*$(this._contentDiv).setStyle({height: null})
      
    var h = $(this._contentDiv).getHeight();
    $(this._contentDiv).setStyle({height: h + "px"})*/
  },
  
  // PRIVATE FUNCTIONS
  _getId: function(prefix) { 
      return (prefix ? prefix + "_" : "") + this._id;
  }
});


Xilinus.Portal = Class.create()
Object.extend(Xilinus.Portal.prototype, {
  lastEvent: null,   
  widgets:   null,
  columns:   null, 
  
  initialize: function(columns, options) {   
    this.options = Object.extend({                  
                     url:          null,                 // Url called by Ajax.Request after a drop
                     onOverWidget: null,                 // Called when the mouse goes over a widget
                     onOutWidget:  null,                 // Called when the mouse goes out of a widget                                           
                     onChange:     null,                 // Called a widget has been move during drag and drop 
                     onUpdate:     null,                 // Called a widget has been move after drag and drop
                     removeEffect: Xilinus.Widget.remove // Remove effect (by default no effect), you can set it to Effect.SwitchOff for example
                   }, options)
    this._columns = (typeof columns == "string") ? $$(columns) : columns;
    this._widgets = new Array();   
    this._columns.each(function(element) {Droppables.add(element, {onHover: this.onHover.bind(this), 
                                                                   overlap: "vertical", 
                                                                   accept: this.options.accept})}.bind(this));  
    this._outTimer  = null;
    
    // Draggable calls makePositioned for IE fix (??), I had to remove it for all browsers fix :) to handle properly zIndex
    this._columns.invoke("undoPositioned");    
    
    this._currentOverWidget = null; 
    this._widgetMouseOver = this.widgetMouseOver.bindAsEventListener(this);
    this._widgetMouseOut  = this.widgetMouseOut.bindAsEventListener(this);
    
    Draggables.addObserver({ onEnd: this.endDrag.bind(this), onStart: this.startDrag.bind(this) });

  },
  
   
  
  ////////////////////////////////
  //Fonction qui ajoute un widget
  ///////////////////////////////
  
  add: function(widget, columnIndex, draggable) {   
    draggable = typeof draggable == "undefined" ? true : draggable
			
    // Add to widgets list
    this._widgets.push(widget);
	
    if (this.options.accept)
      widget.getElement().addClassName(this.options.accept);
	  
    	// Add element to column
			
	var recupId = document.getElementById("widget_col_0").firstChild;
	if(columnIndex == 0){
		this._columns[columnIndex].insertBefore(widget.getElement(), recupId);
		}
		
	var recupId1 = document.getElementById("widget_col_1").firstChild;
	if(columnIndex == 1){
		this._columns[columnIndex].insertBefore(widget.getElement(), recupId1);
		}
		
	var recupId2 = document.getElementById("widget_col_2").firstChild;
	if(columnIndex == 2){
		this._columns[columnIndex].insertBefore(widget.getElement(), recupId2);
		}

    widget.updateHeight();
    
    // Make header draggable   
    if (draggable) {
      widget.draggable = new Draggable(widget.getElement(),{ handle: widget._titleDiv, revert: false});     
      widget.getTitle().addClassName("widget_draggable");   
    }
	   
    // Update columns heights
    this._updateColumnsHeight();  

    // Add mouse observers  
    if (this.options.onOverWidget)
      widget.getElement().immediateDescendants().invoke("observe", "mouseover", this._widgetMouseOver);
    if (this.options.onOutWidget)
      widget.getElement().immediateDescendants().invoke("observe", "mouseout",  this._widgetMouseOut);
  },  
    
	
	
	//////////////////////////
	//Fonction qui supprime le widget
	////////////////////////
  remove: function(widget) {
    // Remove from the list
    this._widgets.reject(function(w) { return w == widget});

    // Remove observers
    if (this.options.onOverWidget)
      widget.getElement().immediateDescendants().invoke("stopObserving", "mouseover", this._widgetMouseOver);
    if (this.options.onOutWidget)
      widget.getElement().immediateDescendants().invoke("stopObserving", "mouseout",  this._widgetMouseOut);

    // Remove draggable
    if (widget.draggable)
      widget.draggable.destroy();
      
    // Remove from the dom
    this.options.removeEffect(widget.getElement(), {afterFinish: function() {widget.destroy();}});
    
    // Update columns heights
    this._updateColumnsHeight(); 
		
	  },
	  
	  
	 
  
    
   ///////////////////////////
  //minimize/maximize le widget
  /////////////////////////////
  miniWidget: function(widget) {
	  this._updateColumnsHeight();
	  Element.toggle(widget._getId("content"));
	  var zone = widget.getElement();
	  var zoneDown = zone.getElementsByClassName('miniBtnClass', 'div');
	  var zoneUp = zone.getElementsByClassName('maxiBtnClass', 'div');
	  zoneDown[0].style.display = "none";
	  zoneUp[0].style.display = "block";
  },
  
  
   maxiWidget: function(widget) {
	  Element.toggle(widget._getId("content"));
	  this._updateColumnsHeight();
	  var zone = widget.getElement();
	  var zoneDown = zone.getElementsByClassName('miniBtnClass', 'div');
	  var zoneUp = zone.getElementsByClassName('maxiBtnClass', 'div');
	  zoneDown[0].style.display = "block";
	  zoneUp[0].style.display = "none";
  },
   
   
   /////////////////////
   //Fonction permettant le choix des couleurs d'affichage
   ////////////////////
   /*
   ColorClick: function(e, widget) {
	 var zone = widget.getElement();
	 var nvColorBut = zone.getElementsByClassName('formEdition', 'div');
	
 	 if (Element.visible(colorpicker.container)) {
    colorpicker.close();
 	 } else {
    RicoUtil.positionCtlOverIcon(colorpicker.container,nvColorBut[3].childNodes[1]);
    colorpicker.open();
 	 }
 	 Event.stop(e);
	 
	},
  
  
    colorPreviewRes: function(widget){
		var zone = widget.getElement();
		var zoneCouleur = zone.getElementsByClassName('ColorChoixClass', 'div');
		zoneCouleur[0].style.backgroundColor = colorBox.value; 
	 },
 
 */
 
 ////////////////////////////////
 //Fonction qui gère le reload du widget
 ///////////////////////////////
reloadRSS: function (url, fieldname)
{
	//alert(getInfoVisitor(fieldname.getElement().id, 4));
  var nbentrees = '12';
  var zone = fieldname.getElement();
  var myAjax = new Ajax.Request (
  url,
  { 
    method : 'post',
    onComplete : function(originalRequest){	
	  items = originalRequest.responseXML;	
      var items_count = items.getElementsByTagName('item').length;
      if(parseInt(nbentrees)==0) nbentrees='5';
      var  items_count = (parseInt(nbentrees)<items.getElementsByTagName('item').length)?parseInt(nbentrees):items.getElementsByTagName('item').length;
	
	  var feedTitre = items.getElementsByTagName('title')[0].firstChild.data;
	  var imageRSS =  items.getElementsByTagName('image')[0].firstChild.firstChild.data;
	  var linkImageRSS = items.getElementsByTagName('image')[0].lastChild.firstChild.data;
	  //alert(imageRSS);
	  //var img = "<div class='imageRSS'><img src="+ imageRSS +" alt='image rss'/></div>";
	  
      var feed="<div class='imageRSS'><a href="+ linkImageRSS + " target='_blank'><img src="+ imageRSS +" /></a></div>";
	  
      for(var i=0; i<items_count; i++) {
	 root_node = items.getElementsByTagName('item')[i];
     titre = root_node.getElementsByTagName('title')[0].firstChild.data;
	 lien = root_node.getElementsByTagName('link')[0].firstChild.data;
	 publication = root_node.getElementsByTagName('pubDate')[0].firstChild.data;
	 description = root_node.getElementsByTagName('description')[0].firstChild.data;
	 auteur = root_node.getElementsByTagName('author')[0].firstChild.data;
     
	 //Affichage du contenu de l'info bulle
	 var str;
	 var tab = new Array();
	 var reg=new RegExp("[ ]+", "g");
	 tab = description.split(reg);
	 var ligne1 = new Array();
	 var ligne2 = new Array();
	 var ligne3 = new Array();
	 
	 for(var j=0; j<8; j++){
	 ligne1 += tab[j] +" ";
	 }
	for(var k=8; k<16; k++){
	 ligne2 += tab[k] +" ";
	 }
	 for(var l=16; l<24; l++){
	 ligne3 += tab[l] +" ";
	 }
	 
	 var regMatch=new RegExp("undefined", "g");
	 
	 str = ligne1+"&#013"+ligne2+"&#013"+ligne3+"[...]";
	 str = str.replace(/\s+/g, "&nbsp;");
	 str = str.replace(regMatch, "");
	//alert(str.substr(0,80));
	
	
    feed += "<div class='contenuRss'><div class='zoneInfoRSS'>"

+ "<a href="+ lien + "><span class='titreArticle' onmouseover='tooltip.show(this);' onmouseout='tooltip.hide(this);' title=" + str + ">" + titre + "</span>"

+ "<br/><span class='extraitArticle'>" + description + "</span></a>"

+ "<br/><span class='nomAuteur'>" + auteur + ",</span> "

+ "<span class='dateGras'>" + publication + "</span></div></div>";
		     
      }
	  //alert(feed);
	var urlLoad = '"'+"../site/home07.html"+'"';
	//alert("le titre de l'article : " + '"' + "les joies de l'informatique" + '"' );
	var nomPopUp = '"'+"Lextenso"+'"';
	feed += "<div id='articlesAJour' onclick='open_popup("+urlLoad+","+nomPopUp+")'><a href='#' id='articleJourText'>Tous les articles mis &agrave; jour</a></div>" ; 
	//alert(feed);
	/*content = feed;
	titreRSS = feedTitre;*/
	//alert(fieldname.getElement().firstChild.nextSibling.nextSibling.nextSibling.firstChild.id);
	var idReception = zone.firstChild.nextSibling.nextSibling.nextSibling.firstChild.id;
 	$(idReception).innerHTML = feed;
 //alert($(idReception).innerHTML);
  //Action sur le nombre d'affichage
 	 var zoneContent = zone.getElementsByClassName('widget_content', 'div');
  	 var nbItems = zoneContent[0].childNodes.length;
	 //alert(nbItems);
 	 if(getInfoVisitor(zone.id, 1) == 3){
	 for(i=0; i<nbItems-1; i++){
		 if(i<4){
		 zoneContent[0].childNodes[i].style.display = 'block';
		 }else{
		 zoneContent[0].childNodes[i].style.display = 'none';
		 }
		 }
		
	 }else if(getInfoVisitor(zone.id, 1) == 5){
	 for(i=0; i<nbItems-1; i++){
		 if(i<6){
		 zoneContent[0].childNodes[i].style.display = 'block';
		 }else{
		 zoneContent[0].childNodes[i].style.display = 'none';
		 }
		 }
	 }else if(getInfoVisitor(zone.id, 1) == 10){
	 for(i=0; i<nbItems-1; i++){
		 if(i<11){
		 zoneContent[0].childNodes[i].style.display = 'block';
		 }else{
		 zoneContent[0].childNodes[i].style.display = 'none';
		 }
		 }
	 }
 
	}
	  
  });
  
  
 
  
},
 
   //////////////////////////
   //Fonction qui met à jour le widget une fois le bouton valder (zone édition) cliqué.
   /////////////////////////
   miseAJourWidget: function(widget){
	  var zone = widget.getElement();
	  var idZonetitre = "widget_title_" + zone.id.substring(7);
	  var nvTitre = zone.getElementsByClassName('formEdition', 'div');
	 
	  //modification du titre
	  if(nvTitre[0].lastChild.value != ''){
		  $(idZonetitre).innerHTML = nvTitre[0].lastChild.value;
	  }
	  
	  //Application de la nouvelle couleur
	  var colorBox=$('ColorValue');
	  $(idZonetitre).style.color = colorBox.value;
	  var spans = zone.getElementsByClassName('dateGras', 'span');
	 
	  for(i=0; i<spans.length; i++){
	  	spans[i].style.color = colorBox.value;
	  }
	 	
	
	  
	  //Action sur le nombre d'affichage
	 var accesForm = zone.getElementsByClassName('formEdition', 'div');
 	 var zoneContent = zone.getElementsByClassName('widget_content', 'div');
 	 var indexForm = accesForm[2].lastChild.value;
  	 var nbItems = zoneContent[0].childNodes.length;
	 //alert(nbItems);
 	 if(indexForm == 3){
	 for(i=0; i<nbItems-1; i++){
		 if(i<4){
		 zoneContent[0].childNodes[i].style.display = 'block';
		 }else{
		 zoneContent[0].childNodes[i].style.display = 'none';
		 }
		 }
		
	 }else if(indexForm == 5){
	 for(i=0; i<nbItems-1; i++){
		 if(i<6){
		 zoneContent[0].childNodes[i].style.display = 'block';
		 }else{
		 zoneContent[0].childNodes[i].style.display = 'none';
		 }
		 }
	 }else if(indexForm == 10){
	 for(i=0; i<nbItems-1; i++){
		 if(i<11){
		 zoneContent[0].childNodes[i].style.display = 'block';
		 }else{
		 zoneContent[0].childNodes[i].style.display = 'none';
		 }
		 }
		
  }
 	  
	  
	 //Action sur le type d'affichage
	 
	 var zoneContenu = zone.getElementsByClassName('nomAuteur');
	 var zoneExtraitContenu = zone.getElementsByClassName('extraitArticle');
	 var accesForm = zone.getElementsByClassName('formEdition', 'div');
	 //var titreBulleAc = zone.getElementsByClassName('titreArticle');
		
	
	 if(accesForm[1].childNodes[1].checked){
		 for(i=0; i<nbItems-2; i++){
			 if(zoneExtraitContenu && zoneExtraitContenu[i]){
				 zoneExtraitContenu[i].style.display = "inline";
			 }
		 }
	 }else{
		 for(i=0; i<nbItems-2; i++){
			 if(zoneExtraitContenu && zoneExtraitContenu[i]){
				 zoneExtraitContenu[i].style.display = "none";
			 }
		 }
	 }
	
	if(accesForm[1].childNodes[3].checked){
		for(i=0; i<nbItems-2; i++){
			if(zoneContenu && zoneContenu[i]){
				zoneContenu[i].style.display = "inline";
			}
		 }
	 }else{
		 for(i=0; i<nbItems-2; i++){
			if(zoneContenu && zoneContenu[i]){
				zoneContenu[i].style.display = "none";
			}		 
		 }	 
	 }
	 
	 
	//Vérifier si la checkbox est checked ou non pour aff det
	if (accesForm[1].childNodes[1].checked==true) {
					var setvalue = "1";
					}
	if (accesForm[1].childNodes[1].checked==false) {
					var setvalue = "0";
					}
	 
		 
	//Vérifier si la checkbox est checked ou non pour auteur
	if (accesForm[1].childNodes[3].checked==true) {
					var setvalueAut = "1";
					}
	if (accesForm[1].childNodes[3].checked==false) {
					var setvalueAut = "0";
					}

	//Vérifier si la on reduit ou non
	var setvalueRed = accesForm[1].childNodes[4].value;

	//Effacer et enregistrer le cookie contenant les infos du visiteur
	 //Delete_Cookie('lextensoCookie_'+zone.id.substring(7));
	 setVisitorInfo(zone.id.substring(7), getInfoVisitor("lextensoCookie_"+zone.id.substring(7), 0), indexForm, colorBox.value, nvTitre[0].lastChild.value, setvalue, setvalueAut,setvalueRed);
	 
	  
	  //On réaffiche le contenu
	  $(widget._getId("content")).show();
	 
	  //Action sur l'icone edition
	  var zoneEdition = zone.firstChild.nextSibling.nextSibling;
	  Element.toggle(zoneEdition.firstChild.id);
	 
	  //Mise à jour de la taille du widget
	   widget.updateHeight();
	  //Mise à jour de la taille des colonnes
	  this._updateColumnsHeight();
	 

   },
   
   //////////////////////
   //Clic sur l'icone edition
   /////////////////////
   changeWidget: function(widget) {
	  var colorBox=$('ColorValue');
	  var zone = widget.getElement();
	  var zoneEdition = zone.firstChild.nextSibling.nextSibling;
	  var idWidget = zone.id.substring(7);
	  Element.toggle(zoneEdition.firstChild.id);

	  var idZonetitre = "widget_title_" + idWidget;
	  var nvTitre = zone.getElementsByClassName('formEdition', 'div');
	  
	  //Mise a jour de la couleur
	  colorBox.value = getInfoVisitor("lextensoCookie_"+idWidget,2);
	  
	  var nvBool = nvTitre[1].getElementsByTagName("input");
	  if(getInfoVisitor("lextensoCookie_"+idWidget,4) == 1) {
		  nvBool[0].checked = true;
	  } else {
		  nvBool[0].checked = false;
	  }
	  if(getInfoVisitor("lextensoCookie_"+idWidget,5) == 1) {
		  nvBool[1].checked = true;
	  } else {
		  nvBool[1].checked = false;
	  }
	  
	  var select = nvTitre[2].getElementsByTagName("select");
	  var nbFlux = getInfoVisitor("lextensoCookie_"+idWidget,1);
	  if(nbFlux==3) select[0].selectedIndex = 0;
	  if(nbFlux==5) select[0].selectedIndex = 1;
	  if(nbFlux==10) select[0].selectedIndex = 2;
	  //Mise a jour du titre
	  nvTitre[0].lastChild.value = $(idZonetitre).innerHTML;
	  
	  //Mise a jour de la taille des colonnes
	  this._updateColumnsHeight();
	   },
   
    //////////////////////
   //Clic sur le lien fermer
   /////////////////////
   fermerEdition: function(widget) {
	  var zone = widget.getElement();
	  var zoneEdition = zone.firstChild.nextSibling.nextSibling;
	  Element.toggle(zoneEdition.firstChild.id);
	  this._updateColumnsHeight();
   },
   
   ////////////////////////////
   ///////
   ////////////////////////////
   recupValueFormEdition: function(widget) {
	   var zone = widget.getElement();
	   var infoTitre = zone.formulaireEdition.titreBloc.value;
   },
   
 
   
////////////////////////////////   
//On récupère la postion de chaque widget
/////////////////////////////
  serialize: function() {
    parameters = ""
    this._columns.each(function(column) {   
      var p = column.immediateDescendants().collect(function(element) {
        return column.id + "[]=" + element.id
      }).join("&") 
      parameters += p + "&"
    });               
    
    return parameters;                      
  },     

//On ajoute les différents contrôles aux widget
  addWidgetControls: function(element) {
    $(element).observe("mouseover", this._widgetMouseOver); 
    $(element).observe("mouseout", this._widgetMouseOut); 
  },
  
  // EVENTS CALLBACKS
  widgetMouseOver: function(event) {   
    this._clearTimer();
      
    var element =  Event.element(event).up(".widget");
    if (this._currentOverWidget == null || this._currentOverWidget != element) {
      if (this._currentOverWidget && this._currentOverWidget != element)
        this.options.onOutWidget(this, this._currentOverWidget.widget)    
        
      this._currentOverWidget = element;
      this.options.onOverWidget(this, element.widget)
    }
  },

  widgetMouseOut: function(event) {    
    this._clearTimer();
    var element =  Event.element(event).up(".widget"); 
    this._outTimer = setTimeout(this._doWidgetMouseOut.bind(this, element), 100);
  },
  
  _doWidgetMouseOut: function(element) {
    this._currentOverWidget = null;
    this.options.onOutWidget(this, element.widget)    
  },                                               
  
  // DRAGGABLE OBSERVER CALLBACKS
  startDrag: function(eventName, draggable) { 
    var widget = draggable.element;
    
    if (!this._widgets.find(function(w) {return w == widget.widget}))
      return;

    var column = widget.parentNode;
    
    // Create and insert ghost widget
    var ghost = DIV({className: 'widget_ghost'}, ""); 
    $(ghost).setStyle({height: widget.getHeight()  + 'px'})

    column.insertBefore(ghost, widget);  

    // IE Does not absolutize properly the widget, needs to set width before
    widget.setStyle({width: widget.getWidth() + "px"});
    
    // Absolutize and move widget on body
    Position.absolutize(widget);  
    document.body.appendChild(widget);   
    
    // Store ghost to drag widget for later use
    draggable.element.ghost = ghost; 
    
    // Store current position
    this._savePosition = this.serialize();   
	
  },   

  endDrag: function(eventName, draggable) {
    var widget = draggable.element;      
    if (!this._widgets.find(function(w) {return w == widget.widget}))
      return;
    
    var column = widget.ghost.parentNode;
    
    column.insertBefore(draggable.element, widget.ghost); 
    widget.ghost.remove();   
    
    if (Prototype.Browser.Opera)     
      widget.setStyle({top: 0, left: 0, width: "100%", height: widget._originalHeight, zIndex: null, opacity: null, position: "relative"})
    else
      widget.setStyle({top: null, left: null, width: null, height: widget._originalHeight, zIndex: null, opacity: null, position: "relative"})
    
    widget.ghost = null;    
    widget.widget.updateHeight();
    this._updateColumnsHeight();
    
    // Fire events if changed
    if (this._savePosition != this.serialize()) {
      if (this.options.url)  
        new Ajax.Request(this.options.url, {parameters: this.serialize()});
      
      if (this.options.onUpdate)
		try 
		{
			this.options.onUpdate(this);
		}
		catch(e) {}
    }
	
  },


  onHover: function(dragWidget, dropon, overlap) {  
    var offset = Position.cumulativeOffset(dropon);
    var x = offset[0] + 10;
    var y = offset[1] + (1 - overlap) * dropon.getHeight();

    // Check over ghost widget
    if (Position.within(dragWidget.ghost, x, y))
      return;
      
    // Find if it's overlapping a widget
    var found = false;
    var moved = false;
    for (var index = 0, len = this._widgets.length; index < len; ++index) {
      var w = this._widgets[index].getElement();
      if (w ==  dragWidget || w.parentNode != dropon)   
        continue;        

      if (Position.within(w, x, y)) {    
        var overlap = Position.overlap( 'vertical', w);     
        // Bottom of the widget
        if (overlap < 0.5) {         
          // Check if the ghost widget is not already below this widget
          if (w.next() != dragWidget.ghost) {
            w.parentNode.insertBefore(dragWidget.ghost, w.next());   
            moved = true;
          }
        } 
        // Top of the widget
        else {       
          // Check if the ghost widget is not already above this widget
          if (w.previous() != dragWidget.ghost) {      
            w.parentNode.insertBefore(dragWidget.ghost, w);   
            moved = true;
          }
        }
        found = true;
        break;
      }
    }
    // Not found a widget
    if (! found) {        
      // Check if dropon has ghost widget
      if (dragWidget.ghost.parentNode != dropon) {
        // Get last widget bottom value
        var last = dropon.immediateDescendants().last();
        var yLast = last ? Position.cumulativeOffset(last)[1] + last.getHeight() : 0; 
        if (y > yLast && last != dragWidget.ghost) {
          dropon.appendChild(dragWidget.ghost);
          moved = true;
        }
      }
    }  
    if (moved && this.options.onChange) 
      this.options.onChange(this)            

    this._updateColumnsHeight();
  },                
  
  // PRIVATE FUNCTIONS
  _updateColumnsHeight: function() {      
    var h = 0;
    this._columns.each(function(col) {
      h = Math.max(h, col.immediateDescendants().inject(0, function(sum, element) { 
        return sum + element.getHeight(); 
      }));
    })
    this._columns.invoke("setStyle", {height: h + 'px'})
  },
  
  _clearTimer: function() {
    if (this._outTimer) {
      clearTimeout(this._outTimer);
      this._outTimer = null;
    }                        
  },
  reduireWidget: function(widget){
	  	var zone = widget.getElement();
	 	var accesForm = zone.getElementsByClassName('formEdition', 'div');
	 	var indexForm = accesForm[2].lastChild.value;
		var colorBox=$('ColorValue');
		var nvTitre = zone.getElementsByClassName('formEdition', 'div');
		//Vérifier si la checkbox est checked ou non pour aff det
		if (accesForm[1].childNodes[1].checked==true) {
						var setvalue = "1";
						}
		if (accesForm[1].childNodes[1].checked==false) {
						var setvalue = "0";
						}
		 
			 
		//Vérifier si la checkbox est checked ou non pour auteur
		if (accesForm[1].childNodes[3].checked==true) {
						var setvalueAut = "1";
						}
		if (accesForm[1].childNodes[3].checked==false) {
						var setvalueAut = "0";
						}
	
		//Vérifier si la on reduit ou non
		var setvalueRed = accesForm[1].childNodes[4].value;
	
		 setVisitorInfo(zone.id.substring(7), getInfoVisitor("lextensoCookie_"+zone.id.substring(7), 0), indexForm, colorBox.value, nvTitre[0].lastChild.value, setvalue, setvalueAut,setvalueRed);
	 
	}
 
});     
