	
	/*
	JS
	--------------------------------------------------------------------------------------------  
	@site				sho.com
	@project			sho (970)
	@package			common
	@file				Dialogs.js
	@modified 			09.11.09
	@author				dpaul
	@desc				Manager for Dialog Boxes
	@depend				Prototype
	@note				New school buttons! yeah!
	@usage
	simple
	Dialogs.out( 'hello there');
		
	w/ buttons
	Dialogs.out( 'You must be logged in to rate a title.', [
		{ label:'cancel', onclick:'close' },
		{ label:'log in', onclick:SHO.membership.login }
	]);
	
	/* =:Dialogs
	-------------------------------------------------------------------------------------------- */  
	namespace('sho.common');
	
	sho.common.Dialogs = function()
	{	
		var _inited = false;
		var _content;
		var _container;	
		var _containerHTML = ([
			'<div id="dialogbox" style="display:none;">',
				'<div id="dialogbox-inner" class="group">',
				'</div>',
			'</div>'
		
		]).join("\n");
		
		var _simple = new Template([
			'<div id="dialogbox-msg">',
				'<p>#{msg}</p>',
			'</div>',
			'<div id="dialogbox-options">',
				'<ul class="btns m">',
					'#{buttons}',
				'</ul>',
			'</div>'			
		].join("\n"));
		
		var CONTAINER_TOP = 170;
		var DIALOG_IN = 0.25;
		var DIALOG_OUT = 0.25;
		var TITLE_CASE = true;
		
		/* =:Startup
		-----------------------------------------------------------------------------------------*/  
		function init()
		{
			$$('body')[0].insert({bottom:_containerHTML });
			
			(function(){  
				_inited = true; 
				_container = $('dialogbox');
				_content = $('dialogbox-inner');
				new Effect.Opacity( _container, { to:0, duration:0 });
			}).defer();
		}
		
		/* =:Runtime
		-----------------------------------------------------------------------------------------*/  
		function openDialog(message, buttons)
		{
			if( !_inited ){ init(); 
				(function(){ openDialog( message, buttons ); }).delay(0.25); }
				
			else update( message, buttons);
		}
		
		function update( message, btns)
		{
			if( _container.viewportOffset()[1] < 0 )
			{
				var y = CONTAINER_TOP + Math.abs( _container.viewportOffset()[1]);
				_container.setStyle({top:y+'px'});
			}
									   
			var buttons = btns == undefined ? 
				[{ label:'okay', onclick:'closeDialog'}] : btns;
		
			_content.update( _simple.evaluate({
				msg:message, 
				buttons:getButtonString(buttons)
			}));
			
			(function(){ 
				setButtonHandlers(buttons); 
				show(); 
			}).defer();
		}
		
		function setButtonHandlers(buttons)
		{
			if( $$('#dialogbox-options a').length !== buttons.length ){ 
				Debugger.t( 'button length doesnt match markup' ); return; }
			
			$$('#dialogbox-options a').each( function(a, idx)
			{
				// callback function with scope as object ie { onclick:sumthing, scope:this}
				if( buttons[idx].scope !== undefined && typeof buttons[idx].onclick !== 'string' ){
					Event.observe( a, 'click', function(e){ Event.stop(e);
						buttons[idx].onclick().bind( buttons[idx].scope );
					});
				}
				// callback function with implicit scope in method ie { onclick:MySingleton.reallyDoit }
				else if( typeof buttons[idx].onclick !== 'string' ){
					Event.observe( a, 'click', function(e){ Event.stop(e);
						buttons[idx].onclick();
					});
				}
				
				// always close the dialog
				Event.observe( a, 'click', function(e){ Event.stop(e);
					Dialogs.closeDialog();
				});
			});
		}
		
		/* =:Util, =:Helpers
		-----------------------------------------------------------------------------------------*/  
		function getButtonString( data )
		{
			return (data.collect(function(b){
				var cn = (b.className !== undefined) ? ' class="'+b.className+'"' : '';
				var label = TITLE_CASE ? titleCase( b.label ) : b.label;
				return '<li><a href="#">'+label+'</a></li>';	
			}).join(''));
		}
		
		function titleCase( str )
		{
			return (str.split(' ').collect( function(w){
				return w.substr(0,1).toUpperCase() + w.substr(1);
			}).join(' '));
		}
		
		function show(){
			 new Effect.Opacity( _container.setStyle({display:'block'}), 
				{ to:1.0, duration:DIALOG_IN });
		}
		
		function hide(){		
			 new Effect.Opacity( _container, { 
					to:0, duration:DIALOG_OUT, 
					afterFinish:(function(){ _container.hide(); }), 
					afterFinishScope:this 
			});
		}
		
		function closeDialog(){
			hide();
		}
		
		
		/* =:Reveal as Public
		-----------------------------------------------------------------------------------------*/  
		return {
			out:openDialog,
			openDialog:openDialog,
			d:openDialog,
			closeDialog:closeDialog,
			c:closeDialog
		}
	
	}();
	
	/* alias for backwards compatibility*/
	window.Dialogs = sho.common.Dialogs;
