// JavaScript Document

function getLoop(start, limit, kat) {
	//alert(start+" "+limit+" "+kat);
	$.post("/ajax/getLoop.php", { start: start, limit: limit, kat: kat, utf: 1 }, function(data){
		//$("div#testdiv").fadeIn("slow").before("<p>TEST</p>");
		$("#Lesmer_"+start).slideUp("slow");
		$("div#loop").append(data).show("slow");
		/*
		alert(doScroll);
		if(doScroll == true) {
			$(document).ready(function () {
				var div = $("#ID_"+limit);
				//alert(div);
				var offset = div.offset();
				alert(offset.top);
				$(window).scrollTop(offset.top);
			});
			
		}
		*/
		//alert("Data Loaded: " + data);
	});
}

function hide_(id) {
	$("#"+id).slideUp("slow");
}
function show_(id) {
	$("#"+id).slideDown("slow");
}

function showHide(id) {
	$("#"+id).toggle("slow");
}

function postStats(PostID, TypeID) {
	$.post("/ajax/stats.php", { PostID: PostID, TypeID: TypeID });
}

function lagreForumInnlegg(id) {
	$("#ForumInnleggNyResponse").html('<img src="/grafikk/loading.gif" width="16" height="11" />');
	$.post("/ajax/lagreForumInnlegg.php", $("#ForumInnleggNy").serialize(), function(data) { 
		
		var response = data.split("---");
		
		if(response[0] == 'success') {
			$("#ForumInnleggNyResponse").html(response[1]).fadeIn(700);
			
			getForumInnlegg(response[2], response[3]);
			$("#ForumNavn").val('');
			$("#ForumInnlegg").val('');
			//$("form")[0].reset();
			
			$("#ForumInnleggNyResponse").delay(2500).fadeOut(700);
		}
		else {
			$("#ForumInnleggNyResponse").html(response[1]).fadeIn(700);
		}
	});
}

function getForumInnlegg(EmneID, InnleggID) {
	$.post("/ajax/getForumInnlegg.php", { InnleggID: InnleggID }, function(data) {
		$('#ForumEmne_'+EmneID).append(data).fadeIn(700);
	});
}

function getBedrift(ForeningID) {
	$.post("/ajax/getBedrift.php", { ForeningID: ForeningID }, function(data) {
		$('#visBedrift').html(data).fadeIn(700);
	});
}

function showBedrift(id) {
	$(".showBedrift").slideUp(200).delay(500);
	$("#"+id).slideDown();
	
}

function showImg(PostID, BildeID) {
	$('#Main_'+PostID).hide();
	$('#Small_'+PostID).show();
	$('#XL_'+PostID).show();
	$.post("/ajax/showImg.php", { PostID: PostID, BildeID: BildeID }, function(data) {
		$('#XL_'+PostID).html(data);
	});
}

function showImgNext(PostID, BildeID) {
	//alert(PostID + ' ' + BildeID);
	//var w = $('#XL_'+PostID).width();
	var h = $('#XL_'+PostID).height();
	//alert(h);
	var top = (h/2);
	//alert(top);
	$('#XL_'+PostID).html('<div style="height: '+h+'px;";><div style="padding-top: '+top+'px;"><img src="/grafikk/load.gif" /></div></div>');
	//$('#Main_'+PostID).hide();
	$('#XL_'+PostID).show();
	$.post("/ajax/showImgNext.php", { PostID: PostID, BildeID: BildeID }, function(data) {
		//alert(data);
		var response = data.split("---");
		//alert(response);
		//$('#XL_'+PostID).width(response[0]);
		$('#XL_'+PostID).height(response[1]);
		$('#XL_'+PostID).html(response[2]);
	});
}

function updateImg(id, div, desc) {
	var Beskrivelse = document.getElementById(desc).value;
	$.post("/ajax/updateImg.php", { BildeID: id, Beskrivelse: Beskrivelse }, function(data) {
		$('#'+div).html(data);
	});
}

function deleteImg(id, div) {
	$.post("/ajax/deleteImg.php", { BildeID: id }, function(data) {
		hide(div);
	});
}


function menyMouseOver(div) {
	document.getElementById(div).style.backgroundImage = "url('/grafikk/pil.png')"; 
	document.getElementById(div).style.backgroundPosition = "center right"; 
	
}

function menyMouseOut(div) {
	document.getElementById(div).style.backgroundImage = ""; 
}

$(document).ready(function() {
   /*$('#s3slider').s3Slider({
      timeOut: 3000
   });
	 */
});

$(function(){
	/*Get features working*/
	feature = {
		interval: false
	}

	feature.interval = setInterval(function(){nextFeature()},4000);

	$('.featurecount > li').click(function(){
		i = $(this).index('.featurecount > li');
		$(this).siblings().removeClass('selected').end().addClass('selected');
		$('#featured').animate({left:-675*i},'slow');
		
		clearTimeout(feature.interval);
		feature.interval = setInterval(function(){nextFeature()},4000);
	})
	$('#featured').mouseover(function(){
		clearTimeout(feature.interval);
	}).mouseout(function(){
		feature.interval = setInterval(function(){nextFeature()},4000);
	}).find('li').click(function(){
		window.location = $(this).attr('permalink');
	});
	
});

function nextFeature(){
	i = $('.featurecount .selected').index('.featurecount > li');
	n = i >= 4 ? 0 : i+1;
	
	$('#featured').animate({left:-675*n},'slow');
	
	$('.featurecount > li').removeClass('selected').eq(n).addClass('selected');
}

