$jq(document).ready(function(){
  initThumbnailPager();
});

function initThumbnailPager() {
  $jq(".page_change input").click(function(){
    if($jq(this).attr("but")=="next")
    {
      var parent=$jq(this).parent().parent().find("img");
      var check=true;
      $jq.each(parent,function(){
        if(!check) return;
        if($jq(this).css("display")=="inline"|| $jq(this).css("display")=="block")
        {
          if($jq(this).next().length==0 || $jq(this).next().attr("class")=="page_change") return;
          $jq(this).hide();
          $jq(this).next().show();
          check=false;
        }
      });
      if(!check)
      {
        var text=$jq(this).parent().find("span").text();
        var parts = text.split(' of ');
        var text = (parseInt(parts[0])+1)+" of "+parts[1];
        $jq(this).parent().find("span").text(text);
      }
    }

    if($jq(this).attr("but")=="previous")
    {
      var parent=$jq(this).parent().parent().find("img");
      var check=false;
      var show=false;
      $jq.each(parent,function(){
        if(check) return;
        if($jq(this).css("display")=="inline"|| $jq(this).css("display")=="block")
        {   
          if($jq(this).prev().length==0) 
          {
            show=true;
            return;
          }
          $jq(this).hide();
          $jq(this).prev().show();
          check=false;
        }
      });
      if(!show)
      {
        var text=$jq(this).parent().find("span").text();
        var parts = text.split(' of ');
        var text = (parseInt(parts[0])-1)+" of "+parts[1];
        $jq(this).parent().find("span").text(text);
      }
    }
  });
}
