/**
 * @author mikep
 */
var FlashImageViewer = {	

	flashObj : '',
	swf_url : "GalleryViewer.swf",
	setup_flag: false,
	no_images : 0,
	m_swf_width : 0,
	m_swf_height : 0,
	m_on_complate_callback : '',
	
	/**
	* Get a handle to the flash object, and set it up as required
	* @para targetDiv - (optional) div for the div section that will contain this widget
	* @para swfHeight - (optional) height of file uploader applet
	*/
	init : function(targetDiv, swfWidth, swfHeight){

		if ((typeof(swfWidth) == "undefined") || (typeof(swfHeight) == "undefined")) {
			dimensions = Element.getDimensions($(targetDiv));
			//		offsets = Position.cumulativeOffset( $('image_holder') );
			swfWidth = dimensions.width;
			swfHeight = dimensions.height;
		}
		
		FlashImageViewer.m_swf_width = swfWidth;
		FlashImageViewer.m_swf_height = swfHeight;
		FlashImageViewer.flashObj = '';
		FlashImageViewer.setup_flag = false;
/*

		Doesn't seem to work in IE 6!!!
		var FO = { movie:this.swf_url, 
				id: 'flashObject',
				width: swfWidth, 
				height: swfHeight, 
				align:'middle', 
				allowScriptAccess:'sameDomain', 
				wmode:'transparent', 
				bgcolor:'#ffffff', 
				quality:'high', 
				majorversion:"8", 
				build:"40" };
		
		UFO.create(FO, targetDiv);
		
*/
	
		text = "";
		
		text += "   <object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' \n";
		text += "           width='" + swfWidth + "' height='" + swfHeight + "' id='flashObject' align='middle'> \n";
		text += "      <param name='allowScriptAccess' value='sameDomain' /> \n";
		text += "      <param name='movie' value='" + this.swf_url + "' /> \n";
		text += "      <param name='align' value='left' /> \n";
		text += "      <param name='quality' value='high' /> \n";
		text += "      <param name='wmode' value='transparent' /> \n";
		text += "      <param name='bgcolor' value='#ffffff' /> \n";
		text += "      <embed src='" + this.swf_url + "' \n";
		text += "         quality='high' \n";
		text += "         wmode='transparent' \n";
		text += "         bgcolor='#ffffff' \n";
		text += "         width='" + swfWidth + "' \n";
		text += "         height='" + swfHeight + "' \n";
		text += "         align='left' \n";
		text += "         name='flashObject' \n";
		text += "         allowScriptAccess='sameDomain'  \n";
		text += "         type='application/x-shockwave-flash'  \n";
		text += "         pluginspage='http://www.macromedia.com/go/getflashplayer' /> \n";
		text += "   </object> \n";
		text += "	<div id='caption'></div>"
		
		$(targetDiv).innerHTML = text;
			
		if (navigator.appName.indexOf('Microsoft') != -1) {
			FlashImageViewer.flashObj = window.flashObject;
		}
		else {
			FlashImageViewer.flashObj = window.document.flashObject;
		}
		
		FlashImageViewer.setupFlash();
						
	},
		
		
	/**
	 * Set an external callback to be called when an image is loaded
	 * @param {Object} func
	 */	
	setOnComplateCallback : function(func){
		FlashImageViewer.m_on_complate_callback = func;
	},
	
	/**
	 * Getter method, used by the flash object to get the width
	 */	
	getWidth : function() {
		return FlashImageViewer.m_swf_width;
	},
	
	/**
	 * Getter method, used by the flash object to get the height
	 */
	getHeight : function() {
		return FlashImageViewer.m_swf_height;
	},
		
	/**
	 * Set the currently displayed image
	 * @param {Object} no
	 */	
	setImage : function(no){
		if (!FlashImageViewer.setup_flag) {
			setTimeout("FlashImageViewer.setImage(" + no + ")", 50);
			return;
		}
		FlashImageViewer.trace('Setting image to ' + no);
		this.flashObj.setImage(no);
	},

	/**
	 * Set the xml filename for the flash object
	 * @param {string} filename
	 */
	setXMLFilename : function(filename){
		if (!FlashImageViewer.setup_flag) {
			setTimeout("FlashImageViewer.setXMLFilename("+filename+")", 50);
			return;
		}
		FlashImageViewer.trace('Setting xml filename to ' + filename);
		this.flashObj.setXMLFilename(filename);	},

	
	/**
	 * Set the size and position of the image viewer (flash object)
	 * @param {int} xPos
	 * @param {int} yPos
	 * @param {int} width
	 * @param {int} height
	 */
	setPosition : function(xPos, yPos, width, height){
		if (!FlashImageViewer.setup_flag) {
			setTimeout("FlashImageViewer.setPosition("+xPos+","+yPos+","+width+","+height+")", 50);
			return;
		}
		FlashImageViewer.trace('Setting position to ' + xPos + " " + yPos + " " + width + " " + height);
		this.flashObj.setPosition(xPos, yPos, width, height);
	},

	
	setSize : function(width, height){
		if (!FlashImageViewer.setup_flag) {
			setTimeout("FlashImageViewer.setSize("+width+","+height+")", 50);
			return;
		}
		FlashImageViewer.trace('Setting size to ' + width + " " + height);
		this.flashObj.setSize(width, height);
	},
	
	/**
	 * Set the speed of the transition effect (0-100)
	 * @param {int} speed (0-100)
	 */
	setEffectSpeed : function(speed){
		if (!FlashImageViewer.setup_flag) {
			setTimeout("FlashImageViewer.setEffectSpeed("+speed+")", 50);
			return;
		}
		FlashImageViewer.trace('Setting speed to ' + speed);
		this.flashObj.setEffectSpeed(speed);
	},

	/**
	 * Set the strength of the transition effect (0-100)
	 * @param {int} strength (0-100)
	 */
	setEffectStength : function(strength){
		if (!FlashImageViewer.setup_flag) {
			setTimeout("FlashImageViewer.setEffectStength("+strength+")", 50);
			return;
		}
		FlashImageViewer.trace('Setting strength to ' + strength);
		this.flashObj.setEffectStength(strength);
	},

	/**
	 * Set the time between image transitions (in secs).
	 * @param {int} time seconds
	 */
	setSlideShowTime : function(time) {
		if (!FlashImageViewer.setup_flag) {
			setTimeout("FlashImageViewer.setSlideShowTime(" + time + ")", 50);
			return;
		}
		FlashImageViewer.trace('Setting slide show time to ' + time);
		this.flashObj.setSlideShowTime(time);
	},


	/**
	 * Returns the status of the preloading
	 * @param {int} total
	 * @param {int} loaded
	 * @param {int} percent
	 */
	onProgress : function(total, loaded, percent){
		FlashImageViewer.trace("Progress Loaded = " + loaded + " / " + total + " bytes (Prog = " + percent + "%)");
		//$('status_msg').innerHTML = percent + "%";
	},
	
	/**
	 * This is called by the flash object once the images have been pre-loaded
	 * @param {string} image_comment the comment associated with the image
	 */
	onComplete : function(image_comment){
		FlashImageViewer.trace('Loading complete');
		if (FlashImageViewer.m_on_complate_callback)
			FlashImageViewer.m_on_complate_callback(image_comment);
			
		$('caption').innerHTML = image_comment;
	},

	/**
	 * Get a reference to the flash object on the page
	 */
	setupFlash : function(){
		try {
			FlashImageViewer.no_images = FlashImageViewer.flashObj.getNumberImages();
			FlashImageViewer.setup_flag = true;
			FlashImageViewer.trace("Setup complete - no images = " + FlashImageViewer.no_images);
		}
		catch(err) {
			FlashImageViewer.trace('trying again - ' + err);
			setTimeout(FlashImageViewer.setupFlash, 100);
		}
	},
	
	trace : function(msg){
		//$('status_msg').innerHTML += msg + "<br>\n";
		//DebugFrame.trace(msg);
	}

}			
