function vote(way, id){
	startload();
	$.get("/vote?"+way+"="+id+"&ajax=true",
	  function(data){
		$("#percent").text(data);
		endload();
	  }
	);
	
}
function showall(id){
	startload();
	$.get("/show/"+id+"/ajax/?ajax=true",
		function(data){
			$("#avsnittbody").html(data);
			endload();
		}
	);
	
}
function startload(){
	$('<div id="loading" style="position: fixed; left: 47%; top: 47%; background:white; border:2px solid black; z-index:100;"><img style="margin:10px;" src="/images/ajax-loader.gif"></div>').prependTo("body");
}
function endload(){
	$("#loading").remove();
}
function slideAndToggle(element){
    $(element).next("div").slideToggle(200);
    $(element).toggleClass("untoggled");
    return false;
}
$(function(){
	if(window.location.hash && window.location.hash.substring(0,2) == "#/"){
		document.location = window.location.hash.substring(1);
	}
    var re = /[0-9]{2,}/;
    if(window.location.hash && window.location.hash.substring(1).match(re)){
        slideAndToggle($(window.location.hash));
        showComments(window.location.hash.substring(1));
        jQuery('#loginreturnto').val(jQuery('#loginreturnto').val() + window.location.hash)
    }
	$(".seach-glass").click(function(){
		$("#search").submit();
	});
	function bigpagingclick(event){
		event.preventDefault();
		startload();
		var called = $(this).attr("href");
		$.get(this.href+ "&ajax=1",'', function(data){
			var count = 0;
			$(".feature-row").remove();
			var current;
			$.each(
				data.objects, 
				function(){
					if(count % 5 == 0){
						current = $("<div class='feature-row'></div>").appendTo(".feature-wrap");
					}
					$(current).append("<div><a href='"+this.link+"'><img alt='" + this.name + "' src='"+this.image+"' /></a><p><a href='"+this.link+"'>"+this.name+"</a></p></div>");
					count++;
				}
				
			);
			$(".feature-row div:last-child").addClass("featured-last");

			if(data.links["<"] == 0){
				$(".bigpaging .left").addClass("disabled");
				$(".bigpaging .left").removeAttr("href");
				$(".bigpaging .left").unbind("click");
			}else{
				$(".bigpaging .left").removeClass("disabled");
				$(".bigpaging .left").attr("href", data.links["<"]);
				$(".bigpaging .left").click(bigpagingclick);
			}
			if(data.links[">"] == 0){
				$(".bigpaging .right").addClass("disabled");
				$(".bigpaging .right").removeAttr("href");
				$(".bigpaging .right").unbind("click");
			}else{
				$(".bigpaging .right").removeClass("disabled");
				$(".bigpaging .right").attr("href", data.links[">"]);
				$(".bigpaging .right").click(bigpagingclick);
			}
			$("#pages ul").remove();
			$("#pages").append(data.paging);
			
			jQuery.bbq.pushState("/" + called);
			
			endload();
			
		},"json");
		
	}
    
	$(".bigpaging a:not(.disabled)").click(bigpagingclick);
    $(".object-link").click(function(event){
        event.preventDefault();
        slideAndToggle($(this));

    });
    $(".object-link:first").next("div").show();
    $(".object-link:first").toggleClass("untoggled");
    
    function showComments(id, a){
        $.get(
            "/kommentarer",
            {"method" : "ajax", "id" : id}, 
            function(data){
                $(".comments:first").html(data);
            }
        );
    }
    $(".commentsinfo").click(function(event){
        event.preventDefault();
        re = /\/kommentarer\?id=([0-9]{2,})/
        showComments(re.exec(this.href)[1])
    });
    
});

