
//for popup windows


var popup_flag=0;

function show_popup(Xwidth,Yheight, url){
	showdeadcenterdiv(Xwidth,Yheight,'popup', 1);
	popup_flag=1;
	
	if (url.indexOf("?")!=-1){
		var pars = url.substring(url.search(/\?/) + 1);
		var url_ajax = url.substring(0, url.search(/\?/));
	}
	else { url_ajax = url; }
	var return_div_id = 'popup';
	
	var myAjax = new Ajax.Updater(
	return_div_id,
		url_ajax, 
		{
			method: 'post', 
			parameters: pars, 
			evalScripts: true
		});
	
	
}

function hide_popup(){
	$('popup').hide();
	grayOut(false);
	popup_flag=0;
}




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 [ scrOfX, scrOfY ];
}



function showdeadcenterdiv(Xwidth,Yheight,divid, showdiv) {
// First, determine how much the visitor has scrolled 

scrl = getScrollXY();
scrolledX = scrl[0];
scrolledY = scrl[1];



// Next, determine the coordinates of the center of browser's window
var centerX, centerY;
if( self.innerHeight ) {
centerX = self.innerWidth;
centerY = self.innerHeight;
} else if( document.documentElement && document.documentElement.clientHeight ) {
centerX = document.documentElement.clientWidth;
centerY = document.documentElement.clientHeight;
} else if( document.body ) {
centerX = document.body.clientWidth;
centerY = document.body.clientHeight;
}

var o = $(divid);
var r=o.style;
if (Xwidth > 1){
	r.width = Xwidth +'px';
}
else{
	Xwidth = r.width.replace('px', '');
}

if (Yheight > 1){
	r.height = Yheight +'px';
}
else{
	Yheight = r.height.replace('px', '');
}



// Xwidth is the width of the div, Yheight is the height of the
// div passed as arguments to the function:
var leftoffset = scrolledX + (centerX - Xwidth) / 2;
var topOffset = scrolledY + (centerY - Yheight) / 2;
// The initial width and height of the div can be set in the
// style sheet with display:none; divid is passed as an argument to // the function



r.position='absolute';
r.top = topOffset + 'px';
r.left = leftoffset + 'px';
if (showdiv){
	o.show();
	grayOut(true, {'zindex':'50', 'bgcolor':'#000000', 'opacity':'70', 'zindex':'10'});
}


} 

window.onscroll = scrollEvent;
function scrollEvent() {
   showdeadcenterdiv('','','popup', 0)
}









function grayOut(vis, options) {
  // Pass true to gray out screen, false to ungray
  // options are optional.  This is a JSON object with the following (optional) properties
  // opacity:0-100         // Lower number = less grayout higher = more of a blackout 
  // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
  // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
  // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
  // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
  // in any order.  Pass only the properties you need to set.
  var options = options || {}; 
  var zindex = options.zindex || 50;
  var opacity = options.opacity || 70;
  var opaque = (opacity / 100);
  var bgcolor = options.bgcolor || '#000000';
  var dark=document.getElementById('darkenScreenObject');
  if (!dark) {
    // The dark layer doesn't exist, it's never been created.  So we'll
    // create it here and apply some basic styles.
    // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
    var tbody = document.getElementsByTagName("body")[0];
    var tnode = document.createElement('div');           // Create the layer.
        tnode.style.position='absolute';                 // Position absolutely
        tnode.style.top='0px';                           // In the top
        tnode.style.left='0px';                          // Left corner of the page
        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            
        tnode.style.display='none';                      // Start out Hidden
        tnode.id='darkenScreenObject';                   // Name it so we can find it later
		tnode.onclick="hide_popup()";
    tbody.appendChild(tnode);                            // Add it to the web page
    dark=document.getElementById('darkenScreenObject');  // Get the object.
  }
  if (vis) {
    // Calculate the page width and height 
    if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
        var pageWidth = document.body.scrollWidth+'px';
        var pageHeight = document.body.scrollHeight;
    } else if( document.body.offsetWidth ) {
      var pageWidth = document.body.offsetWidth+'px';
      var pageHeight = document.body.offsetHeight;
    } else {
       var pageWidth='100%';
       var pageHeight='100%';
    }  
	
	var viewport_dim = viewport_dimensions();
	if (pageHeight < viewport_dim[1]){
		pageHeight = viewport_dim[1]+'px';
	}
	else {
		pageHeight = pageHeight+'px';
	}
    //set the shader to cover the entire page and make it visible.
    dark.style.opacity=opaque;                      
    dark.style.MozOpacity=opaque;                   
    dark.style.filter='alpha(opacity='+opacity+')'; 
    dark.style.zIndex=zindex;        
    dark.style.backgroundColor=bgcolor;  
    dark.style.width= pageWidth;
    dark.style.height= pageHeight;
    dark.style.display='block';                          
  } else {
     dark.style.display='none';
  }
}











function viewport_dimensions(){
	var viewportwidth;
	var viewportheight;
	 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	 if (typeof window.innerWidth != 'undefined')
	 {
	      viewportwidth = window.innerWidth,
	      viewportheight = window.innerHeight
	 }
	 
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	 else if (typeof document.documentElement != 'undefined'
	     && typeof document.documentElement.clientWidth !=
	     'undefined' && document.documentElement.clientWidth != 0)
	 {
	       viewportwidth = document.documentElement.clientWidth,
	       viewportheight = document.documentElement.clientHeight
	 }
	 
	 // older versions of IE
	 else
	 {
	       viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
	       viewportheight = document.getElementsByTagName('body')[0].clientHeight
	 }
	 
	var ret = new Array(viewportwidth, viewportheight);
	return ret;
	
	


}

















function clickedOutsideElement(elemId, evt) {
var theElem = '';
if(window.event)
theElem = getEventTarget(window.event);
else theElem = getEventTarget(evt);

while(theElem != null) {
if(theElem.id == elemId)
return false;

theElem = theElem.offsetParent;
}

return true;
}

function getEventTarget(evt) {
var targ = (evt.target) ? evt.target : evt.srcElement;

if(targ != null) {
if(targ.nodeType == 3)
targ = targ.parentNode;
}

return targ;
}

document.onclick = function(evt) {
	if (popup_flag == 2){
		if(clickedOutsideElement('popup', evt)){
		hide_popup();
		}
		else{
		}
	}
	else {
		popup_flag = 2;
	}
}
