/***
 *
 * rubberband.js
 * rubber band selection routines
 * adapted from http://home.earthlink.net/~wzd3/rubberband.html
 *
 * requires gr.js
 ***/

var gr = new Graphics("canvas");
var c = null;

// setup mouse capturing
// 
// http://www.breakingpar.com/bkp/home.nsf/Doc?OpenNavigator&U=87256B14007C5C6A87256B4B0005BFA6
// Set Netscape up to run the "captureMousePosition" function whenever
// the mouse is moved. For Internet Explorer and Netscape 6, you can capture
// the movement a little easier.

if (document.layers) { // Netscape
    document.captureEvents(Event.MOUSEMOVE);
    document.onmousemove = captureMousePosition;
} else if (document.all) { // Internet Explorer
    document.onmousemove = captureMousePosition;
} else if (document.getElementById) { // Netcsape 6
    document.onmousemove = captureMousePosition;
}

// Global variables
var xMousePos = 0; // Horizontal position of the mouse on the screen
var yMousePos = 0; // Vertical position of the mouse on the screen

function captureMousePosition(e) {
  if (document.layers) {
    // When the page scrolls in Netscape, the event's mouse position
    // reflects the absolute position on the screen. innerHight/Width
    // is the position from the top/left of the screen that the user is
    // looking at. pageX/YOffset is the amount that the user has
    // scrolled into the page. So the values will be in relation to
    // each other as the total offsets into the page, no matter if
    // the user has scrolled or not.
    xMousePos = e.pageX;
    yMousePos = e.pageY;
  } else if (document.all) {
    // When the page scrolls in IE, the event's mouse position
    // reflects the position from the top/left of the screen the
    // user is looking at. scrollLeft/Top is the amount the user
    // has scrolled into the page. clientWidth/Height is the height/
    // width of the current page the user is looking at. So, to be
    // consistent with Netscape (above), add the scroll offsets to
    // both so we end up with an absolute value on the page, no
    // matter if the user has scrolled or not.
    xMousePos = window.event.x+document.body.scrollLeft;
    yMousePos = window.event.y+document.body.scrollTop;
  } else if (document.getElementById) {
    // Netscape 6 behaves the same as Netscape 4 in this regard
    xMousePos = e.pageX;
    yMousePos = e.pageY;
  }
}

var divX1;
var divX2;
var divY1;
var divY2;
var drawing = false;
var startX = 0;
var startY = 0;
var p = null;
var a = 0;

function startLine(){
  var canvas = document.getElementById('canvas');
  divX1 = canvas.offsetLeft;
  divY1 = canvas.offsetTop;
  divX2 = divX1 + canvas.offsetWidth;
  divY2 = divY1 + canvas.offsetHeight;
  startX= (xMousePos-divX1);
  startY= (yMousePos-divY1);
  drawing = true;
  drawBand();
}

function stopLine(){
  drawing = false;
}

function drawBand()
{
  // leave band alone if out of range
  if (xMousePos < divX1 || xMousePos > divX2 || yMousePos < divY1 || yMousePos > divY2){
    window.setTimeout("drawBand();", 10);
    return;
  }

  // don't redraw band if mouse is up
  if (drawing == false) 
    return;

  // remove previous band
  if (p)
    gr.removeShape(p);

  gr.penColor = "#5E4200";

  // compute the exact rectangle from start point to mouse point
  var x1 = startX;
  var x2 = xMousePos - divX1;
  var y1 = startY;
  var y2 = yMousePos - divY1;

  // adjust so x1, y1 is UL corner
  var temp;
  if (x2 < x1) { temp = x1; x1 = x2; x2 = temp; }
  if (y2 < y1) { temp = y1; y1 = y2; y2 = temp; }

  // now adjust it to cover whole 10x10 blocks
  x1 = Math.floor(x1 / 10) * 10;
  y1 = Math.floor(y1 / 10) * 10;
  x2 = Math.ceil(x2 / 10) * 10;
  if (x2 == x1) x2 += 10;
  if (x2 > divX2 - divX1) { x1 -= 10; x2 -= 10; }
  y2 = Math.ceil(y2 / 10) * 10;
  if (y2 == y1) y2 += 10;
  if (y2 > divY2 - divY1) { y1 -= 10; y2 -= 10; }

  // compute region width, height
  var w = x2 - x1;
  var h = y2 - y1;

  // set form values
  var f = document.forms[0];
  if (f) {
    f.x.value = x1
    f.y.value = y1
    f.w.value = w
    f.h.value = h
  }

  // update selection description
  var sel = document.getElementById('selection');
  if (sel) {
    var html = ' ' + w + ' x ' + h + '  => ' + (w * h) + ' pixels';
    if (f) {
      if (f.pixel_price && f.pixel_price.value > 0) {
        var amt = w * h * f.pixel_price.value;
		if ( amt < 2 ) amt = 2 ;
        amt = amt.toFixed(2);
        if (f.decimal_point)
          amt = amt.replace(/\./g, f.decimal_point.value);
        if (f.thousands_separator) {
          while (amt.match(/\d\d\d\d\D/))
            amt = amt.replace(/(\d)(\d\d\d\D.*)/, '$1 $2');
          amt = amt.replace(/ /g, f.thousands_separator.value);
        }
        html = html + ' => ';
        if (f.currency_symbol)
          html = html + f.currency_symbol.value;
        html = html + amt;
      }
    }
    sel.innerHTML = html;
  }

  p = gr.drawRectangle(x1, y1, w, h);
  window.setTimeout("drawBand();", 10);
}

function moveCanvas(to_id)
{
  var to = document.getElementById(to_id);
  var canvas = document.getElementById('canvas');
  var pos = getPos(to);
  canvas.style.left = pos.x + 'px';
  canvas.style.top = pos.y + 'px';
  canvas.style.visibility = 'visible';
}

// get absolute position of element
// from http://www.alphafilter.com/?inc=article&aid=30
function getPos(el) {
  var p = YAHOO.util.Dom.getXY(el);
  var pos = new Object;
  pos.x = p[0];
  pos.y = p[1];
  return pos;
}

