
// リンク切れ保管用
function notFoundImage(self) {
	// 企業画像
	if ($(self).attr('class') == 'company_img') {
		$(self).attr('src', '/images/noimage0'+(parseInt(Math.random()*3+1))+'.gif');
	}
}

// 読めない画像をNOIMAGE画像に置換
function enableReplaceErrorImages(url_error_image) {
  for (var cls in url_error_image) {
    var url = url_error_image[cls];
    var preload = new Image();
    preload.src = url;
    $("img." + cls).error(function(){
      $(this).unbind("success");
      $(this).attr("src", url);
    });
  }
}

// 画像のロールオーバー
function enableRolloverImages(proc_rename) {
  $("img.rollover").each(function(i) {
    var oldSrc = $(this).attr("src");
    var newSrc = proc_rename(oldSrc);
    var obj = new Image(); obj.src = newSrc;
    $(this).mouseover(function() { $(this).attr("src", newSrc); });
    $(this).mouseout(function() { $(this).attr("src", oldSrc); });
    this._rename = proc_rename;
  });
}

// 雲型枠の縦幅補正
// 元の高さ h に対し、[h-3..h+20] の範囲の 24n-10 型の整数に変換
function adjustCloudFrame(elements) {
  elements.each(function(i) {
    new_height = Math.ceil(($(this).height() + 6) / 24) * 24 - 10;
    $(this).css("overflow", "hidden").height(new_height);
  });
}

// トップページへスクロール
function scrollToTop() {
  var offset = $('html,body').offset().top;
  $('html,body').animate({scrollTop: offset + 'px'}, 1000);
}

// ユーザIDのボックスまでスクロール
// @結果ページ
function scrollToUserIdBox() {
  var offset = $('div.userinfo div.idbox').offset().top;
  $('html,body').animate({scrollTop: offset + 'px'}, 1000);
}

// ログインチェック
function checkLoggedIn() {
  $.ajax({
     type: 'GET',
     url: '/api/user_check',
     data: null,
     async: true,
     success: function(xml){
       if ($(xml).find('success').text() == '1') {
       	 $(".ox-link").removeClass("ox-link");
       }
     }
  });
}


// サイトの基底URLを得る
//※ スタイルシートのURLの css/ の前部分
function getSiteBaseUrl() {
  var url = $("link[rel='stylesheet']").eq(0).attr("href") || "";
  var pos = url.indexOf("/css/");
  return (pos >= 0) ? url.substring(0, pos + 1) : "/";
}

// ログイン状態を検査する
// callback の関数をログイン済みかを表す真偽値を引数にして呼ぶ
// この関数は複数回の使用が可能、API呼び出しは1回のみ行う
function checkLoginState(callback) {
  var me = checkLoginState; // この関数自身
  // 既にAPIの結果がキャッシュ済ならそれを返す
  if (me.login != null) {
    callback(me.login); return;
  }
  // コールバックのリストに登録
  if (me.callback == null) { me.callback = []; }
  me.callback.push(callback);
  // 既にAPIを呼び出しているなら終わり
  if (me.waiting) { return; }
  // APIを呼び出す
  me.waiting = true;
  var baseUrl = getSiteBaseUrl();
  $.ajax({
    type: 'GET', async: true, cache: false,
    url: baseUrl + 'api/user_check', data: null,
    success: function(xml) {
      me.login = ($(xml).find('success').text() == '1');
      me.waiting = false;
      doCallback(me.login);
    },
    error: function() {
      me.login = false; me.waiting = false;
      doCallback(me.login);
    }
  });
  // コールバック関数を順番に呼ぶ
  function doCallback(value) {
    for (var i = 0; i < me.callback.length; i++) {
      me.callback[i](value);
    }
  }
}

// ログイン時のみloginonlyクラスの要素を表示する
// 元々は非表示になっていて、それを解除する
function showIfLoggedIn(element) {
  checkLoginState(function(login) {
    if (login) { $(".loginonly").removeClass("loginonly"); }
  });
}

// ユーザーページ・個社ページ・企業一覧ページでは、非ログイン時に
// 「もっと○×つける」の代わりに「さっそく○×つける」を表示
// 画像はログイン判定完了後に表示させる
//※ img.playbtn には最初 visibility:hidden を設定しておく
//※ enableRolloverIamges と併用する場合は、こちらを後に呼ぶこと
function kindOfPlayButton() {
  var changed = { "01":"03", "02":"04" }; // 接尾の変換テーブル
  checkLoginState(function(login) {
    if (login) { // ログインなら即表示させる
      $("img.playbtn").css("visibility", "visible");
    } else { // 非ログイン時
      $("img.playbtn").each(function(i) {
        // 新しいファイル名を得て、プレロード
        var src = $(this).attr("src");
        src = src.replace(/(0[12])/, function(s){ return changed[s]; });
        var obj = new Image(); obj.src = src;
        // 画像を置換、読み込み完了時に表示する
        $(this).load(function() {
          $(this).css("visibility", "visible");
        });
        $(this).attr('src', src);
        // rollover が設定されている場合は再設定する
        if (this._rename) {
          $(this).unbind("mouseover").unbind("mouseout");
          var newSrc = this._rename(src);
          var obj = new Image(); obj.src = newSrc;
          $(this).mouseover(function() { $(this).attr("src", newSrc); });
          $(this).mouseout(function() { $(this).attr("src", src); });
        }
      });
    }
    
  });
}

// クリックした特徴語を削除する用のイベントハンドラを
// delete_tagクラスがついているimgタグに追加
function setDeleteTagLinks() {
  $('img.delete_tag').click(function(){
    // 削除用のタグidはdid_***
    var id = $(this).attr('id').substr(4,10);
    $.ajax({
       type: 'GET',
       url: '/js/delete_tag',
       data: 'tag_id='+id,
       async: true,
       success: function(content){
         // liでtid_***となっている部分の削除
         $('#tid_'+id).animate({ opacity:'hide' }, 'slow');
       }
    });
  });
}

// ユーザー表示のボックスをクリックした時に、中に含まれるリンクに
// 従って遷移する
function makeUserBlockLink () {
  $("div.user li").css("cursor", "pointer").click(function() {
    location.href = $(this).find("a").attr("href");
  });
}

// 半角英数字のURL折り返し
// 各文字の間に<wbr />タグを埋め込むことによって折り返せるようになっている
(function() {
  jQuery.fn.linkwrapper = function(config) {
    config = jQuery.extend({
      pattern: '(.)'
    }, config);
    var pattern = new RegExp(config.pattern, 'g');
    var tag = jQuery.browser.opera ? '&#8203;' : '<wbr />';
    return this.each(function() {
      jQuery(this).html(jQuery(this).text().replace(pattern, '$1' + tag));
    });
  };
})(jQuery);



//親要素のサイズを超えるリストは消去
function overflowListDelete() {
  while($("#axisword ul").height() > 215){
    $('#axisword li:last-child').remove();
  }
}


//トップページのランキングボックス　高さそろえ
function changeSameHeight(elements1, elements2) {
  if(elements1.height() < elements2.height())
    elements1.css("height", elements2.height());
  else
    elements2.css("height", elements1.height());
}