/*
   New Perspectives on JavaScript
   Tutorial 8
   Tutorial Case

   Author: Krystin Scott
   Date:   10/28/09

   Filename: shop.js

   Global Variables:
   IE
      Contains the Boolean value true if the browser supports the IE event model 
      and false if otherwise.
   DOM
      Contains the Boolean value true if the browser supports the DOM event
      model and false if otherwise.

   Functions List:
   placeIt(object, x, y)
      Places an object at the coordiantes (x,y)

   getXCoord(object)
      Returns the x-coordinate of an object

   getYCoord(object)
      Returns the x-coordinate of an object

   withinIt(x, y, object)
      Returns the Boolean value true if the coordiantes (x,y) lie within the
      boundaries of the object and false if otherwise.

   colorIt(object, color)
      Changes the font color and border color of object to "color"

   applyEventF(obj,ename,fname,capture)
      Applies a function fname to an object during an event ename.

   removeEventF(obj,ename,fname,capture)
      Remove a function fname from an object during an event ename.

   eventSource(e)
      Returns the object in which an event has occurred.

   eventPositionX(e)
      Returns the x-coordinate of the position of the event.

   eventPositionY(e)
      Returns the y-coordinate of the position of the event.

   getKeyCode(e)
      Returns the keyCode value of a keyboard event.

*/

var IE = document.attachEvent ? true:false;
var DOM = document.addEventListener ? true:false;


function placeIt(object, x, y) {
   object.style.left=x;
   object.style.top=y;
}


function getXCoord(object) {
   return parseInt(object.style.left);
}


function getYCoord(object) {
   return parseInt(object.style.top);
}


function withinIt(x, y, object) {
   within=false;
   otop=parseInt(object.style.top);
   obottom=otop+parseInt(object.style.height);
   oleft=parseInt(object.style.left);
   oright=oleft+parseInt(object.style.width);

   if ((y>otop && y<obottom) && (x>oleft && x<oright)) within=true;
   return within;
}


function colorIt(object, color) {
   object.style.color=color;
   object.style.borderColor=color;
}

function applyEventF(obj,ename,fname,cap) {
if (IE) obj.attachEvent("on"+ename, fname);
else if (DOM) obj.addEventListener (ename,fname,cap);
}

function removeEventF(obj,ename,fname,cap) {
if (IE) obj.detachEvent("on"+ename, fname);
else if (DOM) obj.removeEventListener(ename,fname,cap);
}

function eventSource(e) {
if (IE) return event.srcElement;
else if (DOM) return e.currentTarget;
}

function eventPositionX(e) {
if (IE) return event.clientX;
else if (DOM) return e.clientX;
}

function eventPositionY(e) {
if (IE) return event.clientY;
else if (DOM) return e.clientY;
}

function getKeyCode(e) {
if (IE) return event.keyCode;
else if (DOM) return e.keyCode;
}


