/*
  PFrameworkCS API
  
  Copyright (C) 2008 by Gregor Kališnik <gregor@unimatrix-one.org>
  Copyright (C) 2008 by Unimatrix-One (www.unimatrix-one.org)
  
  This javascript is part of the PFramework CMS system. Any unauthorised
  use outside of this system is strictly prohibited.
*/

function pfTogglePageSelector(id, anchor)
{
  var pageSelector = $(id);
  if (!pageSelector.visible()) {
    var anchorObj = $(anchor);
    var position = anchorObj.cumulativeOffset();
    pageSelector.setStyle({
      position: 'absolute',
      top: (position[1] + anchorObj.getHeight()) + 'px',
      left: position[0] + 'px'
    });
    pageSelector.show();
    $('page_' + id).focus();
  } else {
    pageSelector.hide();
  }
}

function pfGoToPage(id, maxPage, notIntError, outOfRangeError)
{
  var page = $('page_' + id).getValue();
  if (!(page.toString().search(/^-?[0-9]+$/) == 0)) {
    errorMessage(notIntError);
    return;
  }

  if (parseInt(page) < 1 || parseInt(page) > maxPage) {
    errorMessage(outOfRangeError.replace('%1', page));
    return;
  }

  var link = window.location.href;
  var result = link.match(/&page=\d+/);
  if (result == null) {
    result = link.match(/\/page\/\d+/);
    if (result == null) {
      if (link.match(/\?/) == null)
        link += '/page/' + page;
      else
        link += '&page=' + page;
    } else {
      link = link.replace(result, '/page/' + page);
    }
  } else {
    link = link.replace(result, '&page=' + page);
  }

  window.location.href = link;
}
