NbMooSlider = new Class({
	itemWidth: 0,
	totalWidth: 0,
	visibleItems: 4,
	slideVars: new Array,
	wrapper: null,
	innerWrapper: null,
	initialize:  function(wrapper,items,controls)
	{
		this.wrapper = wrapper;

		this.innerWrapper = new Element('div');
		this.innerWrapper.addClass('nb-slider-innerwrap');
		this.innerWrapper.set('tween',{link:'wait'});

		this.wrapper.adopt(this.innerWrapper);

		this.totalWidth = 0;

		items.each(function(item,index){
			this.innerWrapper.adopt(item);
			this.itemWidth =  item.getSize().x;
			this.totalWidth += item.getSize().x;
		},this);

		this.innerWrapper.setStyle('width',this.totalWidth+'px');
		this.innerWrapper.setStyle('margin-left',0);

		this.initControls(controls);
	},
	initControls: function(controls)
	{
		this.controls = controls;

		this.controls.next.addEvent('click',this.next.bind(this));
		this.controls.prev.addEvent('click',this.prev.bind(this));

		this.controls.disableFn(controls.prev);

		if(this.totalWidth < 460)
		{
			this.controls.disableFn(controls.next);
		}
	},
	autoSlide: function()
	{
		pos = parseInt(this.innerWrapper.getStyle('margin-left'));
		
		this.period = (function() {
			if ((pos-this.itemWidth) >= parseInt('-'+(this.totalWidth-(this.itemWidth*this.visibleItems)))) {
				this.innerWrapper.set('tween', {
					'onComplete' : function() {
						pos = parseInt(this.innerWrapper.getStyle('margin-left'));
					}.bind(this)
				});
				this.innerWrapper.tween('margin-left', (pos-this.itemWidth)+'px');
		
				if((pos-this.itemWidth) <= parseInt('-'+(this.totalWidth-(this.itemWidth*this.visibleItems))))
				{
					this.controls.disableFn(this.controls.next);
				}

				if((pos-this.itemWidth) >= parseInt('-'+(this.totalWidth-(this.itemWidth*this.visibleItems))))
				{
					this.controls.enableFn(this.controls.prev);
				}					
			} else {
				$clear(this.period);
			}
		}).periodical(15000,this);
	},
	next: function(e)
	{
		e = new Event(e);
		e.stop();
		
		$clear(this.period);

		pos = parseInt(this.innerWrapper.getStyle('margin-left'));



		this.innerWrapper.tween('margin-left', (pos-this.itemWidth)+'px');

		if((pos-this.itemWidth) <= parseInt('-'+(this.totalWidth-(this.itemWidth*this.visibleItems))))
		{
			this.controls.disableFn(this.controls.next);
		}

		if((pos-this.itemWidth) >= parseInt('-'+(this.totalWidth-(this.itemWidth*this.visibleItems))))
		{
			this.controls.enableFn(this.controls.prev);
		}
	},
	prev: function(e)
	{
		e = new Event(e);
		e.stop();
		
		$clear(this.period);

		pos = parseInt(this.innerWrapper.getStyle('margin-left'));

		this.innerWrapper.tween('margin-left', (pos+this.itemWidth)+'px');

		if(parseInt(pos+this.itemWidth) >= parseInt('-'+(this.totalWidth-(this.itemWidth*this.visibleItems))))
		{
			this.controls.enableFn(this.controls.next);
		}

		if(parseInt(pos+this.itemWidth) == 0)
		{
			this.controls.disableFn(this.controls.prev);
		}
	}
});

var slider = null;

window.addEvent('domready',function(){

	var disableFunc = function(el){
		el.fade('out')
	};
	var enableFunc = function(el){
		el.fade('in')
	};

	var test = $$('.tx-baekobannerads-pi1').getElement('.banner-wrapper');
	//console.log(test);
	
	
	$$('.tx-baekobannerads-pi1').each(function(plugin){
		plugin.getElements('.banner-wrapper').each(function(el,i) {
			// Retrieve slider and controls 
			if (el.getElement('.banner-slider')!=null) {
				
				var sliderEl 	  = el.getElement('.banner-slider');
				var sliderBack 	  = el.getElement('.sliderBack');
				var sliderForward = el.getElement('.sliderForward');

				// Add NbMooSlider
				new NbMooSlider(
					sliderEl,
					sliderEl.getElements('.banner'),
					{
						'prev':sliderBack,
						'next':sliderForward,
						'disableFn':disableFunc,
						'enableFn':enableFunc
					}
				);
				
				// Add image popup events
				sliderEl.getElements('.banner').each(function(banner){
					banner.getElement('.image').addEvents({
						'mouseover': function(e) {
							var wrapperPos = this.getParent().getParent().getParent().getPosition();
							$('popup-'+this.getParent().get('id')).setStyles({
								'top': window.getScroll().y+e.client.y-this.getParent().getPosition().y-250,
								'left': e.client.x-wrapperPos.x+70
							});
							$('popup-'+this.getParent().get('id')).removeClass('hidden');
						},
						
						'mouseout': function() {
							$('popup-'+this.getParent().get('id')).addClass('hidden');
						},
						
						'mousemove': function(e) {
							var wrapperPos = this.getParent().getParent().getParent().getPosition();
							$('popup-'+this.getParent().get('id')).setStyles({
								'top': window.getScroll().y+e.client.y-this.getParent().getPosition().y-250,
								'left': e.client.x-wrapperPos.x+70
							});
						}
					});
				});
			}
		});
	});
});