var videoComplete = function(){};
var videoReady = function(){};
var videoCuePoint = function(){};

function loadVideo(container,vid,height,width)
{
	container.innerHTML = '<object width="'+width+'" height="'+height+'" '+
				'classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" '+
				'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0">'+
				'<param name="movie" value="/flash/videos/'+vid+'"></param>'+
				'<param name="allowFullScreen" value="false"></param>'+
				'<param name="allowscriptaccess" value="always"></param>'+
				'<embed src="/flash/videos/'+vid+'" '+
				'width="'+width+'" height="'+height+'" allowscriptaccess="always" allowfullscreen="false" '+
				'type="application/x-shockwave-flash" '+
				'pluginspage="http://www.macromedia.com/go/getflashplayer"></embed></object>';
}

function loadCoverVideo(atag,pageid)
{
	var video = document.createElement('div');
	video.style.width='635px';
	video.style.height='1px';
	video.style.overflow='hidden';
	loadVideo(video,pageid+'cover.swf',500,635);
	
	var getFlash = document.createElement('a');
	getFlash.href = '#';
	getFlash.style.width='635px';
	getFlash.style.height='500px';
	getFlash.style.align='center';
	getFlash.style.display='block';
	getFlash.style.color='white';
	getFlash.style.backgroundColor='#003';
	getFlash.innerHTML = '<img style="margin-top: 100px;" src="/img/cover-loader.gif" border="0" height="100" width="100">';
	
	setTimeout(
		function()
		{
			getFlash.href = 'http://get.adobe.com/flashplayer/';
			getFlash.target = '_blank';
			getFlash.innerHTML = '<img style="margin-top: 200px;" border="0" src="http://www.adobe.com/images/shared/download_buttons/get_adobe_flash_player.png" height="39" width="158" alt="Get Adobe Flash Player"><br/>This video requireds Adobe Flash Player';
		},7500);
	
	var container = atag.parentNode;
	container.removeChild(atag);
	container.appendChild(getFlash);
	container.appendChild(video);
	
	videoReady = function()
	{
		getFlash.parentNode.removeChild(getFlash);
		video.style.height='500px';
		trackEvent('video', 'play', pageid);
	}

	videoComplete = function()
	{
		container.removeChild(video);
		container.appendChild(atag);
		trackEvent('video', 'complete', pageid);
	}
	
	var lastCuePoint = 0;
	videoCuePoint = function(cuepoint)
	{
		var incrament = 20;
		if(cuepoint-lastCuePoint>=incrament)
		{
			lastCuePoint += incrament;
			trackEvent('video', 'progress-'+lastCuePoint, pageid);
		}
	}
}


function ShowVideo(code,h,w,a)
{
	var autoplay = 0;
	var height = 120;
	var width = 160;
	var strHTML = '';

	if (a) autoplay = 1;
	if (h) height = h;
	if (w) width = w;
	strHTML = strHTML + '<div id="'+code+'videodiv">';
	strHTML = strHTML + GetVideoHTML(code,height,width,autoplay);
	strHTML = strHTML + '</div>';
	strHTML = strHTML + '<a href="javascript:UpdateHTMLByID(\''+code+'videodiv\',GetVideoHTML(\''+code+'\','+height+','+width+',1));">PLAY VIDEO</a>';
	return strHTML;
}

function GetVideoHTML(code,height,width,autoplay)
{
	//alert('QuickTime: '+detectQuickTime());
	//alert('Windows Media: '+detectWindowsMedia());
	//alert('Real: '+detectReal());
	var strHTML = '';
	var bitQuickTime = detectQuickTime();
	var bitMediaPlayer = detectWindowsMedia();
	var url = '/video/products/'+code;
	
	if(!autoplay)
	{
		strHTML = '<img src="/video/products/'+code+'.jpg" width="'+width+'" height="'+height+'">';
	}
	else if(bitQuickTime || bitMediaPlayer)
	{
		if(navigator.userAgent.toLowerCase().indexOf('windows')>=0)
			strHTML = (bitMediaPlayer)?GetWindowsMediaHTML(url+'.wmv',height,width,autoplay,0):GetQuickTimeHTML(url+'.mov',height,width,autoplay,0);
		else
			strHTML = (bitQuickTime)?GetQuickTimeHTML(url+'.mov',height,width,autoplay,0):GetWindowsMediaHTML(url+'.wmv',height,width,autoplay,0);
	}
	else
	{
		strHTML = GetUnknownMediaPlayerHTML();
	}
	return strHTML;
}

function GetVideoWithControllerHTML(url,height,width)
{
	var strHTML = '';
	var bitQuickTime = detectQuickTime();
	var bitMediaPlayer = detectWindowsMedia();
	
	if(bitQuickTime || bitMediaPlayer)
	{
		if(navigator.userAgent.toLowerCase().indexOf('windows')>=0)
			strHTML = (bitMediaPlayer)?GetWindowsMediaHTML(url+'.wmv',height,width,1,1):GetQuickTimeHTML(url+'.mov',height,width,1,1);
		else
			strHTML = (bitQuickTime)?GetQuickTimeHTML(url+'.mov',height,width,1,1):GetWindowsMediaHTML(url+'.wmv',height,width,1,1);
	}
	else
	{
		strHTML = GetUnknownMediaPlayerHTML();
	}
	return strHTML;
}

function GetQuickTimeHTML(url,height,width,autoplay,controller)
{
	var strHTML = '';
	var a = autoplay?'true':'false';
	var c = controller?'true':'false';
	var h = controller?height+20:height;;
	var w = width;
	strHTML = strHTML + '<OBJECT classid="CLSID:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" ';
	strHTML = strHTML + 'codebase="http://www.apple.com/qtactivex/qtplugin.cab" ';
	strHTML = strHTML + 'width='+w+' height='+h+'>';
	strHTML = strHTML + '<PARAM name="SRC" value="'+url+'">';
	strHTML = strHTML + '<PARAM name="AUTOPLAY" value="'+a+'">';
	strHTML = strHTML + '<PARAM name="CONTROLLER" value="'+c+'">';
	strHTML = strHTML + '<EMBED src="'+url+'" width="'+w+'" height="'+h+'" autoplay="true" controller="'+c+'" pluginspage="http://www.apple.com/quicktime/download/">';
	strHTML = strHTML + '</EMBED>';
	strHTML = strHTML + '</OBJECT>';
	return strHTML;
}

function GetWindowsMediaHTML(url,height,width,autoplay,controller)
{
	var strHTML = '';
	var a = autoplay?'true':'false';
	var c = controller?'true':'false';
	var h = controller?height+45:height;;
	var w = width;
	strHTML = strHTML + '<OBJECT classid="clsid:22D6f312-B0F6-11D0-94AB-0080C74C7E95" ';
	strHTML = strHTML + 'codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,7,1112" ';
	strHTML = strHTML + 'type="application/x-oleobject" ';
	strHTML = strHTML + 'width='+w+' height='+h+' ';
	strHTML = strHTML + 'standby="Loading Windows Media Player components...">';
	strHTML = strHTML + '<PARAM name="filename" value="'+url+'">';
	strHTML = strHTML + '<PARAM name="AUTOSTART" value="'+a+'">';
	strHTML = strHTML + '<PARAM name="SHOWCONTROLS" value="'+c+'">';
	strHTML = strHTML + '<PARAM name="CURRENTPOSITION" value="1">';
	strHTML = strHTML + '<EMBED src="'+url+'" width="'+w+'" width="'+h+'" type="application/x-mplayer2" pluginspage="http://www.microsoft.com/windows/windowsmedia/player/download/">';
	strHTML = strHTML + '</EMBED>';
	strHTML = strHTML + '</OBJECT>';
	return strHTML;
}

function GetUnknownMediaPlayerHTML()
{
	var strHTML = '';
	strHTML = strHTML + '<center>';
	strHTML = strHTML + 'A media player is required to view this video.<BR>';
	strHTML = strHTML + '<A href="http://www.apple.com/quicktime/download/" target=_blank>Download QuickTime Player</A><BR>';
	strHTML = strHTML + '<A href="http://www.microsoft.com/windows/windowsmedia/player/download/" target=_blank>Download Windows Media Player</A><BR>';
	strHTML = strHTML + '</center>';
	return strHTML;
}

function detectQuickTime() {
    pluginFound = detectPlugin('QuickTime');
    // if not found, try to detect with VisualBasic
    if(!pluginFound && detectableWithVB) {
        pluginFound = detectQuickTimeActiveXControl();
    }
    return pluginFound;
}

function detectReal() {
 	pluginFound = detectPlugin('RealPlayer');
   // if not found, try to detect with VisualBasic
    if(!pluginFound && detectableWithVB) {
        pluginFound = (detectActiveXControl('rmocx.RealPlayer G2 Control') ||
                       detectActiveXControl('RealPlayer.RealPlayer(tm) ActiveX Control (32-bit)') ||
                       detectActiveXControl('RealVideo.RealVideo(tm) ActiveX Control (32-bit)'));
    }   
    return pluginFound;
}

function detectWindowsMedia() {
    pluginFound = detectPlugin('Windows Media');
    // if not found, try to detect with VisualBasic
    if(!pluginFound && detectableWithVB) {
        pluginFound = detectActiveXControl('MediaPlayer.MediaPlayer.1');
    }
    return pluginFound;
}


function detectPlugin()
{
    // allow for multiple checks in a single pass
    var daPlugins = detectPlugin.arguments;
    // consider pluginFound to be false until proven true
    var pluginFound = false;
    // if plugins array is there and not fake
    if (navigator.plugins && navigator.plugins.length > 0) {
        var pluginsArrayLength = navigator.plugins.length;
        // for each plugin...
        for (pluginsArrayCounter=0; pluginsArrayCounter < pluginsArrayLength; pluginsArrayCounter++ ) {
            // loop through all desired names and check each against the current plugin name
            var numFound = 0;
            for(namesCounter=0; namesCounter < daPlugins.length; namesCounter++) {
                // if desired plugin name is found in either plugin name or description
                if( (navigator.plugins[pluginsArrayCounter].name.indexOf(daPlugins[namesCounter]) >= 0) || 
                    (navigator.plugins[pluginsArrayCounter].description.indexOf(daPlugins[namesCounter]) >= 0) ) {
                    // this name was found
                    numFound++;
                }   
            }
            // now that we have checked all the required names against this one plugin,
            // if the number we found matches the total number provided then we were successful
            if(numFound == daPlugins.length) {
                pluginFound = true;
                // if we've found the plugin, we can stop looking through at the rest of the plugins
                break;
            }
        }
    }
    return pluginFound;
} // detectPlugin


// Here we write out the VBScript block for MSIE Windows
if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Win') != -1)) {
    document.writeln('<script language="VBscript">');

    document.writeln('\'do a one-time test for a version of VBScript that can handle this code');
    document.writeln('detectableWithVB = False');
    document.writeln('If ScriptEngineMajorVersion >= 2 then');
    document.writeln('  detectableWithVB = True');
    document.writeln('End If');

    document.writeln('\'this next function will detect most plugins');
    document.writeln('Function detectActiveXControl(activeXControlName)');
    document.writeln('  on error resume next');
    document.writeln('  detectActiveXControl = False');
    document.writeln('  If detectableWithVB Then');
    document.writeln('     detectActiveXControl = IsObject(CreateObject(activeXControlName))');
    document.writeln('  End If');
    document.writeln('End Function');

    document.writeln('\'and the following function handles QuickTime');
    document.writeln('Function detectQuickTimeActiveXControl()');
    document.writeln('  on error resume next');
    document.writeln('  detectQuickTimeActiveXControl = False');
    document.writeln('  If detectableWithVB Then');
    document.writeln('    detectQuickTimeActiveXControl = False');
    document.writeln('    hasQuickTimeChecker = false');
    document.writeln('    Set hasQuickTimeChecker = CreateObject("QuickTimeCheckObject.QuickTimeCheck.1")');
    document.writeln('    If IsObject(hasQuickTimeChecker) Then');
    document.writeln('      If hasQuickTimeChecker.IsQuickTimeAvailable(0) Then ');
    document.writeln('        detectQuickTimeActiveXControl = True');
    document.writeln('      End If');
    document.writeln('    End If');
    document.writeln('  End If');
    document.writeln('End Function');

    document.writeln('</scr' + 'ipt>');
}
else
{
    var detectableWithVB = false;
}