(function() {
	$.fn.glider = function(settings) {
		settings = $.extend({
			time: 	1000,
			scrolling: function() {},
			callback: function() {},
			repeat: 'yes'
		}, settings);
		
		return this.each(function() {
			var o = $(this);
			var ul = o.find('ul');
			var items = ul.find('li');
			
			var width, css, prop = {}, prev = 'left', next = 'right';
			
			if (settings.top) {
				width = items.outerHeight(true) + 2;
				css = 'margin-top';
				prev = 'top'; 
				next = 'bottom';
			} else {
				width = items.outerWidth(true);
				css = 'margin-left';
			}
			
			
			
			
			var n = items.length;
			
			if (settings.repeat == 'yes') {
				var text = ul.html();
				text += text;
				text += text;
				text += text;
				text += text;
				ul.html(text);
		
				ul.css(css, -n * width);
				
				
			} else {
				settings[prev].css('visibility', 'hidden');
			}
		
				
			settings[prev].click(function() {
				if (ul.is(':animated')) return false;
				var left = parseInt(ul.css(css));
				settings.scrolling(ul);
				prop[css] = '+=' + width;
				ul.animate(prop, settings.time, function() {
					if (settings.repeat == 'no') {
						left = parseInt(ul.css(css));
						if (left  >= 0) settings[prev].css('visibility', 'hidden');
						else settings[prev].css('visibility', 'visible');
						if (left - o.height() <= -n * width) settings[next].css('visibility', 'hidden');
						else settings[next].css('visibility', 'visible');
					} else {
						if (left == -width) ul.css(css, -n * width);	
					}
					settings.callback(ul);
				});
				return false;
			});
			
			settings[next].click(function() {
				if (ul.is(':animated')) return false;
				var left = parseInt(ul.css(css));
				settings.scrolling(ul);
				prop[css] = '-=' + width;
				ul.animate(prop, settings.time, function() {	
					
					if (settings.repeat == 'no') {
						left = parseInt(ul.css(css));
						if (left  >= 0) settings[prev].css('visibility', 'hidden');
						else settings[prev].css('visibility', 'visible');
						if (left - o.height() <= -n * width) settings[next].css('visibility', 'hidden');
						else settings[next].css('visibility', 'visible');
					} else {
						if (left == -2*width*n + width) ul.css(css, -n * width);	
					}
					settings.callback(ul);
				});
				return false;
			});
			
			settings.callback(ul);
		});
	}
})(jQuery);

$(window).load(function() {
	$('#glider1').glider({
		left: $('#left1'),
		right: $('#right1')
	});
	
	$('#glider2').glider({
		left: $('#left2'),
		right: $('#right2')
	});
	
	$('#glider3').glider({
		left: $('#left3'),
		right: $('#right3')
	});
	
	$('#glider3 li').click(function() {
		var name = $(this).attr('title');
		if (name=='') $('#label div').hide();
		else $('#label div').show().html(name);
	}).eq(0).click();
	
	$('#news').glider({
		top: $('div.to_top'),
		bottom: $('div.to_bot'),
		repeat: 'no',
		time: 200
	});
	
	var links = $('#news a');
	
	links.click(function() {
		links.removeClass('cur');
		var o = $(this);
		o.addClass('cur');
		$('#news_body').load(o.attr('href'));
		return false;
	}).eq(0).click();
	
	$('#glider4').glider({
		left: $('#left4'),
		right: $('#right4')
	});
});
