$(document).ready(function(){
	
	// Removes default search input text on focus, adds on blur
	$("#s").focus(function(){if($(this).val()=="Søkeord"){$(this).val("");}}).blur(function(){if($(this).val()=="Søkeord"||$(this).val()==""){$(this).val("Søkeord");}});
	
	// empty default commentform text on focus
	$("input#author").focus(function(){if($(this).val()=="Ditt navn"){$(this).val("");}}).blur(function(){if($(this).val()=="Ditt navn"||$(this).val()==""){$(this).val("Ditt navn");}});
	$("input#email").focus(function(){if($(this).val()=="Din e-postadresse"){$(this).val("");}}).blur(function(){if($(this).val()=="Din e-postadresse"||$(this).val()==""){$(this).val("Din e-postadresse");}});
	$("input#url").focus(function(){if($(this).val()=="Din nettside (hvis du har)"){$(this).val("");}}).blur(function(){if($(this).val()=="Din nettside (hvis du har)"||$(this).val()==""){$(this).val("Din nettside (hvis du har)");}});
	$("textarea#comment").focus(function(){if($(this).val()=="Din kommentar"){$(this).val("");}}).blur(function(){if($(this).val()=="Din kommentar"||$(this).val()==""){$(this).val("Din kommentar");}});
	
	/* Scroll to top
	$("#scrollTop").click(function(){
		$("body").animate({scrollTop:0}, "slow");
		return false;
	});*/
	
	// Flickr slides (lite)
	$("#flickrWrapper").jCarouselLite({
		btnPrev: "span.previous-button",
		btnNext: "span.next-button",
		auto: 10000,
		circular: true,
		scroll: 5,
		visible: 5
	});
	
	// "Drop down"-meny
	$('#navigation li').hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');
	});
	$('#navigation ul.thirdLevel, #navigation ul.secondLevel').hover(function(){
		$(this).parent('li', this).children('a').addClass('highlight');
	}, function(){
		$(this).parent('li', this).children('a').removeClass('highlight');
	});
	
	// get more youtube videos with ajax
	/*$("#more-videos").get("../", {
		"start": "1",
		"length": "2",
		"path": "../cache/"
	}, function(data));*/
	
	// youtube-karusell
	$("div.youtube-user-videos").cycle({
		fx: "scrollLeft",
		pause: 1,
		timeout: 0,
		pager: "div.youtube-nav"
	});
	
	function externalLinks()
	{
		if (!document.getElementsByTagName) return;
		var anchors = document.getElementsByTagName("a");
		for (var i=0; i<anchors.length; i++)
		{
			var anchor = anchors[i];
			if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") anchor.target = "_blank";
		}
	}
	window.onload = externalLinks;
	
	
	
	
	// navigation through calendars
	var from = 0, step = 5, num = $("#calendarWC ul li").length;
	function showNext(list)
	{
		list
			.find("li").hide().end()
			.find("li:lt(" + (from + step) + "):not(li:lt(" + from + "))")
				.show();
		
		from += step;
		
		if (from > 5)
		{
			$("a#calPrev").show();
		}
		
		if (from == num)
		{
			$("a#calNext").hide();
		}
	}
	function showPrev(list)
	{
		list
			.find("li").hide().end()
			.find("li:lt(" + (from - step) + "):not(li:lt(" + ((from) - (step*2)) + "))")
				.show();
		
		from -= step;
		
		if (from <= 5)
		{
			$("a#calPrev").hide();
		}
		
		if (from < num)
		{
			$("a#calNext").show();
		}
	}
	
	$("a#calPrev").hide();
	
	showNext($("#calendarWC ul"));
	
	$("a#calNext").click(function(e){
		e.preventDefault();
		showNext($("#calendarWC ul"));
	});
	
	$("a#calPrev").click(function(e){
		e.preventDefault();
		showPrev($("#calendarWC ul"));
	});
	
	
	
	
	// Vis flere videoer
	var start = 5, length = 5;
	$("a#moreVideos").click(function(e){
		$.ajax({
			type: "GET",
			url: templateurl + "/simplepie_fetch.php",
			data: "remote=1&start=" + start + "&length=" + length + "&path=" + templatepath,
			success: function(html){
				start = start + length;
				$("div.youtube_video_list").append(html);
			}
		});
		e.preventDefault();
	});
	
});