/**
 * @author alex
 */
if (typeof RadioTime == "undefined") {
	alert("Invalid configuration!");
}
RadioTime.uaDetect = function() {
	this.text = navigator.userAgent;
	var ua = navigator.userAgent.toLowerCase(),
		is = function(t){ 
			return ua.indexOf(t) != -1; 
		};
	this.browser=(!(/opera|webtv/i.test(ua))&&/msie (\d)/.test(ua))?('ie'):
			is('firefox/')? 	'firefox':
			is('opera')?		'opera':
			is('konqueror')?	'konqueror':
			is('chrome')?		'chrome':
			is('applewebkit/')?	'safari':
			is('mozilla/')?		'mozilla':'';
			
	this.os=(is('x11')||is('linux'))?	'linux':
			is('mac')?					'mac':
			is('win')?					'win':'';
			
	switch (this.browser) {
		case 'ie' : {
			this.browserVersion = (/msie ([\d.+]*);/.test(ua)) ? (RegExp.$1) : '';
			this.browserName = "IE " + this.browserVersion;
			break;
		}
		case 'firefox' : {
			this.browserVersion = (/firefox\/([\d]*).([\d]*)/.test(ua)) ? (RegExp.$1)+"."+(RegExp.$2) : '';
			this.browserName = "Firefox " + this.browserVersion;
			break;
		}
		case 'opera' : {
			this.browserVersion = (/opera\/([\d.+]*)/.test(ua)) ? (RegExp.$1) : /opera ([\d.+]*)/.test(ua)? RegExp.$1 : '';
			this.browserName = "Opera " + this.browserVersion;
			break;
		}
		case 'konqueror' : {
			this.browserName = "Konqueror";
			break;
		}
		case 'safari' : {
			this.browserVersion = (/version\/([\d.+]*)/.test(ua)) ? (RegExp.$1) : '';
			this.browserName = "Safari " + this.browserVersion;
			break; 
		}		
		case 'chrome' : {
			this.browserVersion = (/chrome\/([\d.+]*)/.test(ua)) ? (RegExp.$1) : '';
			this.browserName = "Chrome " + this.browserVersion; 
			break;
		}		
		case 'mozilla' : {
			this.browserVersion = (/mozilla\/([\d.+]*)/.test(ua)) ? (RegExp.$1) : '';
			this.browserName = "Mozilla " + this.browserVersion; 
			break;
		}	
	}
	switch (this.os) {
		case 'win' : {
			var ver = 	is("nt 5.0") ? "2000" : 
						is("nt 5.1") ? "XP" : 
						is("nt 6.0") ? "Vista" : 
						is("nt 6.1") ? "7" : "";
			this.osName = "Windows " + ver;
			break;
		}
		case 'mac' : {
			var ver = '';
			if (is('iphone')) 
				this.osName = "iPhone OS X";
			else 
				this.osName = "Mac OS X " + ver;
			break;
		}
		case 'linux' : {
			var ver = '';
			this.osName = "Linux " + ver;
			break;
		}
	}
	this.versions = {"windows":null, "real":null, "quicktime":null, "flash":null, "silverlight":null};
}
RadioTime.uaDetect.prototype.pluginsString = function() {
	var s = "";
	for (var p in this.versions) {
		this.versions[p] = this.getPluginVersion(p);
		if (this.versions[p].num != "-") {
			s += p + " " + this.versions[p].num + (this.versions[p].scriptable ? "s": "") + ", ";
		}
	}
	return s;
}
RadioTime.uaDetect.prototype.isPluginSupported = function(name) {
	return (this.getPluginVersion(name).num != "-");
}

RadioTime.uaDetect.prototype.getPluginVersion = function(name) {
	if (this.versions[name])
		return this.versions[name];
	switch (name) {
		case "windows": 	
			return this.versions[name] = this.getWmpVersion();
		case "real": 		
			return this.versions[name] = this.getRealVersion();
		case "quicktime": 	
			return this.versions[name] = this.getQTVersion();
		case "flash": 		
			return this.versions[name] = this.getFlashVersion();
		case "silverlight": 
			return this.versions[name] = this.getSilverlightVersion();		
		case "vlc": 
			return this.versions[name] = this.getVLCVersion();	
		case "iframe":
		case "link":
			return this.versions[name] = {"num": "1.0", "scriptable": false};	
		default: 			
			return {"num": "-", "scriptable": false};
	}
}
RadioTime.uaDetect.prototype.getFlashVersion = function() {
	var f = "-", n = navigator;
	if (n.plugins && n.plugins.length) {
		for (var ii=0; ii<n.plugins.length; ii++) {
			if (n.plugins[ii].name.indexOf('Shockwave Flash') != -1) {
				f=n.plugins[ii].description.split('Shockwave Flash ')[1];
				break;
			}
		}
	} else if (window.ActiveXObject) {
		for (var ii=10; ii>=2; ii--) {
			try {
				var fl=eval("new ActiveXObject('ShockwaveFlash.ShockwaveFlash." + ii + "');");
				if (fl) { f = ii + '.0'; break; }
			}
			catch(e) {}
		}
	}
	if (f.split(".")[0]) {
		f = f.split(".")[0];
	}
	return {"num": f, "scriptable": (f > 7)};
}
RadioTime.uaDetect.prototype.getSilverlightVersion = function()
{
	var f = "-", a = null, AgControl = null;
	var plugin = navigator.plugins["Silverlight Plug-In"];
	try
    {
	 	if (plugin) {
			a = document.createElement("div");
			document.body.appendChild(a);
			if(this.browser == "safari")
				a.innerHTML = '<embed type="application/x-silverlight" />';
			else 
				a.innerHTML = '<object type="application/x-silverlight"  data="data:," />';
			AgControl = a.childNodes[0];
	 	} else if (window.ActiveXObject) {
	            var AgControl = new ActiveXObject("AgControl.AgControl");
	    }
		document.body.innerHTML;
		f = AgControl.IsVersionSupported("2.0") ? "2.0" : (AgControl.IsVersionSupported("1.0") ? "1.0" : "-");
		delete AgControl;
	} catch (e){
        f = "-";

		if (plugin && plugin.description) {
			var ver = plugin.description.split(".");
			if (isFinite(ver[0]) && isFinite(ver[1])) {

				if (ver[0] > 0) {
					f = ver[0] + "." + ver[1];
				}
			}
		}
    }
	if (a) document.body.removeChild(a);

 	return {"num": f, "scriptable": true};
}
RadioTime.uaDetect.prototype.getVLCVersion = function()
{
	var f = "-", a = null, VlcControl = null, n = navigator, s = true;
	try
    {
	 	if (n.plugins && n.plugins.length) {
			for (var ii=0; ii<n.plugins.length; ii++) {
				if (n.plugins[ii].name.indexOf('VLC') != -1) {
					f = n.plugins[ii].description;
					break;
				}
			}
			if (f.indexOf('Version') > -1) {
				f = f.split('Version ')[1];
				f = f.split(',')[0];
			} else if (f != "-") {
				f = "0.8";
			}
	 	} else if (window.ActiveXObject && this.browserName != "IE 7.0") { //detection shows a scary warning in IE 7 so skip it
	    	var VlcControl = new ActiveXObject("VideoLAN.VLCPlugin");
			f = VlcControl.VersionInfo;
			delete VlcControl;
	    }
		
	} catch (e){
        f = "-";
		s = false;
    }
	if (a) document.body.removeChild(a);
 	return {"num": f, "scriptable": s};
}
RadioTime.uaDetect.prototype.getWmpVersion = function() {
	var f = "-", n = navigator, s = true;
	if (this.browser == "chrome") {
		if (this.isTypeSupported("application/x-ms-wmp")) {
			f = "7.0";
		} else if (this.isTypeSupported("application/x-mplayer2")) {
			f = "6.4";
		}
	} else	if (n.plugins && n.plugins.length) {
		for (var ii=0; ii<n.plugins.length; ii++) {
			if (n.plugins[ii].name.indexOf('Windows Media Player') != -1 || n.plugins[ii].name.indexOf('Windows Media') != -1) {
				if (this.isTypeSupported("application/x-ms-wmp")) {
					f = "7.0";
				} else {
					f = "6.4";
					s = false;
				}
				break;
			}
		}
	} else if (window.ActiveXObject) {
		try {
			var wmp = new ActiveXObject("WMPlayer.OCX.7");	
			f = wmp.versionInfo;
			delete wmp;
		}
		catch(e) {
			s = false;
		}
	}
	return {"num": f, "scriptable": s};
}
RadioTime.uaDetect.prototype.getRealVersion = function() {
	var f = "-", n = navigator, s = true;
	if (n.plugins && n.plugins.length) {
		for (var ii=0; ii<n.plugins.length; ii++) {
			if (n.plugins[ii].name.indexOf('Player Version') != -1 && this.isTypeSupported("audio/x-pn-realaudio-plugin")) {
				f = n.plugins[ii].description;
				break;
			}
		}
		if (f == "-") { // the second try -- will catch Helix DNA on Linux
			for (ii=0; ii<n.plugins.length; ii++) {
				if (n.plugins[ii].name.indexOf('RealPlayer') != -1 && this.isTypeSupported("audio/x-pn-realaudio-plugin")) {
					f = n.plugins[ii].description;
					if (f.indexOf("version") > -1) {
						f = f.split('version ')[1];
						f = f.split(' ')[0];
					} else {
						f = "4.0"; // plugin on Mac doesn't include version info
					}

					break;
				}
			}
		}

		// Real in FF3 doesn't work
		if (f != "-" && this.browser == 'firefox' && parseFloat(this.browserVersion) >= 3.0 ) {
			f = "-";
			s = false;
		}
	} else if (window.ActiveXObject) {
		var definedControls = [
			'rmocx.RealPlayer G2 Control',
			'rmocx.RealPlayer G2 Control.1',
			'RealPlayer.RealPlayer(tm) ActiveX Control (32-bit)',
			'RealVideo.RealVideo(tm) ActiveX Control (32-bit)',
			'RealPlayer'
		];

		var control = null;
		for (var i = 0; i < definedControls.length; i++) {
			try {
				control = new ActiveXObject(definedControls[i]);
			} catch (e) {
				continue;
			}
			if (control) {
			   break;
			}
		}
		
		if (control) {
			try {
				f = control.GetVersionInfo();
				f = parseFloat(version);
				s = true;
				
				delete control;
			} catch(e) {
				f = "4.0";
				s = false;
			}
		}
	}
	return {"num": f, "scriptable": s};
}
RadioTime.uaDetect.prototype.getQTVersion = function() {
	var f = "-", n = navigator;
	if (n.plugins && n.plugins.length) {
		for (var ii=0; ii<n.plugins.length; ii++) {
			if (n.plugins[ii].name.indexOf('QuickTime') != -1) {
				f = n.plugins[ii].name.split('QuickTime Plug-in ')[1];
				if (!f)
					f = n.plugins[ii].name.split('QuickTime Plug-In ')[1];
				break;
			}
		}
	} else if (window.ActiveXObject && this.browser == "ie") {
		try {
			var qt = new ActiveXObject("QuickTime.QuickTime");	
			if (typeof qt == 'object')
				f = "7";//f = qt.GetQuickTimeVersion(); //> this crashes IE! 
		}
		catch(e) {}
	}

	return {"num": f, "scriptable": true};
}
RadioTime.uaDetect.prototype.isTypeSupported = function(mimeType)
{
	for( var i = 0; i < navigator.mimeTypes.length; i++){
		if(navigator.mimeTypes[i].type.toLowerCase() == mimeType){
			return navigator.mimeTypes[i].enabledPlugin;
		}
	}
	return false;
}
RadioTime.ua = new RadioTime.uaDetect();