
var mScroller = function(options) {
	this.indice_act = 0;
	this.config = {
		'container':null,
		'scroller':null,
		'buttons': {
			'bottom':null,
			'left':null,
			'right':null,
			'top':null
		},
		'activeButtonClass': null,
		'unactiveButtonClass': null
	}
	
	this.init = function(options) {
		if (options) {
			jQuery.extend(this.config, options);
		}
		
		// activating buttons
		for (button in this.config.buttons) this.populateButton(button);
		this.indice_act = 0;
		
	}
	
	// scrolls srollables
	this.scroll = function(direction, dimension) {
		var follow = true;
		var container = jQuery(this.config.container), scroller = jQuery(this.config.scroller), mx=0, my=0, dimensio2=0;
		if (direction == 'left' || direction == 'right' && !dimension) dimension = container.innerWidth();
		if (direction == 'top' || direction == 'bottom' && !dimension) dimension = container.innerHeight();
		//dimension =dimension+20;
		if (direction == 'left' || direction == 'right') dimensio2 = scroller.innerWidth();
		if (direction == 'top' || direction == 'bottom') dimensio2 = scroller.innerHeight();
		
		if (direction == 'left') mx=1;	
		if (direction == 'right') mx=-1;
		if (direction == 'top') my=1;
		if (direction == 'bottom') my=-1;

		// todo checking scrollable dimension
		//if (offsetX > scroller.width()) offsetX = scroller.width();
		//if (offsetY > scroller.width()) offsetY = scroller.height();
		if(
		   (direction == 'left' && 	 this.indice_act == 0) ||
		   (direction == 'top' && 	 this.indice_act == 0) ||
		   /*(direction == 'right' &&  dimensio2+50 > dimension) ||*/
		   (direction == 'right' &&  ((this.indice_act + 1 )* dimension) >  dimensio2) ||
		   (direction == 'bottom' && dimensio2+50 > dimension)
		  )
		{
			follow = false;
		}
		else
		{
			if (direction == 'left' && 	 this.indice_act > 0) 		this.indice_act = this.indice_act - 1; 
			if (direction == 'top' && 	 this.indice_act > 0)  		this.indice_act = this.indice_act - 1; 
			if (direction == 'right') 	this.indice_act = this.indice_act + 1; 
			if (direction == 'bottom' && dimensio2+50 < dimension)  this.indice_act = this.indice_act + 1; 
		}

		if(follow)
		{
			scroller.animate({
				'marginLeft': '+='+mx*dimension+'px'
				//'marginTop': offsetY+'px'
			});
		}
	}
	
	// takes a focus to element
	this.focus = function() {
		
	}
	
	
	////// buttons
	this.activateButton = function(button) {
		if (this.config.buttons[button]) {
			var btn = jQuery(this.config.buttons[button]);
			if (this.config.unactiveButtonClass && btn.hasClass(this.config.unactiveButtonClass)) btn.removeClass(this.config.unactiveButtonClass);
			if (this.config.activeButtonClass && !btn.hasClass(this.config.activeButtonClass)) btn.addClass(this.config.activeButtonClass);
			
			btn.bind('click', this, function(e){ 
				e.data.scroll(button); 
				return false;
			});
		}
	},
	this.deactivateButton = function(button) {
		if (this.config.buttons[button]) {
			var btn = jQuery(this.config.buttons[button]);
			if (this.config.activeButtonClass && btn.hasClass(this.config.activeButtonClass)) btn.removeClass(this.config.activeButtonClass);
			if (this.config.unactiveButtonClass && !btn.hasClass(this.config.unactiveButtonClass)) btn.addClass(this.config.unactiveButtonClass);
			btn.unbind('click');
		}
	},
	this.populateButton = function(button) {
		// todo check for avalability of button	
		this.activateButton(button);
	}
	
	
	this.init(options);
}


var mScroller2 = function(options) {
	this.config = {
		'container':null,
		'scroller':null,
		'buttons': {
			'bottom':null,
			'left':null,
			'right':null,
			'top':null
		},
		'activeButtonClass': null,
		'unactiveButtonClass': null
	}
	
	this.init = function(options) {
		if (options) {
			jQuery.extend(this.config, options);
		}
		
		// activating buttons
		for (button in this.config.buttons) this.populateButton(button);
	}
	
	// scrolls srollables
	this.scroll = function(direction, dimension) {
		var container = jQuery(this.config.container), scroller = jQuery(this.config.scroller), mx=0, my=0;
		if (direction == 'left' || direction == 'right' && !dimension) dimension = 430;
		if (direction == 'top' || direction == 'bottom' && !dimension) dimension = container.innerHeight();
		
		if (direction == 'left') mx=1;	
		if (direction == 'right') mx=-1;
		if (direction == 'top') my=1;
		if (direction == 'bottom') my=-1;

		// todo checking scrollable dimension
		//if (offsetX > scroller.width()) offsetX = scroller.width();
		//if (offsetY > scroller.width()) offsetY = scroller.height();
		scroller.animate({
			'marginTop': '+='+mx*dimension+'px'
			//'marginTop': offsetY+'px'
		});
	}
	
	// takes a focus to element
	this.focus = function() {
		
	}
	
	
	////// buttons
	this.activateButton = function(button) {
		if (this.config.buttons[button]) {
			var btn = jQuery(this.config.buttons[button]);
			if (this.config.unactiveButtonClass && btn.hasClass(this.config.unactiveButtonClass)) btn.removeClass(this.config.unactiveButtonClass);
			if (this.config.activeButtonClass && !btn.hasClass(this.config.activeButtonClass)) btn.addClass(this.config.activeButtonClass);
			
			btn.bind('click', this, function(e){ 
				e.data.scroll(button); 
				return false;
			});
		}
	},
	this.deactivateButton = function(button) {
		if (this.config.buttons[button]) {
			var btn = jQuery(this.config.buttons[button]);
			if (this.config.activeButtonClass && btn.hasClass(this.config.activeButtonClass)) btn.removeClass(this.config.activeButtonClass);
			if (this.config.unactiveButtonClass && !btn.hasClass(this.config.unactiveButtonClass)) btn.addClass(this.config.unactiveButtonClass);
			btn.unbind('click');
		}
	},
	this.populateButton = function(button) {
		// todo check for avalability of button	
		this.activateButton(button);
	}
	
	this.init(options);
}

//Singleton object
var mPopup = {
	
	config: {
		'container': null, // popup selector
		'close': null, // closing button selector	
		'closeEvent': 'click'
	},
	
	setup: function(options) {
		if (options) jQuery.extend(this.config, options);
		
		// bind close button
		if (this.config.close) {
			jQuery(this.config.close).bind(this.config.closeEvent, this, function(e) { e.data.close(); return false;});
		}

	},
	
	open: function() {
		this.clear();
		this.load();
		
		jQuery(this.config.container).fadeIn();
		jQuery(this.config.content).slideDown();
	},
	
	close: function() {
		jQuery(this.config.container).fadeOut();
		jQuery(this.config.content).slideUp();
	},
	
	// cleans popup content
	clear: function() {
		if (this.config.content)
			jQuery(this.config.content).text('');
	},
	
	
	setLoader: function() {
		
	},
	
	load: function() {
		if (this.config.onLoad) this.config.onLoad(this);
	}
	
}



var mGallery = function(options) {
	
	
	

	this.scroll = function(direction) {
		
	}
	
	this.next = function() {}
	this.prev = function() {}

}
