$(document).ready(function(){

    setTimeout("update_topnav_latest(2)",60000);

	update_rm_latest("news");

    $("#rmenu li.show_older a").click(function(event){
      event.preventDefault();
      $(this).parent().hide();
      $("#rmenu ul li.hide").slideDown("fast");
    })
	
	$("#latest_menu a").click(function(event){
      event.preventDefault(); 
	  update_rm_latest($(this).attr("gp_show"))
	});
	
    $("#latest a").click(function(event){
      event.preventDefault(); 
       $(this).toggleClass("bgpos_btm");
       $(this).parent().next().toggle(150)
    });
    

    $("a.mailto").click(function(event){
       event.preventDefault();
       window.location = "mailto:"+$(this).attr("mailto:pre")+"@"+$(this).attr("mailto:suf");
    });
    
     $("a.facebook_share").click(function(event){
        event.preventDefault();
        u=location.href;t=document.title;window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
     });

    $("#captcha_code_new").click(function(event){
         event.preventDefault();
         var newsrc = $("#captcha_code").attr("src")+"&rand="+Math.random();
         $("#captcha_code").attr("src", newsrc);
    })

    $("input.clear_content").focus(function(){
         if($(this).attr("value") == $(this).attr("defvalue")){
            $(this).val("");
         }
    })
    
    $("a.js_toggle_elems").click(function(event){
         event.preventDefault();
         $("."+$(this).attr("elem:class")).toggle();
         
         if($(this).attr("elem:status") == "show"){
              $(this).attr("elem:status", "hide");
              $(this).html($(this).attr("this:show"));
         }else{
              $(this).attr("elem:status", "show");
              $(this).html($(this).attr("this:hide"));
         }

    });

});

function update_topnav_latest(n){

  	$.getJSON("?dyn=topnav_latest", function(articles) {
        $("#topnav #lower").hide().html("Siste: ");
        var itemn = 0;
    	  $.each(articles,function(title,url) { itemn++;
      	   $("<a></a>").attr("href", url).html(title).appendTo('#topnav #lower');
		       if(itemn == 1) $("#topnav #lower").append(" | ");
  		});

      (n > 1) ? $("#topnav #lower").fadeIn(800) : $("#topnav #lower").show()

      var topnav_loop = setTimeout("update_topnav_latest("+n+")", 120000);

  	});

}

function update_rm_latest(rm_latest_type){
    $("#latest_menu a").removeClass("active").filter("."+rm_latest_type).addClass("active");
        var a_class = "";
      	$.getJSON("?dyn=rm_latest&c="+rm_latest_type, function(articles) {
             $("#latest_list").html("");
             $.each(articles,function(title,url) {
    			a_class = a_class == "hot" ? "" : "hot";
          	    $("<li class='"+a_class+"'></li>").html("<a href='"+url+"'>"+title+"</a>").appendTo('#latest_list').slideDown("normal");
      		 });
      	});

}