var webnav = {
	position:int = 0,
	items:array = null,
	state:boolean = false,
	commentState:boolean = false,
	init:function(){
		if($("#info"))
		{
			$("#info").click(function(){
				webnav.toggleInfo();
				return false;
			});

		}
		if($("#showComments"))
		{
			$("#showComments").click(function(){
				webnav.commentAdd();
				return false;
			});
		}
		if($(".nav_l"))
		{
			$(".nav_l").click(function(){
				webnav.sc_left();
				return false;
			});
		}
		if($(".nav_r"))
		{
			$(".nav_r").click(function(){
				webnav.sc_right();
				return false;
			});
		}
		
		webnav.setTrace();
		webnav.toggleInfo();
	},
	
	setTrace:function(){
		webnav.items = $(".item");
		
		for(var i = 0; i < webnav.items.length; i++)
		{
			if($(".item:eq("+i+")").hasClass("active"))
			{
				webnav.position = i;
				$(".nav_l > a > .nr").html(webnav.position);
				$("#t").animate({"left": -$(".item").outerWidth()*i}, "slow")
			}
		}
		
		$("#t").css("width", webnav.items.length*$(".item").outerWidth());
	},
	
	toggleInfo:function(){
		if(webnav.state) // tracenav is open
		{
			$("#t_wrapper").animate({
				"left": $("#n").width()-$(".nav_r").width()-$(".nav_l").width()-$("#r").width()-$(".item").outerWidth()
			}, 1000);
			$(".nav_l").animate({
				"left": $("#n").width()-$(".nav_r").width()-$(".nav_l").width()-$("#r").width()-$(".item").outerWidth()
			}, 1000);
		}
		else // tracenav is closed
		{
			//alert("öppna");
			$("#t_wrapper").animate({
				"left": 200
			}, 1000);
			$(".nav_l").animate({
				"left": 200
			}, 1000);
		}
		
		if(webnav.state == false)
			webnav.state = true;
		else
			webnav.state = false;
		return false;
	},
	
	toggleComments:function(){
		if(!webnav.commentState) // comments are closed
		{
			$("#i").animate({
				"height": 200
			}, 200);
			$("body").animate({
				"paddingTop": 200
			}, 200);
		}
		else
		{
			$("#i").animate({
				"height": 40
			}, 200);
			$("body").animate({
				"paddingTop": 40
			}, 200);
		}
		
		if(webnav.commentState == false)
			webnav.commentState = true;
		else
			webnav.commentState = false;	
		return false;
	},
	
	commentAdd:function(){
		$("#showComments").unbind("click");
		$("<div id=\"postComment\" class=\"ajaxtopslide darkload\"></div>").prependTo("#cc");
		
		var linkid = $("#linkid").attr("value");
		$.get("/ajax/comment?link="+linkid, function(data){
			$("#postComment").append(data);
			$("#postComment").removeClass("darkload");
			
			$("dt").hide();
			$("#sendForm").click(webnav.commentPost);
		});
		$("#showComments").click(webnav.toggleComments);
		webnav.toggleComments();
		return false;
	},
	
	commentPost:function(){
		var linkcomment = $("#linkcommentText").attr("value");
		var link = $("#linkid").attr("value");
		
		$("#postComment").empty();
		$("#postComment").append("<h2>Skickar din kommentar.<h2>");
		
		if (linkcomment.length)
		{
			$.ajax({
				type: "POST",
				url: "/ajax/addcomment",
				data: "submit=submit&linkcommentText="+linkcomment+"&link="+link,
				success: function(){
					webnav.commentPosted();
				}
			});
		}
		return false;
	},
	commentPosted:function(){
		$("#postComment").empty();
		$("#postComment").append("<h2>Din kommentar sparades.<h2>");
	},
	
	sc_left:function(){
		if(webnav.position <= 0)
			return;
			
		webnav.position--;
		$(".nav_l > a > .nr").html(webnav.position);
		
		var visibleitems = Math.round($("#t_wrapper").width()/$(".item").width());
		if(webnav.items.length - webnav.position - visibleitems >= 0)
			$(".nav_r > a > .nr").html(webnav.items.length - webnav.position - visibleitems);
		//$(".nav_r > a > .nr").html(webnav.items.length - webnav.position);
		
		
		$("#t").animate({"left": "+="+$(".item").width()}, "slow");
	},
	
	sc_right:function(){
		if(webnav.position >= webnav.items.length-1)
			return;
		
		webnav.position++;
		$(".nav_l > a > .nr").html(webnav.position);
		var visibleitems = Math.round($("#t_wrapper").width()/$(".item").width());
		if(webnav.items.length - webnav.position - visibleitems >= 0)
			$(".nav_r > a > .nr").html(webnav.items.length - webnav.position - visibleitems);
		
		$("#t").animate({"left": "-="+$(".item").width()}, "slow");
	}
}

//window.onload = webnav.init;
$(document).ready(function () {
    webnav.init();
});
