/* Written by Joshua A. S. Allen, 2010, SlaphappyGeeks.com */

var Popup={
	classes:{
		opacityVisible:'opacity_layer_visible',
		tableVisible:'popup_table_visible',
		opacityClear:'opacity_layer_clear',
		hidden:'hidden_element'
	},
	config:{
		fadeInSpeed:500,
		fadeOutSpeed:500,
		tableID:'POPUP_TABLE',
		cellID:'POPUP_TABLE_CELL',
		layerID:'OPACITY_LAYER',
		minOpacity:0.0,
		maxOpacity:0.7
	},
	refs:{
		opacityLayer:null,
		popupTable:null,
		tableCell:null
	},
	
	showPopup:function()
	{// Shows popup table and fades in opacity layer.
	$(this._getPopupTable()).
	stop().
	removeClass(this.classes.hidden).
	addClass(this.classes.tableVisible).
	css('top',$.browser.msie?document.documentElement.scrollTop:window.pageYOffset);
	// Fade in opacity.
	$(this._getOpacityLayer()).
	stop().
	removeClass(this.classes.hidden).
	addClass(this.classes.opacityClear).
	fadeTo(this.config.fadeInSpeed,this.config.maxOpacity,this._opacityFadedIn);
	return this;},
	
	hidePopup:function()
	{// Hides popup table and fades out opacity layer.
	$(this._getPopupTable()).
	stop().
	removeClass(this.classes.tableVisible).
	addClass(this.classes.hidden);
	// Fade out opacity layer.
	$(this._getOpacityLayer()).
	stop().
	removeClass(this.classes.opacityVisible).
	addClass(this.classes.opacityClear).
	fadeTo(this.config.fadeOutSpeed,this.config.minOpacity,this._opacityFadedOut);
	return this;},
	
	loadIntoPopup:function(settings)
	{// Loads AJAX HTTP response text into popup
	// according to jQuery AJAX settings settings.
	// Extra ajax settings.
	var popupSettings={
		complete:function(xhr,stat){
			Popup.
			setPopupContent(xhr.responseText).
			showPopup();}};
	$.extend(settings,popupSettings);
	$.ajax(settings);
	return this;},
	
	setPopupContent:function(content)
	{// Sets content of popup table cell.
	var cell=this._getPopupCell();
	if(cell){
		cell.innerHTML=content;}
	return this;},
	
	writePopupTable:function()
	{// Writes popup table.
	document.write(
	"<table"+
	"	cellpadding='0'"+
	"	cellspacing='0'"+
	"	class='"+this.classes.hidden+"'"+
	"	id='"+this.config.tableID+"'>"+
	"	<tbody>"+
	"		<tr>"+
	"			<td"+
	"				style='WIDTH:AUTO;'>&#160;</td>"+
	"			<td"+
	"				align='center'"+
	"				id='"+this.config.cellID+"'"+
	"				valign='middle'><!-- Content will go here --></td>"+
	"			<td"+
	"				style='WIDTH:AUTO;'>&#160;</td>"+
	"		</tr>"+
	"	</tbody>"+
	"</table>");
	return this;},
	
	writeOpacityLayer:function()
	{// Writes opacity layer.
	document.write("<div "+
	"class='"+this.classes.hidden+"' "+
	"id='"+this.config.layerID+"'></div>");
	return this;},
	
	_opacityFadedIn:function()
	{// Opacity faded in, set classes.
	$(Popup._getOpacityLayer()).
	addClass(Popup.classes.opacityVisible).
	removeClass(Popup.classes.opacityClear);},
	
	_opacityFadedOut:function()
	{// Opacity faded out, set classes.
	$(Popup._getOpacityLayer()).
	removeClass(Popup.classes.opacityClear).
	addClass(Popup.classes.hidden);},
	
	_getOpacityLayer:function()
	{// Returns reference to opacity layer.
	return(
		this.refs.opacityLayer=this.refs.opacityLayer
			?this.refs.opacityLayer
			:document.getElementById(this.config.layerID));},
	
	_getPopupTable:function()
	{// Returns reference to popup table.
	return(
		this.refs.popupTable=this.refs.popupTable
			?this.refs.popupTable
			:document.getElementById(this.config.tableID));},
	
	_getPopupCell:function()
	{// Returns reference to table cell.
	return(
		this.refs.tableCell=this.refs.tableCell
			?this.refs.tableCell
			:document.getElementById(this.config.cellID));},

	showPopupImage:function(outerID,innerID)
	{$('#'+outerID+'>div').// Find child divs of outerID div.
	stop().
	css('display','none').	// Hide all.
	css('visibility','hidden');
	$('#'+innerID).	// Find innerID div.
	css('display','inline').// Show innerID div.
	css('visibility','visible');
	return false;}
};

