/***
*	MediaPlayer 0.0.1
*
*	An Interface Class supporting the generalized functions of the specific MediaPlayer object.
*	Should be called by the MediaPlayerFactory class.
*
*	(c) 2006 Tachometry Corporation.  
*	contact: support@tachometry.com
*
*	Licensed under the Apache License (v. 2.0)
*	http://www.apache.org/licenses/LICENSE-2.0
*/


/**
*	Constructor
*/
function MediaPlayer(src, width, height)
{
	if ( arguments.length > 0 ) {
        this.init(src, width, height);
	}
};

/**
*	Initializor
*/
MediaPlayer.prototype.init = function(src, width, height)
{
	this.src = src || ""; // this should be overridden by the impl classes to a know location.
	this.width = width || 300;
	this.height = height || 200;
	this.requiredVersion = "";
	this.objectId = "MediaPlayer";
	this.applicationType = "";
	this.playerClassId = "";
	this.codebase = "";
	this.pluginName = "";
	this.params = new Object();
	this.variables = new Object();
	this.player_states_map = Array("stopped","contacting","buffering","playing","paused","seeking");
}

/**
*	Accessors
*/
MediaPlayer.prototype.getSrc = function()
{
    return this.src;
};

MediaPlayer.prototype.setSrc = function(src)
{
    this.src = src;
	this.setParam("url", src);
};

MediaPlayer.prototype.getWidth = function()
{
    return this.width;
};

MediaPlayer.prototype.setWidth = function(w)
{
    this.width = w;
};

MediaPlayer.prototype.getHeight = function()
{
    return this.height;
};

MediaPlayer.prototype.setHeight = function(h)
{
    this.height = h;
};

MediaPlayer.prototype.getRequiredVersion = function()
{
    return this.requiredVersion;
};

MediaPlayer.prototype.setRequiredVersion = function(v)
{
    this.requiredVersion = v;
};

MediaPlayer.prototype.getObjectId = function()
{
    return this.objectId;
};

MediaPlayer.prototype.setObjectId = function(id)
{
    this.objectId = id;
};

MediaPlayer.prototype.getPlayerElement = function()
{
	if (document.all) {
		return document.all[ this.getObjectId() ];
	} else {
		return document.getElementById( this.getObjectId() );
	}
}

MediaPlayer.prototype.getApplicationType = function()
{
    return this.applicationType;
};

MediaPlayer.prototype.setApplicationType = function(type)
{
    this.applicationType = type;
	this.setParam("type", type);
};

MediaPlayer.prototype.getPlayerClassId = function()
{
    return this.playerClassId;
};

MediaPlayer.prototype.setPlayerClassId = function(id)
{
    this.playerClassId = id;
};

MediaPlayer.prototype.getCodebase = function()
{
    return this.codebase;
};

MediaPlayer.prototype.setCodebase = function(id)
{
    this.codebase = id;
};

MediaPlayer.prototype.getPluginName = function()
{
    return this.pluginName;
};

MediaPlayer.prototype.setPluginName = function(name)
{
    this.pluginName = name;
};

MediaPlayer.prototype.getParam = function(name)
{
    return this.params[name];
};

MediaPlayer.prototype.getParams = function()
{
    return this.params;
};

MediaPlayer.prototype.setParam = function(name, value)
{
	this.params[name] = value;
};

MediaPlayer.prototype.getVariable = function(name)
{
    return this.variables[name];
};

MediaPlayer.prototype.getVariables = function()
{
    return this.variables;
};

MediaPlayer.prototype.setVariable = function(name, value)
{
    this.variables[name] = value;
};

MediaPlayer.prototype.getVariablePairs = function()
{
    var variablePairs = new Array();
    for (var name in this.getVariables()) {
        variablePairs.push(name + "=" + escape(this.getVariable(name)));
    }
    if (variablePairs.length > 0) {
        return variablePairs.join("&");
    }
    else {
        return null;
    }
};

MediaPlayer.prototype.getParamTags = function()
{
    var paramTags = "";
    for (var param in this.getParams()) {
        paramTags += '<param name="' + param + '" value="' + this.getParam(param) + '" />';
    }
    if (paramTags == "") {
        paramTags = null;
    }
    return paramTags;
};

MediaPlayer.prototype.getStateMap = function( key )
{
	return this.player_states_map[key];
}

MediaPlayer.resizeTo = function(id, w, h)
{
    id = id || this.getObjectId();
	var element = this.getPlayerElement(id);
    element.width = w;
    element.height = h;
};

MediaPlayer.prototype.getHTML = function()
{
    var html = "";
    if (window.ActiveXObject && navigator.userAgent.indexOf('Mac') == -1) { // PC IE
        html += '<object classid="' + this.getPlayerClassId() + '" width="' + this.getWidth() + '" height="' + this.getHeight() + '" id="' + this.getObjectId() + '"';
		if (this.getCodebase != "") { html += ' codebase="' + this.getCodebase() + '"'; }
		if (this.getApplicationType != "") { html += ' type="' + this.getApplicationType() + '"'; }
		html += '>';
        html += '<param name="src" value="' + this.getSrc() + '" />';;
        if (this.getParamTags() != null) {
            html += this.getParamTags();
        }
        if (this.getVariablePairs() != null) {
            html += '<param name="realVars" value="' + this.getVariablePairs() + '" />';
        }
        html += '</object>';
    }
    else { // Everyone else
        html += '<embed src="' + this.getSrc() + '" width="' + this.getWidth() + '" height="' + this.getHeight() + '" id="' + this.getObjectId() + '" name="' + this.getObjectId() +'"';
        for (var param in this.getParams()) {
            html += ' ' + param + '="' + this.getParam(param) + '"';
        }
        if (this.getVariablePairs() != null) {
            html += ' realVars="' + this.getVariablePairs() + '"';
        }
        html += '></embed>';
    }
    return html;
};

MediaPlayer.prototype.render = function(elementId)
{
    if (elementId != "") {
		this.setObjectId = elementId;
	}
	if(this.hasVersion(this.getRequiredVersion())) {
        if (elementId) {
            document.getElementById(elementId).innerHTML = this.getHTML();
        }
        else {
            document.write(this.getHTML());
        }
    }
	else {
		throw new Error("This player requires version "+ this.getRequiredVersion() +".  Please upgrade your player.");
	}
};

MediaPlayer.prototype.hasVersion = function(requiredVersion)
{
    // TODO: Write the logic to test valid version.
	return true;  
};

/* 	Returns a millisecond value for a string in the format hh:mm:ss 
*	If no colons are recognized, the value will be treated as seconds.
*	If only one colon, will only parse minutes and seconds
* 	Two colons will parse hours, minutes and seconds	
*/
MediaPlayer.prototype.getMillisFromHMS = function( hms_string )
{
	var time_array = hms_string.split(":");
	var secs = 0;
	var mins = 0;
	var hrs = 0;
	var millis = 0;
	switch(	time_array.length )
	{
		case 3 :
			hrs = parseInt(time_array[time_array.length - 3]);
		case 2 :
			mins = parseInt(time_array[time_array.length - 2]);
		case 1 :
			secs = parseInt(time_array[time_array.length - 1]);
			break;
		default :
			throw new Error("Could not parse H:M:S value from " + hms_string );
	}
	millis = parseInt(( (hrs*60*60) + (mins*60) + secs )*1000); 
	if(millis%1 == 0) { // this is an integer
		return  millis;
	} else {
		throw new Error("Could not return a millisecond value for " + millis);
	}	
};

/*
MediaPlayer.prototype.toString = function(elementId)
{
	var tostring = "<blockquote><tt>";
	tostring += (this.getHTML().replace(/</g, "&#60;")).replace(/>/g, "&#62;");
	tostring += "</tt></blockquote>";
	if (elementId) {
		document.getElementById(elementId).innerHTML = tostring;
	}
	else {
		document.write(tostring);
	}
	
};
*/





