$(document).ready(function(){

     video_id = $("#webtv_video").attr("video_id");

     /*
        Media Playlist tab
     */

     $("#more_content ul li a").click(function(event){
     
          event.preventDefault();
          
          clicked_link = $(this);
     
          // Mark this tab as active
          
          $("#more_content ul li").removeClass("active");
          
          clicked_link.parent().addClass("active");
          
          // GET-data to the JSON provider

          media_type = clicked_link.attr("class");
          pagenumber = clicked_link.attr("title");

          json_fetch_url = "?dyn=webtv_related_media&media_type="+media_type+"&page="+pagenumber+"&this_vid="+video_id;
          
          // Get results
          
          $.getJSON(json_fetch_url, function(media) {
               
               $("#more_content .view_content").html("");
               
               // Loop results
                
               $.each(media.videos,function(id,details) {

                    video_href = "?pg=webtv&id="+details.id;
                    this_id = "mr_"+id;

                    $("<div></div>").addClass("media_row").hide().attr("id", this_id).appendTo("#more_content .view_content").fadeIn(300);

                    $("<a></a>").attr("href",video_href).appendTo("#"+this_id).html($("<img/>").attr("alt", details.title).attr("src", "images/webtv/230x78/"+details.image));

                    $("<a></a>").attr("href",video_href).appendTo("#"+this_id).html(details.title);

                    $("#"+this_id).append(details.duration + " / " + details.added);


               });

               $(".mc_header ul li.pagenav").remove();

               // Paging through the results

               if(media.results > 3){

                    if(media.current_page < media.total_pages){
                         $("<li></li>").addClass("pagenav").appendTo(".mc_header ul").html("<a href='' title='"+(parseInt(media.current_page)+1)+"'>+</a>");
                    }

                    if(media.current_page > 1){
                         $("<li></li>").addClass("pagenav").appendTo(".mc_header ul").html("<a href='' title='"+(parseInt(media.current_page)-1)+"'>-</a>");
                    }

                    pagestatus_html = pagenumber > 1 ? ((media.total_pages < 10) ? media.current_page + "/" + media.total_pages: media.current_page) : "";

                    $("<li></li>").addClass("pagenav").addClass("summary").appendTo(".mc_header ul").html(pagestatus_html).addClass("end");
               }

               // Close tab if no content

               if(media.results == 0){
                  clicked_link.parent().hide();
                  $("#more_content ul li a.newest").click();
               }

               // Moving to another page?

               $(".mc_header ul li.pagenav a").click(function(event){
                    event.preventDefault();
                    clicked_link.attr("title", $(this).attr("title"));
                    clicked_link.click();
               })

          });   

     })

     /*
      Adding new comment?
     */

     $("#add_new_comment").submit(function(event){
          event.preventDefault();
          
          form = $(this);
          
          $.post("?dyn=webtv_add_comment", { video_id:video_id, message: $(this).children("textarea").val() }, function(returned){
               
               if(returned == "success"){
               
                    $("#add_comment_error").hide().html("");
                    $("#add_comment_error_toforum").hide();
                    form.children("textarea").val("")
                    $("#add_new_comment").hide();
                    $("#add_new_comment_strong").html("Ditt innlegg ble lagt til!");
                    update_comments();
                    
               }else{
               
                    $("#comment_be_first").hide();
                    $("#rate_or_share").hide();
                    $("#add_new_comment_strong").html("Det oppstod et problem!");
                    $("#add_comment_error").show().html(returned);
                    $("#add_comment_error_toforum").show();
               
               }
               
          });

     });
     
     
     $("#more_content ul li:first a").click();
     
     /*
     // Sharing of video
     */
     
     $("#webtv_video a.community_expand").click(function(event){
        event.preventDefault();
        $("#video_community").toggle("show");
     }).toggle(function(){
        $("#video_details a.community_expand").css("background-position", "bottom")
     }, function(){
        $("#video_details a.community_expand").css("background-position", "top")
     });
     

     /*
     // Star rating
     */
     
     $("ul.star_rtn a").click(function(event){
        event.preventDefault();

        $.post("?dyn=webtv_rate_video", { stars: $(this).attr("stars"), vid: video_id  },
           function(data){
                $("#video_community .rtn_response").show("slow").html(data);
           });

     
     })
     
    update_comments();

});


/*
 Update comments
*/

function update_comments(){

     $("#latest_comment").html("Henter kommentarer på nytt ..."); 
    
     $.getJSON("?dyn=webtv_comments&vid="+video_id, function(comments) {
                              
          $("#latest_comment").hide().html("");
          
          $.each(comments.comments,function(id,comment) {
           
               if(id < 1){
          
                    author_link = $("<a></a>").attr("href","?pg=wievprofile&id="+comment.poster_id).html(comment.poster);
                    details = $("<span></span>").addClass("details").html(comment.added);
                    
                    $("<div></div>").addClass("c_row").attr("id", "comment_"+id).appendTo("#latest_comment");
                    
                    $("<div></div>").addClass("info").html(details).append(author_link).appendTo("#comment_"+id);
               
                    $("<div></div>").addClass("message").html(comment.message).appendTo("#comment_"+id);
                    
               }
          
          });
          
          if(parseInt(comments.results)>0){
               
               $("#latest_comment").show();
               $("#comments_strong").show().html("Siste kommentar (totalt "+comments.results+")");
               $("#add_new_comment textarea").css("height", "40px");
               $("#comment_be_first").html("")
               
               $("<a></a>").html("Kommentartråd i forumet").addClass("forum").attr("href", "?pg=forum&viewtopic="+comments.topic_id).appendTo("#latest_comment");
               
          }
     
     });
     
}