// #########################################################################
//
//	Blask Art Studios Javascript Framework
//
// #########################################################################

/*
   Class: BaseObject
   
		A Base implementation for an dynamic HTML object
*/
var BaseObject = new Class( {
	
	options: {
		
		container: null
	
	},

	initialize: function( options ) {

		this.setOptions( options );
		
		this.parameterNames = new Array( );
		this.parameterValues = new Array( );
		
		this.setupParameters( );
		
	},
	
	setupParameters: function( ) {

		var rel = this.options.container.getProperty( 'rel' );

		if ( rel != null )
		{
			rel = rel.replace(/^\s+|\s+$/g, '');
		
			var relParams = rel.split( "," );
			
			$A( relParams ).each( function ( param ) {
							
				this.setParameter(
					param.split( ":" )[ 0 ],
					param.split( ":" )[ 1 ]
					);
	
			
			}, this );	
		}
		
	},
	
	/*
	   Function: SetParameter
	
			Sets a Parmeter for the Object
	
	   Parameters:
	
			name - The name of the Parameter
		  	value - The value of the Parameter
	*/
	setParameter: function( name, value ) {
	
		var exists = false;
	
		for ( var i = 0; i < this.parameterNames.length; i++ )
		{
			if ( this.parameterNames[ i ] == name )
			{
				this.parameterNames[ i ] = name;
				this.parameterValues[ i ] = value;
				exists = true;
			}
		}
			
		if ( !exists )
		{
			this.parameterNames.push( name );
			this.parameterValues.push( value );	
		}
	
	},
	
	/*
	   Function: GetParameter
	
			Gets the specified Parements of the Object
	
	   Parameters:
	
		  	name - The name of the Paremeter
	
	*/
	getParameter: function( name ) {
		
		var returnValue = null;
		
		for ( var i = 0; i < this.parameterNames.length; i++ )
		{
			if ( this.parameterNames[ i ] == name )
			{			
				returnValue = this.parameterValues[ i ];
			}
		}
		
		return returnValue;
		
	},
	
	/*
	   Function: RemoveParameter
	
			Removes the specified Paremeter of the Object
	
	   Parameters:
	
			name - The name of the Paremeter
	
	*/
	removeParameter: function( name ) {
		
		for ( var i = 0; i < this.parameterNames.length; i++ )
		{
			if ( this.parameterNames[ i ] == name )
			{
				this.parameterNames.pop( i );
				this.parameterValues.pop( i );
			}
		}
	
	},
	
	/*
	   Function: Render
	
		Renders the Object to the HTML page
		
	*/
	render: function( ) {
	
	}
	
	
} );

BaseObject.implement( new Options );

// #########################################################################
//
//	Flash
//
// #########################################################################

/*
   Class: FlashMovie
		Dynamically inject the html necessary to render a flash object within HTML
*/
var FlashMovie = BaseObject.extend( {
	
	options: {
		
		container: null,
		title: '',
		transparency: true
	
	},

	initialize: function( options ){
		
		this.parent( options );		
		this.filePath = null;
		this.fileName = null;
		this.commandElement = null;

	},
	
	/*
		Function: Render
	
			Renders the Flash Object to the HTML page
	
	*/
	render: function( ) {
	
		var so = new SWFObject(
			this.filePath + this.fileName, 
			this.options.title, 
			this.options.container.getStyle( 'width' ).replace( 'px', '' ), 
			this.options.container.getStyle( 'height' ).replace( 'px', '' ), 
			'0', 
			'ffffff'
		);
			
		if ( this.options.transparency )
		{
			so.addParam("wmode", "transparent");
		}
	
		for( var i = 0; i < this.parameterNames.length; i++ )
		{
			so.addVariable( this.parameterNames[ i ], this.parameterValues[ i ] );
		}
		
		so.addParam( 'cache', 'true' );
		
		so.write( this.options.container.getProperty( 'id' ) );
		
		this.commandElement = new Element( 'script' )
			.setProperty( 'id', this.options.title + '_command' )
			.setProperty( 'type', 'text/javascript' );
		
		this.commandElement.injectInside( this.options.container );
	
	}
	
	
} );

FlashMovie.implement( new Options );

var TITLECOLOR = 'titlecolor';
var TITLETEXT = 'titletext';
var TITLESIZE = 'titlesize';

/*
   Class: FlashText
   
		FlashText extends the FlashMovie class and implements text reading from inside the div tag.
		Why not just use a property in the rel tag I hear you say!? Think SEO & Accessibility :P
*/
var FlashText = FlashMovie.extend( {
	
	initialize: function( options )
	{
		this.parent( options ); 
		this.fileName = 'Title.swf';
		this.setParameter( TITLETEXT, this.options.container.getText( ) );
	},
	
	render: function( ) {
		
		var size = this.getParameter( TITLESIZE );
		var color = this.getParameter( TITLECOLOR );
		
		if ( size != null )
		{
			this.setParameter( TITLESIZE, this.getParameter( TITLESIZE ).replace( 'pt', '' ).replace( 'px', '' ) );	
		}
		
		if ( color != null )
		{
			this.setParameter( TITLECOLOR, this.getParameter( TITLECOLOR ).replace( '#', '' ) );		
		}
		
		this.parent( );

	}

} );

/*
	Class: FlashManager
	
		FlashManager iterates through the page for Flash Objects and renders them
*/

var FLASHMOVIE = 'flashmovie';
var FLASHTEXT = 'flashtext';

var FlashManager = new Class( {

	options: {
		
		className: new Array( FLASHMOVIE, FLASHTEXT ),
		filePath: libPath + '/flash/'
	
	},

	initialize: function( options ) {
	
		this.flashElements = new Array( );
	
		this.options.className.each( function( className ) {
		
			var flashElementList = $$( 'div.' + className );
			
			for( var i = 0; i!= flashElementList.length; i++ )
			{
				var element = flashElementList[ i ];
			
				if ( element.getProperty( 'id' ) == null || element.getProperty( 'id' ) == '' )
				{
					element.setProperty( 'id', 'flashObject_' + i );
				}
					
				this.flashElements.push( element );
				
			}
		
		}, this );
		
	},
	
	/*
		Function: RenderObjects
	
			Renders all Flash Objects on the page
	
	*/
	
	renderObjects: function( ) {

		for( var i = 0; i != this.flashElements.length; i++ )
		{
			var flashContainer = this.flashElements[ i ];
										  
			var flashObject = null;
			
			if ( flashContainer.getProperty( 'class' ).contains( FLASHMOVIE ) ) {
				
				flashObject = new FlashMovie( {
											  
					container: flashContainer,
					title: flashContainer.getProperty( 'id' ),
					transparency: true
				
				} );
				
			}
			
			if ( flashContainer.getProperty( 'class' ).contains( FLASHTEXT ) ) {
				
				flashObject = new FlashText( {
											  
					container: flashContainer,
					title: flashContainer.getProperty( 'id' ),
					transparency: true
				
				} );
				
			}
			
			flashObject.filePath = this.options.filePath;
			flashObject.fileName = flashObject.getParameter( 'filename' ) + '.swf';
			
			
			flashObject.render( );
			
		}

	}
	
} );

FlashManager.implement( new Options );

window.addEvent( 'domready', function() {
									  
	var flashManager = new FlashManager( {
										
		filePath: libPath + '/flash/'
		
	} );
	
	flashManager.renderObjects( );
		
} );


// #########################################################################
//
//	Modal
//
// #########################################################################

/*
	Class: Modal
	
		Modal places a modal in the center of the HTML page
*/
var Modal = BaseObject.extend( {
					  
	options:
	{
		content: '',
		modalWidth: 0,
		modalHeight: 0
	},
					  
	initialize: function( options )
	{

		this.parent( options ); 
		this.background = new Element( 'div' ).setProperty( 'id', 'modalbackground' );
		
		var height = this.options.container.getStyle( 'height' ).replace( 'px', '' );	
		var width = this.options.container.getStyle( 'width' ).replace( 'px', '' );
		var domBody = $( document.body );
		var bodyWidthOffset = ( window.getWidth( ) - domBody.getCoordinates( ).width );

		var top = ( window.getHeight( ) / 2 ) - ( height / 2 );
		var left = ( ( window.getWidth( ) / 2 ) - ( bodyWidthOffset / 2 ) ) - ( width / 2 );

		this.background.setStyles( { 
			'position': 'absolute', 
			'left': 0, 
			'top': 0, 
			'z-index': 9999998,
			'width': domBody.getCoordinates( ).width,
			'height': domBody.getCoordinates( ).height,
			'opacity': 0
		} );
		
		this.options.container.setStyle( 'z-index', 9999999 );
		this.options.container.setStyle( 'position', 'fixed' );
		this.options.container.setStyle( 'top', top );
		this.options.container.setStyle( 'left', left );
		this.options.container.setStyle( 'opacity', 0 );
		
	},
	
	/*
		Function: Bind
		
			Binds the modal to the action
	*/
	bind: function( ) {
		
		var actionElement = this.getParameter( 'bind' );
		var action = $( actionElement );
		var modal = this;
		
		action.addEvent( 'click', function( e ) {
			
			var event = new Event( e ).stop( );
			modal.render( );
		
		} );
		
	},
	
	/*
		Function: Render
			Renders the Modal to the screen
	*/
	render: function( ) {	
	
		this.background.injectInside( document.body );
		this.background.setStyle( 'opacity', 0.5 );

		this.popup( );
		
	},
	
	/*
		Function: Popup
		Shows the Modal
	*/
	popup: function( ) {
		
		this.options.container.setStyle( 'display', 'block' );
		
		var modalFx = new Fx.Styles( this.options.container, { duration: 1000, wait: false } );
		modalFx.options.transition = Fx.Transitions.Quad.easeOut;
		modalFx.start( { opacity: 1 } );
		
	},
	
	close: function( ) {
		
		var modalFx = new Fx.Styles( this.options.container, { duration: 1000, wait: false } );
		modalFx.options.transition = Fx.Transitions.Quad.easeOut;
		modalFx.start( { opacity: 0 } );
		
		this.options.container.setStyle( 'display', 'none' );
		
		this.background.remove( );
		
	}
	
} );

Modal.implement( new Options );

var AjaxModal = Modal.extend( {
						
	initialize: function( options )
	{
		this.parent( options );
	},
	
	bind: function( )
	{
		this.parent( );
		
		var responseText = '';
		
		var ajax = new Ajax( 
			this.getParameter( 'url' ), { 
				method: 'get', 
				update: this.options.container.getProperty( 'id' )
			}
		).request( );
		
	}

} );

/*
	Class: ModalManager
	
		ModalManager iterates through the HTML page and binds Modals to their Event Handlers
*/
var ModalManager = new Class( {

	options: {
	
		modalClass: new Array( 'modal', 'ajaxModal' )
	
	},

	initialize: function( )
	{

		this.modalElements = $$( 'div.' + this.options.modalClass[ 1 ] );
		this.modalObjects = new Array( );
		
	},
	
	bindModals: function( )
	{
		this.modalElements.each( function( modalElement ) {
		
			var modal = null;
			
			switch ( modalElement.getProperty( 'class' ) )
			{
				case 'modal': modal = new Modal( { container: modalElement } );	
				case 'ajaxModal': modal = new AjaxModal( { container: modalElement } );	
				
			}
			
			if ( null != modal )
			{
				modal.bind( );
				this.modalObjects.push( modal );
			}
									  
		}, this );
	}

} );

ModalManager.implement( new Options );

window.addEvent( 'domready', function( ) {
									  
	var modalManager = new ModalManager( );
	modalManager.bindModals( );
	
} );
