(function($)
{
	$.fn.amArtikelen = function(onMouseOver, onClick, doRotate, rotateTime) {
		return this.each(function() {
			var hover = false;
			$(this).mouseover(function(){
				hover = true;
			});
			$(this).mouseout(function(){
				hover = false;
			});
			var menu = $(this).find('ul>li');
			var children = $(this).children('div');
			var selectedIndex = 0;
			var activateItem = function(item){
				$this = $(item);
				menu.removeClass("selected");
				children.removeClass("selected");
				selectedIndex = $this.index();
				$this.addClass("selected");
				$(children[ selectedIndex	]).addClass("selected");				
			};
			
			if(onMouseOver)
			{
				menu.mouseover(function(){ activateItem(this); });
			}
			if(onClick)
			{
				menu.click(function(){ activateItem(this); });
			}
			if(menu.length > 0)
				activateItem(menu[0]);
			var rotate = null;
			rotate = function(init){
				if(!hover)
				{
					var nextIdx = selectedIndex+1;
					if(nextIdx > menu.length-1)
					{
						nextIdx = 0;
					}
					if (!init)
						activateItem(menu[nextIdx]);
				}
				setTimeout(rotate, rotateTime);
			};
			if(doRotate)
				rotate(true);
		});
	};
	$(function(){
		// initialize all media containers on pageload.
		// onhover,onclick, rotate, rotateTime
		$('div.artikeloverview.large.multi').amArtikelen(false,true,false,6000);
	});
})($);

