/*
 *      -----------------------------------------------------
 *     |                                                     |
 *     |                                     <bluntchisel>   |
 *     |                                                     |
 *     |                                                     |
 *     |                                                     |
 *     |                                                     |
 *     |  content management system v3.0                     |
 *     |  copyright (c) 2007-2008                            |
 *     |                                                     |
 *     |                                                     |
 *      -----------------------------------------------------
 *      
 *      file        : MediaModule.js
 *      description : A media player for the cms v3.0
 *                    to play the .mp3 files under the media directory
 *                    in a flash media player.
 */


var MediaModule = new Class({
	'ajaxURL'        : '',
	'rootURL'        : '',
	'mediaPath'      : '',
	'mediaFilesPath' : '',
	'mediaRowBackground' : '',
	
	/*
	 * Constructor.
	 * 
	 * @param _slot The slot in which the module exists.
	 * @param _page The page ID in which the module is being rendered.
	 * @param _varName The name of the variable of type GalleryModule.
	 * @param _mediaPath The path of the meta data in the data tree
	 * @param _mediaFilesPath The to the media files
	 * @param _options Extra non-critical options
	 */
	initialize: function (_slot, _page, _varName, _mediaPath, _mediaFilesPath, _options)
	{
		// if additional parameters have been supplied
		if (_options) $extend(this, _options);
		
		// element initialisation
		this.slot = _slot;
		this.page = _page;
		this.varName = _varName;
		this.mediaPath = _mediaPath;
		this.mediaFilesPath = _mediaFilesPath;
		
		window.addEvent('domready', function () {
			if (admin)
			{
				admin.addAddButton('uploadMediaButton', '', 'Upload new music files',
				serverURL+'cms/css/MediaModule/uploadMediaLo.jpg',
				serverURL+'cms/css/MediaModule/uploadMediaHi.jpg',
				"admin.showFileUploader(false,'mediaMode','"+this.mediaFilesPath+"');");
				
				admin.addDeleteButton();
			}
		}.bind(this));
	},
	
	
	// a mouseOver function to highlight the file you want to delete
	mouseOver: function(_id)
	{
		if (admin.deleteMode==true)
		{
			if(this.mediaRowBackground!='') this.mediaRowBackground = $(_id).getStyle('background-color');
			$(_id).setStyles({'background-color':'#bbbbbb','cursor':'pointer'});
		}
		
	},
	//a function to display the <tr> as normal again
	mouseOut: function(_id)
	{
		if( admin.deleteMode==true)
		{
			$(_id).setStyles({'background-color':this.mediaRowBackground,'cursor':'default'});
		}
	},
	
	mouseClick: function(_id)
	{
	
		if( (admin) && (admin.deleteMode==true))
		{
			//make sure you ask before deletion
			if (window.confirm('Are you sure you want to delete ' + $(_id).get('filename')))
			{
				//create a new ajax request
				var ajaxRequest = new Request({url:serverURL+'ajax',
								method:'post',
								data : {'slot':this.slot,'page':this.page,'module':'MediaModule','cmd' : 'deleteFile' ,'filename' : $(_id).getProperty('filename')},
								//on complete the function deletes the file from the database and the <tr>
								onComplete: function(_data) {
											var _retVal = '';
											if(_data!='')
												if( (_retVal = JSON.decode(_data) )!=false )
												{
													if(_retVal.status == 'success')
													{
														$(_id).dispose();
														if (resizeMainWindow != null)
															resizeMainWindow();
													} else {
														if(_retVal.status == 'error')
															alert(_retVal.message);
													}
												}
										}});
											
				//sends the ajax request
				ajaxRequest.send();
				
			}
		}
	}
});



