(function(){

  var url = "bin/getpage.cgi";
  var page = 1;
 	var init = function(){
    $.getJSON( url, callback );
	};

  var callback = function(json){
    var base_link = "movie_preview.html?name=";
    $("#newMovieInBox").html("");
    $("#paging").html("");
    $("#paging_bottom").html("");
    $.each(json.result, function(i, item){
      var url = base_link + item.id;
      var title = "";
      if(item.url) title = item.url;
      if(title == "no address") title = "";
      var wp_elem = '';
      var ch_elem = '';
      if(item.wp){
        wp_elem = '<img src="' + item.wp + '" alt="" width="242" height="140" style="position:absolute; top:0; left:-51px;" />';
      }
      if(item.ch){
        ch_elem = '<img src="' + item.ch + '" alt="" width="242" height="140" style="position:absolute; top:0; left:-51px;" />';
      }
      var left = 5;
      if($.browser.msie && $.browser.version < 8 && navigator.userAgent.indexOf("MSIE 8.0") < 0){
          left = 0;
      }
      var html = '<div class="sItemBox">' + '\n' +
      '<p class="sItemImage"><a href="' + url + '" style="display:block; position:relative; top:0; left:' + left + 'px; width:140px; height:140px; overflow:hidden;">' + wp_elem + ch_elem + '</a></p>' + '\n' +
      '<p class="sItemTxt"><a href="' + title + '">' + title + '</a></p>' + '\n' +
      '</div>';
      $("#newMovieInBox").append(html);
    });
    if(json.page > 1){
      var page_last = json.page;
      for(var i = 1;page_last + 1 > i;i++){
        var link = $('<a href="#">' + i + '</a>   ');
        link.bind("click", gotoPage(i));
        if(page == i) link = i + "  ";
        $("#paging").append(link);
//        $("#paging_bottom").append(link);
      }
      $("#paging_bottom").html($("#paging").html());
    }
    $(document).pngFix(); 
  };
  var gotoPage = function(p){
    return function(){
      page = p;
      $.getJSON( url + "?page=" + p, callback );
      return false;
    }
  }
 $(document).ready(init);
})(jQuery);
