//2007-10-04 15:30:00
var skeed = new skeed();
skeed.tvPlayList = new Array();
function skeed() {

// ===== Constants ==================================================================== 
	this.URL_BASE = "http://127.0.0.1:8101/";
	this.MAX_STARTUP_LOOP = 10;
	this.STARTUP_LOOP_INTERVAL = 500;

	this.SR_FORCE_START = 1;

	this.SITE_ID = "500010";
	this.SKEED_NAVIGATOR_URL = "http://127.0.0.1:8101/skeednavigator";

	this.SKEED_TV_EID = "SKEEDTV";
	this.SKEED_TV_SWF_PATH = "swf/SkeedTV.swf";
	this.SKEED_TV_WIDTH = "320";
	this.SKEED_TV_HEIGHT = "240";
	this.SKEED_TV_BGCOLOR = "#ffffff";
	this.SKEED_TV_PLAYLIST_URL = "http://cm.skeedtools.com/";

	this.SKEED_PV_EID = "SKEEDPV";
	this.SKEED_PV_SWF_PATH = "swf/SkeedPreview.swf";
	this.SKEED_PV_WIDTH = "320";
	this.SKEED_PV_HEIGHT = "240";
	this.SKEED_PV_BGCOLOR = "#ffffff";

	this.SKEED_DEFAULT_SWF_PATH = "swf/skeed_top.swf";
	this.SKEED_DEFAULT_IMG_PATH = "images/tv_off.gif";

	this.INSTALL_MODAL_MODE = 0;
	this.INSTALL_MODAL_WIDTH = "400";
	this.INSTALL_MODAL_HEIGHT = "500";
	this.INDUCE_INSTALL_URL = "induce.html";

	this.REGEXP = 'skeedreceiver.*';
	this.ACTIVEX = 'npSkeedReceiver.Plugin.1';
	this.ACTIVEX_GUID = '328fe846-e433-4ccc-9a9c-d5d53d030c2e';
	this.CODEBASE = 'http://download.skeedtools.com/SkeedReceiverSetup.exe';

// ===== function ==================================================================== 
	this.version;
	this.snWindow;
	this.tvPlayList = new Array();

	this.setEnforce = function (force_start) {
		this.SR_FORCE_START = force_start;
	}

    this.addPlayList = function () {
        var list = this.addPlayList.arguments;
        for (var i = 0; i < list.length; i++) {
          this.tvPlayList[this.tvPlayList.length] = list[i];
        }
    }

    this.clearPlayList = function () {
		this.tvPlayList = new Array();
    }

	this.tv = function (siteId, eid, swfPath, width, height, bgColor, playlistUrl) {
		siteId = siteId ? siteId : this.SITE_ID;
		eid = eid ? eid : this.SKEED_TV_EID;
		swfPath = swfPath ? swfPath : this.SKEED_TV_SWF_PATH;
		width = width ? width : this.SKEED_TV_WIDTH;
		height = height ? height : this.SKEED_TV_HEIGHT;
		bgColor = bgColor ? bgColor : this.SKEED_TV_BGCOLOR;
		playlistUrl = playlistUrl ? playlistUrl : this.SKEED_TV_PLAYLIST_URL;
		this.exec01("tvCb", 0, siteId, eid, swfPath, width, height, bgColor, playlistUrl);
	}
	this.tvCb = function (siteId, eid, swfPath, width, height, bgColor, playlistUrl) {
		var e = this.createFlashObjectSrcForTV(siteId, swfPath, width, height, bgColor, playlistUrl);
		this.setValue(eid, e);
		return;
	}

	this.pv = function (skeedId, eid, swfPath, width, height, bgColor, playlistUrl) {
		if (skeedId == null || skeedId.length != 10) {
			return;
		}
		eid = eid ? eid : this.SKEED_PV_EID;
		swfPath = swfPath ? swfPath : this.SKEED_PV_SWF_PATH;
		width = width ? width : this.SKEED_PV_WIDTH;
		height = height ? height : this.SKEED_PV_HEIGHT;
		bgColor = bgColor ? bgColor : this.SKEED_PV_BGCOLOR;
		this.exec01("pvCb", 0, skeedId, eid, swfPath, width, height, bgColor);
	}
	this.pvCb = function (skeedId, eid, swfPath, width, height, bgColor) {
		var e = this.createFlashObjectSrcForPV(skeedId, swfPath, width, height, bgColor);
		this.setValue(eid, e);
		return;
	}

	this.cdl = function (skeedId) {
		skeedId = skeedId ? skeedId : "";
		if (skeedId == null || skeedId == "" || skeedId.length != 10) {
			return;
		}
		this.exec01("cdlCb", 0, skeedId);
	}
	this.cdlCb = function (skeedId) {
		this._(this.URL_BASE + skeedId + "/download");
		return;
	}

	this.sdl = function (siteId) {
		siteId = siteId ? siteId : this.SITE_ID;
		if (siteId == null || siteId == "" || siteId.length != 6) {
			return;
		}
		this.exec01("sdlCb", 0, siteId);
	}
	this.sdlCb = function (siteId) {
		this._(this.URL_BASE + "execute/download/samples?siteid=" + siteId);
		return;
	}

	this.sn = function (url) {
		if (url == null || url == "") {
			url = this.SKEED_NAVIGATOR_URL;
		}
		this.exec01("snCb", 0, url);
	}
	this.snCb = function (url) {
		if (this.snWindow != null) {
			this.snWindow.close();
		}
		this.snWindow = window.open(url, "SkeedNavigator", "width=1000,height=600,location=no,menubar=no,status=no,titlebar=no,toolbar=no,directories=no,scrollbars=no");
		return;
	}

	this.sites = function (cb) {
		if (cb == null || cb == "") {
			return;
		}
		this.exec01("sitesCb", 0, cb);
	}
	this.sitesCb = function (cb) {
		this._(this.URL_BASE + "sites?cb=" + cb);
		return;
	}

	this.ver = function (cb) {
		if (cb == null || cb == "") {
			return;
		}
		this.exec01("verCb", 0, cb);
	}
	this.verCb = function (cb) {
		this._(this.URL_BASE + "version?cb=" + cb);
		return;
	}

	this.contents = function (siteId, cb) {
		if (cb == null || cb == "") {
			return;
		}
		this.exec01("contentsCb", 0, siteId, cb);
	}
	this.contentsCb = function (siteId, cb) {
		this._(this.URL_BASE + "contents?siteid=" + siteId + "&cb=" + cb);
		return;
	}

	this.samples = function (siteId, cb) {
		if (cb == null || cb == "") {
			return;
		}
		this.exec01("samplesCb", 0, siteId, cb);
	}
	this.samplesCb = function (siteId, cb) {
		this._(this.URL_BASE + "samples?siteid=" + siteId + "&cb=" + cb);
		return;
	}

	this.cm = function (siteId, cb) {
		if (cb == null || cb == "") {
			return;
		}
		this.exec01("cmCb", 0, siteId, cb);
	}
	this.cmCb = function (siteId, cb) {
		this._(this.URL_BASE + "cm?siteid=" + siteId + "&cb=" + cb);
		return;
	}

	this.play = function (skeedId) {
		skeedId = skeedId ? skeedId : "";
		if (skeedId == null || skeedId == "" || skeedId.length != 10) {
			return;
		}
		this.exec01("playCb", 0, skeedId);
	}
	this.playCb = function (skeedId) {
		this._(this.URL_BASE + skeedId + "/play.wmv");
		return;
	}
	this.stream = function (skeedId) {
		skeedId = skeedId ? skeedId : "";
		if (skeedId == null || skeedId == "" || skeedId.length != 10) {
			return;
		}
		this.exec01("streamCb", 0, skeedId);
	}
	this.streamCb = function (skeedId) {
		this._2(this.URL_BASE + skeedId + "/stream.asx");
		return;
	}

	this.playAsx = function (skeedId) {
		skeedId = skeedId ? skeedId : "";
		if (skeedId == null || skeedId == "" || skeedId.length != 10) {
			return;
		}
		this.exec01("playAsxCb", 0, skeedId);
	}
	this.playAsxCb = function (skeedId) {
		this._2(this.URL_BASE + skeedId + "/play.asx");
		return;
	}

	this.nodeid = function (cb) {
		if (cb == null || cb == "") {
			return;
		}
		this.exec01("nodeidCb", 0, cb);
	}
	this.nodeidCb = function (cb) {
		this._(this.URL_BASE + "nodeid?cb=" + cb);
		return;
	}

	this.login = function (nodeId, expire, sign, cb, siteId) {
		siteId = siteId ? siteId : this.SITE_ID;
		if (siteId == null || siteId == "" || siteId.length != 6) {
			return;
		}
		this.exec01("loginCb", 0, nodeId, expire, sign, cb, siteId);
	}
	this.loginCb = function (nodeId, expire, sign, cb, siteId) {
		this._(this.URL_BASE + "login?siteid=" + siteId + "&nodeid=" + nodeId + "&expire=" + expire + "&sign=" + sign + "&cb=" + cb);
		return;
	}

	this.logout = function (cb, siteId) {
		siteId = siteId ? siteId : this.SITE_ID;
		if (siteId == null || siteId == "" || siteId.length != 6) {
			return;
		}
		this.exec01("logoutCb", 0, cb, siteId);
	}

	this.logoutCb = function (cb, siteId) {
		this._(this.URL_BASE + "logout?siteid=" + siteId + "&cb=" + cb);
		return;
	}

// ===== exec function ==================================================================== 

	this.exec01 = function () {
		var args = this.exec01.arguments;
		var caller = args[0];
		if (caller == "tvCb" || caller == "pvCb") {
			if (this.SKEED_DEFAULT_SWF_PATH != null && this.SKEED_DEFAULT_SWF_PATH != "") {
				var htmlSrc = this.createFlashObjectSrc(this.SKEED_DEFAULT_SWF_PATH, args[5], args[6], args[7]);
				this.setValue(args[3], htmlSrc);
			} else if (this.SKEED_DEFAULT_IMG_PATH != null && this.SKEED_DEFAULT_IMG_PATH != "") {
				var htmlSrc = "<img src='" + this.SKEED_DEFAULT_IMG_PATH + "' />";
				this.setValue(args[3], htmlSrc);
			}
		}
		// check OS, Browser
		if (!this.isSupportedBrowser() || !this.isWin) {
			return;
		}
		// detectPlugin
		if(!this.detectPlugin) {
			this.insertPluginScript();
			return;
		}
		eval("this.exec02(" + this.arrayToCsv(args) + ")");
	}

	this.exec02 = function () {
		
		var args = this.exec02.arguments;
		var caller = args[0];
		var cnt = parseInt(args[1]);

		if (cnt >= this.MAX_STARTUP_LOOP) {
			if (caller == "tvCb" || caller == "pvCb") {
				if (this.SKEED_DEFAULT_SWF_PATH != null && this.SKEED_DEFAULT_SWF_PATH != "") {
					var htmlSrc = this.createFlashObjectSrc(this.SKEED_DEFAULT_SWF_PATH, args[5], args[6], args[7]);
					this.setValue(args[3], htmlSrc);
					return;
				} else if (this.SKEED_DEFAULT_IMG_PATH != null && this.SKEED_DEFAULT_IMG_PATH != "") {
					var htmlSrc = "<img src='" + this.SKEED_DEFAULT_IMG_PATH + "' />";
					this.setValue(args[3], htmlSrc);
					return;
				}
			} 
		}
		
		if (this.version != null && this.version.length == 6) {
			eval(this.funcStrForExec(args));
			this.version = null;
			return;
		}
		
		if (cnt == 0) {
			if (this.SR_FORCE_START == 0) {
			} else if (this.SR_FORCE_START == 1) {
				this.kickSR();
			}
		}
		
		this._(this.URL_BASE + "version?cb=skeed.getVerCb");
		cnt++;
		setTimeout("skeed.exec02('" + caller + "', " + cnt + this.arrayToCsv2(args) + ")", this.STARTUP_LOOP_INTERVAL);
	}

	this.getVerCb = function (data) {
		this.version = data;
	}

// ===== environment ====================================================================	
	this.agent = navigator.userAgent.toLowerCase();
	this.isIE = (this.agent.indexOf("msie") != -1);
	this.isNN = (this.agent.indexOf("netscape") != -1);
	this.isFF = (this.agent.indexOf("firefox") != -1);
	this.isOP = (this.agent.indexOf("opera") != -1);
	this.isMZ = (this.agent.indexOf("gecko") != -1 && this.agent.indexOf("netscape") == -1 && this.agent.indexOf("firefox") == -1);
	this.isWin = (navigator.appVersion.indexOf('Win') != -1);

	this.getIEVersion = function () {
		var ua = navigator.userAgent;
		var MSIEOffset = ua.indexOf("MSIE ");
		if (MSIEOffset == -1) {
			return 0;
		} else {
			return parseFloat(ua.substring(MSIEOffset + 5, ua.indexOf(";", MSIEOffset)));
		}
	}

	this.getFFVersion = function () {
		var ua = navigator.userAgent;
		var FFOffset = ua.indexOf("Firefox/");
		if (FFOffset == -1) {
			return 0;
		} else {
			return parseFloat(ua.substring(FFOffset + 8));
		}
	}

	this.getNNVersion = function () {
		var ua = navigator.userAgent;
		var NNOffset = ua.indexOf("Netscape/");
		if (NNOffset == -1) {
			return 0;
		} else {
			return parseFloat(ua.substring(NNOffset + 9), ua.indexOf(NNOffset + 9, "("));
		}
	}

	this.getOPVersion = function () {
		var ua = navigator.userAgent;
		var OPOffset = ua.toLowerCase().indexOf("opera/");
		if (OPOffset == -1) {
			return 0;
		} else {
			return parseFloat(ua.substring(OPOffset + 6), ua.indexOf(" ", OPOffset));
		}
	}

	this.getWinVersion = function () {
		var ua = navigator.userAgent;
		var WinOffset = ua.indexOf("Windows NT ");
		if (WinOffset == -1) {
			return 0;
		} else {
			return parseFloat(ua.substring(WinOffset + 11, WinOffset + 14));
		}
	}

	this.isSupportedBrowser = function () {
		if (this.isIE) {
			if (this.getIEVersion() >= 6) {
				return true;
			}
			return false;
		}
		if (this.isFF) {
			if (this.getFFVersion() >= 1.0) {
				return true;
			}
			return false;
		}
		if (this.isNN) {
			if (this.getNNVersion() >= 8.0) {
				return true;
			}
			return false;
		}
		if (this.isOP) {
			if (this.getOPVersion() >= 8.0) {
				return true;
			}
			return false;
		}
		if (this.isMZ) {
			return false;
		}
		return false;
	}

	this.detectPlugin = ( function(ele) {
		
		if (ele.isIE && ele.isWin) {
			 document.writeln('<script language="VBscript">');
			 document.writeln('detectableWithVB = False');
			 document.writeln('If ScriptEngineMajorVersion >= 2 then');
			 document.writeln('  detectableWithVB = True');
			 document.writeln('End If');
			 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('</scr' + 'ipt>');
	
			 return detectActiveXControl(ele.ACTIVEX);
		} else {
			 navigator.plugins.refresh(false);
			 var reg_check = new RegExp(ele.REGEXP, 'i');
			 for (plugin = 0; plugin < navigator.plugins.length; plugin++) {
				if (reg_check.test(navigator.plugins[plugin].name)) {
					return navigator.plugins[plugin];
				}
			 }
		}
		return false;
	})(this);

// ===== kick ====================================================================	
	this.kickSR = function () {
		if (this.isIE) {
			var ieVer = this.getIEVersion();
			var winVer = this.getWinVersion();
			if (ieVer < 6) {
				this.kickSR_IE5();
			} else if(ieVer < 7){
				this.kickSR_IE6();
			} else {
				this.kickSR_IE7();
			}
		} else if (this.isNN) {
			this.kickSR_NN();
		} else if (this.isFF) {
			this.kickSR_FF();
		} else if (this.isOP) {
			this.kickSR_OP();
		} else if (this.isMZ) {
			this.kickSR_MZ();
		} else {
			
		}
	}
	this.kickSR_IE5 = function () {
		var e = document.createElement("IFRAME");
		e.style.width = 0;
		e.style.height = 0;
		e.style.border = 0;
		e.src = "skeed:";
		document.body.appendChild(e);
	}
	this.kickSR_IE6 = function () {
		var e = document.createElement("IFRAME");
		e.style.width = 0;
		e.style.height = 0;
		e.style.border = 0;
		e.src = "skeed:";
		document.body.appendChild(e);
	}
	this.kickSR_IE7 = function () {
		var e = document.createElement("IFRAME");
		e.style.width = 0;
		e.style.height = 0;
		e.style.border = 0;
		e.src = "skeed:";
		document.body.appendChild(e);
	}
	this.kickSR_FF = function () {
		var e = document.createElement("IFRAME");
		e.style.width = 0;
		e.style.height = 0;
		e.style.border = 0;
		e.src = "skeed:";
		document.body.appendChild(e);
	}
	this.kickSR_NN = function () {
		var e = document.createElement("IFRAME");
		e.style.width = 0;
		e.style.height = 0;
		e.style.border = 0;
		e.src = "skeed:";
		document.body.appendChild(e);
	}
	this.kickSR_OP = function () {
		var e = document.createElement("IFRAME");
		e.style.width = 0;
		e.style.height = 0;
		e.style.border = 0;
		e.src = "skeed:";
		document.body.appendChild(e);
	}
	this.kickSR_MZ = function () {
		var e = document.createElement("IFRAME");
		e.style.width = 0;
		e.style.height = 0;
		e.style.border = 0;
		e.src = "skeed:";
		document.body.appendChild(e);
	}
// ===== etc ====================================================================	

	this._ = function (url) {
		if ((url.substring(0, this.URL_BASE.length) == this.URL_BASE) && (url.indexOf('?') != -1)) {
			url += "&qreload=" + Math.random();
		}
		var e = document.createElement("script");
		e.src = url;
		e.charset = "UTF-8";
		document.body.appendChild(e);
	}
	this._2 = function (url) {
		var e = document.createElement("iframe");
		e.src = url;
		e.setAttribute("height", 0);
		e.setAttribute("width", 0);
		e.setAttribute("hspace", 0);
		e.setAttribute("vspace", 0);
		e.setAttribute("border", 0);
		e.setAttribute("frameborder", 0);
		document.body.appendChild(e);
	}
	this.$ = function (eid) {
		if (typeof eid == "string") {
			return document.getElementById(eid);
		}
		return eid;
	}
	this.setValue = function (name, value) {
		var e = this.$(name);
		if (typeof value == "string") {
			e.innerHTML = value;
		}
	}
	this.createFlashObjectSrc = function (swfPath, width, height, bgColor) {
        return this.createFlashObject(swfPath, "", width, height, bgColor);
	}

	this.createFlashObjectSrcForTV = function (siteId, swfPath, width, height, bgColor, playlistUrl) {
        var flashVars = "";
        flashVars += "auto_hide=true";
        flashVars += "&site_id=" + siteId;
        flashVars += "&play_list_url=" + playlistUrl;
        if (this.tvPlayList != null && this.tvPlayList.length != 0) {
          flashVars += "&skeed_id_list=";
          for (var i = 0; i < this.tvPlayList.length; i++) {
            if (i != 0) {
              flashVars += ",";
            }
            flashVars += this.tvPlayList[i];
          }
        }
        return this.createFlashObject(swfPath, flashVars, width, height, bgColor);
	}
	
	this.createFlashObjectSrcForPV = function (skeedId, swfPath, width, height, bgColor) {
        var flashVars = "";
        flashVars += "auto_hide=true";
        flashVars += "&skeedid=" + skeedId;
        return this.createFlashObject(swfPath, flashVars, width, height, bgColor);
	}
	
	this.createFlashObject = function (url, flashVars, width, height, bgColor) {
        var obj = "";
        obj += "<object ";
        obj += "classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' ";
        obj += "codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0'";
        obj += "width='" + width + "' ";
        obj += "height='" + height + "' ";
        obj += "id='SkeedTV' ";
        obj += "align='middle'>";
        obj += "<param name='allowScriptAccess' value='sameDomain' />";
        obj += "<param name='movie' value='" + url + "' />";
        obj += "<param name='quality' value='high' />";
        obj += "<param name='bgcolor' value='" + bgColor + "' />";
        obj += "<param name='FlashVars' value='" + flashVars + "' />";
        if (!this.isIE) {
	        obj += "<embed ";
	        obj += "src='" + url + "' ";
	        obj += "FlashVars='" + flashVars + "' ";
	        obj += "quality='high' ";
	        obj += "bgcolor='" + bgColor + "' ";
	        obj += "width='" + width + "' ";
	        obj += "height='" + height + "' ";
	        obj += "name='SkeedTV' ";
	        obj += "align='middle' ";
	        obj += "allowScriptAccess='sameDomain' ";
	        obj += "type='application/x-shockwave-flash' ";
	        obj += "pluginspage='http://www.macromedia.com/go/getflashplayer' />";
        }
        obj += "</object>";

		return obj;
    }
	
	this.funcStrStartUp = function (name, args) {
		var fStr = "skeed." + name + "(";
		if (args != null && args.length != 0) {
			for (var i = 0; i < args.length; i++) {
				if (i == 0) {
					fStr += "'" + args[i] + "'";
				} else {
					fStr += ",'" + args[i] + "'";
				}
			}
		}
		fStr += ")";
		return fStr;
	}
	this.funcStrForExec = function (args) {
		if (args == null || args.length == 0) {
			return null;
		}
		var fStr = "skeed." + args[0] + "(";
		for (var i = 2; i < args.length; i++) {
			if (i == 2) {
				fStr += "'" + args[i] + "'";
			} else {
				fStr += ",'" + args[i] + "'";
			}
		}
		fStr += ")";
		return fStr;
	}

	this.arrayToCsv = function (args) {
		var str = "";
		for (var i = 0; i < args.length; i++) {
			if (i != 0) {
				str += ",";
			}
			str += "'" + args[i] + "'";
		}
		return str;
	}
	this.arrayToCsv2 = function (args) {
		var str = "";
		for (var i = 2; i < args.length; i++) {
			str += ",'" + args[i] + "'";
		}
		return str;
	}
	this.insertPluginScript = function (type) {
		if(this.INSTALL_MODAL_MODE == 0 || (this.INSTALL_MODAL_MODE == 1 && this.isIE)){
			var obj = '<object src="" classid="clsid:' + this.ACTIVEX_GUID + '" codebase="' + this.CODEBASE + '" width=0 height=0>';
			obj += '<embed src="" width=0 height=0 type="application/x-skeedreceiver" pluginspage="http://www.skeedtools.com/" />';
			obj += '</object>';
			if(type == "dload") {
				document.write(obj);
			} else {
				document.body.innerHTML = document.body.innerHTML + obj;
			}
		} else if(type != "dload") {
			Modalbox.show("Plugin Required", this.INDUCE_INSTALL_URL, {
				width: this.INSTALL_MODAL_WIDTH, 
				height: this.INSTALL_MODAL_HEIGHT,
				loadingString: '',
				closeString: 'Close',
				overlayDuration:0.2,
				slideDownDuration:0.5,
				slideUpDuration:0.5
			});
		}
	}
}
// ===== for current html directly =====	
if(!skeed.detectPlugin) {
	skeed.insertPluginScript("dload");
}

