
var rb = {
    activity: function(Id) {
        var rand = parseInt(Math.random()*1000);
        $.getJSON("/api/activity/read?token=AUTO&id=" + Id + "&rand=" + rand, function(json){
            if( json.is_success ) {
                location.href = json.activity.url;
            } else {
                alert( json.error || "更新に失敗しました" );
            }
        });
    },
    flag_show_hide: function(target) {
        if( $("#flag_"+target+"_t").attr("checked") ) {
            $("."+target).fadeIn("normal");
        } else {
            $("."+target).fadeOut("normal");
        }
    },
    go_top: function() {
        $("html,body").animate({ scrollTop: 0 }, "normal");
    },
    header_navi_active: function() {
        var path = location.pathname.match(/^\/?([^\/]*)/)[1] || "index";
        $("#nav_" + path).addClass("active");
    },
    hide_if_login: function(Id) {
        if( rb.is_login() ) $("#" + Id).hide();
    },
    input_date: function(Id) {
        var date = $("#" + Id).val();
        $("#" + Id).hide();
        $("#" + Id).after('<select id="' + Id + '_day"></select> 日 ');
        $("#" + Id).after('<select id="' + Id + '_month"></select> 月 ');
        $("#" + Id).after('<select id="' + Id + '_year"></select> 年 ');
        $("#" + Id + "_year").append('<option value="">↓選択</option>');
        var today = new Date();
        for( var i = 1900; i <= today.getFullYear(); i++ ) { $("#" + Id + "_year").append('<option value="' + i + '">' + i + '</option>'); }
        $("#" + Id + "_month").append('<option value="">↓選択</option>');
        for( var i = 1; i <= 12; i++ ) { $("#" + Id + "_month").append('<option value="' + i + '">' + i + '</option>'); }
        $("#" + Id + "_day").append('<option value="">↓選択</option>');
        for( var i = 1; i <= 31; i++ ) { $("#" + Id + "_day").append('<option value="' + i + '">' + i + '</option>'); }
        if( date ) {
            var d = date.split("-");
            $("#" + Id + "_year").val(parseInt(d[0],10));
            $("#" + Id + "_month").val(parseInt(d[1],10));
            $("#" + Id + "_day").val(parseInt(d[2],10));
        }
        $("#" + Id + "_year").change(function(){ $("#"+Id).val($("#"+Id+"_year").val()+"-"+$("#"+Id+"_month").val()+"-"+$("#"+Id+"_day").val()); });
        $("#" + Id + "_month").change(function(){ $("#"+Id).val($("#"+Id+"_year").val()+"-"+$("#"+Id+"_month").val()+"-"+$("#"+Id+"_day").val()); });
        $("#" + Id + "_day").change(function(){ $("#"+Id).val($("#"+Id+"_year").val()+"-"+$("#"+Id+"_month").val()+"-"+$("#"+Id+"_day").val()); });
    },
    init_flag_show_hide: function(target) {
        $("#flag_"+target+"_t").click( function(){ $.rb.flag_show_hide(target) } );
        $("#flag_"+target+"_f").click( function(){ $.rb.flag_show_hide(target) } );
        $.rb.flag_show_hide(target);
    },
    input_keyword: function(Id, msg) {
        var input = $("input#" + Id);
        if( ! input ) return;
        if( input.val() ) return;
        if( ! msg ) msg = "キーワードを入力してください";
        input.val(msg);
        input.addClass("input_keyword");
        input.focus(function(){
            $(this).removeClass("input_keyword");
            if( $(this).val() == msg ) $(this).val("");
        });
    },
    input_zip: function(Id) {
        var zip = $("#" + Id).val();
        $("#" + Id).hide();
        $("#" + Id).after('<input type="text" size="4" maxlength="4" id="' + Id + '_2" />');
        $("#" + Id).after('<input type="text" size="3" maxlength="3" id="' + Id + '_1" /> -');
        if( zip ) {
            var z = zip.split("-");
            $("#" + Id + "_1").val(z[0]);
            $("#" + Id + "_2").val(z[1]);
        }
        $("#" + Id + "_1").keyup(function(){ $("#"+Id).val($("#"+Id+"_1").val()+"-"+$("#"+Id+"_2").val()); });
        $("#" + Id + "_2").keyup(function(){ $("#"+Id).val($("#"+Id+"_1").val()+"-"+$("#"+Id+"_2").val()); });
    },
    is_login: function() {
        var token = $.cookie("token");
        return ( ! token || token == "" ) ? 0 : 1;
    },
    pochi: function(Id, statId) {
        if( ! statId ) statId = "pochi"
        var rand = parseInt(Math.random()*1000);
        $.getJSON("/pochi/" + Id + "?rand=" + rand, function(json){
            if( json.is_success ) {
                $("#" + statId).html(json.pochi).css("font-weight","bold")
                $("." + statId).show();
            //} else {
            //    alert( json.error || "更新に失敗しました" );
            }
            location.href = '/profile/' + Id + '/pochi/list';
        });
    },
    scroll_to: function(Id) {
        var top = $("#" + Id).position().top;
        if( top ) $("html,body").animate({ scrollTop: top }, "normal");
    },
    show_if_login: function(Id) {
        if( rb.is_login() ) $("#" + Id).show();
    },
    submit_keyword: function(FormId, InputId) {
        rb.input_keyword(InputId);
        $("#" + FormId).submit(function(){
            var input = $("#" + InputId);
            if( input.hasClass("input_keyword") ) input.val("");
        });
    },
    tabs: function(setting) {
        $.each(setting, function() {
            var item = this;
            $("#" + item.tab + " a")
            .removeAttr("href")
            .css("cursor","pointer")
            .click( function(){ $.rb.tabs_active(setting, item.tab) });
        });
    },
    tabs_active: function(setting, target) {
        $.each(setting, function() {
            if( this.tab == target ) {
                $("#" + this.tab).removeClass("active").addClass("active");
                $("#" + this.panel).fadeIn("normal");
            } else {
                $("#" + this.tab).removeClass("active");
                $("#" + this.panel).hide();
            }
        });
    },
    yummy: function(type, Id, statId) {
        if( ! statId ) statId = "yummy"
        var rand = parseInt(Math.random()*1000);
        $.getJSON("/yummy/" + type + "/" + Id + "?rand=" + rand, function(json){
            if( json.is_success ) {
                $("#" + statId).html(json.yummy).css("font-weight","bold");
                $("." + statId).show();
            } else {
                alert( json.error || "更新に失敗しました" );
            }
        });
    },
    nice_photo: function(type, Id, statId) {
        if( ! statId ) statId = "nice_photo"
        var rand = parseInt(Math.random()*1000);
        $.getJSON("/nice_photo/" + type + "/" + Id + "?rand=" + rand, function(json){
            if( json.is_success ) {
                $("#" + statId).html(json.nice_photo).css("font-weight","bold");
                $("." + statId).show();
            } else {
                alert( json.error || "更新に失敗しました" );
            }
        });
    }
};
$.extend({"rb": rb});

$(function() {
    $.rb.header_navi_active();
    //$.rb.hide_if_login("menuLogin");
    //$.rb.hide_if_login("nav_login");
    //$.rb.show_if_login("nav_logout");
    //$.rb.submit_keyword("header_form", "header_keyword");
    $(".pageTop a").removeAttr("href").css("cursor","pointer").click( function(){ $.rb.go_top() });
});

function goto_item(a,Id) {
    a.target = "_blank";
    a.href = "/go/item/" + Id;
}
function goto_blog(a,Id) {
    a.target = "_blank";
    a.href = "/go/blog/" + Id;
}
function goto_etc(a,Url,Id) {
    a.target = "_blank";
    a.href = "/go/etc?id=" + Id + "&url=" + Url;
}
function goto_recipe(a,Id) {
    a.href = "/go/recipe/" + Id;
}
function scroll_to(Id) {
    $.rb.scroll_to(Id);
}
function dialog_close() {
    window.parent.tb_remove();
}
function dialog_close_and_reload() {
    window.parent.location.reload();
}
function yummy_recipe(Id, statId) {
    $.rb.yummy("recipe", Id, statId);
}
function yummy_item(Id, statId) {
    $.rb.yummy("item", Id, statId);
}
function nice_photo_recipe(Id, statId) {
    $.rb.nice_photo("recipe", Id, statId);
}
function nice_photo_item(Id, statId) {
    $.rb.nice_photo("item", Id, statId);
}
function pochi(Id, statId) {
    $.rb.pochi(Id, statId);
}
function activity(Id) {
    $.rb.activity(Id);
}

function RandomAdNew(c,r,MES,HIT,NGID,ID) {
	m = 0;
	x = 0;
	y = 0;
	var Num = new Array();
	var Chk = new Array();

	if(r == 1){

		// 広告枠ごとの順番設定
		for (i=0;i<MES.length;i++) {
			m = 0, x = 0, y = 0;

			// 確率数の合計取得
			for(j=0; j<=HIT.length-1; j++) {
				if(Num[i] != j){
					m += HIT[j];
				}
			}

			// ランダム確率数取得
			n = Math.floor(Math.random() * m);
			n++;

			// ランダム確率数にヒットする広告枠の取得
			for(j=0; j<=HIT.length - 1; j++) {
				if(Num[i] != j){
					x = y;
					y += HIT[j];
					if(x<n && n<=y){
						Num[i] =j;
					}
				}
			}

			if (Chk[Num[i]] == 1){
				while(Chk[Num[i]] == 1){
					m = 0, x = 0, y = 0;
					for(j=0; j<=HIT.length-1; j++) {
						if(Num[i] != j){
							m += HIT[j];
						}
					}
					n = Math.floor(Math.random() * m);
					n++;

					for(j=0; j<=HIT.length-1; j++) {
						if(Num[i] != j){
							x = y;
							y += HIT[j];
							if(x<n && n<=y){
								Num[i] =j;
							}
						}
					}
				}
				Chk[Num[i]] = 1;
			}
			else {
				Chk[Num[i]] = 1;
			}
		}

		var NoID;
		for (i=0;i<c;i++){
			if(i==0){
				NoID = NGID[Num[i]];
			}
			if(NoID != '' && NoID==ID[Num[i]]){
				c++;
			} else {
				document.write(MES[Num[i]]);
			}
		}
	} else {
		for (i=0;i<c;i++){
			document.write(MES[i]);
		}
	}
}

// sidebanner

function SideBanner() {
    var MES = new Array();
    var HIT = new Array();
    var ADLOG = '/adlog.cgi';
    var RAND = 1;
    var NUM = 2;
    var URL = new Array();
    var IMG = new Array();
    var ALT = new Array();
    var ID = new Array();
    var TARGET = new Array();
    var NGID = new Array();
    

//media side banner start
/*
    URL.push('http://www.recipe-blog.jp/sp/110901acampaign');
    IMG.push('/sp/files/110901acampaign/300x90.jpg');
    ALT.push('朝ごはんレシピ大募集中！');
    ID.push('110901acampaign ');
    TARGET.push('');
    HIT.push(1);
    NGID.push('');
*/
//media side banner end



    URL.push('http://www.recipe-blog.jp/sp/r111205a_aw');
    IMG.push('/sp/files/r111205a_aw/300x90.jpg');
    ALT.push('');
    ID.push('r111205a_aw');
    TARGET.push('');
    HIT.push(1);
    NGID.push('');

    URL.push('http://www.recipe-blog.jp/sp/r120202a');
    IMG.push('/sp/files/r120202a/300x90.jpg');
    ALT.push('');
    ID.push('r120202a');
    TARGET.push('');
    HIT.push(1);
    NGID.push('');

    URL.push('http://www.recipe-blog.jp/sp/r120201a');
    IMG.push('/sp/files/r120201a/300x90.jpg');
    ALT.push('');
    ID.push('r120201a');
    TARGET.push('');
    HIT.push(1);
    NGID.push('');

    URL.push('http://www.recipe-blog.jp/sp/r120201b');
    IMG.push('/sp/files/r120201b/300x90.jpg');
    ALT.push('');
    ID.push('r120201b');
    TARGET.push('');
    HIT.push(3);
    NGID.push('');

/*
    URL.push('http://www.recipe-blog.jp/sp/r120119a');
    IMG.push('/sp/files/r120119a/300x90.jpg');
    ALT.push('');
    ID.push('r120119a');
    TARGET.push('');
    HIT.push(1);
    NGID.push('');
*/


    URL.push('http://www.recipe-blog.jp/special/spiceblog/');
    IMG.push('/sp/files/spiceblog/300x90.jpg');
    ALT.push('');
    ID.push('spiceblog');
    TARGET.push('');
    HIT.push(1);
    NGID.push('');






    for(i=0;i<HIT.length;i++){
        MES[i]='<li ><a href="http://www.recipe-blog.jp/re.cgi?l=' + ID[i] + '&f=sidebanner&url=' + URL[i] + '" ' + TARGET[i] + '><img alt="' + ALT[i] + '" border="0" src="' + IMG[i] + '" width="300" height="90"></a><img src="' + ADLOG + '?referer=' + document.referrer + '&id=' + ID[i] + '&f=sidebanner" width="1" height="1"></li>';
    }
    return {NUM: NUM, RAND: RAND, MES: MES, HIT: HIT, NGID: NGID, ID: ID};
}

// mega banner

function MegaBanner() {
    var MES = new Array();
    var HIT = new Array();
    var ADLOG = '/adlog.cgi';
    var RAND = 1;
    var NUM = 2;
    var URL = new Array();
    var IMG = new Array();
    var ALT = new Array();
    var ID = new Array();
    var TARGET = new Array();
    var NGID = new Array();
    
    

/*　
メガバナーレギュレーション変更　max　5本から　8本に変更
途中で表示率を上げた場合は、その分は本数に追加する。
ex.1本のバナーのみ回っていて、それを3倍の表示率に変更したら、
　　3本分としてカウントする。　　110413　篠原（りささん、ゆうこさん確認済み）

*/


//media mega banner start
/*
    URL.push('http://www.recipe-blog.jp/sp/111114campaign');
    IMG.push('/sp/files/111114campaign/300x150.jpg');
    ALT.push('Facabookお料理コミュニティがオープン♪');
    ID.push('111114campaign ');
    TARGET.push('');
    HIT.push(1);
    NGID.push('');

    URL.push('http://www.recipe-blog.jp/sp/110901acampaign');
    IMG.push('/sp/files/110901acampaign/300x150.jpg');
    ALT.push('朝ごはんレシピ大募集中！');
    ID.push('110901acampaign ');
    TARGET.push('');
    HIT.push(1);
    NGID.push('');
*/


//media mega banner end

//mega banner only (no side banner) start



//mega banner only (no side banner) end



    URL.push('http://www.recipe-blog.jp/sp/r111205a_aw');
    IMG.push('/sp/files/r111205a_aw/300x150.jpg');
    ALT.push('');
    ID.push('r111205a_aw');
    TARGET.push('');
    HIT.push(1);
    NGID.push('');

    URL.push('http://www.recipe-blog.jp/sp/r120202a');
    IMG.push('/sp/files/r120202a/300x150.jpg');
    ALT.push('');
    ID.push('r120202a');
    TARGET.push('');
    HIT.push(1);
    NGID.push('');

    URL.push('http://www.recipe-blog.jp/sp/r120201a');
    IMG.push('/sp/files/r120201a/300x150.jpg');
    ALT.push('');
    ID.push('r120201a');
    TARGET.push('');
    HIT.push(1);
    NGID.push('');

    URL.push('http://www.recipe-blog.jp/sp/r120201b');
    IMG.push('/sp/files/r120201b/300x150.jpg');
    ALT.push('');
    ID.push('r120201b');
    TARGET.push('');
    HIT.push(3);
    NGID.push('');

/*
    URL.push('http://www.recipe-blog.jp/sp/r120119a');
    IMG.push('/sp/files/r120119a/300x150.jpg');
    ALT.push('');
    ID.push('r120119a');
    TARGET.push('');
    HIT.push(1);
    NGID.push('');
*/


    URL.push('http://www.recipe-blog.jp/special/spiceblog/');
    IMG.push('/sp/files/spiceblog/300x150.jpg');
    ALT.push('');
    ID.push('spiceblog');
    TARGET.push('');
    HIT.push(1);
    NGID.push('');
    














    for(i=0;i<HIT.length;i++){
        MES[i]='<li style="margin:0 10px 19px 0; display:inline; float:left;"><a href="http://www.recipe-blog.jp/re.cgi?l=' + ID[i] + '&f=megabanner&url=' + URL[i] + '" ' + TARGET[i] + '><img alt="' + ALT[i] + '" border="0" src="' + IMG[i] + '" width="300" height="150"></a><img src="' + ADLOG + '?referer=' + document.referrer + '&id=' + ID[i] + '&f=megabanner" width="1" height="1"></li>';
    }

    return {NUM: NUM, RAND: RAND, MES: MES, HIT: HIT, NGID: NGID, ID: ID};
}







// top right media banner

function TopRightMediaBanner() {
    var MES = new Array();
    var HIT = new Array();
    var ADLOG = '/adlog.cgi';
    var RAND = 1;
//    var NUM = 2;				// 110124 change
    var NUM = 1;
    var URL = new Array();
    var IMG = new Array();
    var ALT = new Array();
    var ID = new Array();
    var TARGET = new Array();
    var NGID = new Array();
    
    
　　//　クリック数は集計するかもしれないが、PV（インプレッションは必要なし　ちえさん確認済み11/30）
　　
	// 集計する際はflag に media_side_bn が立つ
	
	//　バナーが2本未満になったら、/nfs/storage/recipeblog/www.recipe-blog.jp/inc/2010/top_right_otanoshimi_corner.inc　を書き換える
	
	

    URL.push('http://www.recipe-blog.jp/feature/2012spring/');
    IMG.push('/feature/2012spring/300x90.jpg');
    ALT.push('');
    ID.push('feature2012spring');
    TARGET.push('');
    HIT.push(1);
    NGID.push('');

    URL.push('http://www.recipe-blog.jp/sp/120215campaign');
    IMG.push('/sp/files/120215campaign/300x90.jpg');
    ALT.push('');
    ID.push('120215campaign');
    TARGET.push('');
    HIT.push(1);
    NGID.push('');

    URL.push('http://www.recipe-blog.jp/greetingcard/');
    IMG.push('/greetingcard/300x90.jpg');
    ALT.push('');
    ID.push('greetingcard');
    TARGET.push('');
    HIT.push(1);
    NGID.push('');

    URL.push('http://www.recipe-blog.jp/sp/111114campaign');
    IMG.push('/sp/files/111114campaign/300x90.jpg');
    ALT.push('Facabookお料理コミュニティがオープン♪');
    ID.push('111114campaign');
    TARGET.push('');
    HIT.push(1);
    NGID.push('');
	
/*    
    URL.push('http://www.recipe-blog.jp/sp/110901acampaign');
    IMG.push('/sp/files/110901acampaign/300x90.jpg');
    ALT.push('');
    ID.push('110901acampaign ');
    TARGET.push('');
    HIT.push(1);
    NGID.push('');

    URL.push('http://www.recipe-blog.jp/sp/110623campaign');
    IMG.push('/sp/files/110623campaign/300x90.jpg');
    ALT.push('');
    ID.push('110623campaign');
    TARGET.push('');
    HIT.push(1);
    NGID.push('');
    

    URL.push('http://www.recipe-blog.jp/okaimono/enq2011');
    IMG.push('/sp/files/okaimono/enq2011/300x90.jpg');
    ALT.push('');
    ID.push('');
    TARGET.push('');
    HIT.push(1);
    NGID.push('');

    URL.push('http://www.recipe-blog.jp/movie/');
    IMG.push('/static/images/top/bnr20110222.gif');
    ALT.push('レシピブログチャンネル');
    ID.push('');
    TARGET.push('');
    HIT.push(1);
    NGID.push('');
*/






    for(i=0;i<HIT.length;i++){
//        MES[i]='<li><a href="' + URL[i] + '"><img src="' + IMG[i] + '" alt="' + ALT[i] + '" /></a><!--img src="' + ADLOG + '?referer=' + document.referrer + '&id=' + ID[i] + '&f=media_bn" width="1" height="1"--></li>';
		MES[i]='<p class="menuGuide2"><a href="http://www.recipe-blog.jp/re.cgi?l=' + ID[i] + '&f=media_side_bn&url=' + URL[i] + '"><img src="' + IMG[i] + '" alt="' + ALT[i] + '" width="300" height="90" /></a></p><!--img src="' + ADLOG + '?referer=' + document.referrer + '&id=' + ID[i] + '&f=media_bn" width="1" height="1"--></li>';
		
		
    }





    return {NUM: NUM, RAND: RAND, MES: MES, HIT: HIT, NGID: NGID, ID: ID};
}




////////////////////////
// ad banner
// adlantis 不具合発生など、緊急時専用

//スーパーバナー
function SpbnNoSys() {
	
	var MES = new Array();
	var HIT = new Array();
	var ADLOG = '/adlog.cgi';
	var RAND = 1;
	var NUM = 1;
	var URL = new Array();
	var IMG = new Array();
	var ALT = new Array();
	var ID = new Array();
	var TARGET = new Array();
	var NGID = new Array();



	URL.push('http://www.otoriyose.net/ranking/');
	IMG.push('http://static.adlantis.jp/banner_ads/0009/5841/76f116bba113b9f9673f7e98abf7db74ee6fc34f.jpg');
	ALT.push('');
	ID.push('default');
	TARGET.push('target="_blank"');
	HIT.push(1);
	NGID.push('');


	URL.push('http://www.asajikan.jp/asabijin/');
	IMG.push('http://static.adlantis.jp/banner_ads/0002/9542/7605870a150daf83dfe9f4097656c006ac880636.jpg');
	ALT.push('');
	ID.push('default');
	TARGET.push('target="_blank"');
	HIT.push(1);
	NGID.push('');


	URL.push('http://www.kosodate-style.jp/style/ ');
	IMG.push('http://static.adlantis.jp/banner_ads/0011/9698/db34a886a67cc1d89d593b90b93ac1687fe6e755.jpg');
	ALT.push('');
	ID.push('default');
	TARGET.push('target="_blank"');
	HIT.push(1);
	NGID.push('');











	for(i=0;i<HIT.length;i++){
		MES[i]='<a href="http://www.recipe-blog.jp/re.cgi?l=' + ID[i] + '&f=spbn&url=' + URL[i] + '" ' + TARGET[i] + '><img alt="' + ALT[i] + '" border="0" src="' + IMG[i] + '" width="728" height="90"></a><img src="' + ADLOG + '?referer=' + document.referrer + '&id=' + ID[i] + '&f=adlog_spbn" width="1" height="1">';
		
	}

	return {NUM: NUM, RAND: RAND, MES: MES, HIT: HIT, NGID: NGID, ID: ID};


}




// ワイドレクタングル
function RectNoSys() {
	
	var MES = new Array();
	var HIT = new Array();
	var ADLOG = '/adlog.cgi';
	var RAND = 1;
	var NUM = 1;
	var URL = new Array();
	var IMG = new Array();
	var ALT = new Array();
	var ID = new Array();
	var TARGET = new Array();
	var NGID = new Array();



	URL.push('https://www.kosodate-style.jp/cgi-bin/regist.cgi');
	IMG.push('http://www.adlantis.jp/banner_ads/0004/3790/fc5cbed82e393607ccc5dd1248e4ed86ab524ded.jpg');
	ALT.push('');
	ID.push('default');
	TARGET.push('target="_blank"');
	HIT.push(1);
	NGID.push('');


	URL.push('http://www.kosodate-style.jp/style/');
	IMG.push('http://static.adlantis.jp/banner_ads/0012/0593/c4a1454756dcfd7544035edfc6513ad5fa2f4bf2.jpg');
	ALT.push('');
	ID.push('default');
	TARGET.push('target="_blank"');
	HIT.push(1);
	NGID.push('');


	URL.push('http://www.otoriyose.net/');
	IMG.push('http://static.adlantis.jp/banner_ads/0010/1087/6d6a48e3a6fee4a2d205d7c064bf09b225a10816.jpg');
	ALT.push('');
	ID.push('default');
	TARGET.push('target="_blank"');
	HIT.push(1);
	NGID.push('');





	for(i=0;i<HIT.length;i++){
		MES[i]='<a href="http://www.recipe-blog.jp/re.cgi?l=' + ID[i] + '&f=rect&url=' + URL[i] + '" ' + TARGET[i] + '><img alt="' + ALT[i] + '" border="0" src="' + IMG[i] + '" width="300" height="250"></a><img src="' + ADLOG + '?referer=' + document.referrer + '&id=' + ID[i] + '&f=adlog_rect" width="1" height="1">';
		
	}

	return {NUM: NUM, RAND: RAND, MES: MES, HIT: HIT, NGID: NGID, ID: ID};


}


