/**
 * jQuery Flash Plugins v1.0.0
 *
 * @copyright		2010 - 2011 Inc
 * @author			Jack( 733433@qq.com )
 */



// - package begin
// - (function ($) {


$.fn.writeFlash = function ( attributes )
{
	return this.each( function ()
	{
		new Flash( attributes ).write( this );
	});
};


/**
 *	Cerate global class of Flash
 *
 */
function Flash( attributes )
{
	if ( attributes )
	{
		$.extend( this.attributes, attributes );
	}

	return this;
};


/**
 *	Extend members of rotation
 *
 */
Flash.prototype = {

	/**
	 * Variables
	 *
	 */
	variables : {},
	
	/**
	 * Attributes
	 *
	 */
	attributes :
	{
		src : '',
		width : 0,
		height : 0,
		quality : 'high',
		bgColor : '#FFFFFF',
		version : '8.0.0.0',
		id : 'Flash_Demo',
		type : 'application/x-shockwave-flash'
	},
	

	/**
	 * ParamAttributes
	 *
	 */
	 paramAttributes : {},

	/**
	 * EmbedAttributes
	 *
	 */
	embedAttributes :
	{
		pluginspage : 'http://www.macromedia.com/go/getflashplayer'
	},

	/**
	 * ObjectAttributes
	 *
	 */
	objectAttributes :
	{
		classid : 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000',
		codeBase : 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0'
	},

	/**
	 * GetAttribute
	 *
	 */
	getAttribute : function ( name )
	{
		return this.attributes[ name ];
	},

	/**
	 * SetAttribute
	 *
	 */
	setAttribute : function ( name, value )
	{
		switch ( name.toLowerCase () )
		{
			case 'pluginspage' : case 'classid' : break;

			case 'codebase' :

				this.objectAttributes[ name ] = value;

			break;

			case 'src' :
			case 'movie' :

				this.paramAttributes[ 'movie' ] = value;
				this.embedAttributes[ 'src' ] = value;

			break;

			case 'variables' :

				name = 'FlashVars';
				// - no break here

			case 'flashvars':
				
				for ( name in value )
				{
					this.variables[ name ] = value[ name ];
				}

			break;

			case 'width' :
			case 'height' :
			case 'align' :
			case 'vspace' :
			case 'hspace' :
			case 'title' :
			case 'class' :
			case 'name' :
			case 'id' :
			case 'accesskey' :
			case 'tabindex' :
			case 'type' :

				this.objectAttributes[ name ] = value;
				this.embedAttributes[ name ] = value;

			break;

			default : 
				
				if ( name.substr( 0, 2 ).toLowerCase() == 'on' )
				{
					this.objectAttributes[ name ] = value;
				}
				else
				{
					this.addParam( name, value );
				}

			break;
		}

		this.attributes[ name ] = value;

		return this;
	},

	/**
	 * RemoveAttribute
	 *
	 */
	removeAttribute : function ( name )
	{
		delete this.attributes[ name ];

		return this;
	},
	
	/**
	 * GetVariable
	 *
	 */
	getVariable : function ( name )
	{
		return this.variables[ name ];
	},

	
	/**
	 * AddVariable
	 *
	 */
	addVariable : function ( name, value )
	{
		this.variables[ name ] = value;

		return this;
	},

	/**
	 *	RemoveVariable
	 *
	 */
	removeVariable : function ( name )
	{
		delete this.variables[ name ];

		return this;
	},

	/**
	 *	GetParam
	 *
	 */
	getParam : function ( name )
	{
		return this.paramAttributes[ name ] || this.embedAttributes[ name ];
	},

	/**
	 *	AddParam
	 *
	 */
	addParam : function ( name, value )
	{
		this.paramAttributes[ name ] = value;
		this.embedAttributes[ name ] = value

		return this;
	},

	/**
	 *	RemoveParam
	 *
	 */
	removeParam : function ( name )
	{
		delete this.paramAttributes[ name ];
		delete this.embedAttributes[ name ];

		return this;
	},

	/**
	 * Read
	 *
	 */
	read : function ( set )
	{		
		var i, contents = [],
			
		variables = this.variables,

		objects = this.objectAttributes,
		
		params = this.paramAttributes,
			
		embeds = this.embedAttributes;

		for ( i in variables )
		{
			contents.push( i + '=' + variables[ i ] );
		}

		params.FlashVars = embeds.FlashVars = contents.join( '&' );

		contents = [ "\n<object " ];

		for ( i in objects )
		{
			contents.push( i + " = '" + objects[ i ] + "' " );
		}

		contents.push ( ">\n" );

		for ( i in params )
		{
			contents.push( "\t<param name = '" + i + "' value = '" + params[ i ] + "' />\n" );
		}

		contents.push( "\t<embed " );

		for ( i in embeds )
		{
			contents.push( i + " = '" + embeds[ i ] + "' " );
		}

		variables = objects = params = embeds = null;

		return contents.join( '' ) + "></embed>\n</object>\n";
	},

	/**
	 * Write
	 *
	 */
	write : function ( selector )
	{
		if ( this.container = $( selector ) )
		{
			this.container.html( this.read() );
		}
		
		return this;
	},
	
	/**
	 * Clear
	 *
	 */
	clear : function ()
	{
		if ( this.container )
		{
			this.container.html( '' );
		}

		return this;
	}

};


// - }) (jQuery);
// - package end
