

var item_cnt = 0;
var items_per_page = 16;
var total_pages = 0;
var pager = null;
var my_items = [];
var my_tmp_items;
var curr_tstamp = 0;
function main() {
  var mynow = new Date();
  curr_tstamp = Math.round((mynow.getTime()) / 1000);
  
  if(typeof(album_obj) == "undefined") {
    alert(txt.error_album_deleted);
    if(typeof(lp_view) != "undefined") {
      window.location = "lp_welcome.html";
    } else {
      window.location = "index.html";
    }
    return;
  }

  try{
    if(album_obj.album_display_mode == "themes") {
      if(album_obj.album_theme_id >= 10000) {
        theme_path = public_url + "user_themes/"+album_obj.album_theme_id+"/";
        document.getElementById("display_css").setAttribute("href",public_url + "user_themes/"+album_obj.album_theme_id+"/theme.css");
      } else {
        theme_path = "/mediacenter/public_static/" + "themes/theme_"+album_obj.album_theme_id+"/";
        document.getElementById("display_css").setAttribute("href","/mediacenter/public_static/" + "themes/theme_"+album_obj.album_theme_id+"/theme.css");
      }
    } else {
      theme_path = "/mediacenter/public_static/" + "themes/standard/";
      document.getElementById("display_css").setAttribute("href","/mediacenter/public_static/" +"themes/standard/theme.css");
    }
  }catch(e){}
    try
    {

    document.getElementById("icon_start").src = theme_path + "images/icon_start.png";
    document.getElementById("icon_guestbook").src = theme_path + "images/icon_guestbook.png";

    document.getElementById("icon_contact").src = theme_path + "images/icon_contact.png";
    document.getElementById("icon_charts").src = theme_path + "images/icon_ranking.png";
    }
    catch (e)
    {
    }

  if(album_obj.album_display_mode == "themes") {
    try
    {
      document.getElementById("theme_image_left").src = theme_path + "images/theme_left.jpg";
      document.getElementById("theme_image_right").src = theme_path + "images/theme_right.jpg";
      if(document.getElementById("header_bg")) {
        document.getElementById("header_bg").src = theme_path + "images/theme_header_bg.jpg";
      }


      document.getElementById("icon_charts").src = theme_path + "images/icon_ranking.png";
      document.getElementById("icon_start").src = theme_path + "images/icon_start.png";
      document.getElementById("icon_guestbook").src = theme_path + "images/icon_guestbook.png";
      document.getElementById("icon_contact").src = theme_path + "images/icon_contact.png";
      }
    catch (e)
    {
    }

  } else {
    if(typeof(lp_view) == "undefined") {
      if(album_obj.album_bgcolor) {
        document.body.style.backgroundColor = album_obj.album_bgcolor;
      } 
      if(album_obj.album_font_color) {
        document.body.style.color = album_obj.album_font_color;
      }
    }
  }
  my_tmp_items = album_obj.items;
  for (i in my_tmp_items) {
    if(my_tmp_items[i].media_type == "other" || my_tmp_items[i].media_type == "audio") {
      continue;
    }
    my_items.push(my_tmp_items[i]);
  }

  for (i in my_items) {
    if(my_items[i].media_type == "other" || my_items[i].media_type == "audio") {
      continue;
    }
    item_cnt++;
    if(my_items[i].object_id == album_obj.title_image_id) {
      if(my_items[i].media_type == "video") {
        album_obj.title_image_src = clean_path(my_items[i].src_small);
      } else {
        album_obj.title_image_src = clean_path(my_items[i].src_medium);
      }
      album_obj.title_image_x = my_items[i].dim_x;
      album_obj.title_image_y = my_items[i].dim_y;
    }
  }

  // setting title image has to be after this loop
  set_title_image();

  set_album_info();

  total_pages = item_cnt / items_per_page;
  if(total_pages != parseInt(total_pages)) {
    total_pages++;
  }
  if(item_cnt > 1){
    pager = new ftcPaging(total_pages,"my_pager",item_navigate);
    pager.draw();
  }
  item_navigate(1);

  if(album_obj.album_display_mode == "standard") {
    try {
      var pager_rule = UTIL.getStyleSheet(".ftcp-standard");
    } catch (e) {
      var pager_rule = false;
    }
   
    if(typeof(lp_view) == "undefined") {
      if(album_obj.album_bgcolor) {
        document.body.style.backgroundColor = album_obj.album_bgcolor;
      } 
      if(album_obj.album_font_color) {
        var arr = document.getElementsByTagName("a");
        var len = arr.length;
        if(pager_rule) {
          pager_rule.style.color = album_obj.album_font_color;
          pager_rule = UTIL.getStyleSheet(".ftcp-active");
          pager_rule.style.color = album_obj.album_font_color;
        }
        for(i = 0; i < len; i++) {
          arr[i].style.color = album_obj.album_font_color;
        }
        document.body.style.color = album_obj.album_font_color;
      }
    }
  }

}

clean_path = function(path) {
  if(typeof(lp_view) != "undefined") {
    var new_path = path.replace(/http.*\/\/.*?\//, "/");
    return new_path;
  } else {
    return path;
  }
}

function item_navigate(no) {

  var my_start = (no * items_per_page) - items_per_page;
  var my_end = my_start + items_per_page;

  // dont overflow
  if(my_end > item_cnt) {
    my_end = item_cnt;
  }
  // clear previous page
  document.getElementById("images_content").innerHTML = "";

  // build page for selected range
  for(i = my_start; i < my_end; i++) {
    if(my_items[i]) {
      build_image_html(my_items[i], my_items[i].object_id);
    }
  }
}
function build_image_html(img, idx) {

  var img_stamp = img.publishing_timestamp;
  var is_new = false;

  var image_html_target = "image.html";
  if(typeof(lp_view) != "undefined") {
    image_html_target = "lp_image.html";
  }

  if(img_stamp && new_distance && (curr_tstamp - img_stamp) < new_distance) {
    is_new = true;
  }

  var chg_dt = new dbdDate(img.unix_changed*1000, "d.m.Y");
 // var chg_day = chg_dt.getDate();
 // var chg_mon = chg_dt.getMonth();
 // var chg_year = chg_dt.getFullYear();
  var changed = chg_dt.toString(); //chg_day + "." + chg_mon + "." + chg_year;

  var tbl = document.createElement("table");
  var tbl = document.createElement("table");
  var newimg = new Image();

  var movie_indi_parent = document.createElement("div");
  movie_indi_parent.className = "movie_indi_parent";

  var tr  = new Array();
  var td  = new Array();
  var icon_td;
  var movie_indi = new Image();
  movie_indi.src = "/mediacenter/public_static/pix/public_video.jpg";
  movie_indi.className = "movie_indi";

  tr[0] = tbl.insertRow(0);
  tr[1] = tbl.insertRow(1);
  td[0] = tr[0].insertCell(0);
  td[1] = tr[1].insertCell(0);



  icon_td = tr[0].insertCell(1);  
  icon_td.className = "icon_td";
  tr[1].insertCell(1);

  //icon_td.innerHTML = curr_tstamp + " <br> " + img_stamp + " <br> " + (curr_tstamp - img_stamp) + " <br> " + new_distance + " <br> " + x;
  if(img.attached_object_type == "audio") {
    icon_td.innerHTML += "<img style=\"margin-top:1px\" src=\"/mediacenter/public_static/pix/icon_sound.gif\"/><br/>";    
  }
   if(img.attached_object_type == "pdf") {
    icon_td.innerHTML += "<img style=\"margin-top:1px\" src=\"/mediacenter/public_static/pix/icon_pdf.gif\"/><br/>";    
  }
  if(img.object_comments.length) {
    icon_td.innerHTML += "<img style=\"margin-top:1px\" src=\"../images/lang_neutral/public/icon_comment.gif\"/>";  
  }
  if(is_new) {
   icon_td.innerHTML += "<img style=\"margin-top:1px\" src=\""+new_img_path+"\"/>";   
  }
  tbl.className = "object_frame";
  td[0].className = "img_td";
  td[1].className = "img_title";
  if(img.media_type == "video") {
    newimg.src = clean_path(img.src_small);
  } else {
    newimg.src = clean_path(img.src_small);
  }
  newimg.className = "thumbnail_frame";

  var tst = new Image();
  tst.src = "../images/lang_neutral/icon_info.gif";

  //td[0].appendChild(newimg);
  td[0].appendChild(movie_indi_parent);
  movie_indi_parent.appendChild(newimg);
  if(img.media_type == "video") {
    movie_indi_parent.appendChild(movie_indi);
    if(img.flv_name) img.name = img.title;
  }
  if(img.name.length > 12) {
    td[1].innerHTML = "<span class=\"img_name\">" + img.name.substr(0,12) + "&nbsp;...</span><br/>" + changed;
  } else {
    td[1].innerHTML = "<span class=\"img_name\">" + img.name + "</span><br/>" + changed;
  }
  td[1].title = img.name;
  tbl.onclick = function() {
    if(preview) {
      var url = image_html_target + "?preview=1&album_id=" + album_obj.object_id + "&index=" + (idx) + (typeof(get_string) != "undefined" ? get_string : '');
    } else {
      var url = image_html_target + "?album_id=" + album_obj.object_id + "&index=" + (idx) + (typeof(get_string) != "undefined" ? get_string : '');
    }

    window.location = url;
  }

  document.getElementById("images_content").appendChild(tbl);
  // special Live Pages call
  try { if (typeof parent == 'object' && typeof parent.resizeLPFrame == 'function') parent.resizeLPFrame() } catch(e) {}

}

function set_title_image() {
  if(!album_obj.title_image_src) {
    return("");
  }
  var my_img = new Image();
  var new_dims = [];
  new_dims = fit_image_to_box(200, 150, (album_obj.title_image_x + 0), (album_obj.title_image_y + 0))
  my_img.src = clean_path(album_obj.title_image_src);
  my_img.className = "image_frame";

    my_img.style.width = new_dims[0] + "px";
    my_img.style.height = new_dims[1] + "px";
  
  document.getElementById("title_image").appendChild(my_img);
}

function set_album_info() {
  var box = document.getElementById("album_info_box");
  var img_count = item_cnt;
  var html = "";

  //album_obj.name
  //album_obj.changed
  //album_obj.memo

  html += "<div class=\"album_name\">"+ album_obj.name +"</div>";
  html += "<div class=\"album_date\">"+ album_obj.changed +"</div>";
  html += "<div class=\"album_pic_count\"><img src=\"../images/lang_neutral/icon_image.gif\" style=\"padding-right:3px\">"+ img_count +"&nbsp;" + txt.mediums + "</div>";
try
{
  html += "<div class=\"album_memo\">"+ UTIL.convert2Link(album_obj.memo, true) +"</div>";
}
catch (e)
{
  html += "<div class=\"album_memo\">"+ album_obj.memo +"</div>";
}

if($_GET["lp_callback"]) {
  html += "<a href=\"javascript:window.location='"+ $_GET['lp_callback'] + "?" + get_string + "';\">"+txt.navi_welcome+"</a>";
}
  box.innerHTML = html;
}

  
function fit_image_to_box(max_x, max_y, x, y) {
  if(x < 20) return([x,y]);
  if(y < 15) return([x,y]);
  var new_x = 0;
  var new_y = 0;
  var zb = max_x / max_y;
  var zi = x / y;

  if(zi > zb) { // bild ist breiter
    new_x = max_x;
    new_y = new_x / zi;
  } else {  // und hochkant
    new_y = max_y;
    new_x = new_y * zi;
  }
  new_x = parseInt(new_x);
  new_y = parseInt(new_y);
  return([new_x, new_y]);
}