var gBASE_URL;
function setBASE_URL(url){
	gBASE_URL = url;
}

//
function search_submit(type){
	if(document.searchform.q.value != ''){
		if(type == 'news') { document.searchform.action = gBASE_URL + 'search'; }
		if(type == 'inside') { document.searchform.action = gBASE_URL + 'searchentry'; }
		document.searchform.submit();
	}
}
function unlogined(){
	alert('この操作はログインした後に可能です。');
	return false;
}
function jstrlen(str) {
   var len = 0;
   str = escape(str);
   for (var i = 0; i < str.length; i++, len++) {
      if (str.charAt(i) == "%") {
         if (str.charAt(++i) == "u") {
            i += 3;
            len++;
         }
         i++;
      }
   }
//   return len;
   return Math.floor(len/2);
}
function problem_report(entryid, mode){
    var obj = $('pb-'+entryid);
    var s = obj.style;
    
    if(mode=='close') { s.display = 'none'; }
    else if(mode=='open') { s.display = 'block'; }
    else if(s.display == 'block') { s.display = 'none'; }
    else { s.display = 'block'; }
}
function problem_report_go(entryid, entrytitle, type){
    if(confirm("この記事を通報します。よろしいですか？")){
        var url = gBASE_URL + 'api/problemreport?id=' + entryid + '&type=' + type;
        var myAjax = new Ajax.Request(
            url,
            {
                method: 'get',
                onComplete: alert('記事「' + entrytitle + '」をサイト運営者へ通報しました')
            }
        );
    }
}
function openUrl(){
    var url = document.add.url.value;
    if(!url || url == 'http://'){ return;}
    if(url.substr(0,4) != 'http'){
        url = 'http://' + url;
        document.add.url.value = url;
    }
    window.open(url,'_blank');
}

//エントリーを削除
function delete_entry(entryId){
	return confirm(entryId);
	
	return false;
}

// click
var LinkCount = Class.create();
LinkCount.prototype = {
    initialize: function(className, parentElement) {
        var elements = document.getElementsByClassName(className, parentElement);
        for (var i = 0, len = elements.length; i < len; i++) {
            Event.observe(elements[i], 'click', this.addLinkCount.bindAsEventListener(this));
        }
    },
    addLinkCount: function(event) {
        var element = Event.element(event);
        if(element.getAttribute('src') != null){
            //画像のときはelementが<img>になるため対応。
            element = element.parentNode;
        }
        var image = new Image();
        image.src = gBASE_URL + 'api/click?n=' + encodeURIComponent(element.getAttribute('href'));
    }
}; 

Event.observe(window, 'load', function () {
    new LinkCount('click');
});

// AnnounceClick
var LinkAnnounceCount = Class.create();
LinkAnnounceCount.prototype = {
    initialize: function(aid) {
        this.aid = aid;
        var elements = document.getElementById('announce_id_' + aid);
        if(elements){
            Event.observe(elements, 'click', this.addLinkCount.bindAsEventListener(this));
        }
    },
    addLinkCount: function(event) {
        var element = Event.element(event);
        var image = new Image();
        image.src = gBASE_URL + 'api/clickannounce?aid=' + this.aid;
    }
};

// GraphAnimation
var GraphAnimation = Class.create();
GraphAnimation.prototype = {
	initialize: function(seq, entryId, entryUrl, point, goodVote, badVote, loginStatus){
		this.obj = $('graphanimation-' + seq);;					//親要素
		this.obj.innerHTML = '';
		this.obj.style.color = '#5B5759';

		this.entryId = entryId;
		this.entryUrl = entryUrl;
		this.point = point;
		this.goodVote = goodVote;
		this.badVote = badVote;
		this.loginStatus = loginStatus;
		this.isEnabled = true;			//クリックできるかどうか
		this.voteButton = '';
		
		var htmlButtonClick = '';
		if(this.loginStatus != 'guest'){
			htmlButtonClick = 'javascript:void(0)';
		}
		else{
			//htmlButtonClick = gBASE_URL + 'login';
			htmlButtonClick = 'javascript:void(0)';
		}
		
		//DOMをくみたてる
		//円グラフ
		this.divGraph = document.createElement('div');
		var gs = this.divGraph.style;
		gs.width = '76px';
		gs.height = '76px';
		gs.margin = '0px 6px 5px 6px';
		gs.padding = '0px';
		gs.textAlign = 'center';
		gs.color = '#ffffff';
		gs.fontWeight = 'bold';
		gs.fontSize = '16px';
		gs.lineHeight = '100%';
		gs.fontFamily = '"arial","Sans-serif"';
		this.setGraphImage();
		//グラフ上の数値
		this.linkToEntryBox = document.createElement('div');
		this.linkToEntry = document.createElement('a');
		this.linkToEntry.href = gBASE_URL + 'entry?url=' + encodeURIComponent(this.entryUrl.unescapeHTML());
		this.linkToEntryBox.style.padding = '22px 0px 0px 0px';

		this.pointValue = document.createElement('span');
		this.pointValue.id = 'id-' + this.entryId;
		this.pointValue.innerHTML = this.point; //表示ポイント数値
		this.linkToEntry.appendChild(this.pointValue);
		this.textElem = document.createElement('div');
		this.textElem.innerHTML = 'point';
		this.linkToEntry.appendChild(this.textElem);

		this.linkToEntryBox.appendChild(this.linkToEntry);
		this.divGraph.appendChild(this.linkToEntryBox);

		this.obj.appendChild(this.divGraph);
		
		this.textElement = document.createElement('span');
		this.textElement.innerHTML = 
			'<div class="line88"><img src="/common/images/spacer.gif" width="88" height="1" alt=""></div><div class="title">このニュースは？</div>';
		this.obj.appendChild(this.textElement);
		//○×ボタン
		this.voteButton = document.createElement('div');
		this.voteButton.id = 'vote-' + this.entryId;
		this.voteButton.style.align = 'center';
		this.voteButton.style.width = '88px';

		this.buttonGood = document.createElement('span');
		this.buttonGood.innerHTML = '<a href="' + htmlButtonClick + '"><img src="/common/images/bt_circle.gif" alt="支持する" width="43" height="43" border="0"></a>';

		this.voteButton.appendChild(this.buttonGood);
		this.buttonBad = document.createElement('span');
		this.buttonBad.innerHTML = '<a href="' + htmlButtonClick + '"><img src="/common/images/bt_cross.gif" alt="支持しない" width="43" height="43" border="0"></a>';
		this.voteButton.appendChild(this.buttonBad);
		this.obj.appendChild(this.voteButton);
		//処理中アイコン
		this.executingIcon = document.createElement('div');
		this.executingIcon.style.textAlign = 'center';
		this.obj.appendChild(this.executingIcon);
		
		//alert(this.obj.innerHTML);	// debug
		
		if(this.loginStatus != 'guest'){
			Event.observe(this.buttonGood, 'click', this.clickButtonGood.bindAsEventListener(this), false);
			Event.observe(this.buttonBad,  'click', this.clickButtonBad. bindAsEventListener(this), false);
			
			if(this.loginStatus == 'commented'){
				this.setUnable();
			}
		}
		else{
			Event.observe(this.buttonGood, 'click', this.clickGuestGood.bindAsEventListener(this), false);
			Event.observe(this.buttonBad , 'click', this.clickGuestBad. bindAsEventListener(this), false);
		}
		
	},
	// グラフ画像をセット
	setGraphImage: function(){
		var num = 50;
		if ((this.goodVote + this.badVote) > 0){
			if(this.goodVote / ((this.goodVote-0) + (this.badVote-0)) == 0.25) { num = 25; }
			else if(this.goodVote / ((this.goodVote-0) + (this.badVote-0)) == 0.75) { num = 75; }
			else {
				num = (Math.round(10 * this.goodVote / ((this.goodVote-0) + (this.badVote-0))) * 10);
			}
		}
		
		var graphImageName =  'url(/common/images/graph/graph_' + num + '.gif)'; //円グラフ画像用
		this.divGraph.style.backgroundImage = graphImageName;	//グラフ画像
	},
	// 投票したときのアニメーション　＆　ポイント数と円グラフの表示更新
	voteAnimation: function(){
		var myObj = this;
		new Rico.Effect.FadeTo(this.divGraph, 0.01, 500, 10, {complete:function(){
			$(myObj).pointValue.innerHTML = $(myObj).point; //表示ポイント数値
			$(myObj).setGraphImage();
			new Rico.Effect.FadeTo($(myObj).divGraph, 1, 500, 10, {complete:function(){
				$(myObj).executingIcon.innerHTML = '';
				
				$(myObj).openToCommentWindow();
			}});
		}});
	},
	clickButtonGood: function(event){
		if(this.isEnabled){
			this.setUnable();
			this.goodVote ++;
			this.showExecutingIcon();
			this.voteButton = 'good';
			this.AjaxRequest('t');
		}
		Event.stop(event);
	},
	clickButtonBad: function(event){
		if(this.isEnabled){
			this.setUnable();
			this.badVote ++;
			this.showExecutingIcon();
			this.voteButton = 'bad';
			this.AjaxRequest('f');
		}
		Event.stop(event);
	},
	setEnable: function(){
		this.isEnabled = true;
		new Rico.Effect.FadeTo(this.voteButton, 1.0, 5, 2, {});
	},
	setUnable: function(){
		this.isEnabled = false;
		new Rico.Effect.FadeTo(this.voteButton, 0.3, 5, 2, {});
	},
	showExecutingIcon: function(){
		this.executingIcon.innerHTML = '<img src="/common/images/loading_red.gif" />';
	},
	//○×クリックをサーバ側へAjax送信
	//引数：○のときvoteMode='t', ×のときvoteMode='f'
    AjaxRequest: function(voteMode){
    	voteMode = (voteMode == 't')? 't': 'f';
        var url = gBASE_URL + 'api/vote?t=' + voteMode + '&i=' + this.entryId;
        var myAjax = new Ajax.Request(
            url,
            {
                method: 'get',
                onComplete: this.AjaxReturned.bind(this)
            }
        );
    },
	AjaxReturned: function(originalRequest){
        var res = originalRequest.responseText;
        res = res.strip() - 0;
		if(res >= 0){	//処理成功の場合（res＝増加後のポイント値）
			this.point = res;	//表示増数
			this.executingIcon.innerHTML = '';
			this.voteAnimation();
			
		}
		else{
			if(res == -2){
				//入力済み
				this.executingIcon.innerHTML = '';
			}
			else{
				//その他のエラーー
				this.executingIcon.innerHTML = '';
			}
		}
		
	},
	openToCommentWindow: function(){
		var seq = ("" + this.obj.id).replace('graphanimation-', '');
		if($('commentwindow-' + seq)){
			openCommentWindow(seq);
			
			//○×の初期値
			CommentWindow.instance[seq].setGoodBadButton(this.voteButton);
		}
	},
	//未ログインユーザがボタンクリックしたときはログイン画面へ。
	clickGuestGood: function(){
		var myObj = this;
		this.showExecutingIcon();
		new Rico.Effect.FadeTo(this.divGraph, 0.01, 500, 10, {complete:function(){
			window.open(gBASE_URL + 'comment?url=' + encodeURIComponent($(myObj).entryUrl.unescapeHTML()) + '&submit=true&thumb=1&w=open', '_top');
			new Rico.Effect.FadeTo($(myObj).divGraph, 1, 500, 10, {complete:function(){
				//window.open(gBASE_URL + 'add', '_top');
			}});
		}});
	},
	clickGuestBad: function(){
		var myObj = this;
		this.showExecutingIcon();
		new Rico.Effect.FadeTo(this.divGraph, 0.01, 500, 10, {complete:function(){
			window.open(gBASE_URL + 'comment?url=' + encodeURIComponent($(myObj).entryUrl.unescapeHTML()) + '&submit=true&thumb=0&w=open', '_top');
			new Rico.Effect.FadeTo($(myObj).divGraph, 1, 500, 10, {complete:function(){
				//window.open(gBASE_URL + 'add', '_top');
			}});
		}});
	}
	
}

//クラス初期化
function initGraphAnimation() {
	if(document.body){
		var objLabel = Array();
		var cnt = -1;
		while(true){		//IDの連番0から順に探す
			cnt++;
			var e = document.getElementById('graphanimation-' + cnt);
			if(e){
				var inText = e.innerHTML;	//記事ID、ポイント数、○票数、×票数、commented/notyet/guestが'! !'区切りで入っていることが前提
				var ret = inText.split('! !');
				objLabel[cnt] = new GraphAnimation(e, ret[0], ret[1], ret[2], ret[3], ret[4]);
				
			}
			else{
				//見当たらなかったらループ終了
				break;
			}
		}
	}
	else{
	    //html読み込みが完了していない場合
	    setTimeout("initGraphAnimation()", 190);
	}
}
//Event.observe(window, 'load', function(){
//	initGraphAnimation();
//});


// delete comment
var DeleteComment = Class.create();
DeleteComment.prototype = {
    initialize: function(entryurl, parentElement){
        this.entryurl = entryurl;
        this.Request();
    },
    Success: function(originalRequest){
        var res = originalRequest.responseText;
        res = res.strip();
        if(res == 0){
            new Effect.Puff('entry-'+ this.entryurl);
            current = $('comment_num').innerHTML;
            current = current.stripTags();
            current = current - 0 + 1;
            $('comment_num').innerHTML = current;
        }else{
            $('edit-' + this.entryurl).innerHTML = this.comment_original;
        }
    },
    Request: function(){
        this.loadMessage();
        var url = gBASE_URL + 'api/delete?url=' + this.entryurl;
        var MyAjax = new Ajax.Request(
            url,
            {
                method: 'get',
                onComplete: this.Success.bind(this)
            }
        );
    },
    loadMessage : function(){
        this.comment_original = $('edit-' + this.entryurl).innerHTML;
        $('edit-' + this.entryurl).innerHTML = '<img src="/common/images/loading_red.gif" />';
    }
};

// EditableTextBox
var EditableTextBox = Class.create();
EditableTextBox.prototype = {
    // @param label テキストまたはブロックエレメント
    initialize: function(label) {
        // テキストラベル表示状態のDOM定義
        this.textHolder = document.createElement('span');
        this.text = document.createElement('span');
        this.textHolder.appendChild(this.text);
        this.dummy1 = document.createElement('span');
        this.dummy1.innerHTML = "&nbsp";
        this.textHolder.appendChild(this.dummy1);
        this.toEdit = document.createElement('a');
        this.toEdit.href = "javascript:void(0);";
        this.toEdit.innerHTML = '[編集]';
        this.toEdit.style.fontSize = "90%";
        this.textHolder.appendChild(this.toEdit);
        Event.observe(this.toEdit, 'click', this.edit.bindAsEventListener(this), false);

        // 編集中状態のDOM定義
        this.form = document.createElement('form');
        this.form.style.margin = '0px';
        this.form.style.padding = '0px';
        this.input = document.createElement('input');
        this.input.type = 'text';
        this.input.style.width = '400px';
        this.form.appendChild(this.input);
        this.submitButton = document.createElement('input');
        this.submitButton.type = 'submit';
        this.submitButton.value = '更新';
        this.form.appendChild(this.submitButton);
        this.dummy2 = document.createElement('span');
        this.dummy2.innerHTML = "&nbsp";
        this.form.appendChild(this.dummy2);
        this.retLink = document.createElement('a');
        this.retLink.href = 'javascript:void(0);';
        this.retLink.innerHTML = '[戻る]';
        this.retLink.style.fontSize = '90%';
        this.form.appendChild(this.retLink);

        Event.observe(this.form, 'submit', this.submit.bindAsEventListener(this), false);
        Event.observe(this.retLink, 'click', this.back.bindAsEventListener(this), false);

        this.setLabel((typeof label == 'string') ? label : label.innerHTML);

        // EditableTextBox 全体のエレメント
        if(typeof label == 'string') {
            this.element = document.createElement('div');
        } else {
            this.element = label;
            this.element.innerHTML = '';
        }
        this.element.appendChild(this.textHolder);
        this.name = label.id;        //name属性を代用（id属性だと重複してしまうので）

    },
    // 書いてある文字のアクセサ
    getLabel: function() {
        return $F(this.input);
    },
    setLabel: function(label) {
        this.text.innerHTML = label;
        this.input.value = label;
    },
    // 編集開始
    edit: function(event) {
        this.element.replaceChild(this.form, this.textHolder);
        this.submitButton.disabled = false;
        Field.activate(this.input);  // フォーカス・選択
    },
    // 保存せずに編集終わり
    back: function(event) {
        this.element.replaceChild(this.textHolder, this.form);
        this.setLabel(this.text.innerHTML);
        Event.stop(event);
    },
    // 編集終わり
    submit: function(event) {
        var myObj = this;
        new Ajax.Request('submitted.php', {
            method:'get',
            parameters: 'id=' + $(this).name + '&q=' + encodeURIComponent($F(this.input)),
            onSuccess: function(transport){
                //Ajaxレスポンスが返ってきたときの処理                
                //alert(transport.responseText);
                $(myObj).element.replaceChild($(myObj).textHolder, $(myObj).form);
                $(myObj).setLabel(transport.responseText);    //Ajaxレスポンスの文字列を新しい画面表示文言にする
            }
        });
        
        //もしあれば this.onSubmit を呼ぶ
        if(this.onSubmit) this.onSubmit.call(this, event);
        
        this.submitButton.disabled = true;    //Ajax処理中はグレー表示
        Event.stop(event);
    }

};

//クラス名が'editabletextbox'のものをすべてEditableTextBox部品に変換する
//Event.observe(window, 'load', function() {
//    var objLabel = Array();
//    var elements = document.getElementsByClassName('editabletextbox');    //対象のclass名
//    if(elements && elements.length){
//        for(var i = 0, len = elements.length; i < len; i++){
//            objLabel[i] = new EditableTextBox($(elements[i]));
//        }
//    }
//}, false);

function openCommentWindow(seq){
	if(CommentWindow.instance[seq]){
		CommentWindow.instance[seq].slideOpen();
	} else {
		CommentWindow.instance[seq] = new CommentWindow(seq);
	}
}

var CommentWindow = Class.create();
CommentWindow.instance = {};
CommentWindow.prototype = {
	initialize: function(seq){
		this.seq = seq;
		this.id = CommentWindow_initialValue[seq].entryid;
		this.parentObj = $('commentwindow-' + seq);
		
		this.element = document.createElement('div');

		//DOM組み立て
		var html = 
				'<div class="comment_waku"> '
				+ '	<form action="' + gBASE_URL + 'comment" method="post">'
				+ '	  <input type="hidden" name="action_comment" value="1" >'
				+ '	  <input type="hidden" name="submit" value="submit" >'
				+ '	  <input type="hidden" name="url" value="' + CommentWindow_initialValue[seq].entryurl+ '" >'

				+ '	  <div class="comment_back"> '
				+ '		<table width="100%" border="0" cellspacing="0" cellpadding="0">'
				+ '		  <tr height="20" valign="bottom"> '
				+ '		    <td width="15"><img src="/common/images/spacer.gif" width="15" height="1" alt=""></td>'
				+ '			<td align="left" class="main-txt">コメント</td>'
				+ '			<td align="right"><span id="commentwindow-clickclose-' + this.seq + '"><a href="javascript:void(0);" class="medium"><img src="/common/images/icn/icn_close.gif" width="13" height="13" align="absmiddle" border="0">&nbsp;閉じる</a></span></td>'
				+ '		    <td width="15"><img src="/common/images/spacer.gif" width="15" height="1" alt=""></td>'
				+ '		  </tr>'
				+ '		</table>'
				+ '	  </div>'

				+ '	  &nbsp;&nbsp;このニュースは？'
				+ '	  <table width="100%" border="0" cellspacing="0" cellpadding="0">'
				+ '		<tr>'
				+ '		  <td  valign="middle" align="left" nowrap>'
				+ '		    '
				+ '		    &nbsp;&nbsp;<INPUT id="commentwindow-thumbup-' + this.seq + '" type="radio" value="1" name="thumb" checked ><label for="commentwindow-thumbup-' + this.seq + '">&nbsp;<img src="/common/images/icn/icn_circle.gif" alt="支持する" width="20" height="18" align="absmiddle"></label>&nbsp;&nbsp;'
				+ '			<INPUT id="commentwindow-thumbdown-' + this.seq + '" type="radio" value="0" name="thumb" ><label for="commentwindow-thumbdown-' + this.seq + '">&nbsp;<img src="/common/images/icn/icn_cross.gif" alt="支持しない" width="20" height="18" align="absmiddle"></label>'
				+ '		  </td>'
				+ '		  <td align="right">'
				+ '		  </td>'
				+ '		</tr>'
				+ '		<tr> '
				+ '		  <td colspan="2">'
				+ '		    <textarea name="comment" class="comment_area" id="commentwindow-textarea-' + this.seq + '" wrap="soft">' + CommentWindow_initialValue[seq].comment + '</textarea><br>'
				+ '		    <span style="font-size:90%;">&nbsp;&nbsp;&nbsp;&nbsp;全角200字以内で入力してください。<span id="commentcount-' + this.seq + '"></span></span>'
				+ '		  </td>'
				+ '		</tr>'
				+ '	  </table>'
				+ '	  <table border="0" cellspacing="0" cellpadding="0">'
				+ '		<tr>'
				+ '		  <td>&nbsp;</td>'
				+ '		  <td>';
		//タグ入力UI
		this.tagInputUI = new TagInputInterface(this.seq, ('commentwindow-tag-' + this.seq));
		html += this.tagInputUI.getHtmlInsert();
		
		html +=   '		  </td>'
				+ '		</tr>'
				+ '		<tr valign="middle">'
				+ '		  <td>&nbsp;&nbsp;Tag:&nbsp;</td>'
				+ '		  <td><input type="text" maxlength="50" value="' + CommentWindow_initialValue[seq].tag + '" name="tag" class="comment_tag" style="width:300px;" id="commentwindow-tag-' + this.seq + '"></td>'
				+ '		</tr>'
				+ '		<tr valign="middle">'
				+ '		  <td>&nbsp;</td>'
				+ '		  <td>&nbsp;<span style="font-size:80%;"><span id="alertMaxItemsOver">（空白区切りで最大5個まで）</span> （<a href="' + gBASE_URL + 'help#pickup05" target="_blank">タグとは？</a>）</span></td>'
				+ '		</tr>'
				+ '	  </table>'

				+ '	  <table width="100%" border="0" cellspacing="0" cellpadding="0">'
				+ '		<tr> '
				+ '		  <td align="right" valign="middle">'
				+ '		  <input type="hidden" name="fromcommentwindow" value="true">'
				+ '		 <input value="" type="image" name="submit" src="/common/images/bt_go.gif" width="41" height="22" alt="送信" align="absmiddle" id="commentwindow-form-' + this.seq + '">'
				+ '		 &nbsp;&nbsp;</td>'
				+ '		</tr>'
				+ '	  </table>'
				+ '	</form>'
				+ '</div>';
		this.element.innerHTML = html;
		
		this.parentObj.appendChild(this.element);
		
		this.closeButton = document.getElementById('commentwindow-clickclose-' + this.seq);
		this.commentArea = document.getElementById('commentwindow-textarea-' + this.seq);
		
		this.tagInputUI.setEvent();		//
		this.tagInputUI.refreshTagList();

		this.slideHeight = this.element.offsetHeight;	//この行は「一瞬表示させた状態」で取得する

		var s = this.element.style;
		s.visibility = 'visible';
		s.display = 'none';
		s.overflow = 'hidden';
		s.height = '1px';
		
		Event.observe(this.closeButton,  'click', this.slideClose.bindAsEventListener(this), false);
		Event.observe(this.commentArea,  'keyup', this.setCommentCount.bindAsEventListener(this), false);
		
		this.setGoodBadButton(CommentWindow_initialValue[this.seq].vote);
		this.slideOpen();
		
	},
	slideOpen: function(){
		var objTextArea = $('commentwindow-textarea-' + this.seq);
		var objSubmitButton = $('commentwindow-form-' + this.seq);

		this.element.style.display = 'block';
		new Rico.Effect.Size( this.element, null, this.slideHeight, 120, 8, {complete: function(){objSubmitButton.focus();objTextArea.focus();}});
	},
	slideClose: function(){
		var myObj = this.element;
		myObj.style.overflow = "hidden";
		new Rico.Effect.Size( myObj, null, 1, 120, 8, {complete: function(){myObj.style.display = 'none';}});
	},
	setCommentCount: function(){
		var cnt = jstrlen(this.commentArea.value);
		$('commentcount-' + this.seq).innerHTML = '（現在 ' + cnt + ' 文字）';
		
	},
	setGoodBadButton: function(buttonName){
		if(buttonName == 'good'){
			$('commentwindow-thumbup-' + this.seq).checked = true;
		}
		else if(buttonName == 'bad'){
			$('commentwindow-thumbdown-' + this.seq).checked = true;
		}
	}
}

var CommentWindow_initialValue;
function setCommentWindowInitialValue(value){
	CommentWindow_initialValue = value;
}

//タグ入力支援UI
//var gTagList = [
//	{
//		items: ['ビジネス','国際', '政治','テクノロジー','サイエンス',  '社会', '文化', '歴史']
//	}];
var TagInputInterface = Class.create();
TagInputInterface.prototype = {
	initialize: function(seq, tagfieldID){		//seq: html中で一意な連番、tagfieldID: タグ入力textboxのid
		this.seq = seq;
		this.tagfieldID = tagfieldID;
		this.selectAreaID = 'commentwindow-taglist-' + this.seq;	//タグ選択UIのエリア
		this.tagArea = null;
		
		this.separator = '(　| )+';
	},
	getHtmlInsert: function(){
		/*var html = '		    <table border="0" cellpadding="0" cellspacing="0">'
				+ '		    <tr><td style="margin:0px;padding:0px;"><div style="margin:5px 0px 0px 0px;padding:0px;width:89px;height:15px;"><img src="/common/images/tab_tagcategory.gif" width="89" height="15" alt="ニュースカテゴリ"></div>'
				+ '		      <div style="margin:0px;padding:2px 0px;"><span id="' + this.selectAreaID + '" class="taglist">&nbsp;</span></div></td></tr>'
				+ '		    <tr><td style="margin:0px;padding:0px;"><div style="margin:0px;padding:0px;width:30px;height:7px;text-align:right;"></div></td></tr>'
				+ '		    </table>';
	*/  var html ='おすすめタグ<br><span id="'+this.selectAreaID+'" class="taglist"></span>';
	    return html;
	},
	setEvent: function(){
		this.tagArea = $(this.tagfieldID);		//タグ入力フィールド
		Event.observe(this.tagArea, 'keyup', this.refreshTagList.bindAsEventListener(this), false);
		// このid要素がinnerHTMLに反映されたあとに実行させること。
	},
	refreshTagList: function(){
		var target = document.getElementById(this.selectAreaID);
		var checkstr = ' ' + this.tagArea.value + ' ';
		var html = '';
		
		for(var j = 0; j < gTagList.length; j++){
			//選択中のタブパネルのみを表示
			//if(j != tagList_selectedIndex) { continue; }
			
			//タグごとにループ
			this.tagItem = Array();
			for(var i = 0; i < gTagList[j].items.length; i++){
				var strSelected = checkstr.match(new RegExp(this.separator + gTagList[j].items[i] + this.separator, 'ig')) ? ' style="background-color:#999;color:#fff;"' : '';
				html += '<a href="javascript:void(0);" class="tagitem"' + strSelected + ' onclick="tagInputUI.clickTagItem(\'' + gTagList[j].items[i] + '\')">' + gTagList[j].items[i] + '</a> ';
				// ↑onclickのなかの関数表記に注意（グローバルアクセス可能な関数であること）
			}
		}
		target.innerHTML = html;
	},
	clickTagItem: function(tag){
		var target = this.tagArea;
		var tmpStr = (' ' + target.value + ' ');
		if(tmpStr.match(new RegExp(this.separator + tag + this.separator, 'ig'))){
			//削除 - 前後の空白を考慮
			if(target.value.match(new RegExp(this.separator + tag, 'ig'))) { target.value = target.value.replace(new RegExp(this.separator + tag, 'ig'), ''); }
			else if(target.value.match(new RegExp(tag + this.separator, 'ig'))) { target.value = target.value.replace(new RegExp(tag + this.separator, 'ig'), ''); }
			else { target.value = target.value.replace(tag, ''); }
		}
		else{
			//追加
			target.value += (target.value == '') ? tag : ' ' + tag;
			//項目が多いときはアラート表示
			var tmpStr = target.value.replace(new RegExp(this.separator, 'ig'), " ");
			tmpStr = tmpStr.replace(new RegExp("^( +)"), "");
			tmpStr = tmpStr.replace(new RegExp("( +)$"), "");
			if(tmpStr.split(" ").length > 5){
				fadeYellow2White('alertMaxItemsOver', 40);
			}
		}
		this.refreshTagList();
	}
}


// カレンダー表示サイドバー
function calendar_effect(parentid){
	if($('caleffect') != null) { $('caleffect') = null; }
	var parentobj = $(parentid);
	var linkurl = parentobj.firstChild.href;
	var efobj = document.createElement('div');
	efobj.id = 'caleffect';
	parentobj.style.position = 'relative';
	var s = efobj.style;
	s.position = 'absolute';
	s.whiteSpace = 'nowrap';
	s.top = '0px';
	s.left = '0px';
	s.fontSize = '10px';
	efobj.innerHTML = parentobj.innerHTML.unescapeHTML().escapeHTML();
	parentobj.appendChild(efobj);
	
	setTimeout('changeeffect();',  50);
	new Rico.Effect.FadeTo(efobj, 0.01, 600, 10);
	
	if(linkurl == '') { return true; }
	else{
		setTimeout("window.open('" + linkurl + "','_top');",750);
		return false;
	}
}

function changeeffect(){
	var obj = $('caleffect');
	var s = obj.style;

	var presize = ((s.fontSize).slice(0,-2) - 0);
	var newsize = Math.ceil(presize * 1.5);
	s.fontSize = newsize + 'px';

	s.top  = -(obj.offsetHeight - 10) / 2 + 'px';
	s.left = -(obj.offsetWidth - 22) / 2 + 'px';

	if((s.fontSize).slice(0,-2) < 2000) { setTimeout('changeeffect();',  50); }
	else { s.display = 'none'; }
}

var timerIdYellowFade;
function fadeYellow2White(id, color){
	var obj = $(id);
	var s = obj.style;
	if(timerIdYellowFade) { clearTimeout(timerIdYellowFade); }
	s.backgroundColor = 'rgb(100%, 100%, ' + color + '%)';
	if(color == 100) { return; }
	else{ timerIdYellowFade = setTimeout("fadeYellow2White('" + id + "', " + (color + 10) + ");", 100); }
}

//RSSリーダー登録セレクトボックス
function writeRSSReaderRegist(url, uniqueid){
  var html = '<select name="rssd" size="1" id="' + uniqueid + '" style="font-size:12px;" >' + "\n"
           + '  <option value="http://reader.livedoor.com/subscribe/'                    + url + '">livedoor Reader</option>' + "\n"
           + '  <option value="http://www.bloglines.com/sub/'                            + url + '">Bloglines</option>' + "\n"
           + '  <option value="http://feedpath.jp/feedreader/feeds_add?url='             + url + '">feedpath</option>' + "\n"
           + '  <option value="http://r.hatena.ne.jp/append/'                            + url + '">はてなRSS</option>' + "\n"
           + '  <option value="https://webryreader.sso.biglobe.ne.jp/index.php?fn=grs&apid=bkml&from=grsr&url=' + url + '">ウェブリリーダー</option>' + "\n"
           + '  <option value="http://e.my.yahoo.co.jp/config/jp_promo_content?.module=jp_rss&amp;.url=' + url + '">My Yahoo!</option>' + "\n"
           + '  <option value="http://fusion.google.com/add?feedurl='                    + url + '">Google</option>' + "\n"
           + '  <option value="http://reader.goo.ne.jp/web/bookmarklet.html?,,'          + url + '">goo RSSリーダー</option>' + "\n"
           + '  <option value="http://reader.excite.co.jp/subscribe/?url='               + encodeURIComponent(url) + '">exciteリーダー</option>' + "\n"
           + '  <option value="http://paipo.jp/bookmarklet/?url='                        + url + '">PAIPO READER</option>' + "\n"
           + '  <option value="http://www.newsgator.com/ngs/subscriber/subext.aspx?url=' + url + '">NewsGator</option>' + "\n"
           + '  <option value="http://www.rojo.com/add-subscription?resource='           + url + '">Rojo</option>' + "\n"
           + '</select> '
           + 'に <input value="登録" type="button" onclick="window.open(document.getElementById(\'' + uniqueid + '\').value, \'_blank\');" style="font-size:11px;" />' + "\n";
  
  document.open();
  document.write(html);
  document.close();
}
