/**
 * This script contains embed functions for common plugins. This scripts are complety free to use for any purpose.
 */

function writeVideo( p )
{
    
    var vid = "video" + jQuery( 'video' ).length;
   
    var out = '<div class="video-js-box">';
    out += '<video id="' + vid + '" class="video-js" width="' + p.width + '" height="' + p.height + '" controls="controls" preload="auto" poster="' + p.video.attrs.poster + '">';
    
    var mpg, flv;
    
    var sources; 
    
    if( p.video.sources.length != undefined )
    {
        sources = p.video.sources;
    } else {
        //not an array
        sources = new Array();
        for( s in p.video.sources )
        {
            sources.push( p.video.sources[ s ] );
        }    
    }
    
    
    for( var i = 0; i < sources.length; i++ )
    {
        var src = sources[ i ].src;
        
        if( src.match( /\.mp4/ ) )
        {
            mpg = src; 
        } else if( src.match( /\.flv/ ) ) {
            flv = src;
            continue;
        }
            
        
        var mime = 'video/' + src.substring( src.lastIndexOf( '.' ) + 1 );
        out += '<source src="' + src + '" type="' + mime + '" />';
    }
    
    var stream = ( flv ) ? flv : mpg;
    
    out += '<object ' + ( ( navigator.appName == 'Microsoft Internet Explorer' ) ? ' classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"' : '' ) + ' id="flash_fallback_' + vid + '" class="vjs-flash-fallback" width="' + p.width + '" height="' + p.height + '" type="application/x-shockwave-flash"';
    out += 'data="/sites/all/themes/andco/includes/swf/video.swf">';
    out += '<param name="movie" value="/sites/all/themes/andco/includes/swf/video.swf" />';
    out += '<param name="allowfullscreen" value="true" />';
    out += '<param name="wmode" value="transparent" />'
    out += '<param name="flashvars" value="stream=' + stream + '&poster=' + p.video.attrs.poster + '&width=' + p.width + '&height=' + p.height + '" />';
    out += '<img src="' + p.video.attrs.poster + '" width="' + p.width + '" height="' + p.height + '" alt="Poster Image" title="No video playback capabilities." />';
    out += '</object>';
    
    out += '</video></div>';
    
    //document.write( out );
    jQuery( "#" + p.wid ).append( out );
    VideoJS.setupAllWhenReady();
    
}

function writeFlash(p) {
	writeEmbed(
		'D27CDB6E-AE6D-11cf-96B8-444553540000',
		'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0',
		'application/x-shockwave-flash',
		p
	);
}

function writeShockWave(p) {
	writeEmbed(
	'166B1BCA-3F9C-11CF-8075-444553540000',
	'http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=8,5,1,0',
	'application/x-director',
		p
	);
}

function writeQuickTime(p) {
	writeEmbed(
		'02BF25D5-8C17-4B23-BC80-D3488ABDDC6B',
		'http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0',
		'video/quicktime',
		p
	);
}

function writeRealMedia(p) {
	writeEmbed(
		'CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA',
		'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0',
		'audio/x-pn-realaudio-plugin',
		p
	);
}

function writeWindowsMedia(p) {
	p.url = p.src;
	writeEmbed(
		'6BF52A52-394A-11D3-B153-00C04F79FAA6',
		'http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701',
		'application/x-mplayer2',
		p
	);
}

function writeEmbed(cls, cb, mt, p) {
	var h = '', n;

	h += '<object classid="clsid:' + cls + '" codebase="' + cb + '"';
	h += typeof(p.id) != "undefined" ? 'id="' + p.id + '"' : '';
	h += typeof(p.name) != "undefined" ? 'name="' + p.name + '"' : '';
	h += typeof(p.width) != "undefined" ? 'width="' + p.width + '"' : '';
	h += typeof(p.height) != "undefined" ? 'height="' + p.height + '"' : '';
	h += typeof(p.align) != "undefined" ? 'align="' + p.align + '"' : '';
	h += '>';

	for (n in p)
		h += '<param name="' + n + '" value="' + p[n] + '">';

	h += '<embed type="' + mt + '"';

	for (n in p)
		h += n + '="' + p[n] + '" ';

	h += '></embed></object>';

	if( p.wid )
	{
	    jQuery( "#" + p.wid ).append( h );
	} else {
	    document.write(h);
	}
}

