

/**
 * @Overlay
 */
var Overlay =
{
	overlay 	:undefined,		/* holds the overlay */
	time:		0.0,			/* amount of time fade takes */
	high:		0.0,			/* opacity when in-view */
	low:		0.0,			/* */
	key_bfx:	null,			/* escaping */
	click_bfx:	null,			/* add/removing click event */
	color:		'#ccc',			/* to be overwriter */
	zIndex:		4000,
	container_style:	'overlaid-content rounded-10',
	

	keypress:function(event){
			
		var key = event.which || event.keyCode;
		switch (key) {
		case Event.KEY_ESC:
			this.escape();
			break;
		case Event.KEY_RETURN:
			this.broadcastMessage('returnkeypressed');
			break;
		}
		/*		
		case Event.KEY_UP:
			if((index-1) < 0){index=this.sub_items.length;}
			this.subitem_over(null,this.sub_items[(index-1)]);
			break;
		case Event.KEY_RETURN:
			this.subitem_submit();
			break;
		Event.stop(event);
		*/
	},
	page_load:function(){
		this.build();
	},
	show:function(content)		
	{
		if(this.overlay==undefined)
			this.build();
			
					
		if(content && content.template!=null)
			$(this.overlay).up().appendChild(content.template());
		
		// add as a listener
		this.addListener(content);
		
		// actual showing
		Effect.Appear($(this.overlay),{duration:this.time,from:this.low,to:this.high});
		
		// add the key press event
		Event.observe(document,"keydown",this.key_bfx);
		
		/* removed by nick on 3/2
		// add the click event 
		Event.observe($(this.overlay),"click",this.click_bfx);
		*/
		
		// hide all the selects
		if(Prototype.Browser.IE && $(this.overlay).next())
			$$('select:not(#'+$(this.overlay).next().id+')').each(function(item){
				item.setStyle({visibility:'hidden'});
			});
	},
	hide:function(content)
	{
		if(this.overlay==undefined)
			return;
		Effect.Fade($(this.overlay),{duration:this.time,from:this.high,to:this.low});
		
		this.remove_key_listener();
		
		/* removed by nick on 3/2
		Event.stopObserving($(this.overlay),"click",this.click_bfx);	// remove the click event
		*/
		
		// bring back all the selects
		if(Prototype.Browser.IE)
			$$('select').each(function(item){
				item.setStyle({visibility:'visible'});
			});
	},
	remove_key_listener:function()
	{
		Event.stopObserving(document,"keydown",this.key_bfx);	// remove the key event
	},
	build:function()
	{
		var context 	= $(document.getElementsByTagName("body")[0]);
		var c_width 	= context.getDimensions().width;
		var c_height 	= context.getDimensions().height;
		this.overlay 	= MyBuilder.make('div',{id:'overlay'},null,null);
		
		context.appendChild(this.overlay);
		$(this.overlay).setStyle({zIndex:this.zIndex,background:this.color,position:'absolute',display:'none',top:'0px',left:'0px',width:c_width+'px',height:c_height+'px'});

		// broadcasting 
		InializeAsBroadcaster(this);
		
		// watch those key presses
		this.key_bfx = this.keypress.bindAsEventListener(this);
		
		/* removed on 3/2
		this.click_bfx = this.click.bindAsEventListener(this);	// click events
		*/
	},
	escape:function()
	{
		this.broadcastMessage('hide');
	}
};

var Spinner = 
{
	spinner 	:undefined,		/* holds the spinner */
	time:		0.0,			/* amount of time fade takes */
	high:		1.0,			/* opacity when in-view */
	low:		0.0,			/* */
	content:	undefined,		/* content over the spinner */
	page_load:function(){
		this.build();
	},
	show:function(content)		
	{
		Overlay.show();
		
		if(this.spinner==undefined)
			this.build();
		
		Effect.Appear($(this.spinner),{duration:this.time,from:this.low,to:this.high});
	},
	hide:function(content)
	{
		if(this.spinner==undefined)
			return;
		Overlay.hide();
		Effect.Fade($(this.spinner),{from:this.high,to:this.low});
	},
	build:function()
	{
		var _width		= 16;
		var _height		= 16;
		
		var context 	= $(document.getElementsByTagName("body")[0]);
		this.spinner 	= MyBuilder.make('div',{id:'spinner'},null,null);
		context.appendChild(this.spinner);
		
		var viewport = document.viewport.getDimensions();
		var offsets = document.viewport.getScrollOffsets();
		var vp_width = viewport.width;
		var vp_height = viewport.height;
	
		// positioning
		var top = offsets.top+((vp_height-_height)/2);
		var left = offsets.left+((vp_width-_width)/2);
		
		$(this.spinner).setStyle({background:'url(/i/all/bg/loading.gif) no-repeat center',position:'absolute',display:'none',top:top+'px',left:left+'px',width:_width+'px',height:_height+'px'});
	}
};


/* base for content being laid over the page */
var Overlaid = {
	container_style:'overlaid-content rounded-10',
	id:'generic_overlay'
};
Massify.extend(Overlaid,Page);

Overlaid.template=function()
{	
	return MyBuilder.make('div',{id:this.id,style:'position:absolute;visibility:hidden;top:0px;left:0px'},this.container_style,null,null);
};

Overlaid.add_cancels=function()
{
	var overlaid_id = $(this.id).identify();
	// handle the cancel button/links
	$$('#'+overlaid_id+' .overlay-cancel-link').each(function(item) {
		Event.observe(item,"click",this.hide.bind(this));
	}.bind(this));
	$$('#'+overlaid_id+' .overlay-cancel-box').each(function(item) {
		Event.observe(item,"click",this.hide.bind(this));
	}.bind(this));
};

Overlaid.add_submits=function()
{
	var form = $(this.id).down("form");
	if(form)
	{
		$$('#'+$(this.id).down("form").identify()+' input[type="submit"]').each(function(item) {
	
			Event.observe(item,"click",function(evt){
				var e=Event.element(evt);
				var form=$(e.form);
				new Ajax.Request(form.action,{
					method:form.method,
					parameters:form.serialize()
				});
			});
	
		}.bind(this));
	}
};

Overlaid.position=function()
{
	var viewport = document.viewport.getDimensions();
	var offsets = document.viewport.getScrollOffsets();
	var vp_width = viewport.width;
	var vp_height = viewport.height;
	
	var overlaid_form = $(this.id).down();
	var overlaid_dims = overlaid_form.getDimensions();
	
	// positioning
	var top=offsets.top+(vp_height-overlaid_dims.height)*.7/2;
	var left=((vp_width-overlaid_dims.width)/2)+offsets.left;
	$(this.id).setStyle({zIndex:Overlay.zIndex+1,position:'absolute',top:top+"px",left:left+"px",visibility:'visible'});
	
	// focus first field while we're here!
	try {
		
		var text_input = overlaid_form.down('input[type="text"]');
		var text_area = overlaid_form.down('textarea');
		
		if (text_input && text_input.visible()) {
			text_input.focus();
		} else if (text_area && text_input.visible()) {
			text_area.focus();
		}
	} catch(e) {}
};

Overlaid.show=function(content)
{
	
	// add the content
	if(content)
	{
		Overlay.show(this);
		$(this.id).update(content);
	}
	
	// positioning
	this.position();
	
	// cancel buttons
	this.add_cancels();
	
	// any submits?
	this.add_submits();
		$$('.disabler').each(function(item){
			Event.observe(item,"click",function(){
				item.writeAttribute("disabled","true");
			});	
		});
	
};

Overlaid.update=function(content)
{
	if(Prototype.Browser.IE)
		$$('select:not(#'+this.id+')').each(function(item){
			item.setStyle({visibility:'hidden'});
		});
	
	$(this.id).update(content);
	
	this.add_cancels();
	
	this.add_submits();
};

Overlaid.hide=function()
{	
	if ($(this.id)) $(this.id).remove();
	Overlay.hide();
	Overlay.removeListener(this);
};

Overlaid.confirm=function(header, message, button_text)
{
	Overlay.show(this);
	
	// add the content
	$(this.id).update(
		'<div class="overlaid">'+
		'	<a class="overlay-cancel-box" href="javascript:void(0);"></a>'+
		'	<div class="overlaid-inner">'+
		'		<h2 id="confirm_title"></h2>'+
		'		<p id="confirm_message"></p>'+
		'		<div class="button-line">'+
		'			<input type="button" id="confirm_button" value="" class="button1 overlay-submit-link"/>'+
		'			<a href="javascript:void(0);" id="confirm_button" class="overlay-cancel-link">Cancel</a>'+
		'		</div>'+
		'	</div>'+
		'</div>');
		
	$('confirm_title').update(header);
	$('confirm_message').update(message);
	$('confirm_button').value = button_text;
	
	Event.observe($('confirm_button'),"click",this.onConfirm.bindAsEventListener(this));
	
	// positioning
	this.position();
	
	// cancel buttons
	this.add_cancels();
	
};

Overlaid.block=function(content)
{
	Overlay.show(this);
	Overlay.remove_key_listener();
	
	// add the content
	$(this.id).update(content);
	
	// positioning
	this.position();
	
};