var itemFetch = function ( type, livedoor_id, item_id, row ) {
    this.livedoor_id = livedoor_id;
    this.item_id = item_id;
    this.type = type;
    this.row = row;
    var obj = this;
    window.setTimeout(function () { obj.fetch() }, 100);
};
itemFetch.prototype = {
    fetch : function () {
        var api_url;
        api_url = '/_/api/item/get?livedoor_id='+this.livedoor_id+'&item_id='+this.item_id+'&row='+this.row+'&escape=1';
        var template_url = '/js/template/profile/item/'+this.type+'.txt?'+Math.random().toString(36).slice(2);
        var elm_id = '#item-'+this.item_id;
        $.getJSONWait(
            api_url, {cache: false}, 20,
            function (json) {
                $(elm_id).setTemplateURL(template_url).empty().processTemplate(json);
                $("div.item", $(elm_id)).each(function () {
                    $("div.entry:first", $(this)).each(function () {
                        $("span.arrow", $(this)).addClass("opened").click(function () {
                            $(this).toggleClass("opened").parents("div.entry").children("p.description").toggle();
                        });
                    }).nextAll().each(function () {
                        $("p.description", $(this)).hide();
                        $("span.arrow", $(this)).click(function () {
                            $(this).toggleClass("opened").parents("div.entry").children("p.description").toggle();
                        });
                    });
                });
            });
    }
};
$(function () {
    $("div.item", "div#profile").each(function () {
        $("div.entry:first", $(this)).each(function () {
            $("span.arrow", $(this)).addClass("opened").click(function () {
                $(this).toggleClass("opened").parents("div.entry").children("p.description").toggle();
            });
        }).nextAll().each(function () {
            $("p.description", $(this)).hide();
            $("span.arrow", $(this)).click(function () {
                $(this).toggleClass("opened").parents("div.entry").children("p.description").toggle();
            });
        });
    });
    
    $("a#friendAddRemove").click(function () {
        if ($(this).hasClass("processing"))
            return;
        var parentElm = $(this).parent();
        if (parentElm.hasClass("addFavorite"))
            var act = "add";
        else
            var act = "remove";
        $(this).addClass("processing").html("　処理中...　　　");
        $.getJSON(
            "/_/internal_api/friend/"+act,
            {
                "target_id" : target_id,
                "rkey" : rkey
            },
            function (json) {
                if (json.status == "success") {
                    if (act == "add") {
                        parentElm.addClass("removeFavorite").removeClass("addFavorite");
                        $("a#friendAddRemove", parentElm).removeClass("processing").html("お気に入りから削除");
                    } else {
                        parentElm.addClass("addFavorite").removeClass("removeFavorite");
                        $("a#friendAddRemove", parentElm).removeClass("processing").html("お気に入りに追加");
                    }
                } else {
                    alert("処理に失敗しました");
                    $("a#friendAddRemove", parentElm).removeClass("processing").html("お気に入りに追加/削除");
                }
            }
        );
    });
});
