/* Control page overflow for fixed-sized-and-centered sites.
 * ----------------------------------------------------------------------------
 *
 * Copyright (C) 2009 Raphaël Bois.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * ----------------------------------------------------------------------------
 * page-overflow.js
 */

if (!MooTools || MooTools.version != '1.2.4')
  throw 'This module requires Mootools 1.2.4';

function windowSize() {
  alert('windowSize() is deprecated. Use $(window).getSize().');
  return $(window).getSize();
}

function checkElementOverflow(el, topDiv) {
  /* Manual check, because MooTools container check is buggy. */
  topDiv = $(topDiv);
  el =  $(el);
  var pagePos = topDiv.getPosition();
  var descSize = el.getDimensions();
  var pageSize = topDiv.getSize();
  var winSize = $(window).getSize();
  if (winSize.x < pageSize.x) {
    winSize.x = pageSize.x;
  }
  if (winSize.y < pageSize.y) {
    winSize.y = pageSize.y;
  }
  var minLeft = -pagePos.x, maxLeft = winSize.x - pagePos.x;
  var minTop = -pagePos.y, maxTop = winSize.y - pagePos.y;
  var elPos = { 'x': el.getStyle('left').toInt(), 'y': el.getStyle('top').toInt() };
  if (elPos.x < minLeft) {
    el.setStyle('left', minLeft);
  } else if (elPos.x + descSize.x > maxLeft) {
    el.setStyle('left', maxLeft - descSize.x);
  }
  if (elPos.y < minTop) {
    el.setStyle('top', minTop);
  } else if (elPos.y + descSize.y > maxTop) {
    el.setStyle('top', maxTop - descSize.y);
  }
}

function checkPageOverflow(page) {
  page = $(page);
  if (!page) return;
  s = $(window).getSize();
  ps = page.getSize();

  page.set('class', "");
  if (s.x < ps.x) {
    page.addClass("hleft");
  } else {
    page.addClass("hcenter");
  }
  if (s.y < ps.y) {
    page.addClass("vtop");
  } else {
    page.addClass("vcenter");
  }
}

