/**
 * Disney用UA判定スクリプト<br />
 *
 * memo 例外処理により発生元を特定できるようにしています。
 */

// -- 名前空間定義 ------------------------------------------------------------------------------------------------------------------------------------------
var jp = {};
jp.co = {};
jp.co.disney = {};
jp.co.disney.detect = {};

//-- writer --------------------------------------------------------------------------------------------------------------------------------------
jp.co.disney.detect.writer = {
	// 画面ロード後に処理を行う(window.onloadに対し上書きはされない)
	loadedWrite : function(id, innerHtml) {
		var type = 'load';
		var func = function () {
			try {
				var obj = document.getElementById(id);
				if (obj) {
					if ('innerHTML' in obj) {
						obj.innerHTML = innerHtml;
					} else {
						throw "id[" + id + "]に対し、innerHTMLプロパティが存在しません。";
					}
				} else {
					throw "id[" + id + "]がhtml上に存在しません。";
				}
			} catch (e) {
				var msg = "Error : Exception Message (" + (e.message ? e.message : e) + ")";
				jp.co.disney.detect.debug.write(msg);
				throw msg;
			}
		};
		if (window.addEventListener) {
			window.addEventListener(type, func, false);
		} else if (window.attachEvent) {
			window.attachEvent('on' + type, func);
		}
	}
};

//-- Debug用 ------------------------------------------------------------------------------------------------------------------------------------------------
jp.co.disney.detect.debug = {
	_flg : false
	,_console : null
	,_isOpenConsole : false
	,_consoleName : "jp_co_disney_detect_debug_consoleWindow"
	//,_consoleOption : "width=0, height=0, left=0, top=0, menubar=no, toolbar=no, location=no, status=no, resizable=no, scrollbars=no"
	,_consoleOption : "left=0, top=0, menubar=no, toolbar=no, location=no, status=no"

	,ON : function () {
		this._flg = true;
	}

	,OFF : function () {
		this._flg = false;
	}

	,_openConsole : function () {
		this._isOpenConsole = true;
//		var wnd = window.open("", this._consoleName, this._consoleOption);
//		//this._console = wnd;
//		if (wnd) {
//			this._console = wnd.document;
//		}
		this._console = window.document;
	}
	,write : function (message) {
		if (this._flg) {
			if (!this._isOpenConsole) {
				this._openConsole();
			}
			if (this._console) {
				this._console.write(message);
				this._console.write("<br />");
			}
		}
	}
};
//jp.co.disney.detect.debug.ON();
// -- BlackList ---------------------------------------------------------------------------------------------------------------------------------------------
/**
 * BlackList用
 */
jp.co.disney.detect.blacklist = {

	 myList : new Array()

	,add : function (list) {
		try {
			var separator = " ";
			var ary = list.split(separator);

			this.myList.push(ary);
			jp.co.disney.detect.debug.write("blacklist.added : " + list);
		} catch (e) {
			var msg = "Error : jp.co.disney.detect.blacklist.add : Exception Message (" + (e.message ? e.message : e) + ")";
			jp.co.disney.detect.debug.write(msg);
			throw msg;
		}
	}

	,search : function (ua) {
		try {
			for (var i in this.myList) {
				var isContaints = false;
				for (var j in this.myList[i]) {
					var reg = new RegExp(this.myList[i][j]);
					if (ua.search(reg) == -1) {
						// 存在しない
						isContaints = false;
						break;
					} else {
						isContaints = true;
					}
				}
				if (isContaints) {
					jp.co.disney.detect.debug.write("jp.co.disney.detect.blacklist.search : return true");
					return true;
				}
			}
			jp.co.disney.detect.debug.write("jp.co.disney.detect.blacklist.search : return false");
			return false;
		} catch (e) {
			var msg = "Error : jp.co.disney.detect.blacklist.search : Exception Message (" + (e.message ? e.message : e) + ")";
			jp.co.disney.detect.debug.write(msg);
			throw msg;
		}
	}
};

// -- BlackList定義 -----------------------------------------------------------------------------------------------------------------------------------------
// 例）jp.co.disney.detect.blacklist.add("iPad iPhone OS 3_2");
// スペース区切りで「かつ」を表現
// １行１行が「OR」となる
jp.co.disney.detect.blacklist.add("iPad");
//jp.co.disney.detect.blacklist.add("iPod iPhone OS 3_2");
//jp.co.disney.detect.blacklist.add("iPhone OS 3_2");
//jp.co.disney.detect.blacklist.add("Mozilla Windows Gecko Firefox hoge");
//jp.co.disney.detect.blacklist.add("Mozilla Windows Gecko");
//jp.co.disney.detect.blacklist.add("");
//jp.co.disney.detect.blacklist.add("");
//jp.co.disney.detect.blacklist.add("");
//jp.co.disney.detect.blacklist.add("");
//-- BlackList定義 -----------------------------------------------------------------------------------------------------------------------------------------

// -- UA判定スクリプト --------------------------------------------------------------------------------------------------------------------------------------
jp.co.disney.detect.UserAgentDispatcher = {

	 PC : "pc"
	,SmartPhone : "smph"
	,BlackListPC : "bl_pc"
	,RegSP : /(iPod|iPhone|Android|iPad)/	// 大文字、小文字区別 TODO 本番用
	//,RegSP : /(iPod|iPhone|Android|iPad|Firefox)/	// 大文字、小文字区別   TODO テスト用
	//,RegSPi : /(iPod|iPhone|Android|iPad)/i	// 大文字、小文字区別しない

	,execute : function (funcPC, funcSP, funcBL_PC) {
		try {
			jp.co.disney.detect.debug.write("jp.co.disney.detect.UserAgentDispatcher.execute : start");
			var ret;
			var ua = navigator.userAgent;
			var bl = jp.co.disney.detect.blacklist;

			// 大文字,小文字区別する場合はコメントをはずす
			//ua = ua.toLowerCase();

			jp.co.disney.detect.debug.write("jp.co.disney.detect.UserAgentDispatcher.execute : ua -> " + ua);

			// iPhone/Android判定処理
			if (ua.search(this.RegSP) == -1) {
				// -- PC
				ret = this.PC;
				jp.co.disney.detect.debug.write("jp.co.disney.detect.UserAgentDispatcher.execute : result -> " + ret);
				if(funcPC && this._isFunction(funcPC)) {
					var funcName = this._getFunctionName(funcPC);
					try {
						jp.co.disney.detect.debug.write("jp.co.disney.detect.UserAgentDispatcher.execute : function " + funcName + "() start");
						funcPC();
						jp.co.disney.detect.debug.write("jp.co.disney.detect.UserAgentDispatcher.execute : function " + funcName + "() end");
					} catch (e) {
						var msg = "Error : function " + funcName + " : Exception Message (" + (e.message ? e.message : e) + ")";
						jp.co.disney.detect.debug.write(msg);
						return msg;
					}
				} else {
					var msg = "引数[funcPC]が関数ではありません。funcPC->" + (funcPC ? funcPC : "null");
					jp.co.disney.detect.debug.write("jp.co.disney.detect.UserAgentDispatcher.execute : " + msg);
					throw msg;
				}
			} else {
				// -- iPhone/Android

				// Blacklist内のUserAgentかを判定
				if (bl.search(ua)) {
					// Blacklistに含まれている
					ret = this.BlackListPC;
					jp.co.disney.detect.debug.write("jp.co.disney.detect.UserAgentDispatcher.execute : result -> " + ret);
					if(funcBL_PC && this._isFunction(funcBL_PC)) {
						var funcName = this._getFunctionName(funcBL_PC);
						try {
							jp.co.disney.detect.debug.write("jp.co.disney.detect.UserAgentDispatcher.execute : function " + funcName + "() start");
							funcBL_PC();
							jp.co.disney.detect.debug.write("jp.co.disney.detect.UserAgentDispatcher.execute : function " + funcName + "() end");
						} catch (e) {
							var msg = "Error : function " + this._getFunctionName(funcBL_PC) + " : Exception Message (" + (e.message ? e.message : e) + ")";
							jp.co.disney.detect.debug.write(msg);
							return msg;
						}
					} else {
						var msg = "引数[funcBL_PC]が関数ではありません。funcBL_PC->" + (funcBL_PC ? funcBL_PC : "null");
						jp.co.disney.detect.debug.write("jp.co.disney.detect.UserAgentDispatcher.execute : " + msg);
						throw msg;
					}
				} else {
					// Blacklistに含まれていない
					ret = this.SmartPhone;
					jp.co.disney.detect.debug.write("jp.co.disney.detect.UserAgentDispatcher.execute : result -> " + ret);
					if(funcSP && this._isFunction(funcSP)) {
						var funcName = this._getFunctionName(funcSP);
						try {
							jp.co.disney.detect.debug.write("jp.co.disney.detect.UserAgentDispatcher.execute : function " + funcName + "() start");
							funcSP();
							jp.co.disney.detect.debug.write("jp.co.disney.detect.UserAgentDispatcher.execute : function " + funcName + "() end");
						} catch (e) {
							var msg = "Error : function " + this._getFunctionName(funcSP) + " : Exception Message (" + (e.message ? e.message : e) + ")";
							jp.co.disney.detect.debug.write(msg);
							return msg;
						}
					} else {
						var msg = "引数[funcSP]が関数ではありません。funcSP->" + (funcSP ? funcSP : "null");
						jp.co.disney.detect.debug.write("jp.co.disney.detect.UserAgentDispatcher.execute : " + msg);
						throw msg;
					}
				}
			}

			return ret;

		} catch (e) {
			var msg = "Error : jp.co.disney.detect.UserAgentDispatcher.execute : Exception Message (" + (e.message ? e.message : e) + ")";
			jp.co.disney.detect.debug.write(msg);
			return msg;
		}
	}

	,_isFunction : function (func) {
		if (typeof func == "function") {
			return true;
		} else {
			return false;
		}
	}

	,_getFunctionName : function (func) {
		if (this._isFunction(func)) {
			return func.toString().match(/function[ ]+([a-zA-Z0-9_]+)/)[1];
		} else {
			return func ? func.toString() : "null";
		}
	}
};

