/* Javascript for http://rasmus.krats.se/ */

function showimg(imgid) {
  $("div.imgzoom").hide();
  $("div#z"+imgid).show();
  return false;
}

function images_init() {
  // Find all relevant images and make magic with their links.
  var imglinks = $("a.imgref");
  prevlnk = '';
  var i = 1;
  var closelnk = '<a class="hide" onclick="$(&quot;div.imgzoom&quot;).hide();return false">close</a>';
  imglinks.each(function() {
    lnk = $(this)

    id = lnk.attr('id');
    tt = lnk.attr('title');
    p = tt.lastIndexOf('[');
    iw = tt.substring(p + 1);
    iw = iw.substring(0, iw.indexOf('\u2715'));
    tt = tt.substring(0, p);
    
    var nextlnk = '';
    if (i < imglinks.length) {
      nextlnk = '<a class="next" onclick="showimg(&quot;'+$(imglinks[i]).attr('id')+'&quot;);return false"><span>next</span></a>';
    }
    // Fix this image link
    var id = lnk.attr('id');
    lnk.click(function(){$("div#z"+id).fadeIn(500);return false;});
    lnk.after('<div class="imgzoom" id="z'+id+'" style="display: none;"><div style="width: '+iw+'px;" >'
                + closelnk + ' ' + prevlnk +  ' ' + nextlnk
               + ' <p class="nofm">(' + i + ' of ' + imglinks.length +')</p> '
               + '<img src="' + lnk.attr("href") + '">'
               + '<p>' + tt + '</p>'
               + '</div></div>');
    prevlnk = '<a class="prev" onclick="showimg(&quot;'+id+'&quot;);return false"><span>prev</span></a>';
    i += 1;
  });
}

// Allow for tiny screens.  If the screen is not tiny, modify image links to
// display the image nicely framed.
$(document).ready(function() {
    var width;
    if (document.documentElement) {
      width = document.documentElement.offsetWidth;
    } else if (window.innerWidth && window.innerHeight) {
      width = window.innerWidth;
    }
    if (width < 480) {
      $("body").addClass("narrowPage");
    } else {
      if (width > 1680) {
        $("body").addClass("widePage");
      } else {
        $("body").addClass("normalPage");
      }        
      images_init();
    }
})

// Load block of comments
$(document).ready(function() {
    var link = $("a#RKomLink");
    if (link.length > 0) {
      var base = link.context.location;
      $("div#RKomBlock").load(base.protocol+"//"+base.host+link.attr("href"));
    }
})
