// -----------------------------------------------------------------------
// TINAMI Comike Map System Ver 2010.06.18
// (c) 2006 TINAMI. All rights reserved.
// CircleInfo.js: サークル情報を表示する
// -----------------------------------------------------------------------
var CircleInfo = Class.create();
CircleInfo.prototype = {
  initialize: function (map_id)
  {
  // 表示する対象のdiv
    this.MapID = map_id;
  // サークル情報が開いていればTrue
    this.isOpen = false;
  // 表示位置
    this.panel = new Rectangle(10, 38, 452, 200);
  // イメージがない場合の代替イメージ
    this.blankimage = '/comike/img/0.gif';
  },

  // クリックした位置のサークル情報を取得する
  open: function (comike_no, day, area, x, y)
  {
    this._openBlankPanel(); // レスポンスを良く見せるため、とりあえず空のパネルを表示

    var url = '/comike/map/api/getcircle';
    var param = 'comike_no=' + comike_no + '&day=' + day + '&area=' + area + '&x=' + x + '&y=' + y + '&format=xml';
    var myAjax = new Ajax.Request( url, {
      method: 'get', parameters: param, onComplete: _getCircleComplete});
  },

  // IDからサークル情報を取得する
  openById: function (id)
  {
    this._openBlankPanel(); // レスポンスを良く見せるため、とりあえず空のパネルを表示

    var url = '/comike/map/api/getcircle';
    var param = 'id=' + id + '&format=xml';
    var myAjax = new Ajax.Request( url, {
      method: 'get', parameters: param, onComplete: _getCircleComplete});
  },

  // ブランクのサークル情報を開く（ロード中用）
  _openBlankPanel: function() {
    // 既にパネルを表示している場合、一度削除
    if (this.isOpen) {
      this.close(true);
    }

    // パネルを作成して表示
    // イメージ
    $('image_img').setAttribute("src", this.blankimage);
    $('image_img').setAttribute("width", 120);
    $('image_img').setAttribute("height", 120);

    // サークル情報
    //　よみがな
    Element.update('cyomi', '&nbsp;');

    // サークル名（リンク付き）
    Element.update('linktitle_na', 'サークル情報を読み込んでいます…');
    Element.show('linktitle_na');
    Element.hide('linktitle_a');

    // Twitter
    Element.hide("twitter_user_a");

    // 主催者名
    Element.update('name_na', '&nbsp;');
    Element.show('name_na');
    Element.hide('name_a');

    // サークル位置
    Element.update('space', '&nbsp;');

    // コメント
    Element.update('comment', '&nbsp;');

    // リンク用パネル
    // このサークルへのマップ上のリンク
    $('parmalink').setAttribute("href", 'http://twitcmap.jp/');

    // twit
    $('twit').setAttribute("href", 'http://twitter.com/');

    // 情報登録ボタンの表示
    Element.hide('do-circle-entry');
    Element.update('supporters', '');

    // fav
    Element.hide('favorite_star');
    Element.hide('favorite_count');

    // 画面に表示
    Element.show('circleinfo');
    this.isOpen = true;
  },

  // サークル情報を開く (circle.openCircleから呼び出される）
  _openCirclePanel: function (circle) {
    // まだパネルを表示していない場合、まずブランクを表示
    if (!this.isOpen) {
      this._openBlankPanel();
    }

    // パネルを作成して表示
    // イメージ
    if (circle.imageurl) {
        $('image_img').setAttribute("src", circle.imageurl);
        if (circle.width > 160 || circle.height > 160) {
            // 横の方が長い？
            if (circle.width >= circle.height) {
                var ratio = (160 / circle.width);
                circle.width = 160;
                circle.height = Math.round(circle.height * ratio);
            } else {
                var ratio = (160 / circle.height);
                circle.height = 160;
                circle.width = Math.round(circle.width * ratio);
            }
        }
        $('image_img').setAttribute("width", circle.width);
        $('image_img').setAttribute("height", circle.height);
    }

    // サークル情報
    //　よみがな
    Element.update('cyomi', circle.cyomi);

    // サークル名（リンク付き）
    if (circle.url.length > 0) {
    	$('linktitle_a').setAttribute("href", circle.url);
        Element.update('linktitle_a', circle.name);
        Element.show('linktitle_a');
        Element.hide('linktitle_na');
    } else {
        Element.update('linktitle_na', circle.name);
        Element.show('linktitle_na');
        Element.hide('linktitle_a');
    }
    // Twitter
    if (circle.twitter_user.length > 0) {
    	$('twitter_user_a').setAttribute("href", "http://twitter.com/" + circle.twitter_user);
        Element.update('twitter_user_a', circle.twitter_user);
        Element.show("twitter_user_a");
    } else {
        Element.hide("twitter_user_a");
    }

    // 主催者名
    if (circle.prof_id) {
    	$('name_a').setAttribute("href", "http://www.tinami.com/creator/profile/" + circle.prof_id);
        Element.update('name_a', circle.owner + " さん ");
        Element.show('name_a');
        Element.hide('name_na');
    } else {
        Element.update('name_na', circle.owner + " さん ");
        Element.show('name_na');
        Element.hide('name_a');
    }

    // サークル位置
    Element.update('space', circle.space);

    // コメント
    var comment_text = circle.comment;
    Element.update('comment', comment_text);

    // リンク用パネル
    // このサークルへのマップ上のリンク
    var linkmap = "http://twitcmap.jp/" + circle.hash;
    $('parmalink').setAttribute("href", linkmap);

    // twit
    this._createTwitButton(circle);

    // 情報登録ボタンの表示
    if (control.logged_in) {
        $('do-circle-entry-a').setAttribute("href", "/comike/entry/" + circle.hash);
        Element.show('do-circle-entry');
        Element.update('do-circle-entry-a', 'このスペースの<br>情報を更新する');
    } else {
        Element.hide('do-circle-entry');
    }

    // お手伝いリストを表示
    this._setSupporter(circle);

    // fav
    this._setFavStar(circle);
    this._setFavCount(circle.fav_count);

    // DLsite
    if (circle.dlsite) {
        this._setDLsite(circle.hash);
    }
    
    // TINAMI Content
    if (circle.content_show) {
        this._setTinamiContent(circle.hash);
    }
  },

  // 未登録スペース表示
  _openNoEntryCirclePanel: function(circle)
  {
    // まだパネルを表示していない場合、まずブランクを表示
    if (!this.isOpen) {
      this._openBlankPanel();
    }

    // サークル名
    Element.update('linktitle_na', '&nbsp;');

    // サークル位置
    Element.update('space', circle.space);

    // コメント
    Element.update('comment', 'まだ情報はありません');
    
    // リンク用パネル
    // このサークルへのマップ上のリンク
    var linkmap = "http://twitcmap.jp/" + circle.hash;
    $('parmalink').setAttribute("href", linkmap);
    // twit
    this._createTwitButton(circle);

    // お手伝いリストを表示
    this._setSupporter(circle);

    // 情報登録ボタンの表示
    if (control.logged_in) {
        $('do-circle-entry-a').setAttribute("href", "/comike/entry/" + circle.hash);
        Element.show('do-circle-entry');
        Element.update('do-circle-entry-a', 'このスペースに<br>情報を登録する');
    } else {
        Element.hide('do-circle-entry');
    }

    // fav
    this._setFavStar(circle);
    this._setFavCount(circle.fav_count);
  },

// サークル情報を閉じる
  close: function (pass_adsreload)
  {
    if (this.isOpen) {
        $('image_img').setAttribute("src", this.blankimage);
        // お気に入り
        this._setFavOff();
        // お手伝い
        this._resetSupporter();
        // DLsite
        this._resetDLsite();
        // TINAMI
        this._resetTinamiContent();
        
        // 閉じます
        Element.hide('circleinfo');
        // AdSense枠は閉じた後で差し替える
        if (pass_adsreload == undefined || pass_adsreload == false) {
            $('googleads').setAttribute("src", "/comike/map/framead/" + control.circleInfo.circle.hash);
        }
        this.isOpen = false;
    }
  },

  // パネルはクリックされているか？
  isClick: function (clientX, clientY)
  {
    return (this.isOpen && this.panel.isClick(clientX, clientY)) ? true : false;
  },

  // Twitter
  _createTwitButton: function(circle)
  {
    var twit_status = 'http://twitcmap.jp/' + circle.hash + ' '
               + circle.locate + ' '
               + circle.name + ' '
               + '#twitcmap';
    var twit_link = "http://twitter.com/home?status=" + encodeURIComponent(twit_status);
    $('twit').setAttribute("href", twit_link);
    $('twit').setAttribute("target", "_blank");
  },

  // Fav
    _setFavTopPos: function(circle)
    {
        var top = circle.fav_top;
        $('favorite_star').style.backgroundPosition = '0 ' + top + 'px';
        $('favorite_star').style.cursor = 'pointer';
        // title属性を状態によって変更
        var title = 'このサークルを' + (top == -24 ? 'お気に入りから外す':'お気に入りに追加');
        $('favorite_star').setAttribute('title', title);
    },

  _setFavOff: function()
  {
    if (control.logged_in) {
        $('favorite_star').style.backgroundPosition = '0 -72px';
    } else {
        $('favorite_star').style.backgroundPosition = '0 0';
    }
    $('favorite_star').style.cursor = 'auto';
    Event.stopObserving($('favorite_star'), "click", this._favStarClickHandler);
  },

   _setFavStar: function(circle)
  {
    var id = circle.id;
    Element.show('favorite_star');
    if (control.logged_in) {
    	this._setFavTopPos(circle);
        this._favStarClickHandler = function() {clickFavorite(id);return false;};
    } else {
    	this._setFavOff(id);
        this._favStarClickHandler = function() {return false;};
    }
    Event.observe($('favorite_star'), "click", this._favStarClickHandler);
  },

  _setFavCount: function(fav_count)
  {
    Element.show('favorite_count');
    Element.update('favorite_count', fav_count);
    var classname = (fav_count > 0 ? 'favorite_count_on' : 'favorite_count_off');
    $('favorite_count').setAttribute("class", classname);
  },

  // お手伝いを設定する
  _setSupporter: function(circle)
  {
    var text = ''
    var supported = false;
    for (i=0; i<circle.supporters.length; ++i) {
        text = text + '<span>'
                    + '<a href="http://twitter.com/' + circle.supporters[i] + '" target="_blank">'
                    + circle.supporters[i]
                    + '</a>'
                    + '</span>';
        if (control.user_name != undefined) {
            if (circle.supporters[i] == control.user_name) {
                supported = true;
            }
        }
    }
    if (text == '') {
        text = '<span>まだ登録されていません。</span>';
    }
    Element.update('supporters', text);
    Element.update('supporter_num', circle.supporters.length);

    if (control.logged_in) {
        // 登録クリック時の動作
        this._supporterClickHandler = function() {clickSupporter(circle.hash);return false;};
        Event.observe($('do-circle-support-a'), "click", this._supporterClickHandler);
        // 表記
        Element.update('do-circle-support-a', (supported ? 'お手伝い登録を解除する' : 'お手伝いとして登録する'));
    }
  },

  // お手伝いを閉じる
  _resetSupporter: function()
  {
    if (control.logged_in) {
        Event.stopObserving($('do-circle-support-a'), "click", this._supporterClickHandler);
    }
    Element.hide('supporter');
  },

    // DLsite.comの情報を取得する
    _setDLsite: function(hash)
    {
        var url = '/comike/map/api/dlsite';
        var param = 'hash=' + hash;
        var myAjax = new Ajax.Request( url, {
            method: 'get', parameters: param, onComplete: function(Request) {
                Element.update('dlsite-contents', Request.responseText);
                Element.show('dlsite-open');
            }
        });
    },

    // DLsite.comの情報をクリアする
    _resetDLsite: function()
    {
        Element.update('dlsite-contents', '');
        Element.hide('dlsite-open');
        Element.hide('dlsite');
    },
    
    // TINAMIの情報を取得する
    _setTinamiContent: function(hash)
    {
        var url = '/comike/map/api/content';
        var param = 'hash=' + hash;
        var myAjax = new Ajax.Request( url, {
            method: 'get', parameters: param, onComplete: function(Request) {
                Element.update('tinami-contents', Request.responseText);
                Element.show('tinami');
            }
        });
    },

    // DLsite.comの情報をクリアする
    _resetTinamiContent: function()
    {
        Element.update('tinami-contents', '');
        Element.hide('tinami');
    }
};

// -----------------------------------------------------------------------
// 新しいウィンドウでURLを開く(Global)
function _openCircleSite(event, url)
{
  if (event.button < 2) {
    window.open(url, "site");
  }
}

// 親ウィンドウでURLを開く(Global)
function _openSiteToSelf(event, url)
{
  if (event.button < 2) {
    parent.location.href = url;
  }
}

// サークル情報取得 (Global)
function _getCircleComplete(Request) {
  var circle;
  var responseText;
  try {
    var xmlDoc = Request.responseXML;
    responseText = xmlDoc.documentElement.firstChild.nodeValue;
    circle = eval('(' + responseText + ')');
  } catch(e) {
    _MapInternalErrorMsg("データ解析時に異常が発生しました", e, responseText, '_getCircleComplete');
    return ;
  }

  // サークル情報を保存
  control.circleInfo.circle = circle;

  if (circle.found) {
    // サークル情報がある場合、サークル情報を開く
    control.openCircleInfo(circle);
  } else {
    // サークル情報がなく、ID指定で開かれた場合のみ、リンクのみを通知するウィンドウを開く
    if (/* control.openByIdNow && */ circle.id) {
      control.openCircleNoEntry(circle);
    } else {
        // 何もない場合、サークル情報を閉じる（広告枠はリロードしない）
        control.closeCircleInfo(true);
    }
  }
  // サークルスペースをクリックされた場合、トラックバック情報を開く
  if (circle.id) {
   control.showTrackbackInfo(false);
  }
  control.openByIdNow = false;
}

