$(document).ready(function(){

   // Thumbnail click
   
   $("#image_gallery_table a").click(function(event){
      event.preventDefault();
      show_image($(this).attr("viewi"));
   });
   
  // Open one by default
  
  if($("#screenshot_preview").attr("open_first")){
    show_image($("#screenshot_preview").attr("open_first"));
  }else{ 
    $("#image_gallery_table a:last").click();
  }
  
  // Navigate thumbs
  
  $("#images a.showimages").click(function(event){
      event.preventDefault();
      var id = $(this).parent().attr("version_id");
      //var version_id = $("#imagenav").attr("version_id");
      show_image_thumb(id, 0);
  })
  
  $("#images a.togglecover").click(function(event){
      event.preventDefault();
      $("#images #togglecover").toggle();
  });
  
  $("#togglecover a").click(function(event){
     event.preventDefault();
     $("#cover").attr("src", $(this).attr("href")+".jpg");
     $("#image_thumb").attr("href",$(this).attr("id"));
  });
  
  //
  
});


// Show Screenshot Image Preview
function show_image(i){

   var img_height = $("#screenshot_preview img").attr("height")
  // $("#screenshot_preview .image").html("<div class='loading' style='height:"+(img_height ? img_height : 200)+"px'>&nbsp;</div>");
   
   $("#screenshot_preview").load("?dyn=product_imgview&viewi="+i, function(){
         
         $(".nav a").click(function(event){
           event.preventDefault();
           show_image($(this).attr("viewi"));
         });
         
         //$("a#full_size_image").click(function(event){ event.preventDefault(); alert(10); })
         
         $("a#full_size_image").lightBox({imageLoading:'design/new/product_page/image_loading.gif'});
  
    })
}

var current_image = 1;

// Show Thumbnail Image
function show_image_thumb(version_id, image_id){

    $.getJSON("?dyn=product_image_thumbs_json&vid="+version_id+"&i="+image_id, function(image) {

           
           
           $("#image_thumb").attr("href", "?pg=product&id="+version_id+"&sub=images&viewi="+image_id);// image_id
           $("#image_thumb img").hide().attr("src", image.current_image.src).fadeIn(200);
           $("#imagenav .wrapper").html("");
           
           $("<span></span>").addClass("imageof").appendTo("#imagenav .wrapper").html(current_image+"/"+image.total_images).show("slow");
           
           if(image.next_image){
              $("<a href=\"#nowhere\"></a>").html("Neste bilde »").attr("title", image.next_image.id).addClass("thumbnext").appendTo("#imagenav .wrapper").show("slow");    
           }

           if(image.prev_image){
              $("<a href=\"#nowhere\"></a>").html("« Forrige bilde").attr("title", image.prev_image.id).addClass("thumbprev").appendTo("#imagenav .wrapper").show("slow");        
           }

           $("#imagenav a").click(function(event){
              event.preventDefault();
              show_image_thumb(version_id, $(this).attr("title"));
              if(parseInt($(this).attr("title"))>=image_id){current_image++}else{current_image--}
           })
           
        
    });
       
}

