function FPChecker(){}
FPChecker.FPDownloadURL = "http://www.funshion.com/download/FunshionInstall.exe";
FPChecker.FPButtonCode = '<image src="funplayer_task.gif"/>边下边看';
FPChecker.FPInstallMessage = "您尚未安装边下边看的软件风行，点击确定下载风行。安装完成后再次点击“边下边看”按钮。";
FPChecker.MaxthonOptionMessage = '您使用的可能是Maxthon浏览器。Maxthon如果打开了ActiveX过滤选项则无法安装边下边看功能控件。\n请关闭“菜单->选项->广告猎手->启用ActiveX过滤”选项后再次点击“边下边看”按钮。\n\n如果不希望再次看到此提示请选择“确定”，否则请选择“取消”';
FPChecker.NotIEMessage = '您使用的浏览器无法安装边下边看控件。\n您可以下载安装风行后使用风行打开torrent文件边下边看。\n\n下载风行请点击“确定”，点击“取消”打开torrent文件。';
FPChecker.installFlagCookie = "FPCheckerInstallFlag";
FPChecker.loaded = false;
FPChecker.createCookie = function(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 86400000));
        var expires = "; expires=" + date.toGMTString();
    }
    else expires = "";
    var cstr = name + "=" + escape(value) + expires + "; path=/";
    document.cookie = cstr;
    return cstr;
}
FPChecker.readCookie = function(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return unescape(c.substring(nameEQ.length, c.length));
    }
    return null;
}
FPChecker.deleteCookie = function(name) {
		if(FPChecker.readCookie(name)){
    	document.cookie = name + "=; path=/; expires=Thu, 01-Jan-1970 00:00:01 GMT";
    }
}
FPChecker.isIE = function(){
	if("Microsoft Internet Explorer" == navigator.appName){
		return true;
	}
	return false;
}
FPChecker.isMaxthon = function(){
	if(navigator.appVersion.indexOf("Maxthon") >= 0){
		return true;
	}
	return false;
}
FPChecker.NotifyActiveX = function(e, torrent){
	if ( e.readyState != 4 || !torrent || torrent == "undefined") return;
	
	if(FPChecker.isMaxthon()){
		var cookieName = "FPCheckerMaxthonFlag";
		var flag = FPChecker.readCookie(cookieName);
		if(!flag){ // cookie not found, display the promote message for maxthon
			flag = confirm(FPChecker.MaxthonOptionMessage);
			if(flag){ // add a cookie to disable the promote message in the future
				FPChecker.createCookie(cookieName, "1", 100);
			}
		}
	}
	e.InputTipText = FPChecker.FPInstallMessage;
	e.InputURL = FPChecker.FPDownloadURL;
	e.InputTorrentPath = torrent;
}
FPChecker.openURL = function(url){
	window.location = url;
}
FPChecker.installControll = function(torrent){
	if(!FPChecker.isIE()) return;
	if(!FPChecker.loaded){
		setTimeout(FPChecker.installControll, 500);
		return;
	}
	var _torrent = FPChecker.readCookie(FPChecker.installFlagCookie);
	var shouldMakeCookie = true;
	if(!torrent && _torrent){
		torrent = _torrent;
	}
	if(_torrent){
		FPChecker.deleteCookie(FPChecker.installFlagCookie);
		shouldMakeCookie = false; // prevent page reload reopen torrent again and again, fixme the 1st reload still reopen
	}
	if(!torrent) return;
	var containerID = "fp_caller_container";
	var code = '<OBJECT ID="FunshionChecker" ';
	code += ' onReadyStateChange="FPChecker.NotifyActiveX(this, \'' + torrent + '\')" ';
	code += ' CLASSID="CLSID:B55461E5-1EBE-4B12-B0A6-339DBFAB3D46"  ';
	code += ' CODEBASE="http://www.funshion.com/download/FunshionChecker.CAB#version=1,0,0,5" ';
	code += ' WIDTH="0" ';
	code += ' HEIGHT="0" ';
	code += ' ALIGN="center" ';
	code += '/>';
	var foo = document.getElementById(containerID);
	if(!foo){
		foo = document.createElement("div");
		foo.setAttribute("id", containerID);
		foo.style.width = 0;
		foo.style.Height = 0;
		foo.innerHTML = code;
		var body = document.getElementsByTagName('body')[0];
		body.appendChild(foo);
	}else{
		foo.innerHTML = code;
	}
	if(shouldMakeCookie && !FPChecker.readCookie(FPChecker.installFlagCookie)){
		FPChecker.createCookie(FPChecker.installFlagCookie, torrent, 0);
	}
}
FPChecker.callFP = function(torrent){
	if(!FPChecker.isIE()){ //对于不支持的浏览器，提示安装Funshion，点击“确定”下载Funshion，“取消”下载种子
		var flag = confirm(FPChecker.NotIEMessage);
		if(flag){
			FPChecker.openURL(FPChecker.FPDownloadURL); //fixme, do not work in FF 1.5.0.7, always open torrent
		}else{
			FPChecker.openURL(torrent);
		}
		return false;
	}
	FPChecker.installControll(torrent);
}
FPChecker.fpl = function(torrent, label, tip){
	if(!torrent) return;
	var code = "<a onclick='FPChecker.callFP(\"" + torrent + "\");return false;' href='" + torrent + "'";
	if(tip)	code += "title='" + tip + "'";
	code += ">";
	if(!label) label = FPChecker.FPButtonCode;
	code += label;
	code += "</a>";
	document.write(code);
}
if(FPChecker.isIE()){
	var body = document.getElementsByTagName('body')[0];
	var torrent = FPChecker.readCookie(FPChecker.installFlagCookie);
	if(torrent){
		FPChecker.installControll(torrent);
	}
}
