// Script: Application.js

var Application = {
	
	init: function()
	{
		
		Drilldown.init();
		
		LinkButtons.init();
		
		Behaviours.init();
		
	}
	
};

window.addEvent('domready', Application.init);

var LinkButtons = {
	
	init: function()
	{
		
		var normalLinks = $$( '.linkButton a' );
		var darkLinks = $$( '.linkButtonDark a' );

		normalLinks.each( LinkButtons.createHover );
		darkLinks.each( LinkButtons.createHoverDark );
		
	},
	
	createHover: function( el )
	{
		el.morphOnHover(
			{
				'background-color': '#fffae8',
				'border-color': '#dfd6b9'
			},
			{
				'background-color': '#c7e6ff',
				'border-color': '#6093c8'
			}
		);
	},
	
	createHoverDark: function( el )
	{
		el.morphOnHover(
			{
				'background-color': '#404040',
				'border-color': '#272727'
			},
			{
				'background-color': '#4f4f4f',
				'border-color': '#272727'
			}
		);
	}
	
}

var Drilldown = {

	init: function()
	{
		
		var aa = $$( '.drilldown a' );
		
		aa.each( Drilldown.createHover );
		
	},
	
	createHover: function( el )
	{				
		el.morphOnHover( { 'background-color': Theme.colour4 }, { 'background-color': '#ffffff' } );
	}

}

var Behaviours = {
	
	init: function()
	{
		
		var els = $$( '.has_behaviour' );
		
		els.each(
			function(el)
			{
				var prop_behaviour = initArray( el.getProperty( 'behaviour' ) );
				
				prop_behaviour.each(
					function( b )
					{
									
						if ( b == 'show_hide_containers' )
							Behaviours.Show_Hide.init( el );
							
						else if ( b == 'ajax_get_silent' )
							Behaviours.Ajax_Get.init( el, 'silent' );
							
						else if ( b == 'confirm_delete' )
							Behaviours.Confirm_Delete.init( el );
						
						else if ( b == 'javascript' )
							Behaviours.Javascript.init( el );
					
					}
				);
			}
		);
		
	},
	
	Javascript: {
		
		init: function( srcEl )
		{
			// init properties
			var js = srcEl.getProperty( 'behaviour_javascript' );
			
			srcEl.addEvent( 'click',
				function()
				{
					eval( js );
				}
			);
		}
		
	},
	
	Confirm_Delete: {
		
		init: function( srcEl )
		{
			// init properties
			var url = srcEl.getProperty( 'behaviour_confirmtext' );
			
			srcEl.addEvent( 'click',
				function()
				{
					return confirm('Are you sure you want to do this?');
				}
			);
		}
		
	},
	
	Ajax_Get: {
		
		init: function( srcEl, mode )
		{
			// init properties
			var prop_ajaxUrl = srcEl.getProperty( 'behaviour_ajax_url' );
			
			srcEl.addEvent( 'click',
				function()
				{
					if ( mode == 'silent' )
						$log( prop_ajaxUrl );
						// silent mode, we just fire off the request and forget about it...
						new Request({ url: prop_ajaxUrl }).get();
				}
			);
			
		}
		
	},
	
	Show_Hide: {
		
		init: function( srcEl )
		{
			
			// init properties
			var prop_elsToShow = srcEl.getProperty( 'behaviour_container_show' );
			var prop_elsToHide = srcEl.getProperty( 'behaviour_container_hide' );
			var prop_applyOnInit = srcEl.getProperty( 'behaviour_applyoninit' );
			var prop_invertOnInit = srcEl.getProperty( 'behaviour_invertoninit' );
			var prop_transition = srcEl.getProperty( 'behaviour_transition' );
			var prop_toggleMode = srcEl.getProperty( 'behaviour_toggleMode' );
			
			var currentState = false;
			
			
			// helper to add elements to the array
			addElements = function( arr, id )
			{				
				var el = $( 'container_' + id );
				
				if ( el )
					arr.push( el );
			}
			
			
			// init elements to show
			var elsToShow = [];
			
			if ( prop_elsToShow == 'self' )
				elsToShow.push( srcEl );
			else
				initArray( prop_elsToShow ).each( function( id ) { addElements( elsToShow, id ); } ); // elsToShow.push( $( 'container_' + el ) ); } );
			
			
			// init elements to hide
			var elsToHide = [];
			
			if ( prop_elsToHide == 'self' )
				elsToHide.push( srcEl );
			else
				initArray( prop_elsToHide ).each( function( id ) { addElements( elsToHide, id ); } );
			
			//$log( 'initialised show/hide behaviour for element', srcEl, 'els to hide: (' + prop_elsToHide + ')', elsToHide, 'els to show: (' + prop_elsToShow + ')', elsToShow );
			
			// the function to bind
			var doShowHide = function( transition )
			{
				if ( !currentState || prop_toggleMode )
					currentState = !currentState;
				
				// make sure that if some other behaviour has re-hidden the element, we force showing it again.
				var anyVisible = false;
				elsToShow.each( function( el ) { if ( el.isVisible() ) anyVisible = true; } );
				if ( !anyVisible )
					currentState = true;
				
				// use internal versions of these arrays so we can switch them...
				var _elsToShow = elsToShow;
				var _elsToHide = elsToHide;
				
				// if currentState is false.
				if ( !currentState )
				{
					_elsToHide = elsToShow;
					_elsToShow = elsToHide;
				}
				
				switch ( transition )
				{						
					case 'fade':
						if ( elsToShow.length )
						{							
							_elsToShow.each(
								function( el )
								{
									el.setOpacity( 0 ).show().fade( 'in' );
								}
							);
							
							_elsToHide.each(
								function( el ) { el.hide(); }
							);
							
						}
						else
						{
							_elsToHide.each(
								function( el ) {
									el.fade( 'out' );
									(function() { el.hide(); }).delay(500);
								}
							);
						}
						
						break;
					
					
					case 'slide':
						
						
					default:
						
						_elsToShow.each(
							function( el ) { el.show(); }
						);	
						_elsToHide.each(
							function( el ) { el.hide(); }
						);
						
				}
				
			}
			
			// bind the function to the click event
			srcEl.addEvent( 'click',
				function (e)
				{
					doShowHide( prop_transition );
					new Event( e ).stop();
				}
			);
			
			
			// apply the transtion on init?
			if ( prop_applyOnInit == 'true' )
			{
				doShowHide( 'none' );
			}
			
			// or do we in fact want to invert it? hmmmmm?
			else if ( prop_invertOnInit == 'true' )
			{
				elsToShow.each(
					function( el ) { el.hide(); }
				);	
				elsToHide.each(
					function( el ) { el.show(); }
				);
			}

		}

	}

}