$ = $svjq.noConflict();

$(function(){
	initDebug();
	
	if($(".kommun_rotator").is("*"))
		initKommunRotator("/4.36cdf86c1262715c047800011498.Kommuner.portlet");
		
	if($(".logo_bottom").is("*"))
		initLogosbottom();
})

function initLogosbottom()
{
	$(".logo_bottom > div")
		.css("width","2000em");
	
	$(".logo_bottom > div > div")
		.css("width","1000px")
		.css("float","left");
	
	setTimeout("_logoScroll()", 5500);
}

var _logo_ix = 1;

function _logoScroll()
{
	if(_logo_ix == 1)
		_logo_ix = 2;
	else
		_logo_ix = 1;
	
	$(".logo_bottom").scrollTo(
		$(".logo_bottom > div > div:nth-child("+ _logo_ix +")"), 
		1500, 
		{
			easing: "linear"
		}
	);
	
	setTimeout("_logoScroll()", 5500);
}

function initKommunRotator(url)
{
	$(".kommun_rotator")
		.css("overflow","hidden")
		.css("width","210px")
		.css("height","175px")
		.css("float","left");
	
	var $rotator = $("<div/>");
	$rotator
		.css("float","left")
		.css("width","20000em")
		.css("position","relative");
	
	var $parent = $(".kommun_rotator").parent("div");
	
	var $left = $("div:first", $parent);
	var $right = $("div:nth-child(3)", $parent);
	
	$("img", $left).wrap("<a href='javascript:void(0);' onclick='_spinnerLeft()'></a>");
	$("img", $right).wrap("<a href='javascript:void(0);' onclick='_spinnerRight()'></a>");
	
	$left.css("z-index","500");
	$right.css("z-index","500");
	
	$.get(url, function(callback){
		$("a", callback).each(function(){
			if($(this).attr("href"))
			{
				var $a = $(this);
				$a
					.css("float","left")
					.css("display","block")
					.css("width","210px")
					.css("text-align","center");

				$rotator.append($(this));
			}
		});
	});
	
	$(".kommun_rotator").prepend($rotator);
	
	startRotate();
}

function startRotate()
{
	debug("Start: rotate");
	setTimeout("_spinnerRotate('.kommun_rotator > div')", 4000);
}

var _spin_ix = 1;
var userInterupt = false;

function _spinnerRotate(holder)
{
	if(userInterupt)
		return false;
	
	_spin_ix++;
	
	var $holder = $(holder);
	var $next = $("a:nth-child("+ _spin_ix +")", $holder);
	
	if(!$next.is("*"))
	{
		$next = $("a:nth-child(1)", $holder);
		_spin_ix = 1;
	}

	$holder.parent().scrollTo(
		$next, 
		1500, 
		{
			easing: "linear",
			onAfter: function(){ 
				setTimeout("_spinnerRotate('"+ holder +"')", 4000); 
			}
		}
	);
}

function _spinnerLeft()
{	
	var $holder = $(".kommun_rotator > div");
	
	if(_spin_ix > 1)
	{
		_spin_ix--;
	}
	else
	{
		_spin_ix = $holder.children().size();
	}
	
	var $next = $("a:nth-child("+ _spin_ix +")", $holder);

	$holder.parent().scrollTo(
		$next, 
		500, 
		{
			easing: "linear"
		}
	);
		
	userInterupt = true;
	
}

function _spinnerRight()
{
	var $holder = $(".kommun_rotator > div");
	var len = $(".kommun_rotator > div").children().size();
	
	if(_spin_ix < len)
	{
		_spin_ix++;
	}
	else
	{
		_spin_ix = 1;
	}
	
	var $next = $("a:nth-child("+ _spin_ix +")", $holder);

	$holder.parent().scrollTo(
		$next, 
		500, 
		{
			easing: "linear"
		}
	);
		
	userInterupt = true;
}

function initDebug()
{
	var dbugdiv = $("<div/>")
	.css("position","fixed")
	.css("left",0)
	.css("top", 0)
	.css("padding","1em")
	.attr("id","debug");
	
	//$("body").prepend(dbugdiv);
}

function debug(str)
{
	//$("div#debug").append("<div>"+ str +"</div>");
}