var panelSlide = null;

window.addEvent('domready', function() {
	new SmoothScroll({ duration:700 }, $('ssw-content'));

	$$('#user_navigation .hide').each(function(element){
		element.setStyle('display', 'none');
	});

	SqueezeBox.assign($$('.photos-list > li > a'));

	$$('#ssw-menu > ul > li').each(function(element){
		element.addEvents({
			mouseenter: function(e){
				var submenu = this.getFirst('ul');

				if (submenu)
				{
					submenu.setStyle('display', 'block');

					if (!submenu.retrieve('originalHeight'))
					{
						submenu.store('originalHeight', submenu.getSize().y);
						submenu.setStyles({ height: 0, overflow: 'hidden' });

						submenu.set('tween', {
							duration: 200,
							transition: Fx.Transitions.Quad.easeInOut
						});
					}

					submenu.tween('height', submenu.retrieve('originalHeight'));
				}
			},
			mouseleave: function(e){
				var submenu = this.getFirst('ul');

				if (submenu)
				{
					//submenu.setStyle('display', '');
					submenu.tween('height', 0);
				}
			}
			
		});
	});
});


var IPBoard_Menu = new Class({
	initialize: function(element, menu){
		this.element = element;
		this.menu = menu;

		this.element.addEvent('click', this.toogle.bind(this));

		this.menu.set('tween', {property: 'opacity', duration: 'short'});
		this.menu.set('opacity', 0);

		document.addEvent('click', function(){
			this.close();
		}.bind(this));

		document.addEvent('keypress', function(event){
			if (event.key == 'esc') {
				this.close();
			}
		}.bind(this));
	},

	isOpen: function(){
		return this.menu.get('opacity') == 1;
	},

	toogle: function(event){
		Event.stop(event);

		if (!this.isOpen()){
			this.open();
		} else {
			this.close();
		}
	},

	open: function(){
		document.fireEvent('click');

		parent = this.element.getOffsetParent();
		elementCoords = this.element.getCoordinates(parent);

		this.menu.setStyles({
			'z-index': '9999',
			'position': 'absolute',
			'top': elementCoords.top + elementCoords.height - 1,
			'left': elementCoords.left
		});

		menuCoords = this.menu.getCoordinates(document);
		windowCoords = window.getCoordinates();

		if ((menuCoords.width + menuCoords.left) > windowCoords.width) {
			this.menu.setStyle('left', (elementCoords.left + elementCoords.width) - menuCoords.width);
		}

		this.menu.get('tween').start(1);
	},

	close: function(){
		this.menu.get('tween').start(0);
	}
});


window.addEvent('domready', function() {
	$$('.ipbmenu').each(function(element){
		menuId = element.get('id') + '_menucontent';
		if (menu = $(menuId)) {
			new IPBoard_Menu(element, menu);
		}
	});

	if ($('user_notifications_link')) {
		var loadNotifications = function(event){
			if (!$('user_notifications_link_menucontent')) {
				Event.stop(event);

				new Request.HTML({
					method: 'get',
					url: $('ssw-logo').getFirst('a').get('href')+'get-notifications.html',
					append: 'user_info',
					onRequest: function() {
						loading = new Element('div');
						loading.set('id', 'ajax_loading');
						loading.set('text', 'Chargement...');
						loading.set('opacity', 0);
						loading.set('tween', {property: 'opacity'});

						$('ssw-wrapper').grab(loading, 'bottom');
						$('ajax_loading').get('tween').start(1);
					},
					onComplete: function() {
						$('ajax_loading').get('tween').start(0);
					},
					onSuccess: function() {
						$('user_notifications_link_menucontent').addClass('ipbmenu');
						menu = new IPBoard_Menu($('user_notifications_link'), $('user_notifications_link_menucontent'));
						menu.open();

						$('user_notifications_link').removeEvent('click', loadNotifications);
					},
				}).send();
			}
		}

		$('user_notifications_link').addEvent('click', loadNotifications);
	}
});

