var t;

function back_to_top()
{
    Effect.ScrollTo('top');
}

function scroll_to_edges()
{
    Effect.ScrollTo('more_edges');
}

function addPrompt(element, text, options) {
    options=Object.extend({promptClassName:'prompt',
	hoverClassName:'hover',activeClassName:'active'},options||{});
    element=$(element);

    var focus=function() {
	if(element.value==text){element.value='';
	}
	Element.removeClassName(element,options.promptClassName);
	Element.removeClassName(element,options.hoverClassName);
	Element.addClassName(element,options.activeClassName);
    };

    var blur=function() {
	if(element.value==''||element.value==text){element.value=text;
	    Element.addClassName(element,options.promptClassName);
	}
	Element.removeClassName(element,options.activeClassName);
    }
}

function resizeImg(img_id, s)
{
    var img = $(img_id);
    var h = img.height;
    var w = img.width;
    if (h>w || w==0) {
	img.height = s;
    } else {
	img.width  = s;
    }
    img.show();
}

function tgl_danup(t)
{
    var disp = $('danup_after_'+t).getStyle('display');
    if (disp == 'none') {
	$('danup_after_'+t).show();
	$('danup_link_text_'+t).hide();
	$('danup_close_text_'+t).show();
    } else {
	$('danup_after_'+t).hide();
	$('danup_link_text_'+t).show();
	$('danup_close_text_'+t).hide();
    }
}

function hide(id)
{
    $(id).hide();
}

// 名前チェック
function name_check()
{
    var urle_q = $('urle_q').innerHTML;    
    var url    = "/namecheck/rate/"+urle_q;
    var myAjax = new Ajax.Updater('name_check', url, {method: 'get'});    
}

function callKeyrel(node_id)
{
    var url = '/keyrel/index/'+node_id;
    var myAjax = new Ajax.Updater('keyrel', url, {method: 'get'});
}

//////// 検索

var search_example_text = '氏名・キーワードで人物を検索';

function ch_search_text()
{
  if ($('q').value == '') {
    $('q').value = search_example_text;
    new Element.setStyle($('q'), {'color':'#999999'});    
  }
  else {
    new Element.setStyle($('q'), {'color':'black'});        
  }
}

function on_focus_search_box()
{
  if ($F('q') == search_example_text) {
    $('q').value = '';
    new Element.setStyle($('q'), {'color':'black'});            
  }
}

function submitSearch() 
{
    if ($F('q') == '') {return false;}
    if ($F('q') == search_example_text) {return false;}
    var q = $F('q');
    var urle_q = encodeURI( encodeURIComponent(q));
    var url = '/q/'+urle_q;
    window.location.href = url;
    return false;
}

//////// Enterをキャンセルして検索実行
function enterSubmit(evt) 
{
    if (evt.keyCode == 13) {
	// 普通のsubmitはキャンセル
	if (evt.preventDefault) {
	    evt.preventDefault();
	} else {
	    evt.returnValue = false;
	}
        // 実行
	submitSearch();
    }
}

//////////////////////////////////////////////////////////////////////

//////// ビデオ再生
function playVideo(yt_id, width, height)
{
    $('video_box').innerHTML = '<object height="'+height+'" width="'+width+'"><param name="movie" value="http://www.youtube.com/v/'+yt_id+'&amp;hl=en&amp;fs=1&amp;autoplay=1"><param name="allowFullScreen" value="true"><param name="allowscriptaccess" value="always"><embed src="http://www.youtube.com/v/'+yt_id+'&amp;hl=en&amp;fs=1&amp;autoplay=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" height="'+height+'" width="'+width+'"></object>'
    $('video_box').innerHTML += "<div style='text-align:center; margin-top:6px;'><a href='javascript:closeVideo()'>閉じる</a></div>";
    $('video_box').show();
}

//////// タイトルプレビュー
function prevVideoTitle(title)
{
	// $('video_title').title = $('video_title').innerHTML;
	$('video_title').innerHTML = "<font color='green'>"+title+"</font>";
}

//////// ビデオ閉じる
function closeVideo()
{
	$('video_box').innerHTML = '';    
	$('video_box').hide();
}

///////////////////////// タグ関連 /////////////////////

//////// タグ編集モード
function editTag()
{
    var id     = $('node_id').innerHTML;
    var url    = '/tag_ajax/edit/'+id;
    var myAjax = new Ajax.Updater('tag', url, {method: 'get'});	
}

//////// タグ標準モード
function showTag()
{
    var id     = $('node_id').innerHTML;
    var url    = '/tag_ajax/show/'+id;
    var myAjax = new Ajax.Updater('tag', url, {method: 'get'});	
}

//////// タグ消去
function addTag(tag) 
{
    t = setTimeout("clearTimeout(t); addTag_go('"+tag+"');", 500);
}
function addTag_go(tag)
{
    var id   = $("node_id").innerHTML;
    var date = new Date();
    var ts   = date.getTime();
    var url    = '/tag_ajax/add/'+id+'/'+tag;
    var myAjax = new Ajax.Updater('tag', url, {method: 'get'});	
}

//////// タグ消去
function delTag(tag) 
{
    $("tag_del_"+tag).innerHTML = "<font color='red'>削除中...</font>";
    t = setTimeout("clearTimeout(t); delTag_go('"+tag+"');", 500);
}
function delTag_go(tag)
{
    var id   = $("node_id").innerHTML;
    var date = new Date();
    var ts   = date.getTime();
    var url    = '/tag_ajax/del/'+id+'/'+tag;
    var myAjax = new Ajax.Updater('tag', url, {method: 'get'});	
}

//////// 選択追加
function addSelectTag()
{
    var tag = $('tag_add_select').value;
    if (!tag) return 0;
    $('tag_add_select_button').innerHTML = "<font color='blue'>追加中...</font>";
    addTag(tag);
}

//////// 選択追加
function addInputTag()
{
    var tag = $('tag_add_input').value;
    if (!tag) return 0;
    $('tag_add_input_button').innerHTML = "<font color='blue'>追加しています...</font>";
    addTag(encodeURI(tag));
}

/////////////////////// 共通関数 /////////////////////////////

//////// ブラウザチェック
function chkBrowser() 
{
    var apl = navigator.appName;
    var ver = navigator.appVersion;

    if(apl.indexOf("Microsoft") >= 0){
	return 'ie';
    }else if(apl.indexOf("Netscape") >= 0){
	return 'nn';
    }else{
	return apl;
    }
}

//////// SafariでAjax文字化け回避
function get_response_text ( req ) {
    var text = req.responseText;
    if ( navigator.appVersion.indexOf( "KHTML" ) > -1 ) {
	var esc = escape( text );
	if ( esc.indexOf("%u") < 0 && esc.indexOf("%") > -1 ) {
	        text = decodeURIComponent( esc );
	    }
    }
    return text;
}

//////// 隠す
function Hide(id)
{
    $(id).hide();
}

//////// 見せる
function Show(id)
{
    $(id).show();
}

//////// urlEncode
String.prototype.urlEncode = function() {
    return encodeURIComponent(this);
}

/////// シャッフル
function jumpRandom()
{
    var j = $F('j');
    var urle_j = j.urlEncode();
    var url = "/rand/tag/"+urle_j;
    window.location.href=url;
}

//////// オートプレイ切り替え
function tglAutoPlay()
{
    var check = $F('video_autoplay');
    var url = "/cookie/autoplay/"+check;
    var myAjax = new Ajax.Request(url,{method: 'get'}); 
}

function nega_link(id)
{
    Element.setStyle($(id), {'background':'blue', 'color':'white'});
}

function posi_link(id)
{
    Element.setStyle($(id), {'background':'', 'color':'blue'});
}

//////// ハイライト
function hlight(id, color)
{
  var item = $(id);
  new Element.setStyle(item, {'border':'1px '+color+' solid'});
}

//////// はてな顔画像に変更
function changeHatena(id)
{
  $(id).src = '/_img/hatena.jpg';
}
