registerNS("CM");

// The distance from the bottom of the screen where we flip the popup
// to appear above the button instead of below
CM.flipHeight = 200;

CM.move_box = function(an, box) {
  
  var cleft = 0;
  var ctop = 0;
  var obj = an;
  while (obj.offsetParent) {
    cleft += obj.offsetLeft;
    ctop += obj.offsetTop;
    obj = obj.offsetParent;
  }
  
  // Get the width and height of the viewport
  if (parseInt(navigator.appVersion)>3) {
	 if (navigator.appName=="Netscape") {
	  var winW = window.innerWidth-16;
	  var winH = window.innerHeight-16;
	 }
	 if (navigator.appName.indexOf("Microsoft")!=-1) {
	  var winW = document.body.offsetWidth-20;
	  var winH = document.body.offsetHeight-20;
	 }
	}
	
	//alert("Window height is " + winH);
  
  var bwidthstring = box.style.width;
  var bwidth = bwidthstring.substring(0,bwidthstring.length-2);
  var bheightstring = box.style.height;
  var bheight = bheightstring.substring(0,bheightstring.length-2);
  
  var bheight = box.offsetHeight;
  //alert("height is " + bheight); 
  
  box.style.left = (cleft - bwidth) + 'px';
  //ctop += an.offsetHeight;
  /*if (document.body.currentStyle &&
    document.body.currentStyle['marginTop']) {
    ctop += parseInt(
      document.body.currentStyle['marginTop']);
  }*/
  var bottomSpace = winH - ctop + CM.getScrollTop();
  //alert("bottomSpace is " + bottomSpace + " (need " + bheight + ")");
  //alert(CM.getScrollTop());
  if(bottomSpace < bheight || bottomSpace < CM.flipHeight)
  	ctop -= bheight;
  box.style.top = ctop + 'px';
  
}



CM.getScrollTop = function(){
	
	if (window.innerHeight)
	{
		return window.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	{
		return document.documentElement.scrollTop;
	}
	else if (document.body)
	{
		return document.body.scrollTop;
	}
	
	return 0;
	
}



CM.getScrollLeft = function(){
	
	if (window.innerHeight)
	{
		return window.pageXOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	{
		return document.documentElement.scrollLeft;
	}
	else if (document.body)
	{
		return document.body.scrollLeft;
	}
	
	return 0;
	
}



CM.show_hide_box = function(an, bwidth, bheight) {
  
  var href = an.href;
  var boxdiv = document.getElementById(href);

	// If we have already created the div, just show it
  if (boxdiv != null) {
    if (boxdiv.style.display == 'none') {
      boxdiv.style.display = 'block';
      CM.move_box(an, boxdiv);
      CM.currentBox = boxdiv;
      CM.currentButton = an;
    } else {
      boxdiv.style.display='none';
    }
 
    return false;
  }

	if(!bwidth) bwidth = 200;
	if(!bheight) bheight = 50;
	
  boxdiv = document.createElement('div');
  boxdiv.setAttribute('id', href);
  boxdiv.className = "filePopup";
  boxdiv.style.display = 'block';
  boxdiv.style.padding = '0px';
  boxdiv.style.position = 'absolute';
  boxdiv.style.width = bwidth + 'px';
  boxdiv.style.minHeight = bheight + 'px';
  
  var pars = "";
  
  var myAjax = new Ajax.Request(href, 
			{
				method: 'get', 
				parameters: pars, 
				onComplete: CM.fillContent
			});

  //boxdiv.appendChild(contents);
  boxdiv.innerHTML = "<img src='images/loading.gif' style='margin:10px'/>";
  document.body.appendChild(boxdiv);
  CM.move_box(an, boxdiv);
  CM.currentBox = boxdiv;
  CM.currentButton = an;

  return false;
  
}



CM.fillContent = function(originalRequest){
	
	CM.currentBox.innerHTML = originalRequest.responseText;
	CM.move_box(CM.currentButton, CM.currentBox);

}


CM.mouseClick = function(e){
	
	if (navigator.appName == "Microsoft Internet Explorer"){
		var e = window.event
	}
	
	// Get the width and height of the viewport
  if (parseInt(navigator.appVersion)>3) {
	 if (navigator.appName=="Netscape") {
	  var winW = window.innerWidth-16;
	  var winH = window.innerHeight-16;
	 }
	 if (navigator.appName.indexOf("Microsoft")!=-1) {
	  var winW = document.body.offsetWidth-20;
	  var winH = document.body.offsetHeight-20;
	 }
	}
	
	if(!CM.currentBox || !Position.within(CM.currentBox, e.clientX + CM.getScrollLeft(), e.clientY + CM.getScrollTop())){
		// Click is outside box: hide it, as long as it's not a click on the scrollbar
		if(e.clientX < winW){
			if(CM.currentBox) CM.currentBox.style.display = 'none';
		}
	}
		
}

CM.setFileDesc = function(text){
	
	var desc = document.getElementById("fileDesc");
	desc.innerHTML = text;
	
}


document.onmousedown = CM.mouseClick;