//<script type="text/javascript">
    _ba_location = ('https:' === document.location.protocol ? 'https://' : 'http://') + 'gooddo.jp/gd2/lb/ba/';
    _ba_kvs = {};
    _ba_sync_stat = {};
	_uid = "02cfea4227754f90b346014341098150";

	//console.log("uid=" + _uid);
    //オブジェクト型判定
    function classOf(obj) {
        if ((typeof obj) == "object") {
            if (obj.length != undefined) {
                return "array";
            } else {
                for (t in obj) {
                    if (obj[t] != undefined) {
                        return "hash";
                    } else {
                        return "object";
                    }
                }
            }
        } else {
            return (typeof obj);
        }
    }

    //全てのリクエストのコントローラー
    function ba(type, action, option, location) {

        if (type === 'undefined') { //typeなければさすがに失敗
            return false;
        }

        if (typeof action === 'undefined' || action === null) { //ユーザー定義なし
            action = '';
        }

        if (typeof option === 'undefined' || classOf(option) !== "hash") { //オプション指定なしか変な値
            option = null;
        }

        if (type === 'set') { //送られてきたハッシュをセットして終了
            if (option === null) {
                return false;
            }else{
                ba_set(action, option);
            }
            return true;
        }

        if (type === 'send') { //送られてきたハッシュをセット
            if (option !== null) {
                ba_set(action, option);
            }
        }

		var canonical_url = $("link[rel=canonical]").attr("href");
		if(canonical_url === null || typeof canonical_url === 'undefined'){
			canonical_url = "";
		}

		var window_size = "";
		var windows_width = $(window).width();
		var windows_height = $(window).height();
		if(typeof windows_width != 'undefined' && typeof windows_height != 'undefined'){
			window_size = windows_width + 'x' + windows_height;
		}

        param_hash = {
            "_t": type,
            "_a" : action,
            "_u" : window.location.href,
            "_r" : document.referrer,
			"_cu" : canonical_url,
			"_pt" : document.title,
 			"_ws" : window_size,
        };

        //ハッシュからクエリストリング作成
        if (('' in _ba_kvs)) { //全てに適用する値
            param_hash = ba_ar_merge(param_hash, _ba_kvs['']);
        }

        if (action !== '' && (action in _ba_kvs)) { //アクション名指定のとき上書き
            param_hash = ba_ar_merge(param_hash, _ba_kvs[action]);
        }

        if (type === 'view') {

            ba_async(param_hash);
            return true;

        } else if (type === 'link') {
            //url追加
            param_hash["_f"] = option['href'];
//			ba_sync(param_hash);

            ba_async(param_hash);

            var get_url = _ba_location + '?' + ba_query(param_hash);

            var i = 0;
            var timer = setInterval(function () {
                res = _ba_sync_stat[get_url];
                console.log('[BA] ■res=>' + res);
                console.log('[BA] i=' + i);
                if (i >= 10 || typeof res !== 'undefined' && res === true) {
                    clearInterval(timer);
//					location.href = param_hash["_f"];
                    if (param_hash["_f"]) {
                        if (option['target']) {
                            console.log('[BA] ■targetジャンプ！' + param_hash["_f"]);
                            window.open(option['href'], option['target'], option['option']);
                        } else {
                            console.log('[BA] ■ジャンプ！' + param_hash["_f"]);
                            window.location = option['href'];
                        }
                    }
                } else {
                    console.log('[BA] ■skip ' + i);
                }
                ++i;
            },500);

            return true;

        } else if (type === 'send') {

            ba_async(param_hash, location);
            return true;

        } else {

            return false;

        }

        return false;
    }


    //ハッシュ値を上書き
    function ba_set(action, param_hash) {

        if (!(action in _ba_kvs)) {
            _ba_kvs[action] = {};
        }

        for (var key in param_hash) {
            _ba_kvs[action][key] = param_hash[key];
        }

    }

    //非同期通信
    function ba_async(param_hash, location) {

        if (typeof location === "undefined") {
            location = _ba_location;
        }
        var get_url = location + '?' + ba_query(param_hash);


        _ba_sync_stat[get_url] = null;

        var xhr = new XMLHttpRequest();
        xhr.ontimeout = function () {
            console.error("[BA] The request for " + location + " timed out.");
        };
        xhr.onload = function () {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    _ba_sync_stat[get_url] = true;
                    console.log("[BA] result =>" + xhr.responseText);
                } else {
                    _ba_sync_stat[get_url] = false;
                    console.error("[BA] The request for " + location + " http status is not 200.");
                }
            }
        };
        xhr.open("POST", location, true);
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xhr.timeout = 30 * 1000;
        xhr.withCredentials = true;
        xhr.send(ba_query(param_hash));

    }


    function ba_query(param_hash) {

        var query = '';
        var i = 0;
        for (var key in param_hash) {
            if (i === 0) {
//				query = query + '?';
            } else {
                query = query + '&';
            }
            query = query + key + '=' + encodeURIComponent(param_hash[key]).replace(/%20/g, '+');
            ++i;
        }
        return query;
    }

    //配列をマージする(原則上書き)
    function ba_ar_merge(src_ar, add_ar, overwride) {

        if (typeof overwride === 'undefined' || overwride !== false) {
            overwride = true;
        }

        for (var key in add_ar) {
            if ((key in src_ar) && overwride === false) {

            } else {
                src_ar[key] = add_ar[key];
            }
        }
        return src_ar;
    }

	var ba_params = {};
	function ba_param(name){
		var ba_str = $.cookie('_ba');
		if(!ba_str || typeof ba_str !== 'undefined'){
			ba_params = JSON.parse(ba_str);
		}
		if(name in ba_params){
			return ba_params[name];
		}else{
			return null;
		}
	}

//</script>


