/*

    @file mw_lib_bigpond_movies_entertainmentportal_video_player.js

    $Id: mw_lib_bigpond_movies_entertainmentportal_video_player.js,v 1.11 2007/07/18 08:45:06 james Exp $

    @author     James Mc Parlane

    PROJECT:    MetaWrap JavaScript Library

    COMPONENT:  -

    @date       11 September 2004


    GENERAL INFO:

        Massive Technologies
        PO Box 567
        Darlinghurst 2010
        NSW, Australia
        email:  james@massive.com.au
        tel:    (+61-2) 9331 8699
        fax:    (+61-2) 9331 8699
        mob:    (+61) 407-909-186


    LICENSE:

    Copyright (C) 2006  Massive Technologies, Pty Ltd.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

/*
 * $Log: mw_lib_bigpond_movies_entertainmentportal_video_player.js,v $
 * Revision 1.11  2007/07/18 08:45:06  james
 * Fixed issue with firefox height
 *
 * Revision 1.10  2007/07/18 07:09:57  james
 * This and previous update
 * Fixed issue in buffering
 * Fixed issue in firefox with size of video
 *
 * Revision 1.9  2007/07/18 06:59:16  james
 * *** empty log message ***
 *
 * Revision 1.3  2007/07/18 03:54:16  james
 * Fixed buffering issue
 * Player size now consistent in firefox.
 * Added parameters to choose() for setting include path and flash div
 *
 * Revision 1.2  2007/07/18 02:44:06  stevet
 * 1) FIX: missing path to mw_lib_bigpond_movies_entertainmentportal_video_player_wmp_implementation.js
 *
 * 1.1) update - js/mw_lib.js
 * create new variable - g_includes_dir = "./"
 *
 * 1.2) update - bigpond_movies_video_player.html
 * overide new variable - g_includes_dir = window.location.protocol          + '//'      + window.location.hostname + '/flash/movie_player/'
 *
 * 2) ADDITON: allow for any id to be used for 'flashcontent'
 *
 * 2.1) update - mw_lib_bigpond_movies_entertainmentportal_video_player.js
 * update class - MetaWrap.Bigpond.Movies.Entertainmentportal.Video.Player = MetaWrap.Class.extend
 * create new variable - m_player_id:"flashcontent",
 *
 * 2.2)
 * updated function - getFlashLocation: function()
 * update current variable - var l_div = MetaWrap.$(this.m_player_id);
 *
 * 2.3)
 * update - bigpond_movies_video_player.html
 * create new onload functions  - function onloadFunctions()
 * change current body load - onload  = onloadFunctions
 * overide m_player_id  - g_video_player.m_player_id = "flashcontent";
 *
 * 3) FIX: browser IE form wrapping video.
 *
 * 3.1) add new js include - swfformfix.js
 * 3.2) update  - bigpond_movies_video_player.html
 * include new js - <script language="JavaScript" type="text/javascript" src="js/swfformfix.js"></script>
 * create new code - SWFFormFix(this.m_flash_id);
 *
 * Revision 1.1  2007/07/05 02:29:28  james
 * Adjusted for Bigpond movie downloads
 *
 * Revision 1.8  2007/06/29 06:25:02  james
 * Added Buffering
 *
 * Revision 1.7  2007/06/19 03:37:03  leon
 * change height to 252
 *
 * Revision 1.6  2007/06/18 06:44:11  leon
 * change movie player size
 *
 * Revision 1.5  2007/06/15 04:49:20  james
 * removed double up of some values
 *
 * Revision 1.4  2007/05/31 06:48:32  leon
 * removed this
 * alert(p_state); //when completed
 *
 * Revision 1.3  2007/05/29 01:42:45  james
 * testing with prototype.js
 *
 * Revision 1.2  2007/05/28 02:17:35  james
 * Latest version of video player
 *
 * Revision 1.1  2007/05/25 07:27:40  james
 * Entertainment Video Portal
 *
 * Revision 1.13  2007/04/27 04:20:25  james
 */

/*! \page mw_javascript_lib_bigpond_movies_entertainmentportal_video_player MetaWrap - JavaScript -  Bigpond - Movies - Entertainmentportal - Base
 *
 * \subsection mw_javascript_lib_bigpond_movies_entertainmentportal_video_player Overview
 *
 *  This is the base class for a platform specific implentation for the Bigpond Movies
 *
 *
 */

//debug("$Id: mw_lib_bigpond_movies_entertainmentportal_video_player.js,v 1.11 2007/07/18 08:45:06 james Exp $");

/*! \defgroup mw_javascript_lib_bigpond_movies_entertainmentportal_video_player  MetaWrap - JavaScript - Bigpond - Movies - Entertainmentportal - Base
 *@{
 */

/*!
	@brief 	Global platform variable.
*/
var g_platform = null;

/*!
	@class	MetaWrap.Bigpond.Movies.Entertainmentportal.Video.Player
	@brief 	Base class
*/
MetaWrap.Bigpond.Movies.Entertainmentportal.Video.Player = MetaWrap.Class.extend
    (
        {
			// Location of flash on screen
			m_flash_x : 0,
			m_flash_y : 0,
			m_flash_w : 0,
			m_flash_h : 0,

			m_video_x : 4,
			m_video_y : 4,
			m_video_w : 429,
			m_video_h : 252,

			m_include_path : "./",
			m_flash_div : "flashcontent",
			m_flash_id : "video_player",
			m_position_timer: null,
			m_buffering_timer: null,
			m_url: "",
			m_playing: false,

			//m_player_id:"flashcontent",

			// Constructor
            constructor: function()
            {
				debug("base:constructor");
            },

			// Include any required content for this platform implementation
            include: function()
            {
				debug("base:include");
			},


			embed: function()
			{
			},

			// Start the platform
            start: function()
            {
				//debug("base:start");

				this.getFlashLocation();
			},

			// Stop the platform
            stop: function()
            {
				debug("base:stop");
			},

			// Return the type of the platform
			type: function()
			{
				return "base";
			},

			//
			//
			// Video - Movies
			//
			//

			// Play a video
			launchVideo: function(p_url)
            {
				debug("base:launchVideo " + p_url);
			},

			// Remove Video PIP, Stop playback.
			removeVideo: function()
            {
				debug("base:removeVideo");
			},

			// Play this video
			playVideo: function()
            {
				if (!this.m_playing)
				{
					this.m_playing = true;
					this.launchVideo(this.m_url);
				}

				debug("base:playVideo");
			},

			// Play this video
			setVideo: function(p_url)
            {
				//debug("base:setVideo " + p_url);
				this.m_url = p_url;
			},


			// Pause this video
			pauseVideo: function()
            {
				debug("base:pauseVideo");

				this.m_paused = true;
			},

			// Rewind this video
			restartVideo: function()
            {
				debug("base:restartVideo");
			},

			closeVideo: function()
			{
				//debug("vase:closeVideo");

				// kill timer
				clearTimeout(this.m_position_timer);
				this.m_position_timer = null;
			},


			// mute the video
			muteVideoVolume : function(p_muted)
			{
				debug("base:muteVideoVolume " + p_muted);
			},

			// mute the video
			fullscreenVideo : function(p_fullscreen)
			{
				debug("base:fullscreenVideo " + p_fullscreen);
			},

			// get the duration (seconds)
			getDuration : function()
			{
				return -1;
			},

			// get the position (seconds)
			getPosition : function()
			{
				return -1;
			},

			// Get the location of the flash
			getFlashLocation: function()
			{
				var l_swf = getFlashObject(this.m_flash_id);
				var l_div = MetaWrap.$(this.m_flash_div);

				// What we want
				var l_left = 0;
				var l_top = 0;
				var l_width = l_div.offsetWidth;
				var l_height = l_div.offsetHeight;

				// Width and height was easy, left and top requres some work
				if (l_div.offsetParent)
				{
					l_left = l_div.offsetLeft
					l_top = l_div.offsetTop
					while (l_div = l_div.offsetParent)
					{
						l_left += l_div.offsetLeft
						l_top += l_div.offsetTop
					}
				}
				//alert(l_left + " " + l_top);

				this.m_flash_x = l_left;
				this.m_flash_y = l_top;

				this.m_flash_w = l_width;
				this.m_flash_h = l_height;

			},


			setBuffering: function(p_percentage)
			{
				//alert(p_percentage);

				getFlashObject(this.m_flash_id).FLBufferUpdate(p_percentage);
			},

			videoBuffering: function(p_starting)
			{
				debug("videoBuffering " + p_starting);
			},

			videoMessage: function(p_type,p_param)
			{
				//debug("videoMessage " + p_type + " " + p_param);
			},

			videoMediaError: function(p_media)
			{
				debug("videoMediaError");
			},

			videoError: function()
			{
				debug("videoError");
			},

			startBufferingTimer: function()
			{
				debug("startBufferingTimer");

				// start timer
				this.m_buffering_timer = setTimeout("g_video_player.timerBuffering()",100);

			},

			isBuffering: function()
			{
				return false;
			},

			getBufferingPercentage: function()
			{
				return 0;
			},

			timerBuffering: function()
			{
				this.stopBufferingTimer();

				debug("timerBuffering");

				if (this.getBufferingPercentage() != 100)
				{
					this.videoBuffering(false);

					this.startBufferingTimer();
				}
				else
				{
					//alert("not buffering");
				}
			},

			stopBufferingTimer: function()
			{
				debug("endBufferingTimer");

				if (this.m_buffering_timer !=  null)
				{
					clearTimeout(this.m_buffering_timer);
					this.m_buffering_timer = null;
				}
			},

			videoState: function(p_state)
			{
				debug(this.type() + " videoState " + p_state);

				if ((p_state == "playing" ) && (this.m_position_timer == null))
				{
					this.videoReady();
				}
				else
				if (p_state == "completed")
				{
					getFlashObject(this.m_flash_id).FLFinished();

				}
				else
				if (p_state == "buffering")
				{
					// show buffering status
					//this.videoBuffering(false);

					// start the timer
					this.startBufferingTimer();
				}

			},

			// Rewind this video
			videoReady: function()
            {
				// start timer
				this.m_position_timer = setTimeout("g_video_player.positionTimer()",1000);

				//debug("base:videoReady");

				// Tell flash we are ready
				getFlashObject(this.m_flash_id).FLReady(this.getDuration());
			},

			positionTimer: function()
			{
				// kill timer
				clearTimeout(this.m_position_timer);
				this.m_position_timer = null;

				// If we are not buffering
				if (!this.isBuffering())
				{
					var l_position = this.getPosition();

					//alert("update position");

					getFlashObject(this.m_flash_id).FLPositionUpdate(l_position);
				}

				// re-start timer
				this.m_position_timer = setTimeout("g_video_player.positionTimer()",1000);

			}
	    }
	    ,
	    {

			// Return true if this player has been detected
			sniff: function()
			{
				debug("base:sniff");
				return false;
			},


			// Choose a player
			choose: function(p_include_path,p_flash_div,p_flash_id)
			{

/*
				// loop through each plugin and have a look
				for (var i = 0; i < navigator.plugins.length; i++)
				{
					document.write(navigator.plugins[i].name + "<br/>");
				}
*/

				// Sniff for available players
				for(var l_player_class_name in MetaWrap.Bigpond.Movies.Entertainmentportal.Video.Player)
				{
					//debug("try " + l_player_class_name);

					// Get a reference to the player object
					var l_player_class = MetaWrap.Bigpond.Movies.Entertainmentportal.Video.Player[l_player_class_name];

					try
					{

						// If the class says its happy
						if (l_player_class.sniff())
						{
							debug("choose " + l_player_class_name);

							// instantiate it as our player
							var l_player = new l_player_class();

							if (p_include_path != null) l_player.m_include_path = p_include_path;
							if (p_flash_div != null) l_player.m_flash_div = p_flash_div;
							if (p_flash_id != null) l_player.m_flash_id = p_flash_id;

							// include the player
							l_player.include();

							return l_player;
						}

					}
					catch(l_e)
					{
					}

				}

				debug("choose: returning harmless stub");
				// Return a harmless version of the base class that won't do anything - but it won't break the oage
				return new MetaWrap.Bigpond.Movies.Entertainmentportal.Video.Player();
			}


		}

    );

