// these are library-specific, will work with both jQuery and MooTools
function js_page_width() {return parseInt (window.getSize ? (window.getSize().size ? window.getSize().size.x : window.getSize().x) : $(window).width());}
function js_page_height() {return parseInt (window.getSize ? (window.getSize().size ? window.getSize().size.y : window.getSize().y) : $(window).height());}
function js_pos_left() {return parseInt (window.getScroll ? window.getScroll().x : (window.getSize ? window.getSize().scroll.x : $(window).scrollLeft()));}
function js_pos_top() {return parseInt (window.getScroll ? window.getScroll().y : (window.getSize ? window.getSize().scroll.y : $(window).scrollTop()));}
if (window.addEvent) {
  window.addEvent('scroll', js_update_size);
  window.addEvent('resize', js_update_size);
  window.addEvent('keydown', js_key_down);
}
else if (window.$) {
  $(window).scroll(js_update_size);
  $(window).resize(js_update_size);
  $(window).keypress(js_key_down);
}
else if (window.planyo$) {
  planyo$(window).scroll(js_update_size);
  planyo$(window).resize(js_update_size);
  planyo$(window).keypress(js_key_down);
}

function js_slideshow_full_img(bRun) {
  if (bRun) {
    document.current_full_img = 0;
    document.slideshow_mode = true;
    document.getElementById('play_button').style.display='none';
    document.getElementById('stop_button').style.display='block';
    js_next_full_img(0);
  }
  else {
    document.slideshow_mode = false;
    document.getElementById('play_button').style.display='block';
    document.getElementById('stop_button').style.display='none';
  }
}

function js_is_first_img() {
  return (document.current_full_img <= 1);
}

function js_is_last_img() {
  return (document.getElementById('full'+(document.current_full_img+1))) ? false : true;
}

function js_prev_full_img() {
  if (!js_is_first_img()) {
    js_show_full_img(document.current_full_img-1);
  }
}

function js_next_full_img(bUserInvoked) {
  if (!bUserInvoked && !document.slideshow_mode)
    return;
  
  if (!js_is_last_img()) {
    js_show_full_img(document.current_full_img+1);
    if (document.slideshow_mode) {
      if (!bUserInvoked)
        window.setTimeout("js_next_full_img(0)",3000);
    }
  }
  else
    js_slideshow_full_img(0);
}

function js_close_full_img() {
  js_slideshow_full_img(0);
  document.getElementById('bg_hider').style.display='none';
  document.getElementById('full_img_div').style.display='none';
  document.current_full_img = null;
}

function js_show_full_img(img_number) {
  document.current_full_img = img_number;
  var bg_hider = document.getElementById('bg_hider');
  var image = document.getElementById('full'+img_number);
  var image_width = image.width;
  var image_height = image.height;
  if (!image_width || !image_height) {
    var data = image.parentNode.id;
    var widthStart = data.indexOf('W');
    var heightStart = data.indexOf('H');
    image_width = parseInt (data.substring (widthStart + 1, heightStart));
    image_height = parseInt (data.substring (heightStart + 1));
  }

  var margin = 30;
  var top_offset = 10;
  var full_img_div = document.getElementById('full_img_div');
  var full_img = document.getElementById('full_img');
  full_img_div.style.display='none';
  if (bg_hider.parentNode != document.body)
    document.body.appendChild(bg_hider);
  if (full_img_div.parentNode != document.body)
    document.body.appendChild(full_img_div);

  if (document.is_mobile) {
    var full_img = document.getElementById('full_img');
    full_img.style.width = image_width+'px';
    full_img.style.height = image_height+'px';
  }

  bg_hider.style.display='block';
  bg_hider.style.left = js_pos_left()+'px';
  bg_hider.style.top = js_pos_top()+'px';
  bg_hider.style.width = js_page_width()+'px';
  bg_hider.style.height = js_page_height()+'px';
  if (image_width + margin * 2 > js_page_width())
    bg_hider.style.width = (image_width + margin * 2)+'px';
  if (image_height + margin * 2 + top_offset > js_page_height())
    bg_hider.style.height = (image_height + margin * 2 + top_offset)+'px';
  
  full_img.src=image.src;
  full_img_div.style.left = Math.max(0,js_pos_left() + (js_page_width() - image_width - 2 * margin)/2)+'px';
  full_img_div.style.top = Math.max(0,js_pos_top() + (js_page_height() - image_height - 2 * margin)/2)+'px';
  full_img_div.style.display='';
  document.getElementById ('prev_button').style.backgroundImage = js_is_first_img() ? 'url(http://www.planyo.com/slideshow/btn-prev-dsbld.png)' : 'url(http://www.planyo.com/slideshow/btn-prev.png)';
  document.getElementById ('next_button').style.backgroundImage = js_is_last_img() ? 'url(http://www.planyo.com/slideshow/btn-next-dsbld.png)' : 'url(http://www.planyo.com/slideshow/btn-next.png)';
  document.getElementById ('img_info').innerHTML = document.slideshow_mode ? '' : document.getElementById ('full'+img_number+'info').innerHTML;
  document.getElementById ('img_info').style.display = 'block';
}

function js_update_size (){
  if (document.current_full_img != null) {
    js_show_full_img (document.current_full_img);
  }
}

function js_key_down (e) {
  if (document.current_full_img && e) {
    if (e.key == 'esc' || e.keyCode == 27)
      js_close_full_img();
    else if (e.key == 'right' || e.keyCode == 39)
      js_next_full_img(1);
    else if (e.key == 'left' || e.keyCode == 37)
      js_prev_full_img();
    else if (e.key == 'space' || e.keyCode == 32)
      js_slideshow_full_img(!document.slideshow_mode);
    return false;
  }
  return true;
}
