
var editor = {

  onedit : false,
  oneditid : '',
  
  timeout : 29 * 60 * 1000, // prompt at 29 min
  //  timeout : 10 * 1000, // DBG, prompt at 10 sec

  init:function(){
    editor.epop.init();
    editor.tiny_init();
    setTimeout( editor.saveSession, editor.timeout );
  },

  // TinyMCE GZ init
  tiny_init : function(){
    tinyMCE_GZ.init({
      languages : 'en',
      themes : 'advanced',
      plugins : "safari,advlink,advhr,iespell,preview,searchreplace,print,table,"+
	"contextmenu,paste,fullscreen,nonbreaking,xhtmlxtras",
      disk_cache : true,
      button_tile_map : true,
      debug : true
    });

    tinyMCE.init({
      languages : 'en',
       mode : "none",
      //mode : "textareas",
      theme : "advanced",
      plugins : "safari,advlink,advhr,iespell,preview,searchreplace,print,table,"+
	"contextmenu,paste,fullscreen,nonbreaking,xhtmlxtras",
      theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,"+
	"justifyfull,formatselect,fontselect,fontsizeselect,|,delete_table",
      theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|"+
	",outdent,indent,|,undo,redo,|,link,unlink,cleanup,|"+
	",insertdate,inserttime,code,|,forecolor,backcolor,|,"+
	"hr,advhr,removeformat,visualaid,|iespell,|,fullscreen",
      theme_advanced_buttons3 : "",
      theme_advanced_toolbar_location : "top",
	  //Modification Mehdi le 30 03 2011
	  //Modifier la hauteur du champ
	  height : "400"

//       theme_advanced_statusbar_location : "bottom",
//       theme_advanced_resizing : true

//      setup : function( ed ){
// 	ed.onKeyUp.add( function(event){editor.watchK(event)} );
//    	ed.onClick.add( function(){ed.execCommand( 'mceFocus', false, editor.epop.txta.id ) } );
//      }

    });
  },

  // objet popup
  epop : { 
    pop : $E(),
    fx : [1,10,1],
    fy : [1,10,1],

    wrap : $E(),
    txta : $E('TEXTAREA'),
    btb : $E(),
    tmp : false,

    init : function(){
      this.pop.id = "EPOP";

      this.pop.style.backgroundColor = "#F0F0EE";
      this.pop.style.borderBottom = "1px solid #CCCCCC";
      this.pop.style.zIndex = "111";

      this.txta.id = "EPOP_TXT";
      this.txta.style.position = "relative";
      this.txta.style.width = "100%";
      this.txta.style.height = "90%";

      this.btb.style.position = "relative"; // IE TWEAK !
      this.btb.style.bottom = "0px";
      this.btb.style.height = "10%";
      this.btb.style.textAlign = "center";
      this.btb.style.borderLeft = this.btb.style.borderRight = "1px solid #CCCCCC";
      
      var ie = $E('input'), ia = $E('input');
      ie.type = ia.type = "button";
//      ie.style.margin = ia.style.margin = "0 auto 0 auto";
//      ie.style.width = ia.style.width = "100px";
      ie.value = "Enregistrer";
      ia.value = "Annuler";
      ia.setAttribute( "onclick", "editor.stopEdit();" );
      ie.setAttribute( "onclick", "editor.saveEdit();" );

      this.btb.appendChild( ie );
      this.btb.appendChild( ia );

      this.wrap.appendChild( this.txta );
      this.wrap.appendChild( this.btb );
    },

    closePop : function(){
      this.pop.style.display = "none";
      $D( 'TPCTT_' + this.pop.id );
    }
  },
  
  startEdit : function( id, ctt ){
    editor.onedit = true,
    editor.oneditid = id;
    editor.epop.pop = flexPop( editor.epop.pop, editor.epop.fx, editor.epop.fy,
			       editor.epop.wrap.innerHTML, false );
    
//    var reg1 = new RegExp( "\n", 'g' ), reg2 = new RegExp( "\r", 'g' ),
//    reg3 = new RegExp( "\'", 'g' );    
//    ctt = ctt.replace( reg1 ,'' ).replace( reg2, '' ).replace( reg3, "\'" );

    editor.epop.tmp = ctt;

    tinyMCE.execCommand( 'mceAddControl', false, editor.epop.txta.id );
//     setTimeout("{"+
// 	       "var t = tinyMCE.get( '"+ editor.epop.txta.id +"' );" +
// 	       "t.setContent( '"+ ctt +"' );" +
// 	       "t.execCommand( 'mceFocus', false, '"+ editor.epop.txta.id +"' );"+
// 	       "}", 500 );
    setTimeout( editor.fillEdit , 500 );
    //    addEvent( document.body, "keyup", editor.watchK );
  },

  fillEdit : function(){
    var t = tinyMCE.get( editor.epop.txta.id );
    t.setContent( editor.epop.tmp );
    //t.execCommand( 'mceFocus', false, editor.epop.txta.id );
    tinyMCE.execCommand( 'mceFocus', false, editor.epop.txta.id );
  },

  saveEdit : function(){
    var t = tinyMCE.get( editor.epop.txta.id ),
    ctt = escape( t.getContent() ),
    width = "97%",
    //    ctt = t.getContent();
    // alert( "ITEM ID : " + editor.oneditid +"\n CONTENT :\n" + ctt );
    //    $("FOO").innerHTML = ctt;
    // alert( editor.oneditid );
    
    req = sendData( 'POST', "ajax.php", "ACTION=SAVEEDIT&ART_ID="+editor.oneditid+"&ART_CNT="+ctt );
    req.onreadystatechange = function(){
      if( req.readyState == 4 ){
	if( req.responseText == "BAD" )
	  alert( "Erreur de sauvegarde !" );
        else{
	  
	  //PARTIE CREATION DU FICHIER
	  
	  var arr = location.search.substring(1).split('&');
	  var tab = new Array();

	  //Si on a plusieurs articles , on reduit la longueur de la div
	  if( arr.length > 1 )
	    width = "47%";
	  
	  //Boucle sur les identifiant
	  for( var i = 0 ; i < arr.length ; i++ ){

	    if( arr[i] != "" ){

	      var x = arr[i].split('=');
	      tab[i] = x[1];//Recuperation des id
	     	      
	    }

	  }
	  	 
	  //Envoi de la requete pour creer le fichier statique
	  req2 = sendData( 'POST' , "ajax.php" , "ACTION=WRITE_PAGE&ART_ID="+tab);
	  req2.onreadystatechange = function(){
	    if( req2.readyState == 4 ){
	      if( req2.responseText == "BAD" )
	  	alert("Erreur de creation du fichier statique");
	      else
	      	window.location.reload();	      
	      
	      
	    }
	  }
	}

      }

    }
  
  editor.stopEdit();
  },
  
  stopEdit : function(){
    tinyMCE.execCommand( "mceRemoveControl", false, editor.epop.txta.id );
    editor.onedit = false,
    editor.oneditid = '';
    editor.epop.closePop();
//    removeEvent( document.body, "keyup", editor.watchK );
  },

  // alert 1 min before session timeout on edit state
  saveSession : function(){
    if( !editor.onedit ) return false;
    var c = confirm('Cette session va expirer dans 1 minutes, voulez vous la prolonger ? ');
    if( c ) sendData('GET', document.location );
  },

  watchK : function(e){
    if(!e) var e = window.event ;	// MSIE Hook
    alert(e.keyCode);
    if( e.keyCode == 27 && editor.onedit )
      editor.stopEdit();
  }
};

editor.init();

