/*
 DirTag Class v0.3
 Created by: Valentin Schmidt
	
 Clone of FlashTag Class by Mike Chambers and Christian Cantrell
	
 Generates a browser-specific Director Shockwave tag. Create a new instance, set whatever
 properties you need, then call either toString() to get the tag as a string, or call write() 
 to write the tag out.
 
 v0.4 by Emmanuel Beuque
 Adds parameters and methods to set swRemote and stretch attributes of the Shockwave tag
 
 v0.5 by Emmanuel Beuque
 Takes care of dirVars passed as strings by using ' to define sw param and 
 by replacing the single quote char with '"&numToChar(39)&"' in the OBJECT or EMBED tag
 using value in Director will restore the single quote char.
*/


/**
 * Creates a new instance of the DirTag.
 * src: The path to the DCR file.
 * width: The width of your movie.
 * height: the height of your movie.
 */
function DirTag(src, width, height, forceReloadFlag)
{
    this.src       = src;
    if (forceReloadFlag) this.src += '?'+Math.random();
    this.width     = width;
    this.height    = height;
    this.version   = '10,0,0,0';
    this.id        = null;
    this.bgcolor   = 'ffffff';
    this.dirVars   = null;
	this.swRemoteParams = null;
	this.swRemote = null;
	this.stretchStyle = 'none';
	this.stretchAlign = null;
}

/**
 * Sets the Director Shockwave version used in the Director Shockwave tag.
 */
DirTag.prototype.setVersion = function(v)
{
    this.version = v;
}

/**
 * Sets the ID used in the Director Shockwave tag.
 */
DirTag.prototype.setId = function(id)
{
    this.id = id;
}

/**
 * Sets the background color used in the Director Shockwave tag.
 */
DirTag.prototype.setBgcolor = function(bgc)
{
    this.bgcolor = bgc;
}

/**
 * Sets any variables to be passed into the Director Shockwave content, as externalParamValue("sw1")..("sw9").
   maximum 9 args supported
 */
DirTag.prototype.setDirvars = _setDirvars;
function _setDirvars(){
    this.dirVars = _setDirvars.arguments;
}

/**
 * Sets swRemote variables with an associative array (string-indexed array).
   arg is an array with these props: swSaveEnabled swVolume swRestart swPausePlay swFastForward swContextMenu
 */
DirTag.prototype.setSwRemote = function(srArray)
{
    this.swRemote = srArray;
}

/**
 * Sets the stretch style used to render the Director stage in the HTML frame.
   supported values are: none stage meet fill
 */
DirTag.prototype.setStretchStyle = function(strs)
{
    this.stretchStyle = strs;
}

/**
 * Sets swStretchHAlign and swStretchVAlign params of the Director tag.
   2 args (H and V) with values respectively
   H = Left Center Right
   V = Top Center Bottom
 */
DirTag.prototype.setStretchPosition = _setStretchPos;
function _setStretchPos(){
    this.stretchAlign = _setStretchPos.arguments;
}

/**
 * Get the Director Shockwave tag as a string. 
 */
DirTag.prototype.toString = function()
{
    var ieWin = (/Win/.test(navigator.userAgent) && navigator.appName.indexOf ("Microsoft") != -1) ? 1 : 0;
    var DirTag = new String();
    if (ieWin)
    {
        DirTag += '<object classid="clsid:166B1BCA-3F9C-11CF-8075-444553540000" ';
        if (this.id != null)
        {
            DirTag += 'id="'+this.id+'" ';
        }
        DirTag += 'codebase="http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version='+this.version+'" ';
        DirTag += 'width="'+this.width+'" ';
        DirTag += 'height="'+this.height+'">';
        DirTag += '<param name="src" value="'+this.src+'"/>';
        if (this.swRemote != null){
			DirTag += '<param name="swRemote" value="';
        	for (var p in this.swRemote)
        		DirTag += p+'=\''+this.swRemote[p]+'\' ';
			DirTag += '">';
        }
        DirTag += '<param name="swStretchStyle" value="'+this.stretchStyle+'"/>';
        if (this.stretchAlign != null){
        	var len = Math.min(this.stretchAlign.length, 2);
			var propInner = ['H','V']
        	for (i=0;i<len;i++)
        		DirTag += '<param name="swStretch'+propInner[i]+'Align" value="'+this.stretchAlign[i]+'"/>';
        }
        DirTag += '<param name="bgcolor" value="#'+this.bgcolor+'"/>';
        if (this.dirVars != null){
        	var len = Math.min(this.dirVars.length, 8);
        	for (i=0;i<len;i++) {
				var dirVar = this.dirVars[i];
				if (typeof(dirVar) == "string") {
					dirVar = dirVar.replace(/'/g,'\"&numToChar(39)&\"');
					DirTag += '<param name="sw'+(i+1)+'" value=\''+dirVar+'\'/>';
				}
            	else DirTag += '<param name="sw'+(i+1)+'" value="'+dirVar+'"/>';
			}
        }
        DirTag += '</object>';
    }
    else
    {
        DirTag += '<embed src="'+this.src+'" ';
        DirTag += 'bgcolor="#'+this.bgcolor+'" ';
        DirTag += 'width="'+this.width+'" ';
        DirTag += 'height="'+this.height+'" ';
        DirTag += 'type="application/x-director" ';
        if (this.swRemote != null){
			DirTag += 'swRemote="';
        	for (var p in this.swRemote)
        		DirTag += p+'=\''+this.swRemote[p]+'\' ';
			DirTag += '" ';
        }
        DirTag += 'swStretchStyle='+this.stretchStyle+' ';
        if (this.stretchAlign != null){
        	var len = Math.min(this.stretchAlign.length, 2);
			var propInner = ['H','V']
        	for (i=0;i<len;i++)
        		DirTag += 'swStretch'+propInner[i]+'Align="'+this.stretchAlign[i]+'" ';
        }
        if (this.dirVars != null){
        	var len = Math.min(this.dirVars.length, 8);
        	for (i=0;i<len;i++) {
				var dirVar = this.dirVars[i];
				if (typeof(dirVar) == "string") {
					dirVar = dirVar.replace(/'/g,'\"&numToChar(39)&\"');
					DirTag += 'sw'+(i+1)+'=\''+dirVar+'\' ';
				}
            	else DirTag += 'sw'+(i+1)+'="'+dirVar+'" ';
			}
        }
        if (this.id != null)
        {
            DirTag += 'name="'+this.id+'" ';
        }
        DirTag += 'pluginspage="http://www.adobe.com/fr/shockwave/download/">';
        DirTag += '</embed>';
    }
    return DirTag;
}

/**
 * Write the Director Shockwave tag out. Pass in a reference to the document to write to. 
 */
DirTag.prototype.write = function(doc)
{
  doc.write(this.toString());
}

