(function(){
    var container = $("script:last").parents("div:first");
    $(function(){
        var campaignId = "";

        if ($("#hidTargetCampaignId", container).val() != undefined) {
            campaignId = $("#hidTargetCampaignId", container).val();
        }

        ajaxRequest(getHost() + "/page/campaignGoods/index",
                    "CampaignGoodsLimit=" + $("#hidCampaignGoodsCount", container).val()+"&ImageSize="+$("#hidCampaignGoodsIcnSize", container).val() + "&targetCampaignId=" + campaignId,

                    function(backdata){campaignGoods_disp_callback2(backdata,container);});
    });
})();

function campaignGoods_disp_callback2(data,objParents) {
    var intCount = data.intCount;    //データ件数取得
    var intLoop=0;
    var intMaxIconCount=4;           //アイコンの最大表示数

    //ベース要素の取得・破棄
    var elm = $(".carousel>ul",objParents).children().clone();    //ベース要素を取得
    var wkElm='';
    $(".carousel>ul",objParents).children().remove();            //ベース要素を破棄

    //データの有無をチェック
    if (data.flag == 0 || data == null || data == "") {

        //データが無いとき
        $('.carousel>ul',objParents).hide();        //商品表示欄を隠す
        //メッセージを表示
        $('.Cerrortext>p',objParents).text(data.msg);

    } else {

        var baseWidth = $('.carousel>ul', objParents).innerWidth();
        //データが有るとき
        $('.Cerrortext',objParents).remove();        //エラー表示欄を削除

        $('#hidCampaignGoodsCount',objParents).val(intCount);    //データ件数設定

        var campaignInfo = data.campaignInfo;

        //表示可能データ件数とデータ件数を比較してループ回数を決定する
        if (intCount >= parseInt($("#hidCampaignGoodsVisible", objParents).attr('value'))) {
            //表示可能データ件数分ループ
            intLoop = parseInt($("#hidCampaignGoodsVisible", objParents).attr('value'));
        }
        else {
            //データ件数分ループ
            intLoop=intCount;
        }
        for (var i = 0; i < intLoop; i++) {
            //ワーク変数にベース領域をコピー
            wkElm=$(elm).clone();

            //アイコン設定
            var intCountIcon = campaignInfo[i]["ICON"].length;

            if(intCountIcon>0){
                //アイコンが有るときはアイコンを設定
                //アイコンは4つを最大とする

                if(intCountIcon>=intMaxIconCount){
                    intCountIcon=intMaxIconCount;
                }

                for (var j = 0; j < intCountIcon; j++){
                    if (j > 0) {
                        //アイコンを１つのみ表示するときは以下はコメントアウト
                        var icnElem = $(".icn>span:first",wkElm).clone();
                        $('img',icnElem).attr('src',campaignInfo[i]["ICON"][j]);
                        $(".icn",wkElm).parent().append(icnElem);
                    }
                    else {
                        $(".icn>span>img",wkElm).attr('src',campaignInfo[i]["ICON"][j]);
                    }
                }
            }
            else{
                //アイコンが無いときは領域を削除
                $(".icn",wkElm).remove();
            }


            //画像設定
            var goodsImage = campaignInfo[i]["IMAGE_FULLPATH"];
            $('.photo>a',wkElm).attr('href',getOriginalHost() + "/item/"+campaignInfo[i]["GOODS_CODE"] + "/");
            $('.photo>a>img',wkElm).attr('src',goodsImage);

            //ランキングアイコン設定
            var intCountIcon2 = campaignInfo[i]["RAKICON"].length;

            if(intCountIcon2 > 0){
                for(var k=0;k<intCountIcon2;k++){
                    if(k>0){
                        var ricnElem = $(".ricn:first",wkElm).clone();
                        ricnElem.attr('src',campaignInfo[i]["RAKICON"][k]);
                        $(".ricn:first",wkElm).parent().append(ricnElem);
                    }
                    else{
                        $(".ricn",wkElm).attr('src',campaignInfo[i]["RAKICON"][k]);
                    }
                }
            }
            else{
                //ランキングアイコンが無いときは領域を削除
                $('.ricn',wkElm).remove();
            }

            //名称設定
            $('.name>a',wkElm).attr('href',getOriginalHost() + "/item/"+campaignInfo[i]["GOODS_CODE"] + "/");
            $('.name>a',wkElm).text(htmlspecialchars_decode(campaignInfo[i]["GOODS_NAME"]));

            //値段設定
            $('.price',wkElm).text(campaignInfo[i]["SALES_PRICE"]);

            //要素を親に追加
            $(".carousel>ul",objParents).append(wkElm);

            // サイズ調整
            var width = baseWidth - $('.photo', wkElm).outerWidth({margin: true});
            var icn = $('.icn',wkElm);

//            console.log(icn.css("margin-right"));
            icn.outerWidth(width);
            $('.name',wkElm).outerWidth(width);
            $('.price',wkElm).outerWidth(width);
            // ネガティブマージン対策
            icn.outerWidth(width - (icn.outerWidth({margin: true}) - icn.innerWidth()));
        }
    }
}
