sliders = [];
$(document).ready(function() {
	$('.rolling').each(function() {
		sliders.push(new SliderBox(100, $(this)));
	});
	fixIEZIndex();
});

function fixIEZIndex() {
	if (($.browser.msie && parseInt($.browser.version, 10) <= 7) || 
		($.browser.opera && parseInt($.browser.version, 10) == 9)) {
		$('.main-menu .link').each(function() {
			var clone = $(this).clone();
			clone.appendTo($('#container'))
				.css('top', $(this).offset().top)
				.css('left', $(this).offset().left)
				.css('height', $(this).height())
				.css('width', $(this).width())
				.addClass('ie-fix-z-index')
			if($.browser.opera) {
				$(this).remove();
				clone.css('background', "url('/assets/img/fix.png')")
			}
				
		})
	}
}

SliderBox = function(speed, container) {
	this.speed = speed;
	this.slides = $('.box', container);
	this.current = 0;
	this.slides.eq(this.current).show();
	var self = this;
	this.interval = setInterval(function() {
		if(self.auto > 10)
		self.right();
	}, 3000);
	this.leftArrow = $('.arrow.left', container).click(function() {
		self.clear();
		self.left();
	});
	this.rightArrow = $('.arrow.right', container).click(function() {
		self.clear();
		self.right();
	});
	this.rollers = $('.rollers span:not(.arrow)', container);
	this.rollers.eq(this.current).addClass('active');
	this.watch();
}
sliding = false;
SliderBox.prototype = {
	auto: 11,
	clear: function() {
		this.auto = 0;
	},
	
	watch: function() {
		var self = this;
		setInterval(function() {
			self.auto += 1;
		}, 1000)
	},
	
	right: function() {
		if(sliding) {
			return;
		}
		sliding = true;
		var next = this.slides.eq(this.current + 1);
		var i = this.current + 1;
		if(next.length == 0) {
			next = this.slides.eq(0);
			i = 0;
		}
		var self =this;
		var current = this.slides.eq(this.current);
		current.hide('slide', {direction: 'left'}, this.speed, function() {next.show('slide', {direction: 'right'}, this.speed); sliding = false; self.roller();});
		this.current = i;
	},
	left: function() {
		if(sliding) {
			return;
		}
		sliding = true;
		var i = 0;
		if(this.current - 1 < 0) {
			i = this.slides.length - 1;
			var prev = this.slides.eq(i);
		} else {
			i = this.current - 1;
			var prev = this.slides.eq(i);
		}
		var self = this;
		var current = this.slides.eq(this.current);
		current.hide('slide', {direction: 'right'}, this.speed, function() {prev.show('slide', {direction: 'left'}, this.speed); sliding = false; self.roller();});
		this.current = i;
	},
	
	roller: function() {
		this.rollers.removeClass('active');
		this.rollers.eq(this.current).addClass('active');
	}
}
