/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * File:		SMFlashPlayer.js
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * Purpose:	js video player wrapper control
 *			
 * Copyright:	Synaptic Mash 2008 All Rights Reserved
 *
 * Classes:
 *		*
 *		
 * Requirements:
 *		prototype.js,
 *
 * Developer contact:	Jason Hillier <jason.hillier@synapticmash.com>
 * 
 * Notes:
 * ------
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
 var SMFlashPlayer={
	flashPlayers: [],
	
	LateInit: function(id, options, element){
		new flashPlayer(id, options, element);
	},
	
	ReInit: function(id)
	{
		for(var i=0; i<SMFlashPlayer.flashPlayers.length; i++)
		{
			if (SMFlashPlayer.flashPlayers[i].id == id)
			{
				return SMFlashPlayer.flashPlayers[i].Reinitialize();
			}
		}
		
		return false;
	},
	
	InitByControl: function(id)
	{
		//horrible hack of doom to make stuff work
		var controlTag = document.getElementById(id);
		
		SMLayoutManager.Log.Debug("[SMFlashPlayer.InitByControl]" +$(controlTag));
		
		var imgTag = null;
		
		for (var x = 0; x < controlTag.childNodes.length; x++)
		{
			if (controlTag.childNodes[x].tagName == "IMG")
			{
				imgTag = controlTag.childNodes[x];
				break;
			}
		}
		
		if (imgTag == null)
		{
			SMLayoutManager.Log.Error("Cannot find flash to autoplay");
		}
		
		//SMLayoutManager.Log.Debug(imgTag);
		
		//alert(imgTag.onclick);
		//SMLayoutManager.Log.Debug(imgTag.tagName);
		
		if (imgTag.tagName == "IMG")
		{
			imgTag.onclick();
		}
		
	},
	
	Register: function(flashPlayerObject)
	{
		SMFlashPlayer.flashPlayers.push(flashPlayerObject);
	}
 }
 
 var flashPlayer=Class.create({
	_MashScriptAPI: null,
 
	initialize:function(id, options, element){
		this.id = id;
		this.options = options;
		this.element = element;
		
		if(!document.loaded){
			document.observe('dom:loaded', this.setup.bind(this));
		}
		else{
			this.setup();
		}
		
		SMFlashPlayer.Register(this);
	},
	
	Reinitialize: function()
	{
		SMLayoutManager.Log.Trace("[flashPlayer.Reinitialize]");
		
		var flashNode = document.getElementById(this.id);
		
		while(flashNode.childNodes.length > 0)
			flashNode.removeChild(flashNode.childNodes[0]);
		
		MashScript.UnRegister(this._MashScriptAPI);
		
		this.setup();
	},
	
	GetTemplateControl: function(flash_control_id)
	{
		return SMLayoutManager.GetControl(flash_control_id + ".TemplateControl");
	},
	
	setup:function(){
		var id = this.id;
		var options = this.options;
		var element = this.element;
		
		if ($(id + "_EmbeddedContent") != null) return;
		
		//swap image tag with DIV
		var vpDIV = document.createElement('DIV');
		vpDIV.id = id + "_EmbeddedContent";
		if ($(id + "_EmbeddedImage") != null)
		{
			$(id + "_EmbeddedImage").parentNode.replaceChild(vpDIV, $(id + "_EmbeddedImage"));
		}
		else
		{
			document.getElementById(id).appendChild(vpDIV);
		}
		
		//$(id + "_EmbeddedContent").addClassName("SMFlashPlayer");
		
		//set 'base' parameter
		var base = options.PlayerOptions.src;
		base = base.substring(0, base.lastIndexOf('/'));
		//alert(base);
		options.PlayerOptions = Object.extend(options.PlayerOptions, {id: id + "_player", base: base});
		
		if (options.AutoSize)
		{
			var cWidth = options.PlayerOptions.width;
			var cHeight = options.PlayerOptions.height;
			
			options.PlayerOptions.width = SMLayoutManager.ScaleX([cWidth,cHeight]);
			options.PlayerOptions.height = SMLayoutManager.ScaleY([cWidth,cHeight]);
		}
		else
		{
			if (element != null)
			{
				//override dimensions
				options.PlayerOptions.width = $(element).getStyle('width').replace('px', '');
				options.PlayerOptions.height = $(element).getStyle('height').replace('px', '');
				
				if (options.PlayerOptions.width == 0)
				{
					options.PlayerOptions.width = $(element).width;
					options.PlayerOptions.height = $(element).height;
				}
			}
		}
		
		if (options.FixedDisplayArea)
		{
			$(id + "_EmbeddedContent").setStyle({width: options.PlayerOptions.width + 'px', height: options.PlayerOptions.height + 'px', overflow:'hidden'});
		}
		
		flashembed(id + "_EmbeddedContent", options.PlayerOptions, options.PlayerConfig);
		
		this._MashScriptAPI = new MashScriptAPI(id + "_player");
	}
 });
