function backToTop() 
{
    var x1 = x2 = x3 = 0;
    var y1 = y2 = y3 = 0;

    if (document.documentElement) {
        x1 = document.documentElement.scrollLeft || 0;
        y1 = document.documentElement.scrollTop || 0;
    }

    if (document.body) {
        x2 = document.body.scrollLeft || 0;
        y2 = document.body.scrollTop || 0;
    }

    x3 = window.scrollX || 0;
    y3 = window.scrollY || 0;

    var x = Math.max(x1, Math.max(x2, x3));
    var y = Math.max(y1, Math.max(y2, y3));

    window.scrollTo(Math.floor(x / 2), Math.floor(y / 2));

    if (x > 0 || y > 0) {
        window.setTimeout("backToTop()", 16);
    }
}


function changePage(modifier) {
  pageCount = Math.ceil(g_totalStories / g_pageSize)

  if((g_currentPage + modifier) == 0) {
    g_currentPage = 1;
  } else if((g_currentPage + modifier) > pageCount) {
    g_currentPage = g_currentPage;
  } else {
    g_currentPage = g_currentPage + modifier;
    toggleONthrobber('main_throbber');
    doStories();
  }
}

function gotoPage(pageNum) {
  g_currentPage = pageNum;
  toggleONthrobber('main_throbber');
  doStories();
}

function initPaging() {
  pageCount = Math.ceil(g_totalStories / g_pageSize)

  if (pageCount < 5) {
    pLeftBound = 1;
    pRightBound = pageCount;
  } else {
    pLeftBound = 1;
    pRightBound = 5;
  }
    
}

function CalcRegularBounds() {
  if (pageCount > 5) {
    if(g_currentPage == pageCount) {
      pLeftBound = g_currentPage - 4;
    } else if(g_currentPage == pageCount - 1) {
      pLeftBound = g_currentPage - 3;
    } else {
      pLeftBound = g_currentPage - 2;
      if (pLeftBound < 1) {
        pLeftBound = g_currentPage - 1;
        if (pLeftBound < 1) {
          pLeftBound = g_currentPage;
        }
      }
    }
    if(g_currentPage == 1) {
      pRightBound = 5;
    } else if(g_currentPage ==2) {
      pRightBound = 5;
    } else {
      pRightBound = g_currentPage + 2;
      if(pRightBound > pageCount) {
        pRightBound = g_currentPage + 1;
        if(pRightBound > pageCount) {
          pRightBound = pageCount;
        }
      }
    }
  } else {
    pLeftBound = 1;
    pRightBound = pageCount;
  }
}


function displayPaging() {
  var pageDiv = document.getElementById('pg');
  var html = "";
  var cssClass = "";
  pageCount = Math.ceil(g_totalStories / g_pageSize)
  if (pageCount == 1) { pageDiv.innerHTML = ""; return; } //If only 1 page don't show that crap

  html = "<table class='pg-container' border='0' cellspacing='0' cellpadding='0'><tr class='pgrow'>";
  
  if(g_currentPage > 1) {
    html += "<td class='changepos backbutton'><a onclick='changePage(-1); return false' href='#'>back</a></td>";
  }

  CalcRegularBounds();
  if(pLeftBound > 2) {
    html+= '<td class="left"><a class="small norm" onclick="gotoPage(1); return false" href="#">' + 1 + '</a></td>';  
    html+= '<td class="right"><a class="small norm" onclick="gotoPage(2); return false" href="#">' + 2 + '</a></td>';
    html+= '<td class="break"></td>';
  }
  for(i=pLeftBound;i<=pRightBound;i++) {
    cssClass = "norm";
    if(i == g_currentPage) { cssClass = "sel"; }
    if(i==pLeftBound) {
      html+= '<td class="left"><a class="' + cssClass + '" onclick="gotoPage(' + i + '); return false" href="#">' + i + '</a></td>';
    } else if(i==pRightBound) {
      html += '<td class="right"><a class="' + cssClass + '" onclick="gotoPage(' + i + '); return false" href="#">' + i + '</a></td>';
    } else {
      html +='<td class="mid"><a class="' + cssClass + '" onclick="gotoPage(' + i + '); return false" href="#">' + i + '</a></td>';
    }
  }
 
  if((pageCount - 1) > pRightBound) {
    html+= '<td class="break"></td>';
    html+= '<td class="left"><a class="small norm" onclick="gotoPage(' + (pageCount - 1) + '); return false" href="#">' + (pageCount - 1) + '</a></td>';
    html+= '<td class="right"><a class="small norm" onclick="gotoPage(' + pageCount + '); return false" href="#">' + pageCount + '</a></td>';
  }

  if(g_currentPage < pageCount) {
    html += "<td class='changepos nextbutton'><a onclick='changePage(1); return false' href='#'>next</a></td>";
  }
  html += "</tr></table>";

  //html += '<span class="next"><a href="#" onclick="changePage(1);">next<img src="gfx/sn/nextarrow.gif" border="0" id="iconnext"/></a></span>';
  pageDiv.innerHTML = html;
}
