var articleIx = 0;
var spotlightInterupt = false;
var spotlightTime = null;

$(function(){

	initWaterSplitter();
	
	/* Retreive spotlight function */
	var $spotlight = $("#Spotlight").parent();
	    $spotlight.css("overflow","hidden");
	
	var $spotlight_items = $(".spotlight_item", $spotlight);
	var num_items = $spotlight_items.length;
	
	/* If we have items */
	if(num_items > 0) {
		
		var $spotcounter = $("#browse_articles");
		var miwidth = 0;
		
		/* Loop through articles */
		for(var i=1;i<=num_items;i++) {
			var $d = $("<div/>");
			$d.addClass("spot_plutt");
			if(i==1)
				$d.addClass("active");

			$d.bind("click", rotateArticleClick);
			$spotcounter.append($d);
			miwidth += 20;
		}
		
		/* Set width of plupps */
		$("#browse_articles").css({ width: miwidth+"px" });
		
		spotlightTime = setTimeout(rotateArticles, 5000);
	}
	
	$(".startpage_news_item").hover(function(){
		$(this).css("background-position","left bottom");
	},function(){
		$(this).css("background-position","left top");
	});
	
	$("#arrow_left").bind("click", scrollNews);
	$("#arrow_right").bind("click", scrollNews);
	
	$(".newsfilter a").bind("click", function(){
		alert($(this).text());
		
		return false;
	});
	
	$("#myteam_selects .select_blurb").bind("click", switchMyTeam);
	loadMyTeam();
});

function loadMyTeam() {
	var cookie = $.cookie("osk_startpage");
	if(!cookie) return;

	var r = cookie.split(",");

	for(var i=0;i<r.length;i++) {
		$(".select_blurb:eq("+ r[i] +")").addClass("inactive");
		$(".myteam_wrapper > div:eq("+ r[i] +")").hide();
	}
}

function switchMyTeam() {
	var $s = $(this);
	var $parent = $("#myteam_selects");
	var $myteam = $(".myteam_wrapper");
	
	var my_ix = $(".select_blurb", $parent).index($s);
	var $blurb = $("> div:eq("+ my_ix +")", $myteam);
	
	if($s.hasClass("inactive")) {
		$s.removeClass("inactive");
		$blurb.fadeIn(400);
	} else {
		$s.addClass("inactive");
		$blurb.fadeOut(400);
	}
	
	saveCookie();
}

function saveCookie() {
	var cname = "osk_startpage";
	var saveToCookie = "";
	
	$("#myteam_selects .select_blurb").each(function(){
		if($(this).hasClass("inactive")) {
			saveToCookie += ""+$(".select_blurb").index($(this))+",";
		}
	});
	
	saveToCookie = saveToCookie.substr(0,saveToCookie.length - 1);
	$.cookie(cname, saveToCookie, { expires: 365 });
}

var newsix = 0;
var steps = 3;
function scrollNews() {
	var myid = $(this).attr("id");
	var max = $(".startpage_news_item").length;
	if(myid=="arrow_left") {
		newsix-=steps;
		if(newsix < 0) newsix = 0;
	} else {
		newsix+=steps;
		if(newsix >= max) newsix = max-steps;
	}
	$("#startpage_news_holder").scrollTo($(".startpage_news_item:eq("+ newsix +")"),500);
}

function rotateArticleClick() {
	var thisis = $(".spot_plutt").index($(this));
	spotlightInterupt = true;
	clearTimeout(spotlightTime);
	
	var currIx = thisis;
	
	$(".spotlight_item:visible").fadeOut(300, function(){
		$(".spotlight_item:eq("+ currIx +")").fadeIn(300);
	});
	
	$(".spot_plutt").removeClass("active");
	$(this).addClass("active");
}

function rotateArticles() {
	articleIx++;
	
	if(articleIx >= $(".spotlight_item").length) {
		articleIx = 0;
	}
	
	$(".spotlight_item:visible").fadeOut(800, function(){
		$(".spotlight_item:eq("+ articleIx +")").fadeIn(800);
		$(".spot_plutt").removeClass("active");
		$(".spot_plutt:eq("+ articleIx +")").addClass("active");
	});
	
	if(!spotlightInterupt)
		spotlightTime= setTimeout(rotateArticles, 5000);	
}

function closeWater() {
	$("#watersplitter").animate({ height: 0 }, 500, function(){
		$("#watersplitter").remove();
	});
	
	$.cookie("watersplit", $("input[name='forceOpen']").val(), { expires: 365 });
}

function initWaterSplitter() {
	var forceOpen = $("input[name='forceOpen']").val();
	if($.cookie("watersplit") != forceOpen) {
		var winw = $(window).width();
		var winh = $(window).height();
	
		$(window).bind("resize", function(){
			winw = $(window).width();
			winh = $(window).height();
			$("#watersplitter").css({ width: winw+"px", height: winh+"px" });
		});
	
		$.get("/4.2595a92612f8a8de348800025873.Mittenspalt.html", function(callback){
			$("#watersplitter")
				.html(callback)
				.css( {"width": winw+"px", backgroundColor: "#000" })
				.animate({ height: winh +"px" }, 400);
				$.cookie("watersplit", $("input[name='forceOpen']").val(), { expires: 365 });
		});
	}
	
}

var totalw = 0;
var totall = 0;

$(function(){
	$("#teamInnerwrapper > .teamPartner").each(function(){
		totalw = totalw + 200;
		totall++;
	});
	setTimeout(function(){ moveleft(); }, 800);
});

function moveleft() {
	$("#teamInnerwrapper").animate({ left: "-"+ totalw +"px" }, totall * 2500, "linear", function(){
		$(this).css("left", "0");
		moveleft();
	});
}

