// *************************************
//  Inline pop-up script utilizing YUI
//  Author: Michael Smith
//	Created: 09/24/2008
//  Last Edited: 09/24/2008
//	Last Edited By: Michael Smith
//	Notes:
// *************************************

//attach stylesheet
document.write('<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.5.2/build/container/assets/skins/sam/container.css" />');

//create local namespace
YAHOO.namespace("popup.container");	
YUC = YAHOO.util.Connect;


//******************************************************
function popUp(ajaxUrl,params){
//******************************************************

	//**************************
	//ajaxUrl = 'url' or 0
	//blackout,center,closeBttn,drag = true or false
	//width = '###px'
	//transition = 'SLIDE' or 'FADE' (all caps)
	//sample: popUp('/page/php?var=value', {width:'550px',blackout:true,center:true,drag:true,close:true,transition:'FADE'});
	//**************************
	
	
	if(params.blackout){ //only use loading panel if using blackout
		if(!YAHOO.popup.container.loading){
			//set loading panel properties
			YAHOO.popup.container.loading = new YAHOO.widget.Panel("wait",  
				{ width: "240px", 
				  fixedcenter: true, 
				  close: false, 
				  draggable: false, 
				  zindex: 98,
				  modal: true,
				  visible: false
				} 
			);
			//create loading panel
			YAHOO.popup.container.loading.setHeader("Loading, please wait...");
			YAHOO.popup.container.loading.setBody("<img src=\"/images/loading_bar.gif\"/>");
			YAHOO.popup.container.loading.render(document.body);	
		}
				
		YAHOO.popup.container.loading.show();
	}
	
	
	if(!YAHOO.popup.container.popUp){
		//set popup properties
		YAHOO.popup.container.popUp = new YAHOO.widget.Panel("popUp",  
			{ width: params.width, 
			  fixedcenter: params.center, 
			  close: params.close, 
			  draggable: params.drag, 
			  zindex: 99,
			  modal: params.blackout,
			  visible: false,
			  effect:{effect:YAHOO.widget.ContainerEffect[params.transition],duration:0.25}
			} 
		);
	}
	
	var loadPopUp = function(resp){
		
		if(resp.responseText != "null") {
				//write response to popup
				var jsonObj = eval('(' + resp.responseText + ')');
				YAHOO.popup.container.popUp.setHeader(jsonObj.header);
				YAHOO.popup.container.popUp.setBody(jsonObj.body);
				YAHOO.popup.container.popUp.setFooter(jsonObj.footer);
				YAHOO.popup.container.popUp.render(document.body);
				
			} else {
				YAHOO.popup.container.popUp.setBody("Received a NULL response from PHP");
			}
			
		YAHOO.popup.container.popUp.show();
		YAHOO.popup.container.loading.hide();
	}
	
	if(ajaxUrl != 0){ //if not making an AJAX call, pass in 0 for ajaxUrl
		//ajax call
		YUC.asyncRequest('POST', ajaxUrl,{success: loadPopUp, failure: ajaxFail, timeout: 10000});
	} else {
		YAHOO.popup.container.loading.hide();
		YAHOO.popup.container.popUp.show();	
	}
}


//*******************************
function closePopUp(){
//*******************************
	YAHOO.popup.container.popUp.hide();	
}


function ajaxFail(resp){
	alert('failed');
}
