/* notices */

var hoverL = new Image();
hoverL.src = 'img/scroll_left_hover.png';
var hoverR = new Image();
hoverR.src = 'img/scroll_right_hover.png';

var leftmostNotice = 0;
var numberOfNotices = 0;

var padding = 10;
var noticeWidth = 260;

var allowChange = true;
var loop;

$(document).ready(function() {
	$(".noticeContainer").each(function() {
		numberOfNotices++;
		$(this).find(".noticeArticle").each(function() {
			$(this).parent().find(".notice").append('<p><a href="#" class="readMore">Läs mer</a></p>');
		});
	});
	leftmostNotice = numberOfNotices - 3;
	if (leftmostNotice < 0) leftmostNotice = 0;
	displayNotices();
	
	if (numberOfNotices > 3) {
		loop = setTimeout("loopNotices();", 10000);
		$(".scrollLeft").click(function() { switchNotices(-1) }).hover(function() { $(this).addClass('hoverL'); }, function() { $(this).removeClass('hoverL'); });
		$(".scrollRight").click(function() { switchNotices(1) }).hover(function() { $(this).addClass('hoverR'); }, function() { $(this).removeClass('hoverR'); });
	} else {
		allowChange = false;
		$(".scrollLeft, .scrollRight").css('visibility', 'hidden');
	}
	
	$(".readMore").click(function() {
		$(".scrollLeft, .scrollRight, .noticeArea").hide();
		allowChange = false;
		var content = $(this).parents(".noticeContainer").find(".noticeArticle").html();
		$(".notices").css("height", "auto").append('<div class="noticeContent">' + content + '<p><a href="#" class="readLess">Tillbaka</a></p></div>');
		if ($(".notices").outerHeight() < 250)
			$(".notices").height(250);
		$(".noticeContent").slideDown(250);
		
		
		$(".readLess").click(function() {
			$(".noticeContent").hide();
			$(".notices").delay(250).css("height", "250px");
			$(".noticeArea").slideDown(250); 
			$(".scrollLeft, .scrollRight").show();
			setTimeout('$(".noticeContent").detach(); allowChange = true;', 250);
		});
		
	});
	
});

function loopNotices() {
	if (allowChange) {
		animateNotices(-1);
		displayNotices();
	}
	loop = setTimeout("loopNotices();", 7000);
}

//displays 3 notices in sequence
function displayNotices() {
	var localNoticeCount = 0;
	for (var i = leftmostNotice; i <= leftmostNotice + 2; i++) {
		var y = i % numberOfNotices;
		$("#notice"+y).css("left", ($("#notice"+y).width() + padding)*localNoticeCount).show();
		localNoticeCount++;
	}
}

function animateNotices(dir) {
	allowChange = false;
	if (dir > 0) {
		var newRightmostNotice = (leftmostNotice + 3) % numberOfNotices;
		$("#notice"+newRightmostNotice).css("left", ($("#notice"+newRightmostNotice).width() + padding)*3).show();
	} else {
		var newLeftmostNotice = leftmostNotice - 1;
		if (newLeftmostNotice < 0) newLeftmostNotice = numberOfNotices - 1;
		$("#notice"+newLeftmostNotice).css("left", -(noticeWidth+padding)).show();
	}
	var leftStr = '=' + (noticeWidth + padding);
	if (dir > 0)
		leftStr = '-' + leftStr;
	else
		leftStr = '+' + leftStr;
	$(".noticeContainer").animate({
		left: leftStr
	}, 500);
	setTimeout('if (' + dir + ' > 0) { $("#notice"+leftmostNotice).hide(); } else { $("#notice"+((leftmostNotice + 2) % numberOfNotices)).hide(); } leftmostNotice = leftmostNotice + ' + dir + '; if (leftmostNotice < 0) { leftmostNotice = numberOfNotices - 1; } else { leftmostNotice = leftmostNotice % numberOfNotices; } allowChange = true;', 500);
	
}

function switchNotices(dir) {
	if (allowChange) {
		animateNotices(dir);
		displayNotices();
	}
}
