/************************************************************************************
 * Welcome to the popupbox helper! Written by Mike Potter
 * Last Updated: 2007-03-16
 * v1.1
 * 
 * :id is the popupid of the popup box
 * :title is the title of the popup box
 * :partial is the name of the partial to be loaded into the box. If nil, set to title
 * :partial_path is the path to the partial if needed
 *
 * Usage -  
 * Be sure to include the javascript code at the top/header
 * <script type="text/javascript" src="includes/popUp.js"></script>
 * You may set the name to a variable for using in scripts: 
 *    popname = pop_generate_name
 * Be sure to create a partial with your content in it, then call the writer
 * pop_write( { :id => popname, :title => "Popup Title", :partial => 'partial_name', :partial_path => 'path_to_partial' } )
 *
/***********************************************************************************/
var agt=navigator.userAgent.toLowerCase();
var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_nav = ((agt.indexOf('mozilla')!= -1) && (agt.indexOf('spoofer') == -1) && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera') == -1) && (agt.indexOf('webtv') == -1) && (agt.indexOf('hotjava') == -1));
var is_opera = (agt.indexOf("opera") != -1);
var popResizeFunctions = new Array()

function centerPopupWindow(div){
   	objPopup = document.getElementById(div+'_pop');
    coords = centerPopup(objPopup);
   	objPopup.style.left = coords[0]+'px';
   	objPopup.style.top = coords[1]+'px';
}

function centerAjaxIcon(div){
   	objPopup = document.getElementById(div+'_ajax');
    coords = centerPopup(objPopup);
   	objPopup.style.left = coords[0]+'px';
   	objPopup.style.top = coords[1]+'px';
}

function showPopup(div){
   	objPopup = document.getElementById(div+'_pop');
   	objPopup.style.display='';

   	// center the Pop window
	centerPopupWindow(div);	

	// Pull up the cover and make it translucent  
	var objCover=document.getElementById(div+'_cover');
	var lintopacity = 60;

	if(is_nav){ 
		objCover.style.MozOpacity = lintopacity/100; 
	} else if(is_ie) { 
		objCover.style.filter="alpha(opacity=" + lintopacity + ")"; 
	} else if(is_opera) { 
		objCover.style.opacity = lintopacity/100; 
	}

	objCover.style.display = '';
	resizeCover(div)
	
	// add resize event listener
	if(is_ie){
    	window.attachEvent ( 'onresize', popResizeFunctions['resize_'+div] = function () { resizeCover(div)} );
    	document.attachEvent ( 'onkeydown', popResizeFunctions['keydown_'+div] = function () { if(event.keyCode == 27 ) hidePopup(div); } );
	}else{
        window.addEventListener ( 'resize', popResizeFunctions['resize_'+div] = function () { resizeCover(div) }, false );
        window.addEventListener ( 'keydown', popResizeFunctions['keydown_'+div] = function (e) { if(e.which == 27 ) hidePopup(div); }, false );
    } 
}

function showAjax(div){
   	objPopup = document.getElementById(div+'_ajax');
   	objPopup.style.display='';

   	// center the Pop window
	centerAjaxIcon(div);	

	// Pull up the cover and make it translucent  
	var objCover=document.getElementById(div+'_cover');
	var lintopacity = 60;

	if(is_nav){ 
		objCover.style.MozOpacity = lintopacity/100; 
	} else if(is_ie) { 
		objCover.style.filter="alpha(opacity=" + lintopacity + ")"; 
	} else if(is_opera) { 
		objCover.style.opacity = lintopacity/100; 
	}

	objCover.style.display = '';
	resizeCover(div)
	
	// add resize event listener
	if(is_ie){
    	window.attachEvent ( 'onresize', popResizeFunctions['resize_'+div] = function () { resizeCover(div)} );
	}else{
        window.addEventListener ( 'resize', popResizeFunctions['resize_'+div] = function () { resizeCover(div) }, false );
    } 
}

function hidePopup(div){
    // kill resize event listener
    if(is_ie){
        window.detachEvent('onresize',popResizeFunctions['resize_'+div])
        document.detachEvent ('onkeydown',popResizeFunctions['keydown_'+div])
        
    }else{
        window.removeEventListener('resize',popResizeFunctions['resize_'+div],false)
        window.removeEventListener ('keydown',popResizeFunctions['keydown_'+div],false)
        
    }
    
    // hide popup
   	var objPopup = document.getElementById(div+'_pop');
   	objPopup.style.display='none';

    // hide cover
	var objCover=window.document.getElementById(div+'_cover');
	objCover.style.width='1px';
	objCover.style.height='1px';
    objCover.style.display='none';
}

function hideAjax(div){
    // kill resize event listener
    if(is_ie){
        window.detachEvent('onresize',popResizeFunctions['resize_'+div])
    }else{
        window.removeEventListener('resize',popResizeFunctions['resize_'+div],false)
    }
    
    // hide popup
   	var objAjax = document.getElementById(div+'_ajax');
   	objAjax.style.display='none';

    // hide cover
	var objCover=window.document.getElementById(div+'_cover');
	objCover.style.width='1px';
	objCover.style.height='1px';
    objCover.style.display='none';
}

function clearAndHide(div){
	if(document.getElementById(div+'_form')) document.getElementById(div+'_form').reset();
	hidePopup(div);
}

function addContent(div,obj){
	if(document.getElementById(div+'_content')) {
	   objParent = document.getElementById(div+'_content')
	   if(objParent.firstChild)objParent.removeChild(objParent.firstChild)
       objParent.appendChild(obj.cloneNode(true))
       if(objParent.firstChild.style.display != '') objParent.firstChild.style.display = ''
	}
}

function resizeCover(div){
	var objCover=document.getElementById(div+'_cover');
	docSizes = getDocumentSize();
	winSizes = getWindowSize();
	objCover.style.height = Math.max(docSizes[1],winSizes[1])+'px';
	objCover.style.width = Math.max(docSizes[0],winSizes[0])+'px';
	objCover.style.left = '0px';
	objCover.style.top = '0px';
	
}

function getWindowSize(){
    // from http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
    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;
    }
    return [ myWidth, myHeight ];
}

function getDocumentSize(){
	if(is_ie){
		docHeight = parseInt( document.body.offsetHeight );
		docWidth = parseInt( document.body.offsetWidth );
	}else{
		docHeight = parseInt( self.innerHeight );
		docWidth = parseInt( self.innerWidth );
	}
	if(document.body.scrollHeight>docHeight) docHeight=document.body.scrollHeight;
	if(document.body.scrollWidth>docWidth) docWidth=document.body.scrollWidth;
	if(document.documentElement.scrollHeight>docHeight) docHeight=document.documentElement.scrollHeight;
	if(document.documentElement.scrollWidth>docWidth) docWidth=document.documentElement.scrollWidth;

    return [ docWidth, docHeight ];
}

function getScrolls(){
    // from http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
    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 centerPopup(obj){

    myWidth = parseInt(obj.offsetWidth);
  	myHeight = parseInt(obj.offsetHeight);
    
    winSize = getWindowSize();
    winWidth = winSize[0];
    winHeight = winSize[1];
    scrolls = getScrolls();
    scrollTop = scrolls[1];
    scrollLeft = scrolls[0];
        
    myTop =  ( ( winHeight - myHeight ) / 3 ) + scrollTop;
    myLeft = ( ( winWidth - myWidth ) / 2 ) + scrollLeft;
    
    if ( myTop < 0 ) myTop = 0;
    if ( myLeft < 0 ) myLeft = 0;

    return [ myLeft, myTop ];
}
