// いろんな汎用処理を入れてあります。

// URLを分割する
function splitUrl() {
    var result_a = new Array();
    var varStr = window.location.search.substring(1);
    var para_a = varStr.split("&");
    for (i = 0; i < para_a.length; i++) {
        var var_a = para_a[i].split("=");
        result_a[var_a[0]] = var_a[1];
    }

    return result_a;
}

// 配列をシャッフルする
function randomArray(ary_a, count) {
    if (!(count) || count > ary_a.length) {
        count = ary_a.length;
    }
    var hash = new Array();
    var result_a = new Array();
    for (i = 0; i < count; i++) {
        var r = dropDice(ary_a.length - i);
        var ix = 0;
        for (j = 0; j < ary_a.length; ix++) {
            if (hash[ix] > 0) {
                continue;
            }
            if (j == r) {
                hash[ix] = i+1;
                result_a.push(ix);
                break;
            }
            j += 1;
        }
    }
    for (i = 0; i < result_a.length; i++) {
        result_a[i] = ary_a[result_a[i]];
    }

    return result_a;
}

// 配列の要素を取り除く
function removeArray(ary, value) {
    var result = new Array();
    for (var i = 0; i < ary.length; i++) {
        if (ary[i] != value) {
            result.push(ary[i]);
        }
    }
    return result;
}

// 配列を検索する
function searchArray(ary, value) {
    for (var i = 0; i < ary.length; i++) {
        if (ary[i] == value) {
            return i;
        }
    }
    return -1;
}

// ダイスを振る - Dashboard がいつも同じ乱数を返すのでミリ秒を足す
function dropDice(mensu) {
    var result = parseInt(Math.random() * mensu);
	result += (new Date()).getTime();
	result %= mensu;

    return result;
}

// 透明化アイコン(Widgetのiアイコンのようなもの)の管理クラス
function OpacityIcon(elm, path) {
    this.dispmode = true;
    this.opavalue = 0;
    this.elm = elm;
    this.path = path;
    this.timer = null;

    this.timeout = function() {
        if (this.dispmode) {
            if (this.opavalue >= 1) {
                this.timer = null;
                return;
            }
            this.opavalue += 0.1;
        } else {
            if (this.opavalue <= 0) {
                this.timer = null;
                return;
            }
            this.opavalue -= 0.1;
        }
        if (this.opavalue < 0.1) {
             this.elm.style.opacity = 0;
           this.elm.style.display = "none";
        } else {
             if (this.elm.style.display != "block") {
                this.elm.style.display = "block";
            }
            this.elm.style.opacity = this.opavalue;
        }
        this.timer = setTimeout(this.path + ".timeout()", 100);
    }

    this.start = function(dispmode) {
        this.dispmode = dispmode;
        if (this.timer == null) {
            this.timer = setTimeout(this.path + ".timeout()", 100);
        }
    }
}

// Cookieに保存する
function setCookie(key, value) {
    document.cookie
            = key + "=" + escape(value)
            + "; expires=Tue, 31 Dec 2030 23:59:59;";
}

// Cookieから取り出す
function getCookie(key) {
    var mycookie = document.cookie + ";";
    var ix = mycookie.indexOf(key + "=");
    var str = mycookie.substring(ix + key.length + 1, mycookie.length);
    ix = str.indexOf(";");
    var value = unescape(str.substring(0, ix) );
    return value;
}

// URLを開く（widgetでブラウザに表示）
function openURL(url) {
    if (window.widget) {
        widget.openURL(url);
    } else {
        location.href = url;
    }
}

// 設定値を保存する（CookieとWidgetのPreferenc両用)
function saveValue(key, value) {
    if (window.widget) {
        widget.setPreferenceForKey (value, key);
    } else {
        setCookie(key, value);
    }
}

// 設定値を取り出す（CookieとWidgetのPreferenc両用)
function loadValue(key) {
    var result = null;
    if (window.widget) {
        result = widget.preferenceForKey (key);
    } else {
        result = getCookie(key);
    }
    return result;
}

function clone(obj) {
	var result = new Object();
	for (prop in obj) {
		result[prop] = obj[prop];
	}
	return result;
}

function isIE() {
    if( document.implementation && document.implementation.createDocument ) {
        return false;
    }
    return true;
}

function isKonqueror() {
    var ua = navigator.userAgent;
    return (ua.indexOf("Konqueror") != -1);
}

function isSafari() {
    // とりあえずDashboardはブラウザの振舞としてはSafariと見做す
    if (window.widget) {
        return true;
    }
    var ua = navigator.userAgent;
    return (ua.indexOf("Safari") != -1);
}

function isIPhone() {
    var ua = navigator.userAgent;
    return (ua.indexOf("iPhone") != -1);
}
