var rotationduration = 6000;
var userInterupted = false;
var time = null;

function initpbildspel() {
	var images = $(".imagerotator > div > div:not(:first)");
	var cIndex = 0;
	var userInter = false;
	var timeOut = null;
	
	$(".imagerotator .imagerotator-text").each(function() {
		var me = $(this);
		var w = me.width();
				
		me.css('margin-left', 0 - (w / 2));
	});
	
	var gotoPrev = function() {
		var current = images.eq(cIndex);
		
		cIndex--;
		if (cIndex == -1) {
			cIndex = images.length - 1;
		}
		
		var next = images.eq(cIndex);
		next.css({'z-index': 30});
		current.css('z-index', 29);
		
		next.fadeIn('2000', function() {
			current.hide();
		});
	};
	
	var gotoNext = function() {
		var current = images.eq(cIndex);
		
		cIndex++;
		if (cIndex == images.length) {
			cIndex = 0;
		}
		
		var next = images.eq(cIndex);
		next.css({'z-index': 30});
		current.css('z-index', 29);
		
		next.fadeIn('2000', function() {
			current.hide();
			
			if(!userInter) {
				timeOut = setTimeout(gotoNext, rotationduration);
			}
		});
	};
	
	if (images.length > 1) {
		images.css({
			"position": "absolute",
			"left": 0,
			"top": 0
		}).hide();
		
		images.eq(cIndex).show();
		
		$(".imagerotate_right").bind("click", function(){
			userInter = true;
			clearTimeout(timeOut);
			gotoNext();
		});
		
		$(".imagerotate_left").bind("click", function(){
			userInter = true;
			clearTimeout(timeOut);
			gotoPrev();
		}).parent().css('z-index', 40);
		
		timeOut = setTimeout(gotoNext, rotationduration);
	}
}

$(window).load(function() {
	initpbildspel();
	return;
	/*var cook = readCookie('pbildspel');
	if (cook) {
		initpbildspel();
	} else {
		if ($(".imagerotator > div > div").length > 1) {
			$(".imagerotator .imagerotator-text").each(function() {
				var me = $(this);
				var w = me.width();
				
				me.css('margin-left', 0 - (w / 2));
			});
		}
		
		if($(".imagerotator > div > div:not(:first)").length > 1) {
			$(".imagerotator > div > div:not(:first)").css("position","absolute").css("left", 0).hide();
			$(".imagerotator > div > div:not(:first)").first().show();
			
			$(".imagerotate_right").bind("click", function(){
				userInterupted = true;
				clearTimeout(time);
				rotateImages();
			});
			$(".imagerotate_left").bind("click", function(){
				userInterupted = true;
				clearTimeout(time);
				rotateImagesReverse();
			});
		
			time = setTimeout("rotateImages()", rotationduration);
		}
	}*/
});
/*
function rotateImages() {
	var current = $(".imagerotator > div > div:not(:first)").first();
	var next = $(".imagerotator > div > div:not(:first)").eq(1);
	var last = $(".imagerotator > div > div:not(:first)").last();
	
	next.fadeIn("2000", function(){
		current.hide();
		current.insertAfter(last);
		if(!userInterupted) {
			time = setTimeout("rotateImages()", rotationduration);
		}
	});
}

function rotateImagesReverse() {
	var current = $(".imagerotator > div > div:not(:first)").first();
	var next = $(".imagerotator > div > div:not(:first)").last();
	var last = $(".imagerotator > div > div:not(:first)").last();
	
	next.insertAfter(current).fadeIn("2000", function(){
		current.hide();
		current.insertAfter(last);
		if(!userInterupted) {
			time = setTimeout("rotateImages()", rotationduration);
		}
	});
}

function trace(str) {
	try {
		console.log(str);
	} catch(e) {
	}
}*/
