var getUrlVars = function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++){
        hash = hashes[i].split('=');
        vars.push(decodeURIComponent(hash[0]));
        vars[decodeURIComponent(hash[0])] = decodeURIComponent(hash[1]);
    }
    if(vars[0] == window.location.href){
        vars =[];
    }
    return vars;
}

function goBackTop(txt, speed) {
    var doc = document;
    var top = document.createElement('div');
    top.className = 'go-back-top no-print';
    top.textContent = txt || 'top';
    doc.body.appendChild(top);

    // 判斷是否顯示按鈕
    $(window).scroll(function() {
      if ($(this).scrollTop() !== 0) {
        $('.go-back-top').fadeIn();
      } else {
        $('.go-back-top').fadeOut();
      }
    });

    // 捲動效果
    $('.go-back-top').on('click', function() {
      $('body, html').animate({
        scrollTop: 0
      }, speed || 300);
      return false;
    });
  }

var constructPagination = function(current_page, total_pages){
  var ul = $("<ul class='pagination'></ul>"),
      first_li = $("<li class='first'><a data-page='1' href='#'>First</a></li>"),
      last_li = $("<li class='last'><a data-page='" + total_pages + "' href='#'>Last</a></li>"),
      prev_li = $("<li class='prev'><a data-page='" + (current_page - 1) + "' href='#'><</a></li>"),
      next_li = $("<li class='next'><a  data-page='" + (current_page + 1) + "'href='#'>></a></li>"),
      limit = 3,
      start = 1;

  start = ((current_page - 1) < 1 ? 1 : (current_page - 1));
  limit = (total_pages > limit ? limit : total_pages);
  end = ((current_page + 1) > total_pages ? total_pages : (current_page + 1));
  if(total_pages == limit){
    start = 1;
    end = limit;
  }
  if(end == 2 && total_pages > 2){
    end++;
  }
  if(current_page == total_pages && start > 1){
   start--;
  }

  for(i = start;i <= end; i++){
    var li = $("<li class='page'><a data-page='" + i + "' href='#'>" + i + "</li>");
    if(i == current_page){
      li.addClass("active");
    }
    ul.append(li);
  }
  if(current_page > 1){
    ul.prepend(prev_li);
    ul.prepend(first_li);    
  }
  if(current_page != total_pages){
    ul.append(next_li);
    ul.append(last_li);
  }
  ul.find("a").on("click",function(){
    $("*").animate({scrollTop:0}, '300');
    getObjects($(this).data("page"))
    return false;
  })
  paginationArea.show();
  paginationArea.find("nav").html(ul);
}
$(document).ready(function(){
  goBackTop('top', 500);
});