﻿/*
 * Hongkong Toolbar Scripts
 * version 1.2, 11 June 2010
 * (C) Commercial Radio Productions Limited
 */
// To be used for the case that user's OS or browser doesn't support HKTB video
var HKToolbarVideoSupportList = 'Hong Kong Toolbar直播的系統需求：\n- Hong Kong Toolbar 5.0 或以上\n - MS Internet Explorer 6.x/7.x/8.x 或 Firefox 2.0/3.0/3.5\n- Windows 7/Vista/XP/2003/2000 32 Bit\n';
var HKToolbarAudioSupportList = 'Hong Kong Toolbar 的系統需求：' +
'\n- MS Internet Explorer 6.x/7.x/8.x 或 Firefox 2.0/3.0/3.5' +
'\n- Windows 7/Vista/XP/2003/2000 32 Bit 或 Mac OS X 10.4.9+Flip4Mac';

// To check the existance of Hong Kong Toolbar
function hasHKToolbar()
{
	return (window.external.hktbar_Action != null);
}
function gotoHKToolbarDownloadPage() 
{
	window.open("http://www.881903.com/Page/zh-TW/downloadtoolbar.aspx","hktbDownload","");
}
// For Toolbar Playback (Support crs/mp3/asx/asf/wma)
function ToolBarPlayInternal(title,winUrl,macUrl,hot)
{
	var isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
	var hasHKTB=false;
	try {
	  hasHKTB=isIE?hasHKToolbar():isToolbar();
	} catch(e){ }
	if(isIE && hasHKTB){
		//IE, use CRS
		var params = new Object();
		params["url"] = winUrl;
		params["title"] = title;
		params["hot"] = hot;
		var ret = window.external.hktbar_Action(1, params);	
	}else if(!isIE && hasHKTB){
		// Firefox
		if (navigator.appVersion.indexOf("Mac")!=-1) {
			// Mac Platform, reassigns the URL
			if (macUrl != '') { winUrl = macUrl; }
		}
		var element = document.createElement("MyExtensionDataElement");
		element.setAttribute("title", title);
		element.setAttribute("url", winUrl);
		element.setAttribute("hot", hot);
		element.setAttribute("scrolltitle","1");
		element.setAttribute("funcid", 1);
		
		document.documentElement.appendChild(element);
		var evt = document.createEvent("Events");
		evt.initEvent("MyExtensionEvent", true, false);
		element.dispatchEvent(evt);
	}else{
		//No HKTB installed, but eligible for downloading toolbar
		alert("請安裝Hong Kong Toolbar\n\n"+HKToolbarAudioSupportList);
		gotoHKToolbarDownloadPage();
	}
}

/////////////////// Public API //////////////////////
// Single URL
function ToolBarPlaySingleURL(title,winurl,hot) 
{
	var supportedPlatform = isHKTBAudioSupportedPlatform();
	if (!supportedPlatform) {
		alert(HKToolbarAudioSupportList);
	} else {
		ToolBarPlayInternal(title,winurl,'',hot);
	}
}
// Dual URL for Windows / Mac
function ToolBarPlayDualURL(title,winurl,macurl,hot) 
{
	var supportedPlatform = isHKTBAudioSupportedPlatform();
	if (!supportedPlatform) {
		alert(HKToolbarAudioSupportList);
	} else {
		ToolBarPlayInternal(title,winurl,macurl,hot);
	}
}
// For Windows Only
function ToolBarPlayVideoURL(title,url,hot) {
	var supportedOS = isHKTBVideoSupportedOS();
	var supportedOSBrowser = isHKTBVideoSupportedOSBrowser();
	if (!supportedOS){
		alert(HKToolbarVideoSupportList);
	} else if (supportedOS && !supportedOSBrowser) {
		alert(HKToolbarVideoSupportList);
	} else {
		ToolBarPlaySingleURL(title,url,hot);
	}
}

/////////////////// Checking Scripts //////////////////////
// Check Supported Platform for Audio (i.e. Windows+IE / Windows+Firefox / Mac+Firefox)
// TODO: version check
function isHKTBAudioSupportedPlatform()
{
	var supported = false;
	var agent = navigator.userAgent;
	if (agent.toLowerCase().indexOf("windows") != -1) {
		supported = ((agent.toLowerCase().indexOf("msie") != -1) || (agent.toLowerCase().indexOf("firefox") != -1));
	} else if (agent.toLowerCase().indexOf("macintosh") != -1) {
		supported = (agent.toLowerCase().indexOf("firefox") != -1);
	}
	return supported;
}
// Check Supported OS for Video (i.e. Windows will return TRUE)
// TODO: version check
function isHKTBVideoSupportedOS()
{
	var os = "Others";
	var agent = navigator.userAgent;
	if (agent.toLowerCase().indexOf("windows") != -1) {
		os = "Windows";
	} else if (agent.toLowerCase().indexOf("macintosh") != -1) {
		os = "Macintosh";
	}
	return (os == "Windows");
}
// Check Supported OS / Browser for Video (i.e. Windows+IE / Windows+Firefox)
// TODO: version check
function isHKTBVideoSupportedOSBrowser()
{
	var hktoolbarVideoSupported = false;
	var browser = "Others";
	var agent = navigator.userAgent;

	if (agent.toLowerCase().indexOf("msie") != -1) {
		browser = "IE";
	} else if (agent.toLowerCase().indexOf("firefox") != -1) {
		browser = "Firefox";
	}
	
	if (isHKTBVideoSupportedOS() && (browser == "IE" || browser == "Firefox")) {
		hktoolbarVideoSupported = true;
	}
	
	return hktoolbarVideoSupported;
}

