(function () {
	$.fn.carousel = function () {
		function repeat(str, n) {
			return new Array( n + 1 ).join(str);
		}

        return this.each(function () {
            // magic!
			var $headMask = $('> div', this).css('overflow', 'hidden'),
				$slider = $headMask.find('> ul').width(9999),
				$items = $slider.find('> li'),
				$single = $items.filter(':first')

				singleWidth = $single.outerWidth(),
				visible = Math.ceil($headMask.innerWidth() / singleWidth),
				currentPage = 1,
				pages = Math.ceil($items.length / visible);
                
			/* TASKS */

			// 1. pad the pages with empty element if required
			if ($items.length % visible != 0) {
				// pad
				$slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
				$items = $slider.find('> li');
			}

			// 2. create the carousel padding on left and right (cloned)
			$items.filter(':first').before($items.slice(-visible).clone().addClass('cloned'));
			$items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
			$items = $slider.find('> li');

			// 3. reset scroll
			$headMask.scrollLeft(singleWidth * visible);

			// 4. paging function
			function gotoPage(page) {
				var dir = page < currentPage ? -1 : 1,
					n = Math.abs(currentPage - page),
					left = singleWidth * dir * visible * n;
				
				$headMask.filter(':not(:animated)').animate({
					scrollLeft : '+=' + left
				}, 500, function () {
					// if page == last page - then reset position
					if (page > pages) {
						$headMask.scrollLeft(singleWidth * visible);
						page = 1;
					} else if (page == 0) {
						page = pages;
						$headMask.scrollLeft(singleWidth * visible * pages);
					}
					
					currentPage = page;
				});
			}

			// 5. insert the back and forward link
			$headMask.after('<div class="scroller"><a id="scrollRight" href="#"></a></div><div class="scroller"><a id="scrollLeft" href="#"></a></div>');

			// scroll to next panel
			_nv = document.getElementById('nv');
			function mvBack () {
				var thisPage = (currentPage - 1);
				_a =_nv.getElementsByTagName('a')[Math.abs(thisPage -1)]; //$('#nv a')
				if (thisPage == 0) {
					_a = _nv.getElementsByTagName('a')[pages-1];
				}
				$('#nv a').removeClass('selected');
				$(_a).addClass('selected');
			}

			function mvFwd () {
				_a =_nv.getElementsByTagName('a')[currentPage];
				if (currentPage == pages) {
					_a = _nv.getElementsByTagName('a')[0];
				}
				$('#nv a').removeClass('selected');
				$(_a).addClass('selected');
			}

			// 6. bind the back and forward links
			$('a#scrollLeft', this).click(function () {
				mvBack();		
				gotoPage(currentPage - 1);
				return false;
			});

			$('a#scrollRight', this).click(function () {
				mvFwd();
				gotoPage(currentPage + 1);
				return false;
            });

			$(this).bind('goto', function (event, page) {
				gotoPage(page);
			});
		
			$(this).bind('next', function () {
				mvFwd();
				gotoPage(currentPage + 1);
			});

			// add tabs using rel to navigate
			$('#nv a').click(function() {
				$('#nv a').removeClass('selected');
				$(this).addClass('selected');
				gotoPage(+($(this).attr('rel')));
				return false;
			});

		});
	};
})(jQuery);

$(document).ready(function () {
	var autoscrolling = true;

	function stopscroll() {
		autoscrolling = false;			
	}
	function startscroll() {
		autoscrolling = true;
	}

	$('.headline').carousel().mouseover(function () { stopscroll(); }).mouseout(function () { startscroll(); });
	$('#nv a').mouseover(function () { stopscroll(); }).mouseout(function() { startscroll(); });

	setInterval(function () {
		if (autoscrolling) {
			$('.headline').trigger('next');
		}
	}, 4000);

	$("div.nav ul li ul").parent().append("<span></span>");
	$("div.nav ul li a") .mouseover(function() {
		$(this).parent().find("ul").slideDown('fast').show();
		$(this).parent().hover(function() {
		}, function(){	
			$(this).parent().find("ul").slideUp('medium');
		});
		
		}).hover(function() { 
			$(this).addClass("subhover");
		}, function(){
			$(this).removeClass("subhover");
	});

});
