/*****************************************************************
 * Copyright (c) 2006 BP Studios, Inc. - All Rights Reserved
 *
 * @Author:        Ben Shatley
 * @Date:          05/2006
 *
 * Development Life Cycle:
 * 07/2006 – Original - BTS
 */

function ref(elid){
  var xel = document.getElementById(elid);
  return xel;
}

function SerializeCSSPair(a,b){
  if(a != null && b != null) return a + ":" + b + ";"
  if(a != null) return a + ":;";
  return "";
}

function Rect(x,y,w,h){
  this.X = (null != x ? x : 0);
  this.Y = (null != y ? y : 0);
  this.W = (null != w ? w : 0);
  this.H = (null != h ? h : 0);
  this.Bottom = y + h;
  this.Right = x + w;
  this.Serialize = function(){
    return "(" + this.X + "," + this.Y + "," +
      this.W + "," + this.H + ")";
  }
  this.Transform = function(dx,dy,dw,dh){
    this.X += dx;
    this.Y += dy;
    this.W += dw;
    this.H += dh;
    this.Bottom = this.Y + this.H;
    this.Right = this.X + this.W;
  }
  this.Contains = function(pos){
    if(pos.X < this.X) return false;
    if(pos.X > this.Right)return false;
    if(pos.Y < this.Y) return false;
    if(pos.Y > this.Bottom) return false;
    return true;
  }
  this.SerializeCSS = function(){
    return  SerializeCSSPair("left", this.X + "px") +
            SerializeCSSPair("top", this.Y + "px") +
            SerializeCSSPair("width", this.W + "px") +
            SerializeCSSPair("height", this.H + "px");
  }
}
function Position(x,y){
  this.X = (null != x ? x : 0);
  this.Y = (null != y ? y : 0);
  this.Serialize = function(){
    return "(" + this.X + "," + this.Y + ")";
  }
  this.AddTo = function(pos){
    pos.X += this.X;
    pos.Y += this.Y;
    return pos;
  }
}

function GetMousePosition(event){
	var posx = 0;
	var posy = 0;
	if (event.pageX || event.pageY)
	{
		posx = event.pageX;
		posy = event.pageY;
	}
	else if (event.clientX || event.clientY)
	{
		posx = event.clientX + document.body.scrollLeft;
		posy = event.clientY + document.body.scrollTop;
	}
	return new Position(posx,posy);
}

function makeLink(url, text){
  return "<a href='" + url + "' alt=''>" + text + "</a>";
}

/// event attach and detach courtesy of scottandrew.com

function attachEventHandler(obj, evType, fn){
  if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
  } 
  else if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    return true;
  } 
  else {
    //alert("Handler could not be attached");
  }
} 
function detachEventHandler(obj, evType, fn){
  if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } 
  else if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, false);
    return true;
  } 
  else {
    //alert("Handler could not be removed");
  }
}


/// find the position of an element which is
/// not absolutely positioned
function findPosX(obj){return getRealPos(obj, "Left");}
function findPosY(obj){return getRealPos(obj, "Top");}
function getRealPos(el,which){
	iPos = 0;
	while (el!=null) {
	 	iPos += el["offset" + which];
		el = el.offsetParent;
	}
	return iPos;
}

function getWidth(obj){return obj.offsetWidth;}
function getHeight(obj){return obj.offsetHeight;}
function getAbsoluteRect(obj){
  return new Rect(
    findPosX(obj),findPosY(obj),
    getWidth(obj), getHeight(obj)
  );
}
function getRelativeRect(obj){
  return new Rect(
    obj.offsetLeft, obj.offsetTop, 
    getWidth(obj), getHeight(obj)
  );
}

function makeAbsolute(obj){
  var rect = getAbsoluteRect(obj);
  obj.style.postion = "absolute";
  obj.style.left = rect.X + "px";
  obj.style.top = rect.Y + "px";
  obj.style.zIndex = 100;
}

function moveElement(obj, x, y){
  obj.style.left = x + "px";
  obj.style.top = y + "px";
}

/// cross-browser event processing
/// compliments of or inspired by
/// samples/tutorials at quirksmode.org

/// get the event target
function getEventTarget(event){
  if (event.target) targ = event.target;
	  else if (event.srcElement) targ = event.srcElement;
	  if (targ.nodeType == 3) // defeat Safari bug
		  targ = targ.parentNode;
  return targ;
}

function getEventChar(e){
	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	var character = String.fromCharCode(code);
	if(!character) return "";
	return character;
}

function whichMouseButton(event){
	var rightclick;
	if (event.which) rightclick = (event.which == 3);
	else if (event.button) rightclick = (event.button == 2);
	if(rightclick) return "right";
	return "left";
}

/// end cross-browser event processing from quirksmode.org


function getEventSerial(event){
  var trg = getEventTarget(event);
  var typ = event.type;
  var btn = whichMouseButton(event);
  var elRect = getAbsoluteRect(trg);
  var eSerial = 
    SerializeCSSPair("targrect", elRect.Serialize()) +
    SerializeCSSPair("etype", typ) +
    SerializeCSSPair("mbtn", btn);
  return eSerial;
}

function getAbsoluteRectSerial(obj){
  return getAbsoluteRect(obj).Serialize();
}

function getElementStyleFull(obj){
  if(obj.currentStyle){ // ie
    return obj.currentStyle;
  }
  else if(window.getComputedStyle){
    return window.getComputedStyle(obj,null);
  }
  return "null";
}

function getInlineCSSText(obj,lineEnding){
  var st = obj.style["cssText"];
  st = st.toLowerCase();
  if(lineEnding){
    st = st.split(";").join(";"+lineEnding);
  }
  return st;
}
function getStyle(obj,lineEnding){
  var st = obj.style;
  var s = "";
  for(i in st){
    if(st[i] && st[i] != null && st[i] != "")
      if(i != "cssText"){
        s += SerializeCSSPair(i, st[i]);
        if(lineEnding) s += lineEnding;
      }
  }
  return s;
}

// opacity function inspired by following tutorial:
// http://www.brainerror.net/scripts_js_blendtrans.php
// by ? (see blendtrans.js)
//change the opacity for different browsers
function setOpacity(opacity, obj) { 
    var object = obj.style; 
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
} 

function setStyle(obj,sName, val, vSuffix){
  obj.style[sName] = val + "" + vSuffix;
}
