// MessageBar - Fornisce una messagbox di tipo barra con effetto discesa
//
// Syntax: 
// var MsgBar = new MessageBar(elementBar, elementText, elementButton);
// MsgBar.show(messageText, messageIcon);
//
// Arguments:
// - Class constructor
//   elements - The strings id of Elements for bar, text and close button
// - Show method
//   messageText - The string of text to be displayed
//   messageIcon - String to indicate what type of icon can be displayed
//                 Can be set to: 'msgError', 'msgOk' or 'msgAlert'
//
// Returns:
// - Class constructor
//   object - This MessageBar instance
// - Show method
//   null
var MessageBar = new Class({
	initialize: function(msgBar, msgText, msgBtn){
		this.msgBarPanel = document.id(msgBar);
		this.msgBarText = document.id(msgText);
		this.msgBarButton = document.id(msgBtn);
		this.msgBarIcon = {
			'msgError': '<img src="../img/msgErr.png" />',
			'msgOk': '<img src="../img/msgOk.png" />',
			'msgAlert': '<img src="../img/msgAlert.png" />'
		};
		this.msgBarSlidePanel = new Fx.Slide(this.msgBarPanel).hide();
		this.msgBarPanel.setStyle('visibility', 'visible');
		this.msgBarButton.addEvent (
			'click',
			function(e) {
				e.stop();
				this.msgBarSlidePanel.slideOut();
			}.bind(this)
		);
	},
	show: function(msg, error){
		if (this.msgBarSlidePanel.open) {
			this.msgBarSlidePanel.slideOut().chain(
				function() {
					this.msgBarText.set('html', this.msgBarIcon[error] + msg );
					this.msgBarSlidePanel.slideIn();
				}.bind(this)
			);			
		}
		else {
			this.msgBarText.set('html', this.msgBarIcon[error] + msg );
			this.msgBarSlidePanel.slideIn();
		}	
	}
});

// ScrollBar - Fornisce una scrollbar che controlla lo scorrimento del contenuto di un'oggetto
var ScrollBar = new Class({
	initialize: function(scrArea, scrBar, scrGrab, mode){
		this.scrollArea = document.id(scrArea);
		this.scrollBar = document.id(scrBar);
		this.scrollGrab = document.id(scrGrab);
		this.scrollMode = mode;
		this.visibleArea = 0;
		this.scrollableArea = 0;
		this.slideFx;
		
		this.update();
	},
	update: function() {
		if (this.scrollMode == 'vertical') {
			this.visibleArea = this.scrollArea.getSize().y;
			this.scrollableArea = this.scrollArea.getScrollSize().y;
		}
		else {
			this.visibleArea = this.scrollArea.getSize().x;
			this.scrollableArea = this.scrollArea.getScrollSize().x;
		}
		if (this.scrollableArea > this.visibleArea) {
			this.scrollBar.fade('in');
			this.slideFx = new Slider(this.scrollBar, this.scrollGrab, {
	            steps: this.scrollableArea - this.visibleArea,
	            mode: this.scrollMode,
	            onChange: function(step){
					if (this.scrollMode == 'vertical') {
						this.scrollArea.scrollTo(0, step);
					}
					else {
						this.scrollArea.scrollTo(step, 0);
					}
	            }.bind(this)
	        }).set(0);
			this.scrollArea.addEvent('mousewheel', 
				function(e){
					e = new Event(e).stop();
					var stepWheel = this.slideFx.step - e.wheel * 20;
					this.slideFx.set(stepWheel);
				}.bind(this)
			);
		}
		else {
			this.slideFx = null;
			this.scrollArea.removeEvents();
			this.scrollBar.fade('out');
		}
	}
});

