//~ var liste_categories = new Array();
var afficher_versionning = function(aHTML, id_document) {
	if ($('versioning-'+id_document)) {
		$('versioning-'+id_document).dispose();
		return;
	}
	
	var aHTML = new Element(aHTML);
	
	var div_versionning = new Element('div', {id: 'versioning-'+id_document})
		.setStyles({'border':'1px solid #666', 'margin':'5px'});
	
	aHTML.getParent().adopt(div_versionning);
	
	var requete = new Request({
			url : 'actions/ajax_versioning.php',
			method: 'post',
			data: 'id_document='+id_document,
			onComplete: function (response) {
				if ($('versioning-'+id_document)) {
					$('versioning-'+id_document).set('html', response);
				}
			}
	});
	requete.send();
}


var link = window.location.toString();
var link = link.replace(/.*\//, '').replace(/#.*/, '');
var categorie_principal;
switch (link) {
	case'espace_iim.html':		categorie_principal = 'iim';	break;
	case'communication.html':	categorie_principal = 'comm';	break;
	case'sites_utiles.html':	categorie_principal = 'su';		break;
	default: categorie_principal = '';
}


var EditionCategorie = new Class({
	
	initialize	: function ()
	{
		this.div_form = new Element('div').setStyles({
			'border'			: '1px solid #666',
			'position'			: 'absolute',
			'left'				: '5px',
			'top'				: '5px',
			'background-color'	: '#FFF'
		});
		this.div_form.injectInside($$('body')[0]);
		
		this.requete = new Request({
			method		: 'get',
			//~ evalScripts	: true,
			data		: 'mode_ajax=1',
			onComplete	: this.affiche_formulaire.bind(this)
		});
	},
	
	charger_formulaire	: function ( id_categorie )
	{
		this.requete.setOptions({url: 'admin/categorie-'+categorie_principal+'-'+id_categorie+'.html'});
		this.requete.send();
	},
	
	affiche_formulaire	: function ( formulaire )
	{
		this.div_form.empty().set('html', formulaire);
		
		if ( $('edit-workspace') )
		{
			$('edit-workspace').addEvent('submit', this.envoie_formulaire.bind(this) );
			$('editer-categorie-annuler').addEvent('click', this.fermer.bind(this));
			charger_calendrier($$('.charge-calendrier')[0]);
			this.div_form.setStyle('display', 'block');
		}
		else
			this.fermer();
		
	},
	
	envoie_formulaire	: function (event)
	{
		event = new Event(event).preventDefault();
		var execute = function () {
			$('edit-workspace').set('send', {method:'post', evalResponse: true}).send();
		};
		execute.delay(100);
	},
	
	
	envoie_reussi		: function ()
	{
		this.fermer();
		LoadContent.workspace();
	},
	
	
	fermer				: function ()
	{
		//~ if (this.div_form && this.div_form.empty)
		//~ {
			this.div_form.setStyle('display', 'none');
			//~ this.div_form.empty().dispose();
		//~ }
	}
	
});


var EditionDocument = new Class({
	
	initialize	: function ()
	{
		this.div_form = new Element('div').setStyles({
			'border'			: '1px solid #666',
			'position'			: 'absolute',
			'left'				: '5px',
			'top'				: '5px',
			'width'				: '650px',
			'background-color'	: '#FFF'
		});

		this.requete = new Request({
			method		: 'get',
			//~ evalScripts	: true,
			data		: 'mode_ajax=1',
			onComplete	: this.afficher_formulaire.bind(this)
		});
		tinyMCE.init({
			mode					: "textareas",
			doctype					: '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
			language				: "fr",
			theme					: "advanced",
			skin					: "o2k7",
			strict_loading_mode		: true,
			convert_fonts_to_spans	: true,
			save_callback			: function ( id, content) { $('description-document').set('html', content); },
			editor_selector			: 'tinymce-description-document'
		});
	},
	
	
	charger_formulaire	: function ( id_document )
	{
		if ( this.requete.running )
			this.requete.cancel();
		if ( tinyMCE.getInstanceById('description-document') != null )
			tinyMCE.execCommand('mceRemoveControl', false, 'description-document');
		this.div_form.empty().set('html', 'Chargement du formulaire en cours...');
		this.requete.setOptions({url: 'admin/document-edit-'+id_document+'.html'});
		this.requete.send();
	},
	
	
	afficher_formulaire	: function (formulaire)
	{
		var body = document.getElementsByTagName('body')[0];
		this.div_form.empty().set('html', formulaire);
		this.div_form.injectInside(body);
		
		if ($('description-document'))
		{
			$('description-document').addClass('tinymce-description-document');
			tinyMCE.execCommand('mceAddControl', false, 'description-document');
			
			$('edit-document').addEvent('submit', this.envoie_formulaire);
			$('form-annuler').addEvent('click', this.fermer.bind(this));
			
			charger_calendrier($$('.charge-calendrier')[0]);
			
			initialise_edit_document();
		}
		else
			this.fermer();
	},
	
	
	envoie_formulaire	: function (event)
	{
		event = new Event(event).stop();		
		(function () { $('edit-document').set('send', {method:'post', evalResponse: true}).send(); }).delay(100);
	},
	
	
	envoie_reussi		: function ()
	{
		this.fermer();
		LoadContent.workspace();
		if (calen)
		{
			loadEvenement();
			calen.update();
		}
	},
	
	
	fermer				: function ()
	{
		if ( tinyMCE.getInstanceById('description-document') != null )
			tinyMCE.execCommand('mceRemoveControl', false, 'description-document');
		this.div_form.empty().dispose();
	}

});


var ClassLoadContent = new Class({
	
	workspace : function () {
		var link = window.location.toString().replace(/.*\//, '').replace(/#.*/, '');
		this.load_ajax_ws = new Request({
			url			: link,
			method		: 'get',
			evalScripts	: true,
			data		: 'mode_ajax=1',
			onComplete	: this.definir_contenu_ws.bind(this)
		});
		this.load_ajax_ws.request();
	},
	
	definir_contenu_ws : function (reponse) {
		$('contenu').empty().set('html', reponse);
		this.parse_workspace()
	},
	
	parse_workspace : function () {
		var noJs = document.location.href.toString().test('nojs');
		
		if (noJs) {
			$$('a').each(function (el) {
				if (el.onclick) {
					el.onclick();
					//console.log(el.onclick);
				}
			});
		} else {
			$$('.liste-documents').each(
				function (ul) {
					var id_categorie = ul.id.replace(/[^0-9]*/, '');
					if (categorie_principal != 'su' && !contentCookie.contains(id_categorie)) {
						ul.setStyle('display','none');
					}
				}
			);
			
			$$('.liste-categories').each(
				function (ul) {
					var id_categorie = ul.id.replace(/[^0-9]*/, '');
					if (categorie_principal != 'su' && !contentCookie.contains(id_categorie)) {
						ul.setStyle('display','none');
					}
				}
			);
			
			$('contenu').getElements('h1').each(
				function (h1) {
					if (h1.id.test('categorie-nom')) {
						var id_categorie = h1.id.replace(/[^0-9]*/, '');
						var liste_categories = $('liste-categories-'+id_categorie);
						var liste_documents = $('liste-documents-'+id_categorie);
						if (liste_categories || liste_documents) {
							h1.setStyle('cursor', 'pointer');
							h1.addEvent('click', function () {
								if (categorie_principal == 'su') {
									return;
								}
								
								if (!contentCookie.contains(id_categorie)) {
									contentCookie.push(id_categorie);
								} else {
									contentCookie.erase(id_categorie);
								}
								Cookie.write('categories-ouvertes', JSON.encode(contentCookie));
								
								if (liste_categories) {
									if (liste_categories.getStyle('display') == 'none') {
										liste_categories.setStyle('display','block');
									} else {
										liste_categories.setStyle('display','none');
									}
								}
								
								if (liste_documents) {
									if (liste_documents.getStyle('display') == 'none') {
										liste_documents.setStyle('display','block');
									} else {
										liste_documents.setStyle('display','none');
									}
								}
							});
						}
					}
				}
			);
		}
		
		var mainCat = $$('ul.liste-categories-principales')[0];
		
		mainCat.getElements('a.editer-categorie').addEvent('click', function (event) {
			event = new Event(event).stop();
			var id_categorie = this.href.replace(/.*\//g, '').replace(/[^0-9]*/g, '');
			edit_workspace.charger_formulaire(id_categorie);
		});
		
		mainCat.getElements('a.editer-document').addEvent('click', function (event) {
			event = new Event(event).stop();
			var id_document = this.href.replace(/.*\//g, '').replace(/[^0-9]*/g, '');
			edit_document.charger_formulaire(id_document);
		});
	}
});


var contentCookie;
var edit_workspace, edit_document;
var LoadContent = new ClassLoadContent();

var page_chargee = function() {
	edit_workspace = new EditionCategorie();
	edit_document = new EditionDocument();
	
	var cook = Cookie.read('categories-ouvertes');
	contentCookie = cook?JSON.decode(cook):[];
	
	LoadContent.parse_workspace();
}

window.addEvent('domready', page_chargee);



