//vidswtich tech version 1.7

var defaultimage = "images/cat3.jpg"; //do a full http:// link here

Array.prototype.pad = function(l, v){
	while (this.length < l){this.push(v);}
}
function CustomHTML(html){
	this.html = html;
}
function ImageFile(src){
	this.src = src;
	this.tag = "<img src='" + this.src + "' width='100%' height='100%' />";
}
function FlashFile(src){
	this.src = src
	this.download = "<a href='http://get.adobe.com/flashplayer/' target='_blank' >Flash Player</a>";
}
function VideoFile(src, format, codecs){
	this.format = format;
	this.src = src;
	this.codecs = this.setCodecs(codecs);
	this.tag = "<source src='" + this.src + "' type='video/" + this.format.toLowerCase() + "; codecs=\"" + this.codecs + "\"' />";
	this.download = '<a href="' + this.src + '" target="_blank">' + this.format + '</a>';
}
VideoFile.prototype = {

	setCodecs : function(codecs){
		if(typeof codecs == "undefined"){
			switch(this.format.toLowerCase()){
				case 'mp4':
					return "avc1.42E01E, mp4a.40.2";
					break;
				case 'ogg':
					return "theora, vorbis";
					break;
				case 'webm':
					return "vp8, vorbis";
					break;
				default:
					this.codecs = "";
					break;
			}
		}
		else {
			return codecs;
		}
	}
}
function VideoObject(id, width, height){
	this.id = id;
	this.settings = new Object();
	this.width = width;
	this.height = height;
	this.version = [9,0,0,0];
	this.setDefaults();
	this.videos = new Array();
	this.image = new ImageFile(defaultimage);
}
VideoObject.prototype = {
	
	addVideoFile : function(src, format, codecs){
		this.videos.push(new VideoFile(src, format, codecs));
	},
	addFlashFile : function(src, version){
		this.flash = new FlashFile(src);
		if (typeof version != 'undefined'){
			this.setVersion(version);
		}
	},
	addImageFile : function(src){
		this.image = new ImageFile(src);
	},
	addHTMLBlock : function(id){
		this.htmlBlock = document.getElementById(id);
		this.htmlBlock.style.display = 'none';
	},
	addCustomHTML : function(html){
		this.custom = new CustomHTML(html);
	},
	addSettings : function(obj){
		for(vrbl in obj){this.settings[vrbl] = obj[vrbl];}
	},
	addSetting : function(n,v){
		this.settings[n] = v;
	},
	setVersion : function(v){
		var vrs = v.split('.');
		vrs.pad(4,0);
		this.version = vrs;
	},
	setDefaults : function(){
		this.settings.play = true;
		this.settings.controls = true;
		this.settings.allowFullScreen = true;
		this.settings.allowScriptAccess = 'sameDomain';
		this.settings.type = 'application/x-shockwave-flash';
		this.settings.plugsinpage = 'http://www.adobe.com/go/getflashplayer';
		this.settings.quality = 'high';
		this.settings.wmode = 'transparent';
	},
	getParamElement : function(name, value){
		var param ='<param name="' + name + '" value="' + value + '" />';		
		return param;
	},
	getObjectElement : function(){
		var obj = '<object width="' + this.width + '" height="' + this.height + '" ';
		obj += 'classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" ';
		obj += 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version' + this.version + '">';
		obj += this.getParamElement('movie', this.flash.src);
		obj = this.setAllParams(obj);
		obj += this.getEmbedElement();
		obj += '</object>';
		
		return obj;
	},
	getEmbedElement : function(){
		var embed = '<embed width="' + this.width + '" height="' + this.height + '" src="' + this.flash.src + '" ';
		embed = this.setAllAttributes(embed);
		embed += ' />';
		
		return embed;
	},
	
	setAllAttributes : function(element){
		for (name in this.settings){
			element += name + '="' + this.settings[name] + '" ';
		}
		return element;
	},
	setAllParams : function(element){
		for (name in this.settings){
			element += this.getParamElement(name, this.settings[name]);
		}
		return element;
	},
	getFlashBlock : function(){
		var obj = this.getObjectElement();
		return obj;
	},
	getImageElement : function(){
		var text = "<div class='fallbackimage' style='position:relative;width:" + this.width + "px;height:" + this.height + "px;overflow:hidden;'>" + 
		this.image.tag + "<div class='box'><div class='transparent'></div><div class='content'>";
		text += this.makeImageText();
		text += "</div></div></div>";
		return text;
	},
	makeImageText : function(){
		var text = "<span>Video unplayable.";
		var links = new Array();
		if(this.videos.length > 0){
			for(var i = 0; i < this.videos.length; i++) {
				links.push(this.videos[i].download);
			}
		}
		if(typeof this.flash != "undefined"){links.push(this.flash.download);}
		if(links.length > 0) {
			var last = links.pop();
			text += " Download the "
			text += links.join(', ');
			if(links.length > 0){text += ' or ';}
			text += last;
		}
		text += "</span>";
		return text;
	},
	imageTextCSS : function(){
		var CSS = '';
		CSS += 'div.fallbackimage div.box{position:absolute;left:0px;bottom:0px;z-index:100;width:100%;overflow:hidden;}';
		CSS += 'div.fallbackimage div.transparent{position:absolute;left:0px;top:0px;background:#555;width:100%;height:100%;opacity:0.65;filter: alpha(opacity = 65);}';
		CSS += 'div.fallbackimage div.content{position:relative;margin:0.5em;}';
		
		return CSS;
	},
	imageTextStyling : function(){
		// create style element and set type
		var style = document.createElement('style');
		style.type = 'text/css';
		var CSS = this.imageTextCSS();
		try {
			// append CSS text node
			style.appendChild(document.createTextNode(CSS));
		} catch (ex) {
			// alter properties for IE
			style.styleSheet.cssText = CSS;
		}
		// append to head
		var head = document.getElementsByTagName("head")[0];
		head.appendChild(style);
	},
	getFallback : function(){
		if (typeof this.htmlBlock != 'undefined'){
			this.htmlBlock.style.display = 'block';
			return '<span style="display:block;"></span>';
		}
		else if (typeof this.custom != 'undefined'){
			return this.custom.html;
		}
		else {
			this.imageTextStyling();
			return this.getImageElement();
		}
	},
	openVideoTag : function(){
		var tags = '<video id="' + this.id + '" width="' + this.width + '" height="' + this.height + '"';
		if(this.settings.play){tags += ' autoplay="autoplay"';}
		if(this.settings.controls){tags += ' controls="controls"';}
		tags += '>';
		for(var i = 0; i < this.videos.length; i++) {
			tags += this.videos[i].tag;
		}
		return tags;
	},
	closeVideoTag : function(){
		return '</video>';
	},
	compose : function(){
		var html = "";
		if(this.videos.length > 0){html += this.openVideoTag();}
		if(typeof this.flash != "undefined" && DetectFlashVer(this.version[0],this.version[1],this.version[2])){
			html += this.getFlashBlock();
		}
		else {
			html += this.getFallback();
		}
		if(this.videos.length > 0){html += this.closeVideoTag();}
		var main = document.getElementById(this.id);
		//main.innerHTML = html;
		document.write(html);
	}
}

//v1.7
// Flash Player Version Detection
// Detect Client Browser type
// Copyright 2005-2007 Adobe Systems Incorporated.  All rights reserved.
var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;

function ControlVersion()
{
	var version;
	var axo;
	var e;

	// NOTE : new ActiveXObject(strFoo) throws an exception if strFoo isn't in the registry

	try {
		// version will be set for 7.X or greater players
		axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
		version = axo.GetVariable("$version");
	} catch (e) {
	}

	if (!version)
	{
		try {
			// version will be set for 6.X players only
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
			
			// installed player is some revision of 6.0
			// GetVariable("$version") crashes for versions 6.0.22 through 6.0.29,
			// so we have to be careful. 
			
			// default to the first public version
			version = "WIN 6,0,21,0";

			// throws if AllowScripAccess does not exist (introduced in 6.0r47)		
			axo.AllowScriptAccess = "always";

			// safe to call for 6.0r47 or greater
			version = axo.GetVariable("$version");

		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			// version will be set for 4.X or 5.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
			version = axo.GetVariable("$version");
		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			// version will be set for 3.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
			version = "WIN 3,0,18,0";
		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			// version will be set for 2.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			version = "WIN 2,0,0,11";
		} catch (e) {
			version = -1;
		}
	}
	
	return version;
}

// JavaScript helper required to detect Flash Player PlugIn version information
function GetSwfVer(){
	// NS/Opera version >= 3 check for Flash plugin in plugin array
	var flashVer = -1;
	
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
			var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
			var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
			var descArray = flashDescription.split(" ");
			var tempArrayMajor = descArray[2].split(".");			
			var versionMajor = tempArrayMajor[0];
			var versionMinor = tempArrayMajor[1];
			var versionRevision = descArray[3];
			if (versionRevision == "") {
				versionRevision = descArray[4];
			}
			if (versionRevision[0] == "d") {
				versionRevision = versionRevision.substring(1);
			} else if (versionRevision[0] == "r") {
				versionRevision = versionRevision.substring(1);
				if (versionRevision.indexOf("d") > 0) {
					versionRevision = versionRevision.substring(0, versionRevision.indexOf("d"));
				}
			}
			var flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
		}
	}
	// MSN/WebTV 2.6 supports Flash 4
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
	// WebTV 2.5 supports Flash 3
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
	// older WebTV supports Flash 2
	else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
	else if ( isIE && isWin && !isOpera ) {
		flashVer = ControlVersion();
	}	
	return flashVer;
}

// When called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available
function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision)
{
	versionStr = GetSwfVer();
	if (versionStr == -1 ) {
		return false;
	} else if (versionStr != 0) {
		if(isIE && isWin && !isOpera) {
			// Given "WIN 2,0,0,11"
			tempArray         = versionStr.split(" "); 	// ["WIN", "2,0,0,11"]
			tempString        = tempArray[1];			// "2,0,0,11"
			versionArray      = tempString.split(",");	// ['2', '0', '0', '11']
		} else {
			versionArray      = versionStr.split(".");
		}
		var versionMajor      = versionArray[0];
		var versionMinor      = versionArray[1];
		var versionRevision   = versionArray[2];

        	// is the major.revision >= requested major.revision AND the minor version >= requested minor
		if (versionMajor > parseFloat(reqMajorVer)) {
			return true;
		} else if (versionMajor == parseFloat(reqMajorVer)) {
			if (versionMinor > parseFloat(reqMinorVer))
				return true;
			else if (versionMinor == parseFloat(reqMinorVer)) {
				if (versionRevision >= parseFloat(reqRevision))
					return true;
			}
		}
		return false;
	}
}
