// <![CDATA[

var LanguagesWidget = Class.create(
{
	initialize: function()
	{
		var container = $('language');
		var languages = $('languages').hide();
/*
		var btn       = $('language-btn');
		var timer     = null;
		
		btn.observe('mouseover', function(event){
			event.stop();
			if(!languages.visible())
			{
				new Effect.toggle(languages, 'blind', {duration:.3});
			}
		});

		languages.observe('mousemove', function(){
			timer = clearTimeout(timer);
		});

		container.observe('mousemove', function(){
			timer = clearTimeout(timer);
		});
		container.observe('mouseout', function(){
			if(languages.visible())
			{
				timer = setTimeout(function(){ new Effect.BlindUp(languages, {duration:.3}); }, 1000);
			}
		});
*/
	}
});

var NavWidget = Class.create(
{
	initialize: function()
	{
		var opened = false;
		var locked = false;
		var timer  = null;

		$$('div#navigation li.nav-section-item').each(function(section, i)
		{
			section.observe('mouseover', this.switchon.bind($(section.parentNode)));
		});
		
	},
	
	showLinks: function()
	{
		var parent = $(this.parentNode);

		if(!locked && !parent.hasClassName('selected'))
		{
			var links = this.next();
	
			if(links)
			{
				locked = true;

				if(opened)
				{
					new Effect.Parallel(
					[
						new Effect.BlindUp(opened, {sync: true}),
						new Effect.BlindDown(links, {sync: true})
					],
					{
						duration: .4,
						beforeStart: function()
						{
							parent.addClassName('selected');
						},
						afterFinish: function()
						{
							$(opened.parentNode.parentNode).removeClassName('selected');
							locked = false;
						}
					});
				}
				else
				{
					new Effect.BlindDown(
						links,
						{
							duration: .4,
							beforeStart: function()
							{
								
							},
							afterFinish: function()
							{
								$(this.parentNode).removeClassName('selected');
								locked = false;
							}
						}
					);
				}
			}
		}
	},
	
	hideLinks: function()
	{
		timer = setTimeout();
	},
	
	switchon: function()
	{
		if(!this.hasClassName('selected'))
		{
			this.addClassName('hover');
		}
	},
	
	switchoff: function()
	{
		this.removeClassName('hover');
	}
});

var NavigationWidget = Class.create(
{
	initialize: function()
	{
		this.sections = $$('div#navigation a.nav-section-item');
		this.pages    = $$('div#navigation div.nav-pages');
		this.updating = false;
		this.hover    = null;
		this.timer    = null;

		this.pages.invoke('hide');

		this.sections.each(function(item, i)
		{
			var parent = $(item.parentNode);
			var pages  = item.next();

			if(parent.hasClassName('selected'))
			{
				if(pages)
				{
					item.next().show();
				}
			}
			else
			{
				if(pages)
				{
					item.observe('mouseover',   this.showPages.bindAsEventListener(this, i));
					parent.observe('mousemove', this.clearTimer.bind(this));
					parent.observe('mouseout',  this.hidePages.bindAsEventListener(this, i));
				}
				else
				{
					parent.observe('mouseover', this.activateItem);
					parent.observe('mouseout',  this.deactivateItem);
				}
			}
		}.bind(this));
	},

	activateItem: function()
	{
		this.addClassName('hover');
	},

	deactivateItem: function()
	{
		this.removeClassName('hover');
	},

	showPages: function(event, i)
	{
		if(!this.updating && (i!=this.hover))
		{
			this.updating = true;
			
			this.hidePagesByNb(this.hover);
			
			this.hover    = i;
			var section   = $(this.sections[i].parentNode);
			var pages     = section.childElements()[1];

			new Effect.BlindDown(pages, {duration: .4, beforeStart: function(){ section.addClassName('selected'); }.bind(this), afterFinish: function(){ this.updating = false; }.bind(this)});
		}
	},

	hidePagesByNb: function(i)
	{
		if(i)
		{
			var section   = $(this.sections[i].parentNode);
			var pages     = section.childElements()[1];
			new Effect.BlindUp(pages, {duration: .4, beforeStart:function(){ section.removeClassName('selected'); }.bind(this)});
		}
	},
	
	hidePages: function(event, i)
	{
		if(!this.updating)
		{
			this.updating = true;
			var section   = $(this.sections[i].parentNode);
			var pages     = section.childElements()[1];
	
			this.timer = setTimeout(function(){ new Effect.BlindUp(pages, {duration: .4, beforeStart:function(){ }.bind(this), afterFinish: function(){ section.removeClassName('selected'); this.hover = null; this.updating = false; }.bind(this)}); }.bind(this), 500);
		}
	},

	clearTimer: function()
	{
		this.timer    = clearTimeout(this.timer);
		this.updating = false;
	}
});

var MoreWidget = Class.create(
{
	initialize: function()
	{
		$$('div#more li').each(function(item)
		{
			item.observe('mouseover', function(){ this.addClassName('hover'); });
			item.observe('mouseout', function(){ this.removeClassName('hover'); });
		});
	}
});

var FormsWidget = Class.create({
	initialize: function()
	{
		var btn_cancel = $$('.btn-cancel');
		var btn_save   = $$('.btn-submit');
		var result     = $('result');
		var timer      = null
	
		if(result)
		{
			result.hide();
			new Effect.BlindDown(result, {duration: .5, afterFinish: function(){
				timer = setTimeout(function(){ new Effect.BlindUp(result, {duration: .5}); clearTimeout(timer); }, 2000);
			}});
		}

		btn_cancel.each(function(btn){
			var form = $(btn.id.substr(4, btn.id.length));

			btn.observe('click', function(event){
				event.stop();
				form.reset();
			});
		});
		
		btn_save.each(function(btn){
			var form = $(btn.id.substr(4, btn.id.length));

			btn.observe('click', function(event){
				event.stop();
				form.submit();
			});	
		});
	}	
});

document.observe('dom:loaded', function()
{
	new LanguagesWidget();
	new NavigationWidget();
	new MoreWidget();
	new FormsWidget();
});

// ]]>