/**
 * amsPopupWindow - Class for creating a popup window.
 *
 * CHANGE LOG:
 * - 2005-11-22 - Radek Zak: Class was created
 *
 * @package amsPopupWindow
 * @version 1.0
 * @author Radek Zak <rzak@automs.com>
 * @copyright 2005 Automated Marketing Solutions Inc.
 * @require ams
 */


/**
 * This class is dependent on ams framework.  It may not work properly without it.
 */
if( typeof( ams ) == 'undefined' )
{
	alert( "Error, ams framework not included!!!" );
}


/**
 * Constants for amsPopupWindow class.
 *
 * @access public
 * @return void
 */
function amsPopupWindowConstant()
{
}
amsPopupWindowConstant.CLASS_NAME = 'amsPopupWindow';
amsPopupWindowConstant.WIDTH = 300;
amsPopupWindowConstant.HEIGHT = 300;
amsPopupWindowConstant.BODY = 'body';
amsPopupWindowConstant.TITLE = 'title';
amsPopupWindowConstant.POSITION_CENTER = 1;
amsPopupWindowConstant.POSITION_TOP = 2;
amsPopupWindowConstant.POSITION_BOTTOM = 3;
amsPopupWindowConstant.POSITION_LEFT = 4;
amsPopupWindowConstant.POSITION_RIGHT = 5;
amsPopupWindowConstant.POSITION_TOP_LEFT = 6;
amsPopupWindowConstant.POSITION_TOP_RIGHT = 7;
amsPopupWindowConstant.POSITION_BOTTOM_LEFT = 8;
amsPopupWindowConstant.POSITION_BOTTOM_RIGHT = 9;
amsPopupWindowConstant.TRANSITION_FROM_TOP = 1;
amsPopupWindowConstant.TRANSITION_FROM_BOTTOM = 2;
amsPopupWindowConstant.TRANSITION_FROM_LEFT = 3;
amsPopupWindowConstant.TRANSITION_FROM_RIGHT = 4;
amsPopupWindowConstant.LOAD_ON_PAGE_LOAD = 1;
amsPopupWindowConstant.LOAD_ON_PAGE_UNLOAD = 2;
amsPopupWindowConstant.LOAD_DELAY = 0;

amsPopupWindowConstant.getPopup = amsPopupWindowConstant_getPopup;

/**
 * Links popup element with popup identifier.
 *
 * @param int pid	popup identifier
 * @access public
 * @return object	Returns popup object on success, or void on failure;
 */
function amsPopupWindowConstant_getPopup( pid )
{
	pid = parseInt( pid );
	if ( document.getElementById( 'amsPopupWindow_' + pid ))
	{
		return document.getElementById( 'amsPopupWindow_' + pid );
	}
	else
	{
		ams.debug( 'warning: popup id ("' + pid + '") does not exist' );
	}
}
ams.amsPopupWindowConstant = amsPopupWindowConstant;
ams.debug( '( "amsPopupWindowConstant" ) - loaded' );



/**
 * Constructor for amsPopupWindow class.
 *
 * @param int pid	popup identifier
 * @access public
 * @return void
 */
function amsPopupWindow( pid )
{
	if( typeof( pid ) == 'undefined' ) pid = 0;
	this.popupID = pid;

	this.obj = this;
	this.objName = '';
	this.loaded = false;
	this.popupBody;
	this.popupTitle;
	this.popupWindowWidth;
	this.popupWindowHeight;
	this.popupPosition;
	this.popupPositionX = 0;
	this.popupPositionY = 0;
	this.popupTransition;
	this.popupTransitionX = 0;
	this.popupTransitionY = 0;
	this.popupLoadDelay;

	this.assignBody( amsPopupWindowConstant.BODY );
	this.assignTitle( amsPopupWindowConstant.TITLE );
	this.assignWindowWidth( amsPopupWindowConstant.WIDTH );
	this.assignWindowHeight( amsPopupWindowConstant.HEIGHT );
	this.assignPosition( amsPopupWindowConstant.POSITION_CENTER );
	this.assignLoadDelay( amsPopupWindowConstant.LOAD_DELAY );

	// TESTING //
	this.moveAction;
	this.shakeDirection = 1;
	this.shakeLimit = 20;
}


amsPopupWindow.prototype.init = amsPopupWindow_init;								// initialize popup window
amsPopupWindow.prototype.load = amsPopupWindow_load;								// load popup frame
amsPopupWindow.prototype.show = amsPopupWindow_show;								// show
amsPopupWindow.prototype.hide = amsPopupWindow_hide;								// hide
amsPopupWindow.prototype.move = amsPopupWindow_move;								// TESTING - move
amsPopupWindow.prototype.shake = amsPopupWindow_shake;								// TESTING - shake
amsPopupWindow.prototype.assignBody = amsPopupWindow_assignBody;					// assign content
amsPopupWindow.prototype.assignTitle = amsPopupWindow_assignTitle;					// assign title
amsPopupWindow.prototype.assignWindowWidth = amsPopupWindow_assignWindowWidth;		// assign window width
amsPopupWindow.prototype.assignWindowHeight = amsPopupWindow_assignWindowHeight;	// assign window height
amsPopupWindow.prototype.assignPosition = amsPopupWindow_assignPosition;			// assign loading position
amsPopupWindow.prototype.assignTransition = amsPopupWindow_assignTransition;		// assign loading transition
amsPopupWindow.prototype.assignLoadDelay = amsPopupWindow_assignLoadDelay;			// assign load delay



/**
 * Initializes the class.
 *
 * @access public
 * @return void
 */
function amsPopupWindow_init()
{
	this.load();
	setTimeout( "" + this.objName + ".show( " + this.popupID + " )", parseInt( this.popupLoadDelay * 1000 ) );
}



/**
 * Creates the html content of the popup window.
 *
 * @access public
 * @return void
 */
function amsPopupWindow_load()
{
	if ( ! this.loaded )
	{
		var tmpName = amsPopupWindowConstant.CLASS_NAME;
		var thePopupWindow = '';
		thePopupWindow  = '<div id="' + tmpName + '_' + this.popupID + '" class="' + tmpName + '" style="width: ' + this.popupWindowWidth + 'px; height: ' + this.popupWindowHeight + 'px; left: ' + this.popupPositionX + 'px; top: ' + this.popupPositionY + 'px;">';
		thePopupWindow += '<span id="' + tmpName + 'Title_' + this.popupID + '" class="' + tmpName + 'Title">';
		thePopupWindow += '<span id="' + tmpName + 'TitleBody_' + this.popupID + '" class="' + tmpName + 'TitleBody" onclick="' + this.objName + '.hide(' + this.popupID + ')">[Close]</span>';
		thePopupWindow += '</span>';
		thePopupWindow += '<span id="' + tmpName + 'Content_' + this.popupID + '" class="' + tmpName + 'Content">';
		thePopupWindow += '<span id="' + tmpName + 'ContentBody_' + this.popupID + '" class="' + tmpName + 'ContentBody">' + this.popupBody + '</span>';
		thePopupWindow += '</span>';
		thePopupWindow += '</div>';
		document.write( thePopupWindow );
		this.loaded = true;
	}
}



/**
 * Shows the popup.
 *
 * @param int pid	popup identifier
 * @access public
 * @return void
 */
function amsPopupWindow_show( pid )
{
	ams.amsPopupWindowConstant.getPopup( pid ).style.display = 'block';
//    objName.moveAction = setInterval( "" + this.objName + ".move(" + this.objName + ")", 10 );
}



/**
 * Hides the popup.
 *
 * @param int pid	popup identifier
 * @access public
 * @return void
 */
function amsPopupWindow_hide( pid )
{
	ams.amsPopupWindowConstant.getPopup( pid ).style.display = 'none';
}



/**
 * TESTING - Moves the popup.
 *
 * @param int pid	popup identifier
 * @access public
 * @return void
 */
function amsPopupWindow_move( pid )
{
	ams.amsPopupWindowConstant.getPopup( pid ).style.left = ( parseInt( ams.amsPopupWindowConstant.getPopup( pid ).style.left ) + 5 ) + 'px';
	if (parseInt(ams.amsPopupWindowConstant.getPopup( pid ).style.left) > 500) {
        clearInterval(objName.moveAction);
	    objName.moveAction = setInterval( "" + this.objName + ".shake(" + pid + ")", 40 );
	}
}



/**
 * TESTING - Shakes the popup.
 *
 * @param int pid	popup identifier
 * @access public
 * @return void
 */
function amsPopupWindow_shake( pid )
{
	ams.amsPopupWindowConstant.getPopup( pid ).style.left = ( parseInt( ams.amsPopupWindowConstant.getPopup( pid ).style.left ) + ( 10 * objName.shakeDirection ) ) + 'px';
	objName.shakeDirection = objName.shakeDirection * -1;
	objName.shakeLimit --;
	ams.popup1 = objName;
	if (objName.shakeLimit <= 0) {
        clearInterval(objName.moveAction);
	}
}



/**
 * Assigns the popup body.
 *
 * @param str val	popup body
 * @access public
 * @return void
 */
function amsPopupWindow_assignBody( val )
{
	this.popupBody = val;
	if ( this.loaded )
	{
		document.getElementById( 'amsPopupWindowContentBody_' + this.popupID ).innerHTML = this.popupBody;
	}
}



/**
 * Assigns the popup title.
 *
 * @param str val	popup title
 * @access public
 * @return void
 */
function amsPopupWindow_assignTitle( val )
{
	this.popupTitle = val;
	if ( this.loaded )
	{
		document.getElementById( 'amsPopupWindowTitleBody_' + this.popupID ).innerHTML = this.popupTitle;
	}
}



/**
 * Assigns the popup window width.
 *
 * @param int val	popup window width
 * @access public
 * @return void
 */
function amsPopupWindow_assignWindowWidth( val )
{
	val = parseInt( val );
	this.popupWindowWidth = val;
	if ( this.loaded )
	{
		document.getElementById( 'amsPopupWindow_' + this.popupID ).style.width = this.popupWindowWidth + 'px';
	}
}



/**
 * Assigns the popup window width.
 *
 * @param int val	popup window width
 * @access public
 * @return void
 */
function amsPopupWindow_assignWindowHeight( val )
{
	val = parseInt( val );
	this.popupWindowHeight = val;
	if ( this.loaded )
	{
		document.getElementById( 'amsPopupWindow_' + this.popupID ).style.height = this.popupWindowHeight + 'px';
	}
}



/**
 * Assigns the popup loading position.
 *
 * @param int val	popup position id
 * @access public
 * @return void
 */
function amsPopupWindow_assignPosition( val )
{
	val = parseInt( val );
	if ( self.innerWidth )
	{
		frameWidth = self.innerWidth;
		frameHeight = self.innerHeight;
	}
	else if ( document.documentElement && document.documentElement.clientWidth )
	{
		frameWidth = document.documentElement.clientWidth;
		frameHeight = document.documentElement.clientHeight;
	}
	else if ( document.body )
	{
		frameWidth = document.body.clientWidth;
		frameHeight = document.body.clientHeight;
	}

	this.popupPosition = val;
	frameMarginX = 10;
	frameMarginY = 27;

	/*
	amsPopupWindowConstant.POSITION_CENTER = 1;
	amsPopupWindowConstant.POSITION_TOP = 2;
	amsPopupWindowConstant.POSITION_BOTTOM = 3;
	amsPopupWindowConstant.POSITION_LEFT = 4;
	amsPopupWindowConstant.POSITION_RIGHT = 5;
	amsPopupWindowConstant.POSITION_TOP_LEFT = 6;
	amsPopupWindowConstant.POSITION_TOP_RIGHT = 7;
	amsPopupWindowConstant.POSITION_BOTTOM_LEFT = 8;
	amsPopupWindowConstant.POSITION_BOTTOM_RIGHT = 9;
	*/
	switch( val ) {
		case amsPopupWindowConstant.POSITION_CENTER:
			this.popupPositionX = parseInt( ( frameWidth / 2 ) - ( this.popupWindowWidth / 2 ) );
			this.popupPositionY = parseInt( ( frameHeight / 2 ) - ( this.popupWindowHeight / 2 ) + 50 );
			break;
		case amsPopupWindowConstant.POSITION_TOP:
			this.popupPositionX = parseInt( ( frameWidth / 2 ) - ( this.popupWindowWidth / 2 ) );
			this.popupPositionY = parseInt( 0 );
			break;
		case amsPopupWindowConstant.POSITION_BOTTOM:
			this.popupPositionX = parseInt( ( frameWidth / 2 ) - ( this.popupWindowWidth / 2 ) );
			this.popupPositionY = parseInt( frameHeight  - this.popupWindowHeight  - frameMarginY );
			break;
		case amsPopupWindowConstant.POSITION_LEFT:
			this.popupPositionX = parseInt( 0 );
			this.popupPositionY = parseInt( ( frameHeight / 2 ) - ( this.popupWindowHeight / 2 ) + 50);
			break;
		case amsPopupWindowConstant.POSITION_RIGHT:
			this.popupPositionX = parseInt( frameWidth  - this.popupWindowWidth - frameMarginX );
			this.popupPositionY = parseInt( ( frameHeight / 2 ) - ( this.popupWindowHeight / 2 ) + 50 );
			break;
		case amsPopupWindowConstant.POSITION_TOP_LEFT:
			this.popupPositionX = parseInt( 0 );
			this.popupPositionY = parseInt( 0 );
			break;
		case amsPopupWindowConstant.POSITION_TOP_RIGHT:
			this.popupPositionX = parseInt( frameWidth  - this.popupWindowWidth - frameMarginX );
			this.popupPositionY = parseInt( 0 );
			break;
		case amsPopupWindowConstant.POSITION_BOTTOM_LEFT:
			this.popupPositionX = parseInt( 0 );
			this.popupPositionY = parseInt( frameHeight  - this.popupWindowHeight - frameMarginY );
			break;
		case amsPopupWindowConstant.POSITION_BOTTOM_RIGHT:
			this.popupPositionX = parseInt( frameWidth  - this.popupWindowWidth - frameMarginX );
			this.popupPositionY = parseInt( frameHeight  - this.popupWindowHeight - frameMarginY );
			break;
		default:
			this.popupPositionX = parseInt( ( frameWidth / 2 ) - ( this.popupWindowWidth / 2 ) + 50);
			this.popupPositionY = parseInt( ( frameHeight / 2 ) - ( this.popupWindowHeight / 2 ) + 50);
			break;
	}
}



/**
 * Assigns the popup loading transition.
 *
 * @param int val	popup transition id
 * @access public
 * @return void
 */
function amsPopupWindow_assignTransition( val )
{
	val = parseInt( val );
	if ( self.innerWidth )
	{
		frameWidth = self.innerWidth;
		frameHeight = self.innerHeight;
	}
	else if ( document.documentElement && document.documentElement.clientWidth )
	{
		frameWidth = document.documentElement.clientWidth;
		frameHeight = document.documentElement.clientHeight;
	}
	else if ( document.body )
	{
		frameWidth = document.body.clientWidth;
		frameHeight = document.body.clientHeight;
	}

	this.popupTransition = val;
	frameMarginX = 10;
	frameMarginY = 27;

	/*
	amsPopupWindowConstant.TRANSITION_FROM_TOP = 1;
	amsPopupWindowConstant.TRANSITION_FROM_BOTTOM = 2;
	amsPopupWindowConstant.TRANSITION_FROM_LEFT = 3;
	amsPopupWindowConstant.TRANSITION_FROM_RIGHT = 4;
	*/
	switch( parseInt( val ) ) {
		case amsPopupWindowConstant.TRANSITION_FROM_TOP:
			this.popupTransitionX = this.popupPositionX;
			this.popupTransitionY = parseInt( 0 );
			break;
		case amsPopupWindowConstant.TRANSITION_FROM_BOTTOM:
			this.popupTransitionX = this.popupPositionX;
			this.popupTransitionY = parseInt( frameHeight  - this.popupWindowHeight - frameMarginY );
			break;
		case amsPopupWindowConstant.TRANSITION_FROM_LEFT:
			this.popupTransitionX = parseInt( 0 );
			this.popupTransitionY = this.popupPositionY;
			break;
		case amsPopupWindowConstant.TRANSITION_FROM_RIGHT:
			this.popupTransitionX = parseInt( frameWidth  - this.popupWindowWidth  - frameMarginX );
			this.popupTransitionY = this.popupPositionY;
			break;
		default:
			this.popupTransitionX = this.popupPositionX;
			this.popupTransitionY = this.popupPositionY;
			break;
	}
}



/**
 * Assigns the popup load delay.
 *
 * @param int val	popup load delay
 * @access public
 * @return void
 */
function amsPopupWindow_assignLoadDelay( val )
{
	this.popupLoadDelay = val;
}



ams.amsPopupWindow = amsPopupWindow;
ams.debug( '( "amsPopupWindow" ) - loaded' );


//	PROGRAMMER'S NOTES //
//	ALTERNATIVE OF USING document.write(); IN .show() FUNCTION //
/*
	this.thePopupWindowClose = document.createTextNode(amsPopupWindowConstant.CLOSE_WINDOW);

	this.thePopupWindowTitle = document.createElement( 'span' );
	this.thePopupWindowTitle.setAttribute( 'id', 'amsPopupWindowTitle_' + this.popupID );
	this.thePopupWindowTitle.setAttribute( 'class', this._name( 'Title' ) );
	this.thePopupWindowTitle.setAttribute( 'className', this._name( 'Title') );
	this.thePopupWindowTitle.appendChild( this.thePopupWindowClose );

	this.thePopupWindowContentBody = document.createElement( 'span' );
	this.thePopupWindowContentBody.setAttribute( 'id', 'amsPopupWindowContentBody_' + this.popupID );
	this.thePopupWindowContentBody.setAttribute( 'class', this._name( 'ContentBody' ) );
	this.thePopupWindowContentBody.setAttribute( 'className', this._name( 'ContentBody' ) );
	this.thePopupWindowContentBody.innerHTML = this.content_body;

	this.thePopupWindowContent = document.createElement( 'span' );
	this.thePopupWindowContent.setAttribute( 'id', 'amsPopupWindowContent_' + this.popupID );
	this.thePopupWindowContent.setAttribute( 'class', this._name( 'Content' ) );
	this.thePopupWindowContent.setAttribute( 'className', this._name( 'Content' ) );
	this.thePopupWindowContent.appendChild( this.thePopupWindowContentBody );

	this.thePopupWindow = document.createElement( 'div' );
	this.thePopupWindow.setAttribute( 'id', this._name( this.popupID ) );
	this.thePopupWindow.setAttribute( 'class', this._name() );
	this.thePopupWindow.setAttribute( 'className', this._name() );
	this.thePopupWindow.style.width = this.popupWindowWidth;
	this.thePopupWindow.style.height = this.popupWindowHeight;
	this.thePopupWindow.appendChild( this.thePopupWindowTitle );
	this.thePopupWindow.appendChild( this.thePopupWindowContent );

	this.page_body = document.body;
	this.page_body.appendChild( this.thePopupWindow );
	document.getElementById('amsPopupWindowTitle_' + this.popupID).onclick = this.obj.hide;
*/
