//http://www.pat-burt.com/web-development/how-to-do-a-css-popup-without-opening-a-new-window/
/*function ghost_size (ghost) { //div, divheight
	if (typeof window.innerWidth != 'undefined') {
		viewportheight = window.innerHeight;
	} else {
		viewportheight = document.documentElement.clientHeight;
	}
	
	if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
		blanket_height = viewportheight;
	} else {
		if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
			blanket_height = document.body.parentNode.clientHeight;
		} else {
			blanket_height = document.body.parentNode.scrollHeight;
		}
	}
	
	$(ghost).style.height = blanket_height + 'px';
	//popupHeight = blanket_height / 2 - divheight; //150 half popup height
	//$(div).style.top = popupHeight + 'px';
}*/

//new ghost2.0 function
function ghost_size (ghost) { //div, divheight
	
	if (typeof window.innerWidth != 'undefined') {
		viewportheight = window.innerHeight;		
	} else {
		viewportheight = document.documentElement.clientHeight;
	}
	
	if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
		blanket_height = viewportheight;
	} else {
		if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {			
			blanket_height = document.body.parentNode.clientHeight;
		} else {
			blanket_height = document.body.parentNode.scrollHeight;
		}
	}
		
	if (/MSIE/.test(navigator.userAgent)){ //test for MSIE x.x; http://www.javascriptkit.com/javatutors/navigator.shtml
		//alert('ie');
		document.getElementsByTagName('body')[0].scrollHeight;
		document.getElementsByTagName('body')[0].scrollWidth;
		var dim = $(document.body).getDimensions();
		if (document.getElementsByTagName('body')[0].scrollHeight > dim.height) { //scroll
			//alert('scroll');			
			$(ghost).style.height = document.getElementsByTagName('body')[0].scrollHeight + 'px';
			if (document.getElementsByTagName('body')[0].scrollWidth > dim.width) {
				$(ghost).style.width = document.getElementsByTagName('body')[0].scrollWidth + 'px';			
			} else {
				$(ghost).style.width = dim.width + 'px';					
			}
		} else {
			//alert('no scroll');			
			$(ghost).style.height = dim.height + 'px';
			$(ghost).style.width = '100%';
		}
	} else {
		$(ghost).style.height = blanket_height + 'px';
	}
	
	return false;	
}


//find the total width of the window
function window_pos (div,divwidth) {
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerHeight;
	} else {
		viewportwidth = document.documentElement.clientHeight;
	}
	
	if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
		window_width = document.body.parentNode.clientWidth;
	} else {
		window_width = document.body.parentNode.scrollWidth;
	}
	window_width = window_width/2 - divwidth;
	$(div).style.left = window_width + 'px';
}

function popUp (widget,IE6,query_string) {
	var addTop = getScrollXY() + 50;
	loadPopUp(widget,query_string);	
	ghost_size('ghost');
	if (IE6 == true) {
		//shut off the select dropdown on the page				
		$$('select').each(function(element) { element.style.visibility = "hidden";});
	}
	$('ghost_container').style.top = addTop + 'px';	
	$('ghost').show();
	//if the window resizes, grab the new dimensions for ghost
	Event.observe(window,'resize', window_resize_event);
	return false;
}	

function loadPopUp (widget,query_string) {
	
	if(query_string != undefined){
		request_url = '/ajax/widget_ajax.php?'+query_string;
	}else{
		request_url = '/ajax/widget_ajax.php';
	}	
	new Ajax.Updater('ghost_container',request_url, {parameters: {w: widget}});	
	return false;
}

function closePopUp (widget) {
	//remove window resize event
	Event.stopObserving(window,'resize', window_resize_event);
	$(widget,'ghost').invoke('hide'); 
	//$('ghost').hide(); 	
	//fix IE6 bug
	$$('select').each(function (element) { element.style.visibility = "visible";});
	return false;
}					 

//find the size of the visible window
function shell_size(div) {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  $(div).style.height = myHeight;
   $(div).style.width = myWidth;
  //window.alert( 'Width = ' + myWidth );
  //window.alert( 'Height = ' + myHeight );
}

//find the x and y offset of a scrolled page
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return scrOfY;//[ scrOfX, scrOfY ]
}

function window_resize_event() {
	ghost_size('ghost');
}