var roomsSlider;
var optionsSlider;
var photosSlider;
var offersSlider;
var vouchersSlider;

function Slider(id,instanceId,perpage,style,pw,wrap) {
	var id;
	var obj;
	var items;
	var width;
	var itemWidth
	var instanceId;	
	var slidingObj;
	var lastX;
	var currentItem;
	var perpage;
	var w;
	var h;
	var style;
	var wrap;
			
	this.lastX = 0;
	this.currentItem = 1;
	this.wrap = wrap;
	
	this.setup(id,instanceId,perpage,style,pw,wrap);
}

Slider.prototype.setup = function(id,instanceId,perpage,style,pw,wrap) {
	
	if(!style) { var style = 0; }
	this.style = style;
				
	this.id = id;
	this.obj = $(id);	
	this.perpage = perpage;
	
	var goto = this.wrapPages();
	this.obj.style.height = this.perpage*this.h+'px';
	
	this.slidingObj = $(this.id+'_inner');
	this.instanceId = instanceId;
		
	if (pw) {
		this.itemWidth = pw;
	}
	else {
		this.itemWidth = this.slidingObj.getWidth();
	}
	this.width = this.w*this.items;
		
	this.slidingObj.style.width = this.width+'px';
	this.slidingObj.style.position = 'absolute';
	this.slidingObj.style.left = 0+'px';
	
	this.obj.style.overflow = 'hidden';
	
	//console.log('items:'+this.items);
	//console.log('itemWidth:'+this.itemWidth);
	
	if (this.items > 1) {
		this.render();
		//this.slide(goto);
		$(this.id+'_slider_navi').show();
	} else {
		$(this.id+'_slider_navi').hide();
	}	
	if (wrap) {		
		this.slide(1);
	}
}

Slider.prototype.wrapPages = function() {
	
	var items = $$('#'+this.id+' > div');
		
	var w = items[0].getWidth();
	var marginTop = $(items[0]).getStyle('margin-top');
	if(marginTop) {
		marginTop = marginTop.replace('px','')*1;
	}
	var marginBottom = $(items[0]).getStyle('margin-bottom');
	if(marginBottom) {
		marginBottom = marginBottom.replace('px','')*1;
	}
	//console.log(marginTop +' '+marginBottom);
	var h = items[0].getHeight()+marginTop+marginBottom;
	
	this.w = w;
	this.h = h;
	
	var pages = new Array;
	var itemPage = this.newPage(w);
	pages.push(itemPage);
	
	items[0].addClassName('top');
	
	for(var i = 0; i < items.length; i++) {
		itemPage.appendChild(items[i]);
		if(items[i].hasClassName('act')) {
			var scrollToPage = pages.length;
		}

		if(((i+1)%this.perpage == 0)&&(items[i+1])) {
			items[i].addClassName('bottom');
			items[i+1].addClassName('top');
			var itemPage = this.newPage(w);
			pages.push(itemPage);	
		}
	}
		
	var inner = document.createElement('div');
	inner.id = this.id+'_inner';
	
	for(var i = 0; i < pages.length; i++) {
		inner.appendChild(pages[i]);
	}
	$(this.id).appendChild(inner);
	this.items = pages.length;
	
	return scrollToPage;
	
}

Slider.prototype.newPage = function(w) {
	var itemPage = document.createElement('div');
	itemPage.className = 'page';
	itemPage.style.width = w+'px';	
	itemPage.style.cssFloat = 'left';
	return itemPage;
}

Slider.prototype.next = function() {
	var n = this.currentItem+1;
	if(n > this.items) {
		var n = 1;
	}
	this.slide(n);
}

Slider.prototype.slide = function(item,quick) {
	
	if (this.wrap) {
	
		var pages = this.items;
		
		for (var i = 0; i < pages; i++) {
			var e = $$('.sw' + (i + 1));
			
			var lrange = (3 - item);
			var rrange = 0;
			if (lrange < 0) {
				lrange = 0;
			}
			if (item > (pages - 2)) {
				var rrange = 2 - (Math.abs(item - pages));
			}
			
			if (((i + 1) < (item - 2 - rrange)) || (i + 1) > (item + 2 + lrange)) {
				e[0].hide();
			}
			else {
				e[0].show();
			}
		}
		
	}
	
	
	var x = 0-((item-1)*this.itemWidth);
	
	$$('#'+this.id+'_slider_navi .slider_switch').each(function(e){
		e.removeClassName('act');
	});
	
	$$('#'+this.id+'_slider_navi .sw'+item)[0].addClassName('act');
	
	if (this.lastX > x) {
		var p = {
			type: 'left',
			to: x,
			step: -25,
			delay: 10
		}
	}
	else {
		var p = {
			type: 'left',
			to: x,
			step: 25,
			delay: 10
		}
	}
	
	var quick = true;
	if (!quick) {
		$fx(this.slidingObj).fxAdd(p).fxRun();
	} else {
		this.slidingObj.style.left = x+'px';
	}
	
	this.lastX = x;
	this.currentItem = item;
	if($(this.id+'_slider_value')) {
		$(this.id+'_slider_value').value = this.currentItem;
	}
}

Slider.prototype.next = function() {
	var n = this.currentItem+1;
	if(n > this.items) {
		n = 1;
	}
	this.slide(n);
}

Slider.prototype.prev = function() {
	var n = this.currentItem-1;
	if(n < 1) {
		n = (this.items);
	}
	this.slide(n);
}

Slider.prototype.render = function() {
	
	var d = '';
	
	if(this.style == 1) {
		d += '<div class="slider_switch sw_prev" onclick="'+this.instanceId+'.prev();" onmouseover="$(this).addClassName(\'hv\');" onmouseout="$(this).removeClassName(\'hv\');"></div>';
	}
	
	for(var i = 0; i < this.items; i++) {
		d += '<div class="sw'+(i+1)+' slider_switch" onclick="'+this.instanceId+'.slide('+(i+1)+');" onmouseover="$(this).addClassName(\'hv\');" onmouseout="$(this).removeClassName(\'hv\');">'+(i+1)+'</div>';
	}
	
	if (this.style == 0) {
		d += '<div class="slider_switch sw_more" onclick="' + this.instanceId + '.next();" onmouseover="$(this).addClassName(\'hv\');" onmouseout="$(this).removeClassName(\'hv\');">'+Lang.get('wiecej')+' &raquo;</div>';
	}
	
	if(this.style == 1) {
		d += '<div class="slider_switch sw_next" onclick="'+this.instanceId+'.next();" onmouseover="$(this).addClassName(\'hv\');" onmouseout="$(this).removeClassName(\'hv\');"></div>';
	}
	
	d += '<div style="clear:both;"></div>';	
	$(this.id+'_slider_navi').innerHTML = d;
	
	$$('#'+this.id+'_slider_navi .sw1')[0].addClassName('act');
	var w = $$('#'+this.id+'_slider_navi .sw1')[0].getWidth();
	w++;
	
	if(this.style == 1) {
		$(this.id+'_slider_navi').style.width = (this.items*w)+'px';
	}
}

