/* init namespace */
var PicasaHS = {};

$(document).ready(function() {
  
  /* try to find gallery div */
  var div = $("#picasa_hs_gallery");
  
  if (div.attr("username"))
    PicasaHS.username = div.attr("username");
  if (div.attr("albumName"))
    PicasaHS.albumName = div.attr("albumName");
  if (div.attr("thumbnailSize"))
    PicasaHS.thumbnailSize = div.attr("thumbnailSize");
  if (div.attr("imageSize"))
    PicasaHS.imageSize = div.attr("imageSize");
  
  /* if image size not defined, try define it from window width */
  if (!PicasaHS.imageSize) {
    var available_sizes = [ 94, 110, 128, 200, 220, 288, 320, 400, 512, 576, 640, 720, 800, 912, 1024, 1152, 1280,
        1440, 1600 ];
    var size = PicasaHS.getWindowHeight();
    /* first of all, select maximum size */
    PicasaHS.imageSize = 1600;
    /* search for minimum size that greater than window height */
    for ( var i = 0; i < available_sizes.length; i++)
      if (available_sizes[i] > size) {
        PicasaHS.imageSize = available_sizes[i];
        break;
      }
  }
  
  /* album name can be written in div body */
  if (!PicasaHS.albumName)
    PicasaHS.albumName = div.html();
  
  /* if username and albumName defined, try to load photos */
  if (PicasaHS.username && PicasaHS.albumName)
    PicasaHS.loadAlbumPhotos(div, PicasaHS.username, PicasaHS.albumName);
  
});

PicasaHS.getWindowHeight = function() {
  var frameHeight = 640;
  if (self.innerHeight)
    frameHeight = self.innerHeight;
  else if (document.documentElement && document.documentElement.clientHeight)
    frameHeight = document.documentElement.clientHeight;
  else if (document.body)
    frameHeight = document.body.clientHeight;
  return frameHeight;
}

PicasaHS.getWindowWidth = function() {
  var frameWidth = 800;
  if (self.innerWidth)
    frameWidth = self.innerWidth;
  else if (document.documentElement && document.documentElement.clientWidth)
    frameWidth = document.documentElement.clientWidth;
  else if (document.body)
    frameWidth = document.body.clientWidth;
  return frameWidth;
}

/* This function init a request to picasa server */
PicasaHS.loadAlbumPhotos = function(div, username, albumName) {
  PicasaHS.galleryDiv = div;
  $.getScript("http://picasaweb.google.com/data/feed/api/user/" + username + "/album/" + albumName
      + "?fields=entry(content(@type),title,summary,"
      + "media:group/media:thumbnail, media:group/media:content, media:group/media:keywords)" + "&thumbsize="
      + PicasaHS.thumbnailSize + "," + PicasaHS.imageSize + "&alt=json-in-script"
      + "&callback=PicasaHS.parseAlbumPhotos");
}

/* This function process album entries response */
PicasaHS.parseAlbumPhotos = function(data) {
  /* clear gallery div */
  $(this.galleryDiv).empty();
  
  /* append photo and video ul to gallery div */
  var ul = $("<ul/>");
  this.galleryDiv.addClass("highslide-gallery").append(ul);
  
  /* process and append photos and videos to ul */
  var photo, a, img, alt, li, div;
  var thumb_size = parseInt(this.thumbnailSize);
  for ( var i in data.feed.entry) {
    photo = data.feed.entry[i];
    /* list of picasa thumbnails */
    var thumbnails = photo.media$group.media$thumbnail;
    
    /* create li element */
    li = $("<li/>").appendTo(ul);
    
    if (photo.media$group.media$content.length > 1) {
      /* creating a video */

      /* try to found biggest video in entry object */
      var max_cont = {
        width : 0
      };
      var cont;
      for ( var i = 0; i < photo.media$group.media$content.length; i++) {
        cont = photo.media$group.media$content[i];
        if (cont.width > max_cont.width && cont.type == "application/x-shockwave-flash"
            && cont.width <= this.imageSize)
          max_cont = cont;
      }
      /* create an ancor link */
      a = $('<a href="#" onclick="return hs.htmlExpand(this,' + "{wrapperClassName: 'titlebar', slideshowGroup: 'photo_group', " + 
          "width: '" + (max_cont.width+2) + "', height: '" + (max_cont.height+65) + "'});\"/>");
      
      var link_div = $("<div></div>");
      link_div.appendTo(li).append(a);
      div = $('<div/>').addClass("highslide-maincontent").appendTo(link_div);
      
      /* create a embed player div */
      div.append('<embed width="' + max_cont.width + '" height="' + max_cont.height + '" allowfullscreen="true"'
          + 'flashvars="fs=1&amp;hl=ru&amp;autoplay=1&amp;ps=picasaweb&amp;' + 'fmt_list=1%2F' + max_cont.width + 'x'
          + max_cont.height + '%2F9%2F0%2F115' + '&amp;fmt_stream_map=1%7C'
          + encodeURIComponent(max_cont.url.replace(/,/g, '%2C'))
          + '&amp;playerapiid=uniquePlayerId&amp;video_id=picasacid&amp;t=1&amp;vq=large&amp;auth_timeout=86400000000"'
          + 'scale="noScale" wmode="opaque"' + 'allowscriptaccess="always" type="' + max_cont.type
          + '" src="http://www.youtube.com/get_player?enablejsapi=1">');
      
    } else {
      /* creating an image */

      /* create an ancor link */
      a = $('<a onclick="return hs.expand(this,gallery_config);" full_image="' + photo.media$group.media$content[0].url
          + '" full_width="' + photo.media$group.media$content[0].width + '" full_height="'
          + photo.media$group.media$content[0].height + '"/>').addClass("highslide");
      $("<div></div>").appendTo(li).append(a);
      
      /* try use big thumbnail for expanded image, or true size if big thumbnail not available */
      if (thumbnails && thumbnails[1]
          && (thumbnails[1].height == this.imageSize || thumbnails[1].width == this.imageSize)) {
        a.attr("href", thumbnails[1].url);
      } else {
        a.attr("href", photo.media$group.media$content[0].url);
        /* set flag "already full expanded" to disable "fill expand" button */
        a.attr("full_expanded", true);
      }
    }
    
    /* create thumbnail image */
    img = $("<img/>").css("width", thumb_size).css("height", thumb_size).appendTo(a);
    /* specify thumbnail source only if has it */
    if (thumbnails && thumbnails[1] && (thumbnails[0].height == thumb_size || thumbnails[0].width == thumb_size))
      img.attr("src", thumbnails[0].url);
    /* put keywords or summary or title to alt */
    alt = photo.title.$t;
    if (photo.summary)
      alt = photo.summary.$t;
    if (photo.media$group.media$keywords.$t)
      alt = photo.media$group.media$keywords.$t;
    img.attr("alt", alt);
    
    /* Put caption into tip */
    a.attr("title", alt);
    a.addClass("highslide");
    
    /* Make caption */
    $('<div class="photo_caption">' + alt + '</div>'). appendTo(li).attr("title", alt);
  }
  
  /* bind photos and videos to slideshow group */
  hs.updateAnchors();

}

PicasaHS.thumbnailSize = "160c";
/* 32, 48, 64, 72, 104, 144, 150, 160 u or c */
/* 94, 110, 128, 200, 220, 288, 320, 400, 512, 576, 640, 720, 800, 912, 1024, 1152, 1280, 1440, 1600 */
PicasaHS.imageSize = null;


/* hs modifications */

/* enable "full expand" button if big thumbnail used for expanded image */
hs.Expander.prototype.onAfterExpand = function(sender) {
  if (!$(sender.a).attr("full_expanded") && sender.slideshow)
    sender.slideshow.enable('full-expand');
}

/* change image to original size on full expanding */
hs.Expander.prototype.onDoFullExpand = function(sender) {
  sender.content.src = $(sender.a).attr("full_image");
  sender.x.full = parseInt($(sender.a).attr("full_width"));
  sender.y.full = parseInt($(sender.a).attr("full_height"));
  
  return true;
}

