$(function(){
	$.fn.newsSlide = function(options){
		var defaults = {
			time: 5000,
			height: 138
		}

		var options = $.extend(defaults, options);

		return this.each(function(){
			var $this = $(this);
			var obj = this;
			var $ul = $this.find('ul');
			var current = 0;
			var interval = null;

			this.init = function(){
				$ul.css({top:0});

				$this.find('a.next').unbind('click').click(function(){
					obj.next(true);
					return false;
				})
				$this.find('a.prev').unbind('click').click(function(){
					obj.prev(true);
					return false;
				})

				if($ul.children('li').length > 1)
					this.initInterval();
				$this.find('span.nr').text(1);
				$this.hover(
					function(){$(this).addClass('hover')},
					function(){$(this).removeClass('hover');}
				);
			}

			this.next = function(force){
				if(force || !$(obj).hasClass('hover')){
					this.setCurrent(current+1, 'next');
					this.initInterval();
				}
			}

			this.prev = function(){
				if(force || !$(obj).hasClass('hover')){
					this.setCurrent(current-1, 'prev');
					this.initInterval();
				}
			}

			this.setCurrent = function(nr, how){
				var top = nr == 0 ? 0 : -options.height;
				if(how == 'prev'){
					top = 0;
					$ul.find('li:last').prependTo($ul);
					$ul.css('top', -options.height);
				}
				$ul.animate({top: top}, function(){
					if(how == 'next'){
						$ul.find('li:first').appendTo($ul);
						$ul.css('top', 0);
					}
				});
				current = how == 'prev' ? (current - 1 >= 0 ? current - 1 : $ul.find('li').length-1) : (current + 1 < $ul.find('li').length ? current + 1 : 0);
				$this.find('span.nr').text(current + 1);
			}

			this.initInterval = function(){
				if(interval) clearInterval(interval);
				interval = setInterval(function(){
					obj.next(false);
				}, options.time);
			}

			this.init();
		})
	}

	if($.browser.msie){
		$('body').addClass('ie');
		$('body').addClass('ie' + parseInt($.browser.version));
		$('a.button').hover(
				function(){$(this).addClass('hover')},
				function(){$(this).removeClass('hover')}
		)
	}

	$('#menu li').hover(
		function(){
			$(this).addClass('hover');
			$(this).find('ul').slideDown('fast');
		},
		function(){
			$(this).removeClass('hover');
			$(this).find('ul').slideUp('fast');
		}
	)
	$('#menu > li > ul').each(function(){$(this).find('li:last').addClass('last');})

	$('input.tick, textarea.tick').each(function(){
		var $this = $(this);
		$this.attr("rel", $this.val());
		$this.click(function(){
			var $this = $(this);
			if($this.val() == $this.attr("rel")){
				$this.val('');
			}
		})
		$this.blur(function(){
			var $this = $(this);
			if($this.val() == ''){
				$this.val($this.attr("rel"));
			}
		})
	})

	$('a.submit').click(function(){$(this).closest('form').submit()})

	$('.news-slide').newsSlide();
});
