/*global variables and functions*/

/*browser*/
var jsGlob = new makeJsGlob();
function makeJsGlob(){
  //properties
  this.browserCode;
  this.browserVersion;
  this.bodyWidth;
  this.bodyHeight;
  //methods
  this.checkBrowser = checkBrowser;
  this.splitOnUpper = splitOnUpper;
  this.makeAccesKey = makeAccesKey;
  this.getElementDimensions = getElementDimensions;
  this.getBodyDimension = getBodyDimension;
  this.setElementDimension = setElementDimension;
  this.setElementWidth = setElementWidth;
  this.setElementHeight = setElementHeight;
  this.getElementPosition = getElementPosition;
  this.centerElement = centerElement;
  this.inArray = inArray;
  this.makeArrayFromPhp = makeArrayFromPhp;
  this.decToHex = decToHex;
  this.hexToDec = hexToDec;
  this.isGreater = isGreater;
  this.setAttribute = setAttribute;
  this.getBackgroundColor = getBackgroundColor;
  this.checkBrowser();
}
function checkBrowser(){
  if(navigator.appName == 'Netscape'){
    this.browserCode = 'm';
  }else if(navigator.appName == 'Microsoft Internet Explorer'){
    this.browserCode = 'e';
    strVersion = new String(navigator.appVersion);
    var startVersion = strVersion.indexOf('MSIE') + 5;
    var stopVersion = strVersion.indexOf(';',startVersion);
    this.browserVersion = strVersion.substring(startVersion,stopVersion);
  }
}

/*display*/

function splitOnUpper(text){
  var strText = new String(text);
  var rExp = /([A-Z])/g;
  var rep = ' $1';
  var newText = strText.replace(rExp,rep);
  return newText;
}

function makeAccesKey(text){
  var strText = new String(text);
  var char;
  for(i=0;i<strText.length;i++){
    char = strText.charAt(i);
    if(!this.inArray(aAccessKeys,char)){
      aAccessKeys.push(char);
      return ' accesskey="'+char+'"';
    }
  }
}

function getElementDimensions(id){
  var oElement = document.getElementById(id);
  var elementWidth = Number(oElement.offsetWidth);
  var elementHeight = Number(oElement.offsetHeight);
  var aDimensions = new Array(elementWidth,elementHeight);
  return aDimensions;
}

function getBodyDimension(){
  var aDimensions = this.getElementDimensions('body');
  this.bodyWidth = aDimensions[0];
  this.bodyHeight = aDimensions[1];
}

function setElementDimension(id,horizontalLoss,verticalLoss){
  this.getBodyDimension();
  this.setElementWidth(id,horizontalLoss);
  this.setElementHeight(id,verticalLoss);
}

function setElementWidth(id,horizontalLoss){
  this.getBodyDimension();
  var oElement = document.getElementById(id);
  var newWidth = this.bodyWidth - horizontalLoss;
  oElement.style.width = newWidth + 'px';
}

function setElementHeight(id,verticalLoss){
  this.getBodyDimension();
  var oElement = document.getElementById(id);
  var newHeight = this.bodyHeight - verticalLoss;
  oElement.style.height = newHeight + 'px';
}

function centerElement(id){
  var aDimensions = this.getElementDimensions(id);
  var elementWidth = aDimensions[0];
  var elementHeight = aDimensions[1];
  
  var oElement = document.getElementById(id);
  this.getBodyDimension();
  if(elementHeight > this.bodyHeight){
    elementHeight = this.bodyHeight;
    oElement.style.height = elementHeight+'px';
  }
  oElement.style.position = 'absolute';
  oElement.style.left = '50%';
  oElement.style.top = '50%';
  var backLeft = '-' + elementWidth / 2 + 'px';
  oElement.style.marginLeft = backLeft;
  /*oElement.style.marginLeft = '-' + backLeft + 'px';*/
  oElement.style.marginTop = '-'+(elementHeight / 2)+'px';
}

function getElementPosition(id){
  var oElement = document.getElementById(id);
	var curLeft = curTop = 0;
	if (oElement.offsetParent) {
		curLeft = oElement.offsetLeft;
		curTop = oElement.offsetTop;
		while (oElement = oElement.offsetParent){
			curLeft += oElement.offsetLeft
			curTop += oElement.offsetTop
		}
	}
  var aPosition = new Array(curLeft,curTop);
  return aPosition;
}

/*data manipulation*/
function inArray(array,toFind){
	return ('_|_'+array.join('_|_')+'_|_').indexOf('_|_'+toFind+'_|_') > -1;
}
function makeArrayFromPhp(arrayText){
  var sArray = new String(arrayText);
  var sElement;
  var aElement;
  var a = sArray.split('\n');
  for(i=0;i<a.length;i++){
    sElement = new String(a[i]);
    aElement = sElement.split('\t');
    a[i] = aElement;
  }
  return a;
}
function decToHex(d){
  return d.toString(16);
}
function hexToDec(h){
  return parseInt(h,16);
}
function isGreater(a,b,c){
  if((a > b) && (a > c)) return true;
  else return false;
}

/*DOM*/
function setAttribute(obj,name,value){
  var oAttribute = document.createAttribute(name);
  oAttribute.value = value;
  obj.setAttributeNode(oAttribute);
}

function getBackgroundColor(id){
  var oElement = document.getElementById('tool_tip');
  var colorHexStart;
  var sColorHexStart;
  if(oElement.currentStyle){
    sColorHexStart = new String(oElement.currentStyle['backgroundColor']);
    sColorHexStart = sColorHexStart.replace('#','');
  }else if(window.getComputedStyle){
    sColorHexStart = new String(document.defaultView.getComputedStyle(oElement,null).getPropertyValue('background-color'));
    sColorHexStart = sColorHexStart.replace('rgb(','');
    sColorHexStart = sColorHexStart.replace(')','');
    sColorHexStart = sColorHexStart.replace(' ','');
    sColorHexStart = sColorHexStart.replace(' ','');
    var aColorHexStart = sColorHexStart.split(',');
    var r = new Number(aColorHexStart[0]);
    r = jsGlob.decToHex(r);
    var g = new Number(aColorHexStart[1]);
    g = jsGlob.decToHex(g);
    var b = new Number(aColorHexStart[2]);
    b = jsGlob.decToHex(b);
    sColorHexStart = r+g+b;
  }
  return sColorHexStart;
}