// JavaScript Document

// JavaScript Document

var mousex = 0;
var mousey = 0;

var PageModeType = { STANDARD:0, GALLERY:1 };
var ThumbsScrollDirection = { HORIZONTAL:0, VERTICAL:1 };

var PAGEMODE;

var FRAME_WIDTH = 995;
var FRAME_HEIGHT = 800;


/**
 *
 */
function dtOnLoad(pageMode) {
  initDHTMLAPI();
  dtResetElements(pageMode);
}

/**
 *
 */
function dtOnResize(pageMode) {
  dtResetElements(pageMode);
}

/**
 *
 */
function dtCenterThem(pageMode) {
  var winWidth = getInsideWindowWidth();
  var winHeight = getInsideWindowHeight();
  
  var dw = winWidth - FRAME_WIDTH;
  var dh = winHeight - FRAME_HEIGHT;
  
  var dx = 0;
  var dy = 0;
              
  if( dw > 0 ) {
    dx = dw/2;
  }

  if( dh > 0 ) {
    dy = dh/2-50;
								
    if( dy < 20 )
    dy = 20
  }

  shiftTo( 'layerMain', dx, dy );        
}

/**
 *
 */
function dtHideLayers() {
  hide('layerMain');
}

/**
 *
 */
function dtShowLayers(pageMode) {
  show('layerMain');

}

/**
 *
 */
function dtResetElements(pageMode) {
  dtHideLayers();
  dtCenterThem();  
  dtShowLayers(pageMode);
}

/**
   * Retrieve the coordinates of the given event relative to the center
   * of the widget.
   *
   * @param event
   *   A mouse-related DOM event.
   * @param reference
   *   A DOM element whose position we want to transform the mouse coordinates to.
   * @return
   *    A hash containing keys 'x' and 'y'.
   */
  function getRelativeCoordinates(event, reference) {
    var x, y;
    event = event || window.event;
    var el = event.target || event.srcElement;

    if (!window.opera && typeof event.offsetX != 'undefined') {
      // Use offset coordinates and find common offsetParent
      var pos = { x: event.offsetX, y: event.offsetY };

      // Send the coordinates upwards through the offsetParent chain.
      var e = el;
      while (e) {
        e.mouseX = pos.x;
        e.mouseY = pos.y;
        pos.x += e.offsetLeft;
        pos.y += e.offsetTop;
        e = e.offsetParent;
      }

      // Look for the coordinates starting from the reference element.
      var e = reference;
      var offset = { x: 0, y: 0 }
      while (e) {
        if (typeof e.mouseX != 'undefined') {
          x = e.mouseX - offset.x;
          y = e.mouseY - offset.y;
          break;
        }
        offset.x += e.offsetLeft;
        offset.y += e.offsetTop;
        e = e.offsetParent;
      }

      // Reset stored coordinates
      e = el;
      while (e) {
        e.mouseX = undefined;
        e.mouseY = undefined;
        e = e.offsetParent;
      }
    }
    else {
      // Use absolute coordinates
      var pos = getAbsolutePosition(reference);
      x = event.pageX  - pos.x;
      y = event.pageY - pos.y;
    }
    // Subtract distance to middle
    return { x: x, y: y };
  }
  
  function getAbsolutePosition(element) {
    var r = { x: element.offsetLeft, y: element.offsetTop };
    if (element.offsetParent) {
      var tmp = getAbsolutePosition(element.offsetParent);
      r.x += tmp.x;
      r.y += tmp.y;
    }
    return r;
  };

/**
 *
 */
function getMouseXY(e) // works on IE6,FF,Moz,Opera7
{ 
  if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)
 
  if (e)
  { 
    if (e.pageX || e.pageY)
    { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
      mousex = e.pageX;
      mousey = e.pageY;
      algor = '[e.pageX]';
      if (e.clientX || e.clientY) algor += ' [e.clientX] '
    }
    else if (e.clientX || e.clientY)
    { // works on IE6,FF,Moz,Opera7
      // Note: I am adding together both the "body" and "documentElement" scroll positions
      //       this lets me cover for the quirks that happen based on the "doctype" of the html page.
      //         (example: IE6 in compatibility mode or strict)
      //       Based on the different ways that IE,FF,Moz,Opera use these ScrollValues for body and documentElement
      //       it looks like they will fill EITHER ONE SCROLL VALUE OR THE OTHER, NOT BOTH 
      //         (from info at http://www.quirksmode.org/js/doctypes.html)
      mousex = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
      mousey = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
      algor = '[e.clientX]';
      if (e.pageX || e.pageY) algor += ' [e.pageX] '
    }
  }
}
  
/**
 *
 */
function dtThumbsControl( containerName, event, scrollDirection ) {
	
  scrollStep = 1;
 
  var thumbsLayerWidth=getObjectWidth(containerName);
  var thumbsLayerHeight=getObjectHeight(containerName);

  getMouseXY(event);
  pos_x=mousex-document.getElementById(containerName).offsetLeft;
  pos_y=mousey-document.getElementById(containerName).offsetTop;
	
  vecX = thumbsLayerWidth/2-pos_x;
  vecY = thumbsLayerHeight/2-pos_y;
  
  //document.getElementById("infoText").innerHTML=thumbsLayerHeight+"/"+pos_y+"/"+vecY;

  if( ThumbsScrollDirection.HORIZONTAL == scrollDirection ) {
	if( vecX < -20 ) {
	  scrollDivLeftStep(containerName,scrollStep);
	}
	else if( vecX > 20 ) {
	  scrollDivRightStep(containerName,scrollStep);
	}
  }
  else {
	if( vecY < -40 ) {
	  scrollDivUpStep(containerName,scrollStep);
	}
	else if( vecY > 40 ) {
	  scrollDivDownStep(containerName,scrollStep);
	}
  }  
}

/**
 *
 */
function countChildElements(parent, child) {
  var parent = document.getElementById(parent);
  var childCount = parent.getElementsByTagName(child).length;
  return childCount;
}

/**
 *
 */
function processForm( errorMessage ){
  if( document.contantForm.email.value == '' ||
	  document.contantForm.message.value == '' ) {
    alert( errorMessage );
    return false;
  }
  
  return jcap();
}

