 /**
 * PopUp Manager for the creation and management of PopUps.
 * @author Scott Murphy
 * 
 * Copyright (c) 2008 Pictage Inc.
 * ALL RIGHTS RESERVED
 */
var PopUps = [];
var PopUp = new Class({
	Implements: Options,
	options: {
		okText: 'Save',
		closeable: true,
		disposeOnClose: true
	},
    initialize: function(options) {
		this.setOptions(options);

		this.cover = new Element('div', {
			styles : {
				'position': 'absolute',
				'left': '0px',
				'top': '0px',
				'background-color' : '#242424',
				'opacity': '0',
				'z-index': '10000',
				'width': '100%'
			}			
		}).inject(document.body);
		
		this.resizeCover = function() {
			this.cover.setStyle( 'height', window.getScrollSize().y + 'px');
		}.bind(this);
		this.resizeCover();

		this.popup = new Element('div', {
			'class':'popupWrapper',
			styles : {
				'border': '1px solid black',
				'position': 'absolute',
				'background-color' : 'white',
				'opacity': '1',
				'z-index': '10013',
				'display': 'none'
			}
		});
		if ($defined(options.width))
			this.popup.setStyle('width', options.width);
		this.container = new Element('div', {'class':'popup'}).inject(this.popup);
		if ($defined(options.id))
			this.container.id = options.id;
    	if ($defined(options.popupClass))
    		this.container.addClass(options.popupClass);

		if (this.options.closeable) {
			new Element('a', {
				'class':'btnClose',
				href   :'javascript:void(0);',
				text   :'Close [X]',
				events : {
					'click': function() { this.close() }.bind(this)
				}
			}).inject( this.container );
		}    	
    	
		var title = new Element('h1', { html : '<span></span>'+this.options.title }).inject( this.container );
		title.setStyle('cursor','move');
		if ( $defined(this.options.titleStyles) )
			title.set('styles',this.options.titleStyles);
		
		if ($defined(options.content)) {
			var copy = this.options.disposeOnClose? $(this.options.content) : $(this.options.content).clone();
			copy.setStyle('display','block');
			copy.inject(this.container);
		}		

		if (options.prompt) {
			var label = new Element('label', {
				'for' : 'promptValue',
				text  : this.options.label
			}).inject( this.container );		
			this.prompt = new Element('input', {
				'class'  : 'promptValue',
				type     : 'text',
				maxlength: 30,
				events	 : {
					'keydown' : function(event) {
						if (event.key == "enter") {
							var promptValue = this.prompt.get('value');
							this.close();
							if ($defined(this.options.okAction))
								this.options.okAction(promptValue);						
						}	
					}.bind(this)
				}				
			});
	
			this.prompt.inject( this.container ); 

			if ( $defined(this.options.defaultInput) )
				this.prompt.set('value', this.options.defaultInput);
			var saveButton = new Element('input', {
				'class'  : 'btnSave',
				value    : this.options.okText,
				type     : 'button',
				events : {
					'click': function() {
						var promptValue = this.prompt.get('value');
						this.close();
						if ($defined(this.options.okAction))
							this.options.okAction(promptValue);
					}.bind(this)
				}			
			}).inject( this.container );
		}
		this.popup.inject(document.body);
		new Drag( this.popup, {handle:title} );
	},
	show: function() {
	
		// This is for IE6 only - as the select box seem to have a highest z-index
		if(Browser.Engine.trident4) {
			$$('select').each(function(ele) {
				if(!$defined(ele.getParent('ul.productList')))
                                  ele.setStyle('visibility','hidden');
			});
		}
		if (this.options.disposeOnClose)
		{
			this.cover.setStyle('opacity','0');
			this.cover.setStyle('display','block');		
		}
		this.cover.fade('.8'); 
		window.addEvent('resize', this.resizeCover);	
		
		var windowSize = window.getSize();
		var scroll = window.getScroll();	
		this.popup.setStyle('display','block');
		var popupSize = this.popup.getSize();
		
		var left = Math.max( 0, ((windowSize.x - popupSize.x)/2) + scroll.x );
		var top = Math.max( 0, ((windowSize.y - popupSize.y)/2) + scroll.y );
			
		this.popup.setStyles({
			'opacity':'0',
			'left': left + 'px',
			'top': top + 'px'
		});
		this.popup.fade('1'); 
		if ($defined(this.prompt) && Browser.Engine.gecko )
			this.prompt.focus();
	},
	close: function() {
	
		// This is for IE6 only - as the select box seem to have a highest z-index
		if(Browser.Engine.trident4) {
			$$('select').each(function(ele) {
                                // check to see if this select was hidden by some one else, e.g: slide show
                                if(!$chk(ele.retrieve('isHidden'))) 
                                  ele.setStyle('visibility','');
			});
		}
		if (!this.options.disposeOnClose)
		{
			window.removeEvent('resize',this.resizeCover);
			this.cover.destroy();
			this.popup.destroy();
		}
		else
		{
			this.popup.setStyle('display','none');
			this.cover.setStyle('display','none');
		}
		if ($defined(this.options.closeAction))
			this.options.closeAction();
	}
});	
