$(function () {
    if (location.hash == '#LIFEENTAME') {
        Tab("LIFEENTAME");
    }
    else if (location.hash == '#BIZIT') {
        Tab("BIZIT");
    }
    else if (location.hash == '#KONETA') {
        Tab("KONETA");
    }
    else{
        Tab("PCDIGITAL");
    }
});

function Tab(chsname) {
    //getPicad(chsname.toLowerCase());
    getTieup(chsname.toLowerCase());
    $(".chs").filter(":not(.chs-" + chsname + ")").hide();
    $(".chs").filter(".chs-" + chsname).show();

    $("#navbar li > a").removeClass("navbar-selected");
    $("#navbar-" + chsname).addClass("navbar-selected");

    if(chsname == "PCDIGITAL") {
      $(".slider-pagination a").eq(0).click();
    }else if(chsname == "BIZIT") {
      $(".slider-pagination a").eq(2).click();
    }else if(chsname == "LIFEENTAME"){
      $(".slider-pagination a").eq(4).click();
    }else {
      $(".slider-pagination a").eq(6).click();
    }
    return false;
}

function getTieup(chtname) {
    $("li.tieup").hide();
    var rand = Math.floor(Math.random()*1000);
    var url = "tieup.php?ch="+chtname+"&rand="+rand;
    iframe_ajax(url, function (html) {
        json = JSON.parse(html);
        if (json.clickurl) {
            $("li.tieup a").attr("href",json.clickurl.replace(/amp;/g,''));
            $("li.tieup a > img").attr("src","/thumbs/journal.mycom.co.jp"+json.indextop);
            $("li.tieup a > h4").text(json.title);
            $("li.tieup").show();
        }
    }, "input-value");
}

function getPicad_old(chpname){
    var adspace = $("div#adspace");
    //adspace.hide();
    adspace.slideUp(500, function () {
        var rand = Math.floor(Math.random()*1000);
        var url = "adpic.php?ch="+chpname+"&rand="+rand;
        iframe_ajax(url, function (html) {
            if (html) {
                $("body", html).children().appendTo(adspace);
                    //adspace.empty().append(html).ready(function () {
                        alert(html);
                        //adspace.show();
                    //    adspace.slideDown(500);
                    //});
            }
        }, "body");
    });
}

function getPicad(chpname){
    var adspace = $("iframe#adiframe");
    var rand = Math.floor(Math.random()*1000);
    var url = "adpic.php?ch="+chpname+"&rand="+rand;
    adspace.load(function () {
        if (!$("body", adspace.contents()).html()) {
            adspace.hide();
        } else {
            var h = $("body iframe", adspace.contents()).height();
            if (h) {
                adspace.height(h);
            }
            else {
                adspace.height(50);
            }
        }
    });
    adspace.attr("height", 0);
    adspace.show();
    adspace.attr("src", url);
}

/**
 * もっと読む機能
 */
function readmore(ch_group)
{
    // 最後尾の記事LIをの算出
    var last_article_list_item = $("#new-article-" + ch_group + "> li[articleid]:last");
    var maxid = last_article_list_item.attr("articleid");

    // iFrame AJAXでリストに過去記事を追加
    var url = "readmore.php?ch_group=" + ch_group + "&maxid=" + maxid;

    //$("#loading .loadimg").css({top: $(window).scrollTop()+$(window).height()/2});

    //$("#loading").css({width: $("body").width(),
    //                   height: $("body").height()
    //                  }).show();

    $(".mj-articles li.readmore div.bl, .mj-articles li.readmore .load").show();

    iframe_ajax(url, function (html) {
        $(last_article_list_item).after(html);
        init_article_ongesturechange();
        //$("#loading").hide();
        $(".mj-articles li.readmore div.bl, .mj-articles li.readmore .load").hide();
    });

    // リンクアクションをキャンセル
    return false;
}

