/**
 *  init
 */
jQuery(document).ready(function($) {
    if (document.body.id == 'kakaku') {
        // init Tab
    }
});

/*
 *  タブブラウザ
 */
var current_tabs = [];
var current_pos = -1;
var pos_offset = 3;    // パーツ選択、合計表示、価格.com用ダミーの3フレーム分

/**
 *  タブを開く
 */
function open_tab(parts_type, item_id, url, open_background) {
    var pos = tabIndexOf(item_id);
    if (pos != -1) {
        focus_tab(pos);
    } else {
        create_tab(parts_type, item_id, url, open_background);
    }
    $("#no_selected").hide();
}

/**
 *  タブを閉じる
 */
function close_tab(closebutton) {
    var linktag = closebutton.parentNode;
    var item_id = linktag.href.replace(/[^\d]/g, "");
    var pos = tabIndexOf(item_id);

    // 本体（フレーム）削除
    var frameset = parent.document.getElementById("topframe");
    $(frameset).children("frame").eq(pos + pos_offset).remove();
    frameset.rows = get_frame_rows(pos + pos_offset - 1);

    // ダミー本体を削除
    $("#tabbodys #tabbody_" + item_id).remove();

    // タブ削除
    $("#tabs > ul").tabs("remove", pos);
    if ($("#tabs > ul li").size() > 0) {
        if (pos == 0) {
            focus_tab(0);
        } else {
            focus_tab(pos - 1);
        }
    } else {
        $("#tabs > ul").tabs("destroy");
        $("#tabs > ul").empty();
        $("#no_selected").show();
    }

    // タブ情報削除
    current_tabs.splice(pos, 1);

    return false;
}

/**
 *  指定タブをフォーカス
 */
function focus_tab(pos) {
    current_pos = pos;
    var frameset = parent.document.getElementById("topframe");
    frameset.rows = get_frame_rows(pos + pos_offset);
    $("#tabs > ul").tabs("select", pos);
}

/**
 *  存在チェック
 */
function tabIndexOf(item_id) {
    var pos = 0;
    for (var i = 0; i < current_tabs.length; i++) {
        if (current_tabs[i].item_id == item_id) {
            return pos;
        }
        pos ++;
    }
    return -1;
}

/**
 *  タブを新規作成
 */
function create_tab(parts_type, item_id, url, open_background) {
    // ラベル

    // 挿入位置
    var pos = 0;
    var num = 1;
    $("#tabs ul li").each(function(i, element){
        var tabinfo = current_tabs[i];
        var tab_parts_type = parseInt(tabinfo.parts_type);
        if (tab_parts_type == parts_type) {
            num ++;
        }
        if (tab_parts_type <= parts_type) {
            pos ++;
        }
    });
    var newtab_url = "#tabbody_" + item_id;
    var newtab_name = parts_names[parts_type];
    if (num > 1) {
        newtab_name += ':' + num;
    }

    // 本体ダミー
    var tab_body_dummy = document.createElement("div");
    tab_body_dummy.id = "tabbody_" + item_id;
    $("#tabbodys").append(tab_body_dummy);

    // タブ管理配列にパーツ情報を挿入
    current_tabs.splice(pos, 0, {
        parts_type: parts_type,
        item_id:    item_id,
        url:        url
    });

    if ($("#tabs ul li").size() == 0) {
        // 一つ目
        current_pos = 0;
        var tab_label = document.createElement("li");
        $(tab_label).append('<a href="' + newtab_url + '"><span>' + newtab_name + '</span><font class="tabclose" onclick="close_tab(this)">x</font></a>');
        $("#tabs > ul").append(tab_label);
        $("#tabs > ul").tabs().bind('select.ui-tabs', function(e, ui) {
            var newpos = tabIndexOf(ui.panel.id.replace(/[^\d]/g, ""));
            try {
                var frameset = parent.document.getElementById("topframe");
                var frame = $(frameset).children("frame").eq(newpos + pos_offset);
                if (frame.attr("src") == "about:blank" || frame.attr("title") != "") {
                    frame.attr("src", frame.attr("title"));
                    frame.attr("title", "");
                }
            } catch (e) {alert(e);}
            if (current_pos != newpos) {
                focus_tab(newpos);
            }
        }).bind('add.ui-tabs', function(e, ui) {
            $(ui.tab).append('<font class="tabclose" onclick="close_tab(this)">x</font>');
        }).bind('load.ui-tabs', function(e, ui) {
            $('a', ui.panel).bind('click', function() {
                $(ui.panel).load(this.href);
                return false;
            });
        });

    } else {
        // 指定位置（パーツの種類順）
        $("#tabs > ul").tabs("add", newtab_url, newtab_name, pos);
    }

    // 本体フレーム
    var frameset = parent.document.getElementById("topframe");
    var new_frame;
    var flg_new = false;
    if (get_frame_count() > pos + pos_offset && current_tabs.length - 1 <= pos) {
        // すでにあるフレームを再利用
        new_frame = $(frameset).children("frame").get(pos + pos_offset);
    } else {
        // フレームを新規作成
        flg_new = true;
        new_frame = parent.document.createElement("frame");
    }

    // フレームの属性を設定
    new_frame.id = "tabbody_" + item_id;
    new_frame.name = new_frame.id;
    new_frame.border = 0;
    new_frame.className = "tabbody";
    new_frame.title = "";

    if (pos == 0 || !open_background) {
        if (jQuery.browser.opera) {
            // Operaの読み込みエラー対策
            setTimeout(function(){
                new_frame.src = url;
            }, 500);
        } else {
            new_frame.src = url;
        }
    } else {
        // 後読み
        new_frame.src = "about:blank";
        new_frame.title = url;
    }

    if (open_background) {
        // バックグラウンドで開く
        // IEのframes.length反映遅れ防止
        setTimeout(function(){
            frameset.rows = get_frame_rows(get_current_frame(), 1);
        }, 1);
    } else {
        // IEのframes.length反映遅れ防止
        setTimeout(function(){
            frameset.rows = get_frame_rows(pos + pos_offset, 1);
            // 追加したタブを表示
            setTimeout(function(){
                $("#tabs > ul").tabs("select", pos);
            }, 10);
        }, 1);
    }

    // 新規作成フレームをframesetに追加
    if (flg_new) {
        // Firefoxの画面フラッシュ防止
        setTimeout(function(){
            $(frameset).children("frame").eq(pos + pos_offset - 1).after(new_frame);
        }, 1);
    }
}
