﻿/*
  sigma bcn
  http://www.sigmabcn.com
	
  Servicios de Ingeniería y Gestión para la Mejora de Actividades, S.L.
	
  Copyright (c) 2008
*/

//**************************************************************************************************************

// DOM FUNCTIONS
// =============

// myGetElementById(sElem)
//  Return a reference to an Element object or null if sElem is invalid.
//  Parameters: sElem - string with the id object reference
function myGetElementById(sElem) {
	var oRef = null;
  if(typeof(sElem)=='string') {
    if(document.getElementById) {
    	oRef=document.getElementById(sElem);
    } else {
    	if(document.all) {
    		oRef=document.all[sElem];
    	} else {
    		oRef=null;
    	}
    }
  }
  return oRef;
}

// myGetElementsByTagName(sTagName[, oParentElem])
//  Returns an array of elements which are descendants of parentElem and have sTagName. If parentElem is null or
//  not present, document will be used. If sTagName is null or not present, "*" will be used. This even works
//  with IE4. Looks for the sTagName HTMLTagElements defined inside oParentElem.
//   Parameters: sTagName - Tag string
//							 oParentElem - object reference (optional)
function myGetElementsByTagName(sTagName, oParentElem) {
  var list = null;
  
  sTagName = sTagName || '*';
  oParentElem = oParentElem || document;
  
  if(oParentElem.getElementsByTagName(sTagName)) {
  	// DOM1
    list = oParentElem.getElementsByTagName(sTagName);
    if(sTagName=='*' && (!list || !list.length)) {
    	// IE5 '*' bug
    	list = oParentElem.all;
    }
  } else {
  	// IE4 object model
  	if(sTagName=='*') {
  		list = oParentElem.all;
  	} else {
  		if(oParentElem.all && oParentElem.all.tags) list = oParentElem.all.tags(sTagName);
  	}
  }
  return list || new Array();
}

// myGetElementsByAttribute(sTagName, sAtt, sRE[, fn])
//  Return an array of all sTagName elements whose sAtt attribute matches sRE. sAtt can also be a property name but
//  the property must be of type string.
//  Parameters: sTagName - Tag string
//	 					  sAtt - Attribute name in the sTagName string
//		 				  sRE - Attribute value to match for sAtt in sTagName string
//			 			  fn - callback function, iterates thru the returned list (optional)
function myGetElementsByAttribute(sTagName, sAtt, sRE, fn) {
  var a, list, found = new Array(), re = new RegExp(sRE, 'i');
  list = myGetElementsByTagName(sTagName);
  for(var i = 0; i < list.length; ++i) {
    a = list[i].getAttribute(sAtt);
    if(!a) {a = list[i][sAtt];}
    if(typeof(a)=='string' && a.search(re) != -1) {
      found[found.length] = list[i];
      if(fn) fn(list[i]);
    }
  }
  return found;
}

// myParent(sElem[, bNode])
//  Return a reference to the element's parent element, or null if not found.
//  Parameters: sElem - string with the id object reference
//						  bNode - pass true if you want parentNode, else you get offsetParent (optional)
function myParent(sElem, bNode) {
  if(!(sElem=myGetElementById(sElem))) {
  	return null;
  }
  var p=null;
  if(!bNode && myDef(sElem.offsetParent)) {
  	p=sElem.offsetParent;
  } else {
  	if(myDef(sElem.parentNode)) {
  		p=sElem.parentNode;
  	} else {
  		if(myDef(sElem.parentElement)) p=sElem.parentElement;
  	}
  }
  return p;
}

// myGetComputedStyle(oEle, sProp, bInt)
//  For sProp use the css property name, not the object property name. For finding width this works
//  in Moz and Op, but in IE this only works if padding and border use pixel units in the CSS.
//  Parameters: oElem - element object
//						  sProp - css property name
//						  bInt - if true, return value is an integer
function myGetComputedStyle(oElem, sProp, bInt) {
  var s, p = 'undefined';
  var dv = document.defaultView;
  if(dv && dv.getComputedStyle){
    s = dv.getComputedStyle(oElem,'');
    if(s) p = s.getPropertyValue(sProp);
  }
  else if(oElem.currentStyle) {
    // convert css property name to object property name for IE
    var i, c, a = sProp.split('-');
    sProp = a[0];
    for (i=1; i<a.length; ++i) {
      c = a[i].charAt(0);
      sProp += a[i].replace(c, c.toUpperCase());
    }
    p = oElem.currentStyle[sProp];
  }
  else return null;
  return bInt ? (parseInt(p) || 0) : p;
}

//**************************************************************************************************************

// APPEARANCE FUNCTIONS
// ====================

// myBackground(sElem[, sBgColor[, sBgImage]])
//  Return and optionally set the element's background color and image.
//  Parameters: sElem - string with the id object reference
//						  sBgColor - background color string (optional)
//						  sBgImage - background image URL string (optional)
function myBackground(sElem, sBgColor, sBgImage) {
  if(!(sElem=myGetElementById(sElem))) return '';
  var bg='';
  if(sElem.style) {
    if(myStr(sBgColor)) {
    	sElem.style.backgroundColor=sBgColor;
    }
    if(myStr(sBgImage)) {
    	sElem.style.backgroundImage=(sBgImage!='')? 'url('+sBgImage+')' : null;
    }
    bg=sElem.style.backgroundColor;
  }
  return bg;
}

// myColor(sElem[, sColor])
//  Return and optionally set the element's text color.
//  Parameters: sElem - string with the id object reference
//						  sColor - color string (optional)
function myColor(sElem, sColor) {
  if(!(sElem=myGetElementById(sElem))) return '';
  var c='';
  if(sElem.style && myDef(sElem.style.color)) {
    if(myStr(sColor)) sElem.style.color=sColor;
    c=sElem.style.color;
  }
  return c;
}

// myVisibility(sElem, bShow)
//  Return and optionally set the element's visibility.
//  Parameters: sElem - string with the id object reference
//						  bShow - true = show, false = hide
function myVisibility(sElem, bShow) {
  if(!(sElem=myGetElementById(sElem))) return null;
  if(sElem.style && myDef(sElem.style.visibility)) {
    if(myDef(bShow)) {
    	sElem.style.visibility = bShow ? 'visible' : 'hidden';
    }
    return sElem.style.visibility;
  }
  return null;
}

// myHide(sElem)
//  Set the element's visibility to 'hidden' ('hide' for NN4). Calls myVisibility().
//  Parameters: sElem - string with the id object reference
function myHide(sElem) {
	return myVisibility(sElem, 0);
}

// myShow(sElem)
//  Set the element's visibility to 'visible' ('show' for NN4). Calls myVisibility().
//  Parameters: sElem - string with the id object reference
function myShow(sElem) {
	return myVisibility(sElem, 1);
}

// myDisplay(sElem, sProp)
//  Return and optionally set the element's display property.
//  Parameters: sElem - string with the id object reference
//						  sProp - 'none', 'inline', 'block', etc.
function myDisplay(sElem, sProp) {
  if(!(sElem=myGetElementById(sElem))) return null;
  if(sElem.style && myDef(sElem.style.display)) {
    if(myStr(sProp)) sElem.style.display = sProp;
    return sElem.style.display;
  }
  return null;
}

// myZIndex(sElem[, uZ])
//  Return and optionally set the element's z-index.
//  Parameters: sElem - string with the id object reference
//						  uZ - unsigned integer z-index (optional)
function myZIndex(sElem, uZ) {
  if(!(sElem=myGetElementById(sElem))) return 0;
  if(sElem.style && myDef(sElem.style.zIndex)) {
    if(myNum(uZ)) sElem.style.zIndex=uZ;
    uZ=parseInt(sElem.style.zIndex);
  }
  return uZ;
}

//**************************************************************************************************************

// GENERIC FUNCTIONS
// =================

// myDef()
//  Return true if all arguments are defined.
function myDef() {
  for(var i=0; i<arguments.length; ++i) {
  	if(typeof(arguments[i])=='undefined' || typeof(arguments[i])==null) return false;
  }
  return true;
}

// myNum()
//  Return true if typeof(ref) == 'number'.
function myNum() {
  for(var i=0; i<arguments.length; ++i) { 
  	if(isNaN(arguments[i]) || typeof(arguments[i])!='number') return false;
  }
  return true;
}

// myStr()
//  Return true if typeof(ref) == 'string'.
function myStr() {
  for(var i=0; i<arguments.length; ++i) {
  	if(typeof(arguments[i])!='string') return false;
  }
  return true;
}

// myGetInnerText(sElem)
//  Return the inner text of the element.
//  Parameters: sElem - string with the id object reference
function myGetInnerText(sElem) {
	var sInnerText = null;
	if(!(sElem=myGetElementById(sElem))) return null;
	if(document.all){
		sInnerText = sElem.innerText;
	} else {
		sInnerText = sElem.textContent;
	}
	return sInnerText;
}

// myGetInnerTextFromObj(oElem)
//  Return the inner text of the element.
//  Parameters: oElem - object reference
function myGetInnerTextFromObj(oElem) {
	var sInnerText = null;
	if(document.all){
		sInnerText = oElem.innerText;
	} else {
		sInnerText = oElem.textContent;
	}
	return sInnerText;
}

//**************************************************************************************************************

// WINDOW FUNCTIONS
// ================

// myClientHeight()
//  Return the inner height of the window.
function myClientHeight() {
  var v=0,d=document,w=window;
  if(d.compatMode == 'CSS1Compat' && d.documentElement && d.documentElement.clientHeight)
    {v=d.documentElement.clientHeight;}
  else if(d.body && d.body.clientHeight)
    {v=d.body.clientHeight;}
  else if(myDef(w.innerWidth,w.innerHeight,d.width)) {
    v=w.innerHeight;
    if(d.width>w.innerWidth) v-=16;
  }
  return v;
}

// myClientWidth()
//  Return the inner width of the window.
function myClientWidth() {
  var v=0,d=document,w=window;
  if(d.compatMode == 'CSS1Compat' && d.documentElement && d.documentElement.clientWidth)
    {v=d.documentElement.clientWidth;}
  else if(d.body && d.body.clientWidth)
    {v=d.body.clientWidth;}
  else if(myDef(w.innerWidth,w.innerHeight,d.height)) {
    v=w.innerWidth;
    if(d.height>w.innerHeight) v-=16;
  }
  return v;
}

//**************************************************************************************************************

// SIZE FUNCTIONS
// ==============

// myHeight(sElem[, iHeight])
//  Return and optionally set the element's height.
//  Parameters: sElem - string with the id object reference
//						  iHeight - unsigned integer height; it is rounded to an integer (optional)
function myHeight(sElem, iHeight) {
  if(!(sElem=myGetElementById(sElem))) return 0;
  if (myNum(iHeight)) {
    if (iHeight<0) iHeight = 0;
    else iHeight=Math.round(iHeight);
  }
  else iHeight=-1;
  var css=myDef(sElem.style);
  if (sElem == document || sElem.tagName.toLowerCase() == 'html' || sElem.tagName.toLowerCase() == 'body') {
    iHeight = myClientHeight();
  }
  else if(css && myDef(sElem.offsetHeight) && myStr(sElem.style.height)) {
    if(iHeight>=0) {
      var pt=0,pb=0,bt=0,bb=0;
      if (document.compatMode=='CSS1Compat') {
        var gcs = myGetComputedStyle;
        pt=gcs(sElem,'padding-top',1);
        if (pt !== null) {
          pb=gcs(sElem,'padding-bottom',1);
          bt=gcs(sElem,'border-top-width',1);
          bb=gcs(sElem,'border-bottom-width',1);
        }
        // Should we try this as a last resort?
        // At this point getComputedStyle and currentStyle do not exist.
        else if(myDef(sElem.offsetHeight,sElem.style.height)){
          sElem.style.height=iHeight+'px';
          pt=sElem.offsetHeight-iHeight;
        }
      }
      iHeight-=(pt+pb+bt+bb);
      if(isNaN(iHeight)||iHeight<0) return;
      else sElem.style.height=iHeight+'px';
    }
    iHeight=sElem.offsetHeight;
  }
  else if(css && myDef(sElem.style.pixelHeight)) {
    if(iHeight>=0) sElem.style.pixelHeight=iHeight;
    iHeight=sElem.style.pixelHeight;
  }
  return iHeight;
}

// myWidth(sElem[, iWidth])
//  Return and optionally set the element's width.
//  Parameters: sElem - string with the id object reference
//						  iWidth - unsigned integer width; it is rounded to an integer (optional)
function myWidth(sElem, iWidth) {
  if(!(sElem=xGetElementById(sElem))) return 0;
  if (myNum(iWidth)) {
    if (iWidth<0) iWidth = 0;
    else iWidth=Math.round(iWidth);
  }
  else iWidth=-1;
  var css=myDef(sElem.style);
  if (sElem == document || sElem.tagName.toLowerCase() == 'html' || sElem.tagName.toLowerCase() == 'body') {
    iWidth = myClientWidth();
  }
  else if(css && myDef(sElem.offsetWidth) && myStr(sElem.style.width)) {
    if(iWidth>=0) {
      var pl=0,pr=0,bl=0,br=0;
      if (document.compatMode=='CSS1Compat') {
        var gcs = myGetComputedStyle;
        pl=gcs(sElem,'padding-left',1);
        if (pl !== null) {
          pr=gcs(sElem,'padding-right',1);
          bl=gcs(sElem,'border-left-width',1);
          br=gcs(sElem,'border-right-width',1);
        }
        // Should we try this as a last resort?
        // At this point getComputedStyle and currentStyle do not exist.
        else if(myDef(sElem.offsetWidth,sElem.style.width)){
          sElem.style.width=iWidth+'px';
          pl=sElem.offsetWidth-iWidth;
        }
      }
      iWidth-=(pl+pr+bl+br);
      if(isNaN(iWidth)||iWidth<0) return;
      else sElem.style.width=iWidth+'px';
    }
    iWidth=sElem.offsetWidth;
  }
  else if(css && myDef(sElem.style.pixelWidth)) {
    if(iWidth>=0) sElem.style.pixelWidth=iWidth;
    iWidth=sElem.style.pixelWidth;
  }
  return iWidth;
}

// myResizeTo(sElem, iWidth, iHeight)
//  Set the element's width and height.
//  Parameters: sElem - string with the id object reference
//						  iWidth - unsigned integer width; it is rounded to an integer
//						  iHeight - unsigned integer height; it is rounded to an integer
function myResizeTo(sElem, iWidth, iHeight) {
  myWidth(sElem,iWidth);
  myHeight(sElem,iHeight);
}

//**************************************************************************************************************

// POSITION FUNCTIONS
// ==================

// myLeft(sElem[, iX])
//  Return and optionally set the element's x coordinate.
//  Parameters: sElem - string with the id object reference
//						  iX - integer x coordinate (optional)
function myLeft(sElem, iX) {
  if(!(sElem=myGetElementById(sElem))) return 0;
  var css=myDef(sElem.style);
  if(css && myStr(sElem.style.left)) {
    if(myNum(iX)) {
    	sElem.style.left=iX+'px';
    } else {
      iX=parseInt(sElem.style.left);
      if(isNaN(iX)) iX=myGetComputedStyle(sElem,'left',1);
      if(isNaN(iX)) iX=0;
    }
  } else {
  	if(css && myDef(sElem.style.pixelLeft)) {
    	if(myNum(iX)) sElem.style.pixelLeft=iX;
    	else iX=sElem.style.pixelLeft;
  	}
	}
  return iX;
}

// myTop(sElem[, iY])
//  Return and optionally set the element's y coordinate.
//  Parameters: sElem - string with the id object reference
//						  iY - integer y coordinate (optional)
function myTop(sElem, iY) {
  if(!(sElem=myGetElementById(sElem))) return 0;
  var css=myDef(sElem.style);
  if(css && myStr(sElem.style.top)) {
    if(myNum(iY)) {
    	sElem.style.top=iY+'px';
    } else {
      iY=parseInt(sElem.style.top);
      if(isNaN(iY)) iY=myGetComputedStyle(sElem,'top',1);
      if(isNaN(iY)) iY=0;
    }
  } else {
  	if(css && myDef(sElem.style.pixelTop)) {
    	if(myNum(iY)) sElem.style.pixelTop=iY;
    	else iY=sElem.style.pixelTop;
  	}
  }
  return iY;
}

// myMoveTo(sElem,iX , iY)
//  Set the element's x and y coordinates.
//  Parameters: sElem - string with the id object reference
//						  iX - integer x coordinate
//						  iY - integer y coordinate
function myMoveTo(sElem, iX, iY) {
  myLeft(sElem,iX);
  myTop(sElem,iY);
}

// myOffsetLeft(sElem)
//  Return the element's X offset within its parent element.
//  Parameters: sElem - string with the id object reference
function myOffsetLeft(sElem) {
  if (!(sElem=myGetElementById(sElem))) return 0;
  if (myDef(sElem.offsetLeft)) return sElem.offsetLeft;
  else return 0;
}

// myOffsetTop(sElem)
//  Return the element's Y offset within its parent element.
//  Parameters: sElem - string with the id object reference
function myOffsetTop(sElem) {
  if (!(sElem=myGetElementById(sElem))) return 0;
  if (myDef(sElem.offsetTop)) return sElem.offsetTop;
  else return 0;
}

// myPageX(sElem)
//  Return the element's absolute x coordinate.
//  Parameters: sElem - string with the id object reference
function myPageX(sElem) {
  if (!(sElem=myGetElementById(sElem))) return 0;
  var x = 0;
  while(sElem) {
    if(myDef(sElem.offsetLeft)) x += sElem.offsetLeft;
    sElem = myDef(sElem.offsetParent) ? sElem.offsetParent : null;
  }
  return x;
}

// myPageY(sElem)
//  Return the element's absolute y coordinate.
//  Parameters: sElem - string with the id object reference
function myPageY(sElem) {
  if (!(sElem=myGetElementById(sElem))) return 0;
  var y = 0;
  while(sElem) {
    if(myDef(sElem.offsetTop)) y += sElem.offsetTop;
    sElem = myDef(sElem.offsetParent) ? sElem.offsetParent : null;
  }
  return y;
}

// myScrollLeft(sElem, bWin)
//  Return the number of pixels the element (or window) has scrolled horizontally.
//  Parameters: sElem - string with the id object reference. If undefined return document scrollLeft.
function myScrollLeft(sElem, bWin) {
  var offset=0;
  if (!myDef(sElem) || bWin || sElem == document || sElem.tagName.toLowerCase() == 'html' || sElem.tagName.toLowerCase() == 'body') {
    var w = window;
    if (bWin && sElem) w = sElem;
    if(w.document.documentElement && w.document.documentElement.scrollLeft) offset=w.document.documentElement.scrollLeft;
    else if(w.document.body && myDef(w.document.body.scrollLeft)) offset=w.document.body.scrollLeft;
  } else {
    sElem = myGetElementById(sElem);
    if (sElem && myNum(sElem.scrollLeft)) offset = sElem.scrollLeft;
  }
  return offset;
}

// myScrollTop(sElem, bWin)
//  Return the number of pixels the element (or window) has scrolled vertically.
//  Parameters: sElem - string with the id object reference. If undefined return document scrollTop.
function myScrollTop(sElem, bWin) {
  var offset=0;
  if (!myDef(sElem) || bWin || sElem == document || sElem.tagName.toLowerCase() == 'html' || sElem.tagName.toLowerCase() == 'body') {
    var w = window;
    if (bWin && sElem) w = sElem;
    if(w.document.documentElement && w.document.documentElement.scrollTop) offset=w.document.documentElement.scrollTop;
    else if(w.document.body && myDef(w.document.body.scrollTop)) offset=w.document.body.scrollTop;
  } else {
    sElem = myGetElementById(sElem);
    if (sElem && myNum(sElem.scrollTop)) offset = sElem.scrollTop;
  }
  return offset;
}

//**************************************************************************************************************
