//calculate top of element from screen

function CalculateTop( ctObj, fromRel ){
  tmpObj = ctObj;
  tmpTop = ctObj.offsetTop;
    while( tmpObj.offsetParent != null ){
          
          tmpObj = tmpObj.offsetParent;
          tmpTop += tmpObj.offsetTop;
          tmpBorderSize = parseInt( PS_getCurrentStyle(tmpObj,"border-top-width") ); 
          if( tmpBorderSize  > 0){
            tmpTop +=	tmpBorderSize;
          }
          if( fromRel == true && PS_getCurrentStyle(tmpObj,"position") == "relative" ){
          	break;
          }
  }
  return tmpTop;
}


//calculate top of element from screen

function CalculateLeft( clObj, fromRel ){
  tmp1Obj = clObj;
  tmpLeft = clObj.offsetLeft;
  while( tmp1Obj.offsetParent != null ){
     
          tmp1Obj = tmp1Obj.offsetParent;
          tmpLeft += tmp1Obj.offsetLeft;
          
          tmpBorderSize = parseInt( PS_getCurrentStyle(tmp1Obj,"border-left-width") ); 
          if( tmpBorderSize  > 0){
            tmpLeft +=	tmpBorderSize;
          }
          if( fromRel == true && PS_getCurrentStyle(tmp1Obj,"position") == "relative" ){
          	break;
          }
     		
  }
  
  return tmpLeft;
}

// get current style
function PS_getCurrentStyle(elem, prop)
{
	
   if( elem.currentStyle )
   {  
      var ar = prop.match(/\w[^-]*/g);
      var s = ar[0];
      
      for(var i = 1; i < ar.length; ++i)		   
      {
         s += ar[i].replace(/\w/, ar[i].charAt(0).toUpperCase());
      }

      return elem.currentStyle[s]
   }
   else if( document.defaultView.getComputedStyle )
   {
      return document.defaultView.getComputedStyle(elem, null).getPropertyValue(prop);
   }else{
   	return 0;
   }
   
}



function CalculateOffset( ctObj ){
	
	if( document.documentElement["getBoundingClientRect"] ){
	
		var box  = ctObj.getBoundingClientRect(), doc = ctObj.ownerDocument, body = doc.body, docElem = doc.documentElement,
			clientTop = docElem.clientTop || body.clientTop || 0, clientLeft = docElem.clientLeft || body.clientLeft || 0,
			top  = box.top  + (self.pageYOffset  || body.scrollTop ) - clientTop;
			left = box.left + (self.pageXOffset ||  body.scrollLeft) - clientLeft;
		return { top: top, left: left };
	  
	}else{
		alert('normal');
		var top, left;
		if ( !ctObj ) return { top: 0, left: 0 };
		if ( ctObj === ctObj.ownerDocument.body ){
			top  = body.offsetTop,
			left = body.offsetLeft;
			return { top: top, left: left };
		}
		var elem = ctObj, offsetParent = elem.offsetParent, prevOffsetParent = elem,
			doc = elem.ownerDocument, computedStyle, docElem = doc.documentElement,
			body = doc.body, defaultView = doc.defaultView,
			prevComputedStyle = defaultView.getComputedStyle(elem, null),
			top = elem.offsetTop, left = elem.offsetLeft;

		while ( (elem = elem.parentNode) && elem !== body && elem !== docElem ) {
			computedStyle = defaultView.getComputedStyle(elem, null);
			top -= elem.scrollTop, left -= elem.scrollLeft;
			if ( elem === offsetParent ) {
				top += elem.offsetTop, left += elem.offsetLeft;
				if (  elem.tagName =='table' || elem.tagName =='td' || elem.tagName =='th' )
					top  += parseInt( computedStyle.borderTopWidth,  10) || 0,
					left += parseInt( computedStyle.borderLeftWidth, 10) || 0;
				prevOffsetParent = offsetParent, offsetParent = elem.offsetParent;
			}
			if ( computedStyle.overflow !== "visible" )
				top  += parseInt( computedStyle.borderTopWidth,  10) || 0,
				left += parseInt( computedStyle.borderLeftWidth, 10) || 0;
			prevComputedStyle = computedStyle;
		}

		if ( prevComputedStyle.position === "relative" || prevComputedStyle.position === "static" )
			top  += body.offsetTop,
			left += body.offsetLeft;

		if ( prevComputedStyle.position === "fixed" )
			top  += Math.max(docElem.scrollTop, body.scrollTop),
			left += Math.max(docElem.scrollLeft, body.scrollLeft);

		 return { top: top, left: left };
	}
  
}


//calculate top of element from screen

function CalculateRelativeOffset( clObj ){
  tmp1Obj = clObj;
  tmpLeft = clObj.offsetLeft;
  while( tmp1Obj.offsetParent != null ){
     
          tmp1Obj = tmp1Obj.offsetParent;
          if( PS_getCurrentStyle(tmp1Obj,"position") == "relative" ){
          	break;
          }
          
     		
  }
  curOffsets = CalculateOffset( clObj );
  parentOffsets = CalculateOffset( tmp1Obj );
  
  return { top: (curOffsets.top-parentOffsets.top), left: (curOffsets.left-parentOffsets.left)};
}

/*
function PS_getCurrentStyle(elem, prop)
{
	
   if( document.defaultView.getComputedStyle )
   {
      return document.defaultView.getComputedStyle(elem, null).getPropertyValue(prop);
   }else if( elem.currentStyle )
   { */ 
     // var ar = prop.match(/\w[^-]*/g);
    /*  var s = ar[0];
      
      for(var i = 1; i < ar.length; ++i)		   
      {
         s += ar[i].replace(/\w/, ar[i].charAt(0).toUpperCase());
      }

      return elem.currentStyle[s]
   } else{
   	return 0;
   }
   
}*/


var readyBound;
function bindReady(){
	if ( readyBound ) return;
	readyBound = true;

	// Mozilla, Opera and webkit nightlies currently support this event
	if ( document.addEventListener ) {
		// Use the handy event callback
		document.addEventListener( "DOMContentLoaded", function(){
			document.removeEventListener( "DOMContentLoaded", arguments.callee, false );
			DocumentOnReadyCallBack();
		}, false );

	// If IE event model is used
	} else if ( document.attachEvent ) {
		// ensure firing before onload,
		// maybe late but safe also for iframes
		document.attachEvent("onreadystatechange", function(){
			if ( document.readyState === "complete" ) {
				document.detachEvent( "onreadystatechange", arguments.callee );
				DocumentOnReadyCallBack();
			}
		});

		// If IE and not an iframe
		// continually check to see if the document is ready
		if ( document.documentElement.doScroll && window == window.top ) (function(){
			

			try {
				// If IE is used, use the trick by Diego Perini
				// http://javascript.nwbox.com/IEContentLoaded/
				document.documentElement.doScroll("left");
			} catch( error ) {
				setTimeout( arguments.callee, 0 );
				return;
			}

			// and execute any waiting functions
			DocumentOnReadyCallBack();
		})();
	}

	// A fallback to window.onload, that will always work
	window.onload = DocumentOnReadyCallBack
}

function DocumentOnReadyCallBack(){
	
	// PUT ALL FUNCTIONS TO BE CALLED ON DOCUMENT READY HERE
	//alert( "Document is ready.");
	
}

bindReady();
	

function GetOverlaySize(divid){
	var obj= document.getElementById(divid)
	var box = obj.getBoundingClientRect();
	alert( box.left + " - " + box.right + " - "+ box.top + " - " +box.bottom)
	
	var overlay = document.getElementById('overlay')
	
}

function GetRenderedHeight(obj, currentMax ){
	var box = obj.getBoundingClientRect();
	var max = box.bottom;
	
}