//$(document).ready(function(){
function initialize(){

  $(".article_title-02 a").bt({
      contentSelector: "$(this).parent().parent().parent().parent().find('.description').html()",
      trigger: 'hover',
      width: '600px',
      centerPointY: .1,
      spikeLength: 10,
      cornerRadius: 4,
      fill: '#FFF',
      strokeStyle: '#ABABAB',
      strokeWidth: 2,
      positions: ['top', 'bottom'],
      cssStyles: {
        fontSize: '13px'
      }
    });

  $(".article_title a").bt({
      contentSelector: "$(this).parent().parent().parent().parent().find('.description').html()",
      trigger: 'hover',
      width: '600px',
      centerPointY: .1,
      spikeLength: 10,
      cornerRadius: 4,
      fill: '#FFF',
      strokeStyle: '#ABABAB',
      strokeWidth: 2,
      positions: ['top', 'bottom'],
      cssStyles: {
        fontSize: '13px'
      }
    });

	$('input[name="handle"]').val(get_cookie('handle'));
//	$(".form_for_upinfo").ajaxForm({
//		beforeSubmit: function(){
//			$(this).parent().find('.status').html('登録中…');
//		},
//		success: function(){
//			$(this).parent().html('コメント投稿完了。');
//alert('ok');
//		alert($(this));
//		}
//	});
}

//	$(".form_for_upinfo").submit(function(){
function submit_form_upinfo(t){
		o = $(t).parent();
		content = o.find("textarea[name='comment_content']").val();
		handle  = o.find('input[name="handle"]').val();
		url     = o.find('input[name="url"]').val();
		title   = o.find('input[name="title"]').val();
		tweet   = o.find('input[name="tweet"]').is(':checked');

		set_cookie('handle', handle, 24*30);

		if(window.confirm('以下のコメントを送信してよろしいですか？(空の場合は評価のみ)\n\n' + content)){ // 確認ダイアログを表示
			$(t).ajaxSubmit({
				beforeSubmit: function(){
					o.html('<p style="color: red">登録中…</p>');
				},
				success: function(){
					//size = o.parent().parent().find(".comment_size").val();
					//o.parent().parent().find(".comment_size").html(parseInt(size)+1);
					o.html('コメント投稿完了。');

					if(tweet) window.open('http://twitter.com/home?status='+content+' : '+title+' '+url, '紳士協定同時投稿機能', 'width=500, height=230, menubar=no, toolbar=no, scrollbar=yes');
				}
			
			});
		}

		return false;
	};
// })
//	$(".form_for_site").submit(function(){
function submit_form_site(t){
		o = $(t).parent();
		content = o.find("textarea[name='comment_content']").val();
		handle  = o.find('input[name="handle"]').val();
		url     = o.find('input[name="url"]').val();
		title   = o.find('input[name="title"]').val();
		tweet   = o.find('input[name="tweet"]').is(':checked');

		set_cookie('handle', handle, 24*30);

		$(t).ajaxSubmit({
			beforeSubmit: function(){
				if(!window.confirm('以下のコメントを送信してよろしいですか？(空の場合は評価のみ)\n\n' + content)){ // 確認ダイアログを表示
					return false; // 送信を中止
				}
				o.html('<p style="color: red">登録中…</p>');
			},
			success: function(){
				o.html('コメント投稿完了。');

				if(tweet) window.open('http://twitter.com/home?status='+content+' : '+title+' '+url, '紳士協定同時投稿機能', 'width=500, height=230, menubar=no, toolbar=no, scrollbar=yes');
			}
		
		});


		return false;
}
//	});
// end initialize
////////////////////////////////////////////////////////////////////////////////////////////////


// Set cookie data to browser
function set_cookie(name, value, hour){
  // クッキーへの書き込み
  // 　　引数：name=保存先の名前, data=データ　　day=保存期間(日数)
  // 　　返却値：なし
  if(!navigator.cookieEnabled){    // クッキーが利用可能かどうか
    alert("クッキーへの書き込みができません、クッキーが許可になっているか確認してください。");
    return;
  }
  expires = new Date();
  expires.setTime(expires.getTime()+(hour*60*60*1000));
  
  document.cookie = name + "=" + escape(value) + "; path=/; expires=" + expires.toGMTString() + ";";
  //document.cookie = name + "=" + escape(value) + "; path=/;";
}

function get_cookie(name){
  // クッキーから読み込み
  // 　　引数：name=キーワード
  // 　　返却値：データ
  if(typeof(name) == "undefined")　　// キーワードなし
    return "";        // 何もしないで戻る
  name = name + "=";
  kdata = "";
  scookie = document.cookie + ";";　　　　// クッキー情報を読み込む
    start = scookie.indexOf(name);   　// キーワードを検索
    if (start != -1){    // キーワードと一致するものあり
      end = scookie.indexOf(";", start);    // 情報の末尾位置を検索
      kdata = unescape(scookie.substring(start + name.length, end));  // データ取り出し
    }
  return kdata;
}




function countUp(t, target, id_upinfo){

  if (target != 'accesses') $('#action_recom-'+id_upinfo).html('<img src="../images/loading.gif" />');
  cookie_data = get_cookie(target); // 投票情報がtarget

  re = new RegExp(id_upinfo); // それぞれのコメントに投票済みかどうか

  if (cookie_data && cookie_data.search(re) != -1){
    if (target != 'accesses') alert("この記事についてはすでに報告済みです");
    $('#action_recom-'+id_upinfo).fadeOut(1500);
    return;
  }

  data = cookie_data + "," +id_upinfo;
  set_cookie(target, data, 5); 

  $.ajax({
    url : ".",
    type : "post",
    data : { "id_upinfo": id_upinfo.toString(),
             "cmd"      : "count_up_" + target },
    success: function(o){
      if (target != 'accesses'){
        $(t).parent().parent().find('#informing-'+id_upinfo).html(o);
      }
    }
  });  
}

function countUpAtTop(t, target, id_upinfo){

  //cookie_data = get_cookie_data(target); // 投票情報がtarget
  if (target != 'accesses') $('#action_recom_top-'+id_upinfo).html('<img src="../images/loading.gif" />');
  cookie_data = get_cookie(target); // 投票情報がtarget

  re = new RegExp(id_upinfo); // それぞれのコメントに投票済みかどうか
  if (cookie_data && cookie_data.search(re) != -1){
    if (target != 'accesses') alert("この記事についてはすでに報告済みです");
    return;
  }

  data = cookie_data + "," +id_upinfo;
  set_cookie(target, data, 5); 

  $.ajax({
    url : ".",
    type : "post",
    data : { "id_upinfo": id_upinfo.toString(),
             "cmd"      : "count_up_" + target },
    success: function(o){
      if (target != 'accesses'){
        $(t).parent().parent().find('#informing_top-'+id_upinfo).html(o);
      }
    }
  });  

	return false;
}

function check(URL, Comment, NTimes){

  //「OK」時の処理開始 ＋ 確認ダイアログの表示
  if(window.confirm(Comment)){
    if ((NTimes - 1) == 0) {
      location.href = URL; // example_confirm.html へジャンプ
    }   
    else { 
      check(URL, '「本当」に'+Comment, NTimes - 1); 
    }   
  }
  // 「OK」時の処理終了

  // 「キャンセル」時の処理開始
  else{
    window.alert('キャンセルされました'); // 警告ダイアログを表示
  }
  // 「キャンセル」時の処理終了

}

function new_comment(t, type_target, id_target){

  p = $(t).parent();
  content = p.find('textarea').val();

  if(content && content != ""){
    if(window.confirm('以下のコメントを送信してよろしいですか？\n\n' + content)){ // 確認ダイアログを表示
      $.ajax({
        url : ".",
        type : "post",
        data : { "id_target":        id_target.toString(),
                  "type_target":     type_target,
                  "comment_content": content,
                  "cmd":             "modify_evaluation"},
        success: function(o){
          // window.location.reload();
          p.html('<p>完了しました。</p>');
        },
        error: function(o){
          p.html('<p>失敗しました。時間を空けて再度投稿してください。</p>');
        }
      })

    }
  }
}

function check_comment(){
  Content = $("textarea[name='comment_content']").val();
  if(Content && Content != ""){
    if(window.confirm('以下のコメントを送信してよろしいですか？\n\n' + Content)){ // 確認ダイアログを表示
      return true; // 「OK」時は送信を実行
    }
    else{ // 「キャンセル」時の処理
      //window.alert('キャンセルされました'); // 警告ダイアログを表示
      return false; // 送信を中止
    }
  }else{
  }
}

function display_bt(id, anchor){
  $(anchor).bt({
    contentSelector: "$('" + id + "')",
    trigger: 'hover',
    //contentSelector: "$('#res-content')",
    width: '600px',
    centerPointY: .1,
    spikeLength: 10,
    cornerRadius: 2,
    fill: '#FFF',
    strokeStyle: '#ABABAB',
    strokeWidth: 2,
    positions: ['right']
  });
  $(anchor).btOn();
}


