
acms.modules.element = {

	init:function(){ //{{{
		$('.acms-module-element').each(function(){ });
	},//}}}
	initToolBar:function(){//{{{
		$('body').append("<div id='acms-module-element'></div>");

		$('.acms-module-element').each(function(){
			var that = this;
			var id_box = $(this).attr('id').substr(4);
			$('#btEdit', that).button({'icons':{'primary':'ui-icon-pencil'}, 'text':false}).click(function(){
				acms.modules.element.admin(id_box);
			});
		});

	},//}}}
	add:function(box_parent){ //{{{
		acms.modules.element.admin(undefined, box_parent);
	},//}}}
	admin:function(id_box, box_parent){//{{{

		var that = this;
		var tab = undefined;

		var w = $('#acms-module-element');
		w.dialog({ 'title':"Edition de l'élément", 'width':1020, 'height':660, 'modal':true, 'autoOpen':false });

		$.ajax({
			'url':'/modules/element/controller/form.html', 
			'dataType':'html',
			'data':'id_box='+id_box,
			'success':function(data){
				w.html(data);
				init();
			}
		});

		var init = function(){//{{{

			var tiny = acms.clone(acms.tinyMCE);
			tiny.setup = function(ed){
				ed.onInit.add(function(ed, evt){
					tab = $('#fr_content', w);
					ed.setContent(tab.val());
				});
			};
			$('#editeur', w).tinymce(tiny);

			$("#tab_langs", w).tabs({'select':function(event, ui){
				if(tab !== undefined){
					tab.val(tinyMCE.get('editeur').getContent());
				}
				tab = $('#'+$(ui.tab).attr('href').substr(5, 2)+'_content', w);
				tinyMCE.activeEditor.setContent(tab.val());
			}});

			$(".ui-tabs-nav", w).removeClass( "ui-widget-header");

			$('#btFermer', w).button({'icons':{'primary':'ui-icon-circle-close'}}).click(function(){ w.dialog('close'); });
			$('#btValider', w).button({'icons':{'primary':'ui-icon-circle-check'}}).click(valider);

			w.dialog('open');
		};//}}}
		var valider = function(){//{{{
			tab.val(tinyMCE.activeEditor.getContent());

			$.ajax({
				'url':'/modules/element/controller.php', 
				'data':$("#frm_element", w).serialize(),
				'success':function(data){

					if(data.error === true){
						$("#msg").alert(data.msg);
					}else{
						if(box_parent !== undefined && data.id_box !== undefined){
							var html = "<div id=box_"+data.id_box+" class='acms-box'/>";
							if($(box_parent).hasClass('acms-zone')){
								box_parent.before(html);
							}else{
								box_parent.after(html);
							}
							acms.updateContainer({'container_id':box_parent.parent().attr('id'), 'boxes':box_parent.parent().sortable("toArray"), 'id_page':$(".acms-page").attr('id').substr(5), 'success':function(){
								w.dialog('close');
								window.location = window.location;
							}});
						}else{
							w.dialog('close');
							window.location = window.location;
						}
					}
				}
			});
		}//}}}
		
	}//}}}

};


