// videxstation.js

var Videx; if (!Videx) Videx = {};

// Videx.VidexStation class
Videx.VidexStation = {
	// shop attributes:
	name:'Videx Station For TOEI ANIME-BB Premium',
	cookieDomain: 'videx.jp',
	installURL: '/vs/download_confirm.php',
	verChkClsId: 'CLSID:B5D8BD75-9EF4-4A16-B300-152E45743A96',
	
	// commen attributes:
	installMarkName:'vsdownload',
	documentarySupportedVersion:[1,0,1,120], // 複数ビットレート作品に対応しているかどうか
	duplicateProductSupportedVersion:[2,0,6,225], // 準プレミアムなど同じ作品で違うrights(variant)の表示がおかしくなるvsかどうか
	version:null,
	verChk:null,
	
	// method:
	isIE: function() {
		return (navigator.appName == "Microsoft Internet Explorer") ? true : false; // IE check
	},
	isOcxSupported: function() { 
		return this.isIE();
	},
	isInstalled:function() { 
		var isOcxSupported = this.isOcxSupported();
		var isVerChkAvailabled = false;
		if (isOcxSupported) {
			// ActiveXが利用できる場合はVidexStationがインストールするAcriveXでチェック
			try {
				if (!this.verChk) {
					var obj = document.createElement('object');
					document.body.appendChild(obj);
					obj.classid = this.verChkClsId;
					obj.width = obj.height = 1;
					var versionString = obj.GetVSVersion();
					this.verChk = obj;
//					this.version = new Videx.Version(1,0,1,110);
//					this.version.parse(versionString);
					isVerChkAvailabled = true;
				}
				//alert("VIDEXSTATION is installed: "+versionString);
				return true;
			} catch (ex) {}
		}

		// ActiveXで判断できない場合は、Cookieで判断する
		if (this.hasInstallMark()) {
			// IEにおいて、ActiveXが見つからないのにCookieではインストールの痕跡がある場合
			if (isOcxSupported && !isVerChkAvailabled) {
				this.setInstallMark(false); // Cookieをクリア
				var msg = "『Videx Station For TOEI ANIME-BB Premium』を認識することができませんでした。\n";
				msg += "高速ダウロードを行うには『Videx Station For TOEI ANIME-BB Premium』が必要です。\n";
				msg += "\n";
				msg += "■■■『Videx Station For TOEI ANIME-BB Premium』をセットアップしているのにこの表示が出る場合■■■\n";
				msg += "セキュリティレベルが「高」に設定されているか、『Videx Station For TOEI ANIME-BB Premium』の読み込み\n";
				msg += "がキャンセルされた可能性があります。\n";
				msg += "次の事項を確認してください。\n";
				msg += "　・Internet Explorer のセキュリティレベル\n";
				msg += "　・ページが表示されるときに警告が表示され、その場面でキャンセルをしていないか\n";
				msg += "　・ウィスルチェックを行うソフトウェアの設定で、この種のプログラムをプロテクト\n";
				msg += "　　するようになっていないか\n";
				msg += "\n";
				msg += "■■■『Videx Station For TOEI ANIME-BB Premium』をアンインストールしてしまった場合■■■\n";
				msg += "再度、『Videx Station For TOEI ANIME-BB Premium』をインストールしてください。\n";
				//alert( msg );
				return false;
			}
			return true;
		}
		return false;
	},

	hasInstallMark: function() {
		return ( this.getCookie(this.installMarkName) == "on" ) ? true : false;
	},
	
	setInstallMark: function(enable) {
		// ActiveXが使えないブラウザではVidexStationがインストールされているかどうかを判定できない
		// そこでCookieにインストールの印をつけそれを代用する
		this.setCookie(this.installMarkName, (enable) ? "on" : "off");
	},

	getVersion: function() { 
		return this.version || new Videx.Version(1,0,1,110);
	},
	
	request: function(url) {
		var ret = this.isInstalled();
		if (ret) {
			location.href = url;
		} else {
			var installURL = this.installURL;
			location.href = installURL + ((installURL.indexOf('?')==-1) ? '?' : '&') + 'vsurl='+encodeURIComponent(url);
		}
		return ret;
	},
	
	hasDeferDownloadUrl: function() {
		var match = /vsurl=(.*)?/.exec(location.href);
		return (match) ? true : false;
	},
	
	deferRequest: function() {
		try {
			var match = /vsurl=(.*)?/.exec(location.href);
			if (match) {
				var ret = this.isInstalled();
				if (!ret) {
					alert(this.name+'のインストールが確認できないため、高速ダウンロードが実行できませんでした。');
				} else {
					// retry
					var u = decodeURIComponent(match[1]);
					location.href = u;
				}
			} else {
				alert('再ダウンロード用のURLが取得できないため実行できませんでした。ページを戻って再度ダウンロードボタンを実行してください。');
			}
		} catch (ex) {
		}
	},
	
	
	// Cookie Supports to check Install VidexStation for Firefox
	getCookieValue: function(offset) {
		var maxlength = document.cookie.indexOf(";", offset);
		if (maxlength == -1) maxlength = document.cookie.length;
		return unescape(document.cookie.substring(offset, maxlength));
	},

	setCookie: function(name,value) {
		var domain = this.cookieDomain;
		var nowtime = new Date().getTime();
		var clear_time = new Date(nowtime + (60 * 60 * 24 * 1000 * 365));
		var expires = clear_time.toGMTString();
		document.cookie = name + '=' + escape(value) + '; path=/; expires=' + expires;
	},
	
	getCookie: function(name) {
		var arg = name + "=";
		var argLength = arg.length;
		var cookieLength = document.cookie.length;
		var i = 0;
		while (i < cookieLength) {
			var j = i + argLength;
			if (document.cookie.substring(i, j) == arg) {
				return this.getCookieValue(j);
			}
			i = document.cookie.indexOf(" ", i) + 1;
			if (i == 0)
				break;
		}
		return null;
	}
};

function downloadVidexStation()
{
	location.href='/vs/latest/setup/';
}
