//
// User Topics Manager
// David Georges - Globule Bleu
//

if(typeof(UtmController) == 'undefined')
	UtmController = new Object();

Object.extend(UtmController, {
			  
	_portal: null,
	_currentEffect: null,
	
	init: function() 
	{
		// Portal initialization.
		this._portal = new Xilinus.Portal(".droppable", {
			onUpdate: this.onUpdate.bindAsEventListener(this),
			removeEffect: Effect.SwitchOff
		});
		
		// Blocs initialization.
		$$('.widget.cadre').each(function(w) {
			var widget = new Xilinus.Widget('cadre', w.id);
			var movable = (w.readAttribute('mode') == 'movable') ? true : false;
			this._portal.add(widget, null, movable);
			if(w.hasClassName("collapsed")) w.select('.cadre_w').invoke("setStyle", {display: 'none'});
		}.bind(this));
		
		$A($$('.show')).each(function(e) {
			e.show();
		}.bindAsEventListener(this));
		
		$A($$('.hide')).each(function(e) {
			e.setStyle({
				left: '-5000px',
				position: 'absolute'
			});
		}.bindAsEventListener(this));
		
		// Dynamic event handlers (to avoid JavaScript detection).
		$A($$('a.action')).each(function(a) {
			a.observe('click', this.a_handler.bindAsEventListener(this, a));
		}.bindAsEventListener(this));
		
		$A($$('h1.handle')).each(function(h) {
			h.observe('mouseover', this.h1_over_handler.bindAsEventListener(this, h));
			h.observe('mouseout', this.h1_out_handler.bindAsEventListener(this, h));
		}.bindAsEventListener(this));
		
		if($('closeCustomizer')) {
			$('closeCustomizer').observe('click', function(e) {
				this.customize();
				Event.stop(e);
			}.bindAsEventListener(this));
		}
		
		//CookiesManager.save("test_cookie", true);
	},
	
	onUpdate: function() 
	{
		CookiesManager.saveUtm("utm", this._portal.serialize());
		this._currentEffect = null;
	},
	
	edit: function(a) 
	{
		var widget = a.up(".widget");
		var config = widget.down(".cadre_cw");
		
		if(config.empty()) this.reloadOptions(widget.id, true);
		
		if(config.visible())
		{
			this.collapse(config);
			widget.down('.utils .edit a.action').removeClassName('hover');
		}
		else
		{
			this.expand(config);
			widget.down('.utils .edit a.action').addClassName('hover');
		}
	},             
	
	a_handler: function(e, a) 
	{
		var url = a.href;
		var action = url.substr(url.indexOf('?')+1);
		eval('this.'+ action +'(a)');
		Event.stop(e);
	},
	
	form_handler: function(e, f)
	{		
		var name = $(f).up(".widget").id;
		
		if(FormValidate.validate('options_'+name, true, showFormError.bind(FormValidate)))
		{
			var data = Object.toJSON(f.serialize(true));
			
			$(name).down(".cadre_cw form").setStyle("visibility: hidden");
						
			this.applyOptions(name, data, function(request) 
			{
				if(!request.responseJSON || request.responseJSON.failed || request.responseJSON.out == 0)
				{
					var form = $$('#'+name+' .cadre_cw form').first();
					
					if(request.responseJSON && request.responseJSON.failed)
						showError(form, request.responseJSON.errors.error);
					else if(request.responseJSON)
						showError(form, 'Une erreur est survenue (1)');
					else
						showError(form, 'Une erreur est survenue (2)');
				}
				else
				{
					var o = eval(name);
					o.optionsApplied(name, data);
					
					this.reloadOptions(name, false);

					$('content_'+name).update(request.responseJSON.out);
					
					this.collapse($(name).down(".cadre_cw"));
					this.collapse($('errorMsg'));
				}
				
				$(name).down(".cadre_cw form").setStyle("visibility: visible");
				$('ajax_loader').remove();
				
			}.bind(this));
		}
		
		Event.stop(e);
	},
	
	reloadOptions: function(name, expand)
	{
		$('ajax_loader_'+name).show();
		
		this.loadOptions(name, function(transport, json) 
		{
			var config = $(name).down(".cadre_cw");
			
			config.update(json.out);
			$('ajax_loader_'+name).hide();
			
			var form = config.down('form.action');
			var cancel = form.down('a.action');
			
			form.observe('submit', this.form_handler.bindAsEventListener(this, form));
			cancel.observe('click', this.a_handler.bindAsEventListener(this, cancel));
			
			FormValidate.buildValidators(form.id);
			
			initHighlighting();			
			
		}.bind(this));
	},
	
	h1_over_handler: function(e, h)
	{
		var bloc = $(h).up(".contenuCadre");
		
		if(bloc.readAttribute("mode") == 'locked')
			return;
		
		if(!bloc.hasClassName('cadreHover'))
			bloc.addClassName('cadreHover');
	},
	
	h1_out_handler: function(e, h)
	{
		var bloc = $(h).up(".contenuCadre");
		
		if(bloc.readAttribute("mode") == 'locked')
			return;
		
		if(bloc.hasClassName('cadreHover'))
			bloc.removeClassName('cadreHover');
	},
	
	customize: function()
	{
		if($('customizer').visible())
			this.collapse('customizer');
		else
			this.expand('customizer');
	},
	
	expand: function(element)
	{
		if(!element) return;
		if(this._currentEffect != null) return;
		
		this._currentEffect = Effect.BlindDown(element, {
			duration: 0.3,
			transition: Effect.Transitions.linear,
			afterFinish: this.onUpdate.bindAsEventListener(this)
		});
	},
	
	collapse: function(element) 
	{
		if(!element) return;
		if(this._currentEffect != null) return;
		
		this._currentEffect = Effect.BlindUp(element, {
			duration: 0.3,
			transition: Effect.Transitions.linear,
			afterFinish: this.onUpdate.bindAsEventListener(this)
		});
	},
	
	btoggle: function(a) 
	{
		if(this._currentEffect != null) return;
		
		var widget = a.up(".widget");
		var config = a.up(".widget").down(".cadre_cw");
		var content = a.up(".widget").down(".cadre_w");
		
		if(widget.hasClassName("collapsed")) 
		{
			this.expand(content);
			widget.removeClassName("collapsed");
			widget.down(".utils .action img").src = "/media/images/interface/puce-ouvert.png";
		}
		else 
		{
			this.collapse(content);
			if(config) config.hide();
			widget.addClassName("collapsed");
			widget.down(".utils .action img").src = "/media/images/interface/puce-ferme.png";
		}
	}
});
