var glb_word_sent_flg = false;

var Ad = /* {{{ */ {

  Show: function(position,thum){

    var div_position = "";
    if(position == 0){
      div_position = 'header_banner';
    }else if(position == 1){
      div_position = 'right_top_banner';
    }else if(position == 2){
      div_position = 'right_bottom_banner';
    }
    
    new Ajax.Request('/api/home/ad_show/',{
      evalScripts: true,
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
	    parameters: 'position='+position+'&thum='+thum+'&div_position='+div_position,
      onComplete: function(request){
        if(request.responseText.charAt(0) == '<' && position == 0){
          $('header_banner').update(request.responseText);
        }else if(request.responseText.charAt(0) == '<' && position == 1){
          $('right_top_banner').update(request.responseText);
        }else if(request.responseText.charAt(0) == '<' && position == 2){
          $('right_bottom_banner').update(request.responseText);
        }
      }
    });
  }
} /* }}} */

var User = /* {{{ */ {
  ChangePasswordDialog: function(){
    this.dialog = new Window({url:"/popup/change_password",width:"400"});
    this.dialog.setDestroyOnClose();
    this.dialog.showCenter(true);
  },
  LoginDialog: function(){
    this.dialog = new Window({url:"/popup/page_login",width:"400"});
    this.dialog.setDestroyOnClose();
    this.dialog.showCenter(true);
  },
  ShowAreaSelect: function(nationality_status,nationality,area){

    new Ajax.Request('/api/home/nationality_select/',{
      evalScripts: true,
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
	    parameters: 'nationality_status='+nationality_status+'&nationality='+nationality+'&area='+area,
      onComplete: function(request){
        if(!nationality){
          nationality = 'jp';
        }
        User.ChangeAreaSelect(nationality,area);
      }
    });
  },
  ChangeAreaSelect: function(nationality,area){
    new Ajax.Request('/api/home/area_select/',{
      evalScripts: true,
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
	    parameters: 'nationality='+nationality+'&area='+area,
      onComplete: function(request){
        $('area_flag').update(request.responseText);
      }
    });
  },
  
  ChangeLocationAreaSelect: function(nationality,area){
    new Ajax.Request('/api/home/location_area_select/',{
      evalScripts: true,
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
	    parameters: 'nationality='+nationality+'&area='+area,
      onComplete: function(request){
        $('location_area_flag').update(request.responseText);
      }
    });
  },
  
  ChangeSearchLocationAreaSelect: function(nationality,selected_value,nationality_name,area_name){

    new Ajax.Request('/api/home/search_location_area_select/',{
      evalScripts: true,
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
	    parameters: 'nationality='+nationality+'&area='+selected_value+'&nationality_name='+nationality_name+'&area_name='+area_name,
      onComplete: function(request){
        $('location_area_flag').update(request.responseText);
      }
    });
  },
  
  Block: function(user_name){
  
    this.dialog = new Window({url:"/popup/block/" + user_name,width:"400"});
    this.dialog.setDestroyOnClose();
    this.dialog.showCenter(true);
    
//    if( $('block_link').hasClassName('blocking') ){
//      if( !confirm(MSG_CONFIRM_DELETE_DESCRIPTION) ){
//	      return;
//     }
//    }
//    new Ajax.Request('/api/home/block/'+user_name,{
//      evalScripts: true,
//      onComplete: function(request){
//alert(request.responseText);
//        if( $('block_link').hasClassName('blocking') ){
//          top.location.reload();

//        }
//      }
//    });

  },
  
  // 2009-08-10 Y.Uemura Add.
  DlContents: function(){
    this.dialog = new Window({url:"/popup/dl_contents",width:"700"});
    this.dialog.setDestroyOnClose();
    this.dialog.showCenter(true);
  },

  // 2009-09-08 Y.Uemura Add.
  AboutTicket: function(){
    this.dialog = new Window({url:"/popup/about_ticket",width:700,height:350});
    this.dialog.setDestroyOnClose();
    this.dialog.showCenter(true);
  }
} /* }}} */

var Word = /* {{{ */ {
  rest: 140,
  Watch: function(){
    $('word_rest').update(this.rest-$('word_text').value.length);
  },
  Remove: function(id,url){
    if( !confirm(MSG_CONFIRM_DELETE_DESCRIPTION) ){
      return;
    }

    new Ajax.Request('/api/home/remove_twitter/'+id, {
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
      onComplete: function(request){
//alert(request.responseText);
        if(url){
          self.location.replace(url);
        }
      },
      evalScripts: true
    });
  },
  SetInvisible: function(id){
    if( !confirm(MSG_CONFIRM_INVISIBLE_DESCRIPTION) ){
      return;
    }

    new Ajax.Request('/api/home/set_word_invisible/'+id, {
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
      onComplete: function(request){
//alert(request.responseText);
      },
      evalScripts: true
    });
  },
  Follow: function(user_name){
    if( $('follow_link').hasClassName('following') ){
      //if( !confirm(MSG_CONFIRM_DELETE_DESCRIPTION) ){
      //  return;
      //}
    }
    new Ajax.Request('/api/home/follow/'+user_name,{
      evalScripts: true,
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
      onComplete: function(request){
//alert(request.responseText);

      }
    });
  },

  Translate: function(configs){

    if( configs.length<=0 ){
      return;
    }
//    if( $('word_description_translate') ){
//      $('word_description_translate').update('');
//    }
    var emoji = '';
    var notranslate = '';
//alert("Translate:"+Object.toJSON(configs));
    this.tasks = configs;
    var task = configs.shift();
//alert("task:"+Object.toJSON(task));
    if( task ){
      var target = $(task.target);
      var change = task.change;
      var mode = task.mode;
      var translate_prefix = task.translate_prefix;
      if (typeof translate_prefix == 'undefined' || translate_prefix == 'undefined'){
        translate_prefix = '';
      }
      
      emoji = task.emoji;
      notranslate = task.notranslate;
      if (typeof emoji == 'undefined' || emoji == 'undefined'){
        emoji = '';
      }
      if(target.tagName.toLowerCase()=="textarea"){
	      target = target.value;
      }else if(target.tagName.toLowerCase()=="input" && target.type.toLowerCase()=="text" ){
	      target = target.value;
      }
      if( !target ){
	      return;
      }
      var modes = mode.split("_");
      if( $("word_machine_translate"+translate_prefix+"_"+modes[1]) && !notranslate){
	      $("word_machine_translate"+translate_prefix+"_"+modes[1]).value = 1;
      }
      
      if(!notranslate){
        notranslate = "";
      }

      var chara = String.fromCharCode(8203);
      target = target.split(chara).join("");
	  
      new Ajax.Request('/api/home/translate',{
        parameters: "target="+encodeURIComponent(target)+"&mode="+mode+"&emoji="+emoji+"&notranslate="+notranslate,
        method: 'POST',
        requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
        onComplete:function(request){
          var responseText = request.responseText;
          
          if(task.htmlentity){
	          responseText = responseText.replace(RegExp("&lt;","g"), "<");
	          responseText = responseText.replace(RegExp("&gt;","g"), ">");
          }
          if( $(change).tagName.toLowerCase()=="textarea" || ($(change).tagName.toLowerCase()=="input" && $(change).type.toLowerCase()=="text")){
            $(change).value = responseText;
          }else{
            $(change).update(responseText);
          }
          Word.Translate(configs);
        }
      });
    }
  },
  
  TranslateSubmit: function(from_org,to_org,form,prefix,no_translate){

    if(!prefix){
      prefix = 'word_description_';
    }
    var mode_from_org = prefix + from_org;
    var mode_to_org = prefix + to_org;
    var mode_from = prefix + from_org;
    var mode_to = prefix + to_org;
    
    
	  var chara = String.fromCharCode(8203);
	  $(mode_from).value = $(mode_from).value.split(chara).join("");
	  $(mode_to).value = $(mode_to).value.split(chara).join("");
	  
	  
    if(!$(mode_from).value && !$(mode_to).value){
      form.submit();
      return;
    }
    if($(mode_from).value && $(mode_to).value){
  
      form.submit();
      return;
    }
    // change mode 
    var from;
    var to;
    if($(mode_from).value && !$(mode_to).value){
      from = from_org;
      mode_from = prefix + from_org;
      to = to_org;
      mode_to = prefix + to_org;
    }
    if($(mode_to).value && !$(mode_from).value){
  	  from = to_org;
      mode_from = prefix + to_org;
		  to = from_org;
      mode_to = prefix + from_org;
		}
		mode = from + "_" + to;

		$(mode_to).readOnly = true;

    //if( $('word_description_translate') ){
    //  $('word_description_translate').update('');
    //}
    var emoji = '';
  
    var target = $(mode_from);
    var change = mode_to;
    var emoji = '';
    if (typeof translate_prefix == 'undefined' || translate_prefix == 'undefined'){
      translate_prefix = '';
    }
      
    if(target.tagName.toLowerCase()=="textarea"){
     target = target.value;
    }else if(target.tagName.toLowerCase()=="input" && target.type.toLowerCase()=="text" ){
     target = target.value;
    }
    if( !target ){
     return;
    }
    
    if( $("word_machine_translate"+translate_prefix+"_"+to_org) && !$(mode_to_org).value ){
      $("word_machine_translate"+translate_prefix+"_"+from_org).value = 0;
      $("word_machine_translate"+translate_prefix+"_"+to_org).value = 1;
    }else if( $("word_machine_translate"+translate_prefix+"_"+from_org) && !$(mode_from_org).value ){
      $("word_machine_translate"+translate_prefix+"_"+from_org).value = 1;
      $("word_machine_translate"+translate_prefix+"_"+to_org).value = 0;
    }
    
    glb_word_sent_flg = false;
    this.try_count = 0;
    this.send_form = form;
    if(no_translate){
      this.no_translate = true;
    }else{
      this.no_translate = false;
    }  
    
	  var chara = String.fromCharCode(8203);
	  target = target.split(chara).join("");
	  
    new Ajax.Request('/api/home/translate',{
			parameters: "target="+encodeURIComponent(target)+"&mode="+mode+"&emoji="+emoji,
			method: 'POST',
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
			onComplete:function(request){
			
			  if(!this.no_translate){
				  if( $(change).tagName.toLowerCase()=="textarea" || $(change).tagName.toLowerCase()=="input"&&$(change).type.toLowerCase()=="text" ){
				    $(change).value = request.responseText;
				  }else{
				    $(change).update(request.responseText);
				  }
				}
        if(!glb_word_sent_flg){
			    glb_word_sent_flg = true;
				  form.submit();
				}
			},
		 
      onFailure: function(request) {  
        if(!glb_word_sent_flg){
  			  glb_word_sent_flg = true;
	  			form.submit();
	  	  }
      }
    });
    
    this.TranslateSetTimeOut(this.try_count);
  },
  
  TranslateSetTimeOut: function(try_count){
    setTimeout("Word.TranslateWait('"+try_count+"')",1000);
  },
  
  TranslateWait: function(try_count){
    try_count++;
    if(!try_count){
    }else if(try_count > 0 && try_count * 1000 >= TRANSLATE_WAIT_TIME * 1000) {
      if(!glb_word_sent_flg){
        this.send_form.submit();
        this.no_translate = true;
        glb_word_sent_flg = true;
      }
    }else{
      Word.TranslateSetTimeOut(try_count);
    }
  },
  
  Update: function(id,mode){
//alert("Word.Update("+id+","+mode+")");
    var target,change;
    if( mode=="jp_cn" ){
      target = $(id+"_jp").innerHTML;
      change = id+"_cn";
    }else if( mode=="cn_jp" ){
      target = $(id+"_cn").innerHTML;
      change = id+"_jp";
    }
    new Ajax.Updater(change,'/api/home/translate/'+id, {
//      parameters: "target="+encodeURIComponent(target)+"&mode="+mode,
      parameters: "mode="+mode,
      method: 'GET'
    });
//alert(change);
    var img_html = $('translate_'+id).innerHTML;
    if( img_html.match(/left/) ){
      img_html = '/images/gray_left_arrow.gif';
    }else{
      img_html = '/images/gray_right_arrow.gif';
    }
    new Insertion.Before('translate_'+id,'<img src="'+img_html+'" />');
    Element.remove($('translate_'+id));
    if( mode=="jp_cn" ){
      $('machine_translate_cn_'+id).show();
    }else{
      $('machine_translate_jp_'+id).show();
    }
  },

  UpdateNew: function(id,mode){
//alert("Word.Update("+id+","+mode+")");
    var target,change;
    if( mode=="jp_cn" ){
      target = $(id+"_jp").innerHTML;
      change = id+"_cn";
    }else if( mode=="cn_jp" ){
      target = $(id+"_cn").innerHTML;
      change = id+"_jp";
    }
    new Ajax.Updater(change,'/api/home/translate/'+id, {
//      parameters: "target="+encodeURIComponent(target)+"&mode="+mode,
      parameters: "mode="+mode+"&emoji=1",
      method: 'GET'
    });
    
    // 翻訳後のクラスに書き換える
    //var translated = $(id);
    //alert($(id));
    //if ($(id).hasClassName("pt2")) {
    //  $(id).removeClassName("pt2"); 
    //  $(id).addClassName("pt1"); 
    //}
    //if ($(id).hasClassName("pt4")) {
    //  $(id).removeClassName("pt4");  
    //  $(id).addClassName("pt3"); 
    //}
    
//alert(change);
    var img_html = $('translate_'+id).innerHTML;
    if( img_html.match(/event/) ){
      img_html = '/images/event01_img_lobo.gif';
    }else{
      img_html = '/images/common/ico_logo_lobo01.gif';
    }
    new Insertion.Before('translate_'+id,'<img src="'+img_html+'" />');
    Element.remove($('translate_'+id));
  },
  
  //2008.10.08 syuumitsu
  UpdateNew_news: function(id,mode){
//alert("Word.Update("+id+","+mode+")");
    var target,change;
    if( mode=="jp_cn" ){
      target = $(id+"_jp").innerHTML;
      change = id+"_cn";
    }else if( mode=="cn_jp" ){
      target = $(id+"_cn").innerHTML;
      change = id+"_jp";
    }
    new Ajax.Updater(change,'/api/home/translate/'+id, {
//      parameters: "target="+encodeURIComponent(target)+"&mode="+mode,
      parameters: "mode="+mode,
      method: 'GET'
    });
    
//alert(change);
    var img_html = $('translate_'+id).innerHTML;
    if( img_html.match(/left/) ){
      img_html = '/images/icons/ico_logo_lobo01.gif';
    }else{
      img_html = '/images/icons/ico_logo_lobo01.gif';
    }
    new Insertion.Before('translate_'+id,'<img src="'+img_html+'" />');
    Element.remove($('translate_'+id));
  },

//2008.10.09 syuumitsu
  UpdateNew_answers: function(id,mode){
//alert("Word.Update("+id+","+mode+")");
    var target,change;
    if( mode=="jp_cn" ){
      target = $(id+"_jp").innerHTML;
      change = id+"_cn";
    }else if( mode=="cn_jp" ){
      target = $(id+"_cn").innerHTML;
      change = id+"_jp";
    }
    new Ajax.Updater(change,'/api/home/translate/'+id, {
//      parameters: "target="+encodeURIComponent(target)+"&mode="+mode,
      parameters: "mode="+mode,
      method: 'GET'
    });
//alert(change);
    var img_html = $('translate_'+id).innerHTML;
    if( img_html.match(/left/) ){
      img_html = '/images/icons/ico_logo_lobo01.gif';
    }else{
      img_html = '/images/icons/ico_logo_lobo01.gif';
    }
    new Insertion.Before('translate_'+id,'<img src="'+img_html+'" />');
    Element.remove($('translate_'+id));
  },

  UpdateVertical: function(id,mode){
//alert("Word.Update("+id+","+mode+")");
    new Ajax.Request('/api/home/translate/'+id, {
      parameters: "target=&mode="+mode,
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
      method: 'GET',
      onComplete: function(request){
	      location.href = location.href;
      }
    });
  },

  // つぶやきのお気に入りを登録 2009-02-17 Y.Uemura Add.
  Clip: function(id, mode){
    new Ajax.Request('/api/home/word_clip/'+id, {
      //parameters: "mode="+mode,
      method: 'GET',
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
      onComplete: function(request){
        var img_html = $('clip_'+id).innerHTML;
        var img_class = "";
        var small_js = "";
        var msg_clip_text = MSG_CLIP;
        if(mode == 'small'){
          img_class="ico_cliped_small";
          img_html = '/images/common/ico_cliped_small.gif';
          small_js = ',\'small\'';
          msg_clip_text = '';
        }else{
          img_class="ico_cliped";
          img_html = '/images/common/ico_cliped.gif';
        }
        new Insertion.After('clip_'+id,'<a href="#" id="clip_'+id+'" onClick="Word.UnClip(\''+id+'\''+small_js+'); return false;">'+msg_clip_text+'<img class="'+img_class+'" src="'+img_html+'" /></a>');
        Element.remove($('clip_'+id));
      }
    });
  },
  
  // つぶやきのお気に入りを解除 2009-02-17 Y.Uemura Add.
  UnClip: function(id, mode){
    new Ajax.Request('/api/home/word_unclip/'+id, {
      //parameters: "mode="+mode,
      method: 'GET',
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
      onComplete: function(request){
        var img_html = $('clip_'+id).innerHTML;
        var img_class = "";
        var small_js = "";
        var msg_clip_text = MSG_CLIP;
        if(mode == 'small'){
          img_class="ico_clip_small";
          img_html = '/images/common/ico_clip_small.gif';
          small_js = ',\'small\'';
          msg_clip_text = '';
        }else{
          img_class="ico_clip";
          img_html = '/images/common/ico_clip.gif';
        }
        new Insertion.After('clip_'+id,'<a href="#" id="clip_'+id+'" onClick="Word.Clip(\''+id+'\''+small_js+'); return false;">'+msg_clip_text+'<img class="'+img_class+'" src="'+img_html+'" /></a>');
        Element.remove($('clip_'+id));
      }
    });
  },

  // 2009-04-14 I.Yamaguchi Add.
  Watching: function(id, mode){
    new Ajax.Request('/api/home/word_watch/'+id, {
      //parameters: "mode="+mode,
      method: 'GET',
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
      onComplete: function(request){
        var img_html = $('watch_'+id).innerHTML;
        var mode_js = '';
        if(mode == 'list'){
          img_html = '/images/common/ico_watch.gif';
          mode_js = ',\'list\'';
          new Insertion.After('watch_'+id,'<a href="#" id="watch_'+id+'" onClick="Word.UnWatch(\''+id+'\''+mode_js+'); return false;">'+MSG_WATCHING_OFF+'<img src="'+img_html+'" /></a>');
        }else{
          img_html = '/images/home/btn_watching_active'+((SITE_LANGUAGE == 'cn')?'_cn':'')+'.gif';
          new Insertion.After('watch_'+id,'<a href="#" id="watch_'+id+'" onClick="Word.UnWatch(\''+id+'\'); return false;">'+'<img src="'+img_html+'" /></a>');
        }
        Element.remove($('watch_'+id));
      }
    });
  },
  
  // 2009-04-14 I.Yamaguchi Add.
  UnWatch: function(id, mode){
    new Ajax.Request('/api/home/word_unwatch/'+id, {
      //parameters: "mode="+mode,
      method: 'GET',
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
      onComplete: function(request){
        var img_html = $('watch_'+id).innerHTML;
        var mode_js = '';
        if(mode == 'list'){
          img_html = '/images/common/ico_watch.gif';
          mode_js = ',\'list\'';
          new Insertion.After('watch_'+id,'<a href="#" id="watch_'+id+'" onClick="Word.Watching(\''+id+'\''+mode_js+'); return false;">'+MSG_WATCHING_ON+'<img src="'+img_html+'" /></a>');
        }else{
          img_html = '/images/home/btn_watching'+((SITE_LANGUAGE == 'cn')?'_cn':'')+'.gif';
          new Insertion.After('watch_'+id,'<a href="#" id="watch_'+id+'" onClick="Word.Watching(\''+id+'\'); return false;">'+'<img src="'+img_html+'" /></a>');
        }
        Element.remove($('watch_'+id));
      }
    });
  },

  CheckboxConfirmTranslate: function(){
    var oCookie = new Cookie();
//alert($('translate_confirmation').checked+","+document.word_form.translate_confirmation[1].checked);
//alert("CheckboxConfirmTranslate:"+$('translate_confirmation_checkbox').checked);
    if( $('translate_confirmation_checkbox').checked ){
      $('translate_confirmation_panel').show();
      oCookie.set('translate_confirmation_checked',1);
    }else{
      $('translate_confirmation_panel').hide();
      oCookie.set('translate_confirmation_checked',0);
    }
  },
  InitCheckboxConfirmTranslate: function(){
    var oCookie = new Cookie();
    if( oCookie.get('translate_confirmation_checked')==1 ){
      $('translate_confirmation_checkbox').checked = "checked";
    }
    Word.CheckboxConfirmTranslate();
  },
  InsertEmoji: function(target){
    this.dialog = new Window({url:"/popup/insert_emoji/" + target,width:"400"});
    this.dialog.setDestroyOnClose();
    this.dialog.showCenter(true);
  },
  PickupEmoji: function(target, carrier, emoji){
    var str = "[icon src='"+carrier+":"+emoji+"']";
    
    this.GetTextareaSelect(str, target);
        
  },  
  InsertLink: function(target){
		var target_obj = self.$(target);
		var pos = this.getTextareaSelectPos(target_obj); 
		if(pos.end == pos.start){
		  alert(MSG_ALERT_NEED_SELECTED_TEXT);
		}else{
      this.dialog = new Window("insert_link_window",{url:"/popup/insert_link?id=" + target + "&start="+pos.start + "&end="+pos.end,top:"-10px",left:"-10px",width:"400",height:"150"});
      this.dialog.setDestroyOnClose();
      this.dialog.showCenter(true);
    }
  },
  
  ModifyLink: function(target,value,start,end){
  
		var target = parent.$(target);
		  
		var val = target.value; 
		var range = val.slice(start, end); 
		var beforeNode = val.slice(0, start); 
		var afterNode = val.slice(end); 
		var insertNode; 
		var tag = 'a';
		
    var str = value.toString();
		var regex = new RegExp("^http:\/\/", "gi"); 
    if (!str.match(regex)){
      value = 'http://' + value;
    } 
		
		if ((range || start != end) && value) { 
			insertNode = '[' + tag + ' href="'+value.replace("'", "%27")+'"'+']' + range + '[/' + tag + ']'; 
			target.value = beforeNode + insertNode + afterNode; 
		} else if ((start == end) && value) { 
			insertNode = '[' + tag + ' href="'+value.replace("'", "%27")+'"'+']' + '[/' + tag + ']'; 
			target.value = beforeNode + insertNode + afterNode; 
		} 
  
  },

  InsertIMG: function(target){
		var target_obj = self.$(target);
		var pos = this.getTextareaSelectPos(target_obj); 
		
    this.dialog = new Window("insert_img_window",{url:"/popup/insert_img?id=" + target + "&start="+pos.start + "&end="+pos.end,top:"-10px",left:"-10px",width:"400",height:"150"});
    this.dialog.setDestroyOnClose();
    this.dialog.showCenter(true);
  },
  
  ModifyIMG: function(target,value,start,end){
  
		var target = parent.$(target);
		  
		var val = target.value; 
		var range = val.slice(start, end); 
		var beforeNode = val.slice(0, start); 
		var afterNode = val.slice(end); 
		var insertNode; 
		var tag = 'img';
		
    var str = value.toString();
		var regex = new RegExp("^http:\/\/", "gi"); 
    if (!str.match(regex)){
      value = 'http://' + value;
    } 
		
		if ((range || start != end) && value) { 
			insertNode = '[' + tag + ' src="'+value.replace("'", "%27")+'"'+'/]' + range; 
			target.value = beforeNode + insertNode + afterNode; 
		} else if ((start == end) && value) { 
			insertNode = '[' + tag + ' src="'+value.replace("'", "%27")+'"'+'/]'; 
			target.value = beforeNode + insertNode + afterNode; 
		} 
  
  },

  InsertYoutube: function(target){
        var target_obj = top.$(target);
        var pos = this.getTextareaSelectPos(target_obj); 
    this.dialog = new Window({url:"/popup/insert_youtube?id=" + target + "&start="+pos.start + "&end="+pos.end,width:"500",height:"400"});
    this.dialog.setDestroyOnClose();
    this.dialog.showCenter(true);
  },
  
  ModifyYoutube: function(target,value,start,end){
  
        var target = top.$(target);
          
        var val = target.value; 
        var range = val.slice(start, end); 
        var beforeNode = val.slice(0, start); 
        var afterNode = val.slice(end); 
        var insertNode; 
        if ((range || start != end) && value) { 
            insertNode  = "<youtube id='"+value+"'>"+range;
            target.value = beforeNode + insertNode + afterNode; 
      
        } else if ((start == end) && value) { 
            insertNode  = "<youtube id='"+value+"'>";
            target.value = beforeNode + insertNode + afterNode; 
        } 
  
  },
  
  InsertFlickr: function(target){
		var target_obj = top.$(target);
		var pos = this.getTextareaSelectPos(target_obj); 
    this.dialog = new Window({url:"/popup/insert_flickr?id=" + target + "&start="+pos.start + "&end="+pos.end,width:"400"});
    this.dialog.setDestroyOnClose();
    this.dialog.showCenter(true);
  },
  
  ModifyFlickr: function(target,value,start,end){
  
		var target = top.$(target);
		  
		var val = target.value; 
		var range = val.slice(start, end); 
		var beforeNode = val.slice(0, start); 
		var afterNode = val.slice(end); 
		var insertNode; 
    var tag = 'img';
    if ((range || start != end) && value) { 
      insertNode = '[' + tag + " src='"+value.replace("'", "?")+"'"+' /]' + range; 
      target.value = beforeNode + insertNode + afterNode; 
    } else if ((start == end) && value) { 
      insertNode = '[' + tag + " src='"+value.replace("'", "?")+"'"+' /]'; 
      target.value = beforeNode + insertNode + afterNode; 
		} 
  
  },
  
  InsertBr: function(target){
		var target_obj = self.$(target);
		var pos = this.getTextareaSelectPos(target_obj); 
      
    var val = target_obj.value; 
    var range = val.slice(pos.start, pos.end); 
    var beforeNode = val.slice(0, pos.start); 
    var afterNode = val.slice(pos.end); 
    var insertNode; 
    if ((range || pos.start != pos.end)) { 
        insertNode = "[br]" + range;
        target_obj.value = beforeNode + insertNode + afterNode; 
    } else if ((pos.start == pos.end)) { 
        insertNode  = "[br]";
        target_obj.value = beforeNode + insertNode + afterNode; 
    } 
  
  },
  
  
  getTextareaSelectPos: function(target){
//		var obj = top.$(target);
    var obj = target;
    
		var pos = new Object(); 
		if(navigator.appName.indexOf("Microsoft") > -1){
		  obj.focus(); 
			if (!document.selection) return;
		  var range = document.selection.createRange(); 
		  var clone = range.duplicate(); 
		  
		  clone.moveToElementText(obj); 
		  clone.setEndPoint( 'EndToEnd', range ); 
		   
		  pos.start = clone.text.length - range.text.length; 
		  pos.end = clone.text.length - range.text.length + range.text.length; 
		} else if(window.getSelection()) { 
		  pos.start = obj.selectionStart; 
		  pos.end = obj.selectionEnd; 
		} 
		
		return pos; 
		// alert(pos.start + "," + pos.end);  
  },
  GetTextareaSelect: function(InTexts,elements){
    var Txtarea;
  	Txtarea = self.$(elements);
		InTexts = '' + InTexts + '';
		if(navigator.appName.indexOf("Microsoft") > -1){
			Txtarea.focus();
			if (!document.selection) return;
			var range = document.selection.createRange();
			range.text=InTexts;
			Txtarea.focus();
		} else {
			if (Txtarea.createTextRange && Txtarea.caretPos) {
			  var caretPos = Txtarea.caretPos;
			  caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == '' ? InTexts + '' : InTexts;
				
				Txtarea.focus();
			} else {
				var length = Txtarea.textLength;
				var start = Txtarea.selectionStart;
				var end = Txtarea.selectionEnd;
				var Txtarea1 = Txtarea.value.substring(0, start);
				var Txtarea2 = Txtarea.value.substr(end, length);
				Txtarea.value = Txtarea1 + InTexts + Txtarea2;
				Txtarea.focus();
		  }
		}
  },
  // 2009-05-28 S.Onuma Add.
  ResTwitter: function(id){

    if(top.$('adspace')){
      top.$('adspace').style.display='none';
    }
    this.dialog = new Window("twitter_window",{url:'/word/twitter_word/'+id,width:'670px',height:'420px',margin:'0px',padding:'0px',zIndex:10000});
    this.dialog.setDestroyOnClose();
    this.dialog.showCenter(true);
    this.dialog.setCloseCallback(this.ReTwitterClose);

  },
  
  ReTwitterClose: function(){
    if(top.$('adspace')){
      top.$('adspace').style.display='block';
    }
    return true;
  }
  
} /* }}} */

var Answer = /* {{{ */ {
  Update: function(id,mode){
//alert("Word.Update("+id+","+mode+")");
    var target,change;
    if( mode=="jp_cn" ){
      target = $(id+"_jp").innerHTML;
      change = id+"_cn";
    }else if( mode=="cn_jp" ){
      target = $(id+"_cn").innerHTML;
      change = id+"_jp";
    }
//alert(change);
    new Ajax.Updater(change,'/api/home/translate/'+id, {
//      parameters: "target="+encodeURIComponent(target)+"&mode="+mode,
      parameters: "mode="+mode,
      method: 'GET'
    });

    var img_html = $('translate_'+id).innerHTML;
    if( img_html.match(/left/) ){
      img_html = '/images/gray_left_arrow.gif';
    }else{
      img_html = '/images/gray_right_arrow.gif';
    }
    new Insertion.Before('translate_'+id,'<img src="'+img_html+'" />');
    Element.remove($('translate_'+id));
    if( mode=="jp_cn" ){
      $('machine_translate_cn_'+id).show();
    }else{
      $('machine_translate_jp_'+id).show();
    }
  },
  
  UpdateNew: function(id,mode){
//alert("Word.Update("+id+","+mode+")");
    var target,change;
    if( mode=="jp_cn" ){
      target = $(id+"_jp").innerHTML;
      change = id+"_cn";
    }else if( mode=="cn_jp" ){
      target = $(id+"_cn").innerHTML;
      change = id+"_jp";
    }
    new Ajax.Updater(change,'/api/home/translate/'+id, {
//      parameters: "target="+encodeURIComponent(target)+"&mode="+mode,
      parameters: "mode="+mode+"&emoji=1",
      method: 'GET'
    });
//alert(change);
    var img_html = $('translate_'+id).innerHTML;
    if( img_html.match(/left/) ){
      img_html = '/images/img_lobo01.gif';
    }else{
      img_html = '/images/img_lobo01.gif';
    }
    new Insertion.Before('translate_'+id,'<img src="'+img_html+'" />');
    Element.remove($('translate_'+id));
  },
  
  Remove: function(id,url){
    if( !confirm(MSG_CONFIRM_DELETE_DESCRIPTION) ){
      return;
    }
    new Ajax.Request('/api/home/remove_question/'+id, {
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
      onComplete: function(request){
        if(url){
          self.location.replace(url);
        }
//alert(request.responseText);
      },
      evalScripts: true
    });
  },
  RemoveAnswer: function(id,url){
    if( !confirm(MSG_CONFIRM_DELETE_DESCRIPTION) ){
      return;
    }

    new Ajax.Request('/api/home/remove_answer/'+id, {
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
      onComplete: function(request){
        if(url){
          self.location.replace(url);
        }
//alert(request.responseText);
      },
      evalScripts: true
    });
  },
  
  // お気に入りの質問を登録 2009-03-16 Y.Uemura Add.
  Clip: function(id, mode){
    new Ajax.Request('/api/home/question_clip/'+id, {
      //parameters: "mode="+mode,
      method: 'GET',
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
      onComplete: function(request){
        var img_html = $('clip_'+id).innerHTML;
        var img_class = "";
        var small_js = "";
        var msg_clip_text = MSG_CLIP;
        if(mode == 'small'){
          img_class="ico_cliped_small";
          img_html = '/images/common/ico_cliped_small.gif';
          small_js = ',\'small\'';
          msg_clip_text = '';
        }else{
          img_class="ico_cliped";
          img_html = '/images/common/ico_cliped.gif';
        }        new Insertion.After('clip_'+id,'<a href="#" id="clip_'+id+'" onClick="Answer.UnClip(\''+id+'\''+small_js+'); return false;">'+msg_clip_text+'<img class="'+img_class+'" src="'+img_html+'" /></a>');
        Element.remove($('clip_'+id));
      }
    });
  },
  
  // お気に入りの質問を解除 2009-03-16 Y.Uemura Add.
  UnClip: function(id, mode){
    new Ajax.Request('/api/home/question_unclip/'+id, {
      //parameters: "mode="+mode,
      method: 'GET',
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
      onComplete: function(request){
        var img_html = $('clip_'+id).innerHTML;
        var img_class = "";
        var small_js = "";
        var msg_clip_text = MSG_CLIP;
        if(mode == 'small'){
          img_class="ico_clip_small";
          img_html = '/images/common/ico_clip_small.gif';
          small_js = ',\'small\'';
          msg_clip_text = '';
        }else{
          img_class="ico_clip";
          img_html = '/images/common/ico_clip.gif';
        }
        new Insertion.After('clip_'+id,'<a href="#" id="clip_'+id+'" onClick="Answer.Clip(\''+id+'\''+small_js+'); return false;">'+msg_clip_text+'<img class="'+img_class+'" src="'+img_html+'" /></a>');
        Element.remove($('clip_'+id));
      }
    });
  },
  
  // お気に入りの回答を登録 2009-03-16 Y.Uemura Add.
  AnswerClip: function(id, mode){
    new Ajax.Request('/api/home/answer_clip/'+id, {
      //parameters: "mode="+mode,
      method: 'GET',
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
      onComplete: function(request){
        var img_html = $('clip_'+id).innerHTML;
        var img_class = "";
        var small_js = "";
        var msg_clip_text = MSG_CLIP;
        if(mode == 'small'){
          img_class="ico_cliped_small";
          img_html = '/images/common/ico_cliped_small.gif';
          small_js = ',\'small\'';
          msg_clip_text = '';
        }else{
          img_class="ico_cliped";
          img_html = '/images/common/ico_cliped.gif';
        }
        new Insertion.After('clip_'+id,'<a href="#" id="clip_'+id+'" onClick="Answer.AnswerUnClip(\''+id+'\''+small_js+'); return false;">'+msg_clip_text+'<img class="'+img_class+'" src="'+img_html+'" /></a>');
        Element.remove($('clip_'+id));
      }
    });
  },
  
  // お気に入りの回答を解除 2009-03-16 Y.Uemura Add.
  AnswerUnClip: function(id, mode){
    new Ajax.Request('/api/home/answer_unclip/'+id, {
      //parameters: "mode="+mode,
      method: 'GET',
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
      onComplete: function(request){
        var img_html = $('clip_'+id).innerHTML;
        var img_class = "";
        var small_js = "";
        var msg_clip_text = MSG_CLIP;
        if(mode == 'small'){
          img_class="ico_clip_small";
          img_html = '/images/common/ico_clip_small.gif';
          small_js = ',\'small\'';
          msg_clip_text = '';
        }else{
          img_class="ico_clip";
          img_html = '/images/common/ico_clip.gif';
        }
        new Insertion.After('clip_'+id,'<a href="#" id="clip_'+id+'" onClick="Answer.AnswerClip(\''+id+'\''+small_js+'); return false;">'+msg_clip_text+'<img class="'+img_class+'" src="'+img_html+'" /></a>');
        Element.remove($('clip_'+id));
      }
    });
  },
  // 2009-04-14 I.Yamaguchi Add. 2009-04-15 S.Onuma Mod. 
  Watching: function(id, mode){
    new Ajax.Request('/api/home/question_watch/'+id, {
      //parameters: "mode="+mode,
      method: 'GET',
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
      onComplete: function(request){
        var img_html = $('watch_'+id).innerHTML;
        var mode_js = '';
        if(mode == 'list'){
          img_html = '/images/common/ico_watch.gif';
          mode_js = ',\'list\'';
          new Insertion.After('watch_'+id,'<a href="#" id="watch_'+id+'" onClick="Answer.UnWatch(\''+id+'\''+mode_js+'); return false;">'+MSG_WATCHING_OFF+'<img src="'+img_html+'" /></a>');
          
        }else{
          img_html = '/images/home/btn_watching_active'+((SITE_LANGUAGE == 'cn')?'_cn':'')+'.gif';
          new Insertion.After('watch_'+id,'<a href="#" id="watch_'+id+'" onClick="Answer.UnWatch(\''+id+'\'); return false;">'+'<img src="'+img_html+'" /></a>');
        }
        Element.remove($('watch_'+id));
      }
    });
  },
  
  // 2009-04-14 I.Yamaguchi Add. 2009-04-15 S.Onuma Mod.
  UnWatch: function(id, mode){
    new Ajax.Request('/api/home/question_unwatch/'+id, {
      //parameters: "mode="+mode,
      method: 'GET',
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
      onComplete: function(request){
        var img_html = $('watch_'+id).innerHTML;
        var mode_js = '';
        if(mode == 'list'){
          img_html = '/images/common/ico_watch.gif';
          mode_js = ',\'list\'';
          new Insertion.After('watch_'+id,'<a href="#" id="watch_'+id+'" onClick="Answer.Watching(\''+id+'\''+mode_js+'); return false;">'+MSG_WATCHING_ON+'<img src="'+img_html+'" /></a>');
        }else{
          img_html = '/images/home/btn_watching'+((SITE_LANGUAGE == 'cn')?'_cn':'')+'.gif';
          new Insertion.After('watch_'+id,'<a href="#" id="watch_'+id+'" onClick="Answer.Watching(\''+id+'\'); return false;">'+'<img src="'+img_html+'" /></a>');
        }
        Element.remove($('watch_'+id));
      }
    });
  },
  
  // 2009-05-28 S.Onuma Add.
  ResTwitter: function(user_name,id){

    if(top.$('adspace')) { 
      top.$('adspace').style.display='none';
    }
    
    this.dialog = new Window("twitter_window",{url:'/answer/user/'+user_name+'/twitter_answer/'+id,width:670,height:420,margin:'0px',padding:'0px',zIndex:10000});
    this.dialog.setDestroyOnClose();
    this.dialog.showCenter(true);
    this.dialog.setCloseCallback(this.ReTwitterClose);

  },
  
  ReTwitterClose: function(){
    if(top.$('adspace')) { 
      top.$('adspace').style.display='block';
    }
    return true;
  }
  
}; /* }}} */


var Category = /* {{{ */ {
  selected: '0',
  Show: function(id){
    Element.removeClassName($('dir_link_'+this.selected),'selected');
    this.selected = id;
    var el = $('dir_link_'+id);
    Element.addClassName(el,'selected');
  },
  RightMenu: [
    {
      name: 'Add',
      callback: function(){
	var node_jp = prompt('Add Node(Japanese):');
	if(!node_jp){
	  return;
	}
	var node_cn = prompt('Add Node(Chinese):');
	if(!node_cn){
	  return;
	}

	var dir_name_jp = encodeURIComponent(node_jp);
	var dir_name_cn = encodeURIComponent(node_cn);
	var parent_id = Category.selected||"0";
	new Ajax.Request(
	  '/api/admin/add_category',
	  {
	    method:'GET',
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
	    parameters: 'parent_id='+parent_id+'&dir_name_jp='+dir_name_jp+'&dir_name_cn='+dir_name_cn,
	    onComplete: function(request){
//alert(request.responseText);
	    }
	  }
	);
      }
    },
    {
      name: 'Rename',
      callback: function(){
	var text = $('dir_link_'+Category.selected).innerHTML;
	var node = prompt('Rename Node:',text);
	if(!node){
	  return;
	}
	var dir_name = encodeURIComponent(node);
	var dir_id = Category.selected||"0";
	new Ajax.Request(
	  '/api/admin/rename_category',
	  {
	    method: 'GET',
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
	    parameters: 'dir_id='+dir_id+'&dir_name='+dir_name,
	    onComplete: function(request){
//alert(request.responseText);
	  }
	});
      }
    },
    {
      name: 'Delete',
      callback: function(){
	if( !confirm('Are you sure to delete?') ){
	  return;
	}
	new Ajax.Request(
	  '/api/admin/delete_category/'+Category.selected,
	  {
	    method: 'GET',
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
	    onComplete: function(request){
//alert(request.responseText);
	    }
	  }
	);
      }
    }
  ]
}; /* }}} */

var Community = /* {{{ */ {
  IsJoin: function(id,community_id){
    new Ajax.Updater(id,'/api/home/community_is_join/'+community_id,{
      method: 'GET'
    });
  },
  
  IsEventJoin: function(id,community_id,event_view_name){
    new Ajax.Updater(id,'/api/home/community_is_event_join/'+community_id,{
      parameters: "event_view_name="+event_view_name,
      method: 'GET'
    });
  },
  
  Join: function(id){
//    Dialog.alert({url:"/popup/community_join/"+id,options:{method:"get"}}, {width:400});
    this.dialog = new Window({url:"/popup/community_join/"+id,width:"400"});
    if( Prototype.Browser.IE && navigator.userAgent.indexOf('MSIE 7')<=-1 ){
      this.dialog.setDestroyOnClose();
    }
    this.dialog.showCenter(true);
  },
  
  // コミュつぶやきのお気に入りを登録 2009-03-04 Y.Uemura Add.
  Clip: function(id, mode){
    new Ajax.Request('/api/home/community_word_clip/'+id, {
      method: 'GET',
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
      onComplete: function(request){
        var img_html = $('clip_'+id).innerHTML;
        var img_class = "";
        var small_js = "";
        var msg_clip_text = MSG_CLIP;
        if(mode == 'small'){
          img_class="ico_cliped_small";
          img_html = '/images/common/ico_cliped_small.gif';
          small_js = ',\'small\'';
          msg_clip_text= '';
        }else{
          img_class="ico_cliped";
          img_html = '/images/common/ico_cliped.gif';
        }
        new Insertion.After('clip_'+id,'<a href="#" id="clip_'+id+'" onClick="Community.UnClip(\''+id+'\''+small_js+'); return false;">'+msg_clip_text+'<img class="'+img_class+'" src="'+img_html+'" /></a>');
        Element.remove($('clip_'+id));
      }
    });
  },
  
  // コミュつぶやきのお気に入りを解除 2009-03-04 Y.Uemura Add.
  UnClip: function(id, mode){
    new Ajax.Request('/api/home/community_word_unclip/'+id, {
      method: 'GET',
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
      onComplete: function(request){
        var img_html = $('clip_'+id).innerHTML;
        var img_class = "";
        var small_js = "";
        var msg_clip_text = MSG_CLIP;
        if(mode == 'small'){
          img_class="ico_clip_small";
          img_html = '/images/common/ico_clip_small.gif';
          small_js = ',\'small\'';
          msg_clip_text = '';
        }else{
          img_class="ico_clip";
          img_html = '/images/common/ico_clip.gif';
        }
        new Insertion.After('clip_'+id,'<a href="#" id="clip_'+id+'" onClick="Community.Clip(\''+id+'\''+small_js+'); return false;">'+msg_clip_text+'<img class="'+img_class+'" src="'+img_html+'" /></a>');
        Element.remove($('clip_'+id));
      }
    });
  },

  // 2009-04-14 I.Yamaguchi Add. 2009-04-16 S.Onuma. Mod.
  Watching: function(id, mode){
    new Ajax.Request('/api/home/community_word_watch/'+id, {
      //parameters: "mode="+mode,
      method: 'GET',
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
      onComplete: function(request){
        var img_html = $('watch_'+id).innerHTML;
        var mode_js = '';
        if(mode == 'list'){
          img_html = '/images/common/ico_watch.gif';
          mode_js = ',\'list\'';
          new Insertion.After('watch_'+id,'<a href="#" id="watch_'+id+'" onClick="Community.UnWatch(\''+id+'\''+mode_js+'); return false;">'+MSG_WATCHING_OFF+'<img src="'+img_html+'" /></a>');
        }else{
          img_html = '/images/home/btn_watching_active'+((SITE_LANGUAGE == 'cn')?'_cn':'')+'.gif';
          new Insertion.After('watch_'+id,'<a href="#" id="watch_'+id+'" onClick="Community.UnWatch(\''+id+'\'); return false;">'+'<img src="'+img_html+'" /></a>');
        }
        Element.remove($('watch_'+id));
      }
    });
  },
  
  // 2009-04-14 I.Yamaguchi Add. 2009-04-16 S.Onuma. Mod.
  UnWatch: function(id, mode){
    new Ajax.Request('/api/home/community_word_unwatch/'+id, {
      //parameters: "mode="+mode,
      method: 'GET',
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
      onComplete: function(request){
        var img_html = $('watch_'+id).innerHTML;
        var mode_js = '';
        if(mode == 'list'){
          img_html = '/images/common/ico_watch.gif';
          mode_js = ',\'list\'';
          new Insertion.After('watch_'+id,'<a href="#" id="watch_'+id+'" onClick="Community.Watching(\''+id+'\''+mode_js+'); return false;">'+MSG_WATCHING_ON+'<img src="'+img_html+'" /></a>');
        }else{
          img_html = '/images/home/btn_watching'+((SITE_LANGUAGE == 'cn')?'_cn':'')+'.gif';
          new Insertion.After('watch_'+id,'<a href="#" id="watch_'+id+'" onClick="Community.Watching(\''+id+'\'); return false;">'+'<img src="'+img_html+'" /></a>');
        }
        Element.remove($('watch_'+id));
      }
    });
  },

/*
  Invite: function(id){
alert("Invite:"+id);
  },
*/

  Withdrawal: function(id){
    this.dialog = new Window({url:"/popup/community_withdrawal/"+id,width:"420"});
    this.dialog.setDestroyOnClose();
    this.dialog.showCenter(true);
  },
  AssignAdministratorDialog: function(id,mode){
    this.dialog = new Window({url:"/popup/community_assign_administrator/"+id+"?mode="+mode,width:"400"});
    this.dialog.setDestroyOnClose();
    this.dialog.showCenter(true);
  },
  AssignAdministrator: function(community_id,mode,user_name){
    $('community_'+mode).update(user_name);
    document.community_form['community/'+mode].value = user_name;
    //window.top.document.community_form['community/'+mode].value = user_name;
    this.dialog.close();

    this.CheckAssign(community_id,mode);
  },
  BlacklistDialog: function(id,mode){
    this.dialog = new Window({url:"/popup/community_blacklist/"+id+"?mode="+mode,width:"400"});
    this.dialog.setDestroyOnClose();
    this.dialog.showCenter(true);
  },
  DismissAdminisotrator: function(community_id,mode){
    if( !confirm(MSG_CONFIRM_DISMISS_ADMINISTRATOR) ){
      return;
    }

    new Ajax.Request('/api/home/dismiss_administrator/'+community_id,{
      parameters: "mode="+mode,
      evalScripts: true,
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
      onComplete: function(request){
        $('community_'+mode).update("");
        Community.CheckAssign(community_id,mode);
        document.community_form['community/'+mode].value = "";
      }
    });
/*
    $('community_'+mode).update("");
    document.community_form['community/'+mode].value = "";
*/
  },
  
  CheckAssign: function(community_id,mode){
    var user_name = $('community_'+mode).innerHTML;
    var html = '';
    if( user_name ){
      html = '<span class="text_btn"><a href="#" onclick="Community.DismissAdminisotrator(\''+community_id+'\',\''+mode+'\'); return false;">'+MSG_DISMISS+'</a></span>';
    }else{
      html = '<span class="text_btn"><a href="#" onclick="Community.AssignAdministratorDialog(\''+community_id+'\',\''+mode+'\'); return false;">'+MSG_ASSIGN_MANAGER+'</a></span>';
    }
    
    try{
      $('community_assign_'+mode).update(html);
    }catch(e){}
  },
  ReplaceUserName: function(community_id){
    this.dialog = new Window({url:"/popup/community_assign_administrator/"+community_id+'?mode=user_name',width:"400"});
    this.dialog.setDestroyOnClose();
    this.dialog.showCenter(true);
  },
/*
  Remove: function(id){
    new Ajax.Request('/api/home/remove_community/'+id,{
      evalScripts: true,
      onComplete: function(request){
//alert(request.responseText);
      }
    });
  },
*/
  RemoveWord: function(id,url){
    if( !confirm(MSG_CONFIRM_DELETE_DESCRIPTION) ){
      return;
    }

    new Ajax.Request('/api/home/remove_community_word/'+id, {
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
      onComplete: function(request){
//alert(request.responseText);
        if(url){
          self.location.replace(url);
        }
      },
      evalScripts: true
    });
  },
  
  
  // 2009-05-28 S.Onuma Add.
  ResTwitter: function(community_id,parent_id){
    if(top.$('adspace')){
      top.$('adspace').style.display='none';
    }  
    
    
    this.dialog = new Window("twitter_window",{url:'/community/twitter_word/'+community_id+'/'+parent_id,width:670,height:420,margin:'0px',padding:'0px',zIndex:10000});
    this.dialog.setDestroyOnClose();
    this.dialog.showCenter(true);
    this.dialog.setCloseCallback(this.ResTwitterClose);

  },
  
  ResTwitterClose: function(){
    if(top.$('adspace')){
      top.$('adspace').style.display='block';
    }
    return true;
  },
  
  // 2009-05-29 S.Onuma Add.
  ResEventTwitter: function(community_id,parent_id){
    if(top.$('adspace')){
      top.$('adspace').style.display='none';
    }  
    
    
    this.dialog = new Window("twitter_window",{url:'/community/etwitter_word/'+community_id+'/'+parent_id,width:670,height:420,margin:'0px',padding:'0px',zIndex:10000});
    this.dialog.setDestroyOnClose();
    this.dialog.showCenter(true);
    this.dialog.setCloseCallback(this.ResEventTwitterClose);

  },
  
  ResEventTwitterClose: function(){
    if(top.$('adspace')){
      top.$('adspace').style.display='block';
    }
    return true;
  }
  
  
} /* }}} */


var Report = /* {{{ */ {
  CommentWindow: function(id){
    this.dialog = new Window({url:"/popup/report_mistranslation_comment/"+id,width:"400"});
    this.dialog.setDestroyOnClose();
    this.dialog.showCenter(true);
  },

  RemoveMistranslation: function(id,reload){
    if( !confirm(MSG_CONFIRM_DELETE_DESCRIPTION) ){
      return;
    }

    new Ajax.Request('/api/home/remove_report_mistranslation/'+id, {
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
      onComplete: function(request){
        if(reload){
          top.location.reload();
        }
      },
      evalScripts: true
    });
  },
  RemoveMistranslationChild: function(id){
    if( !confirm(MSG_CONFIRM_DELETE_DESCRIPTION) ){
      return;
    }
    new Ajax.Request('/api/home/remove_report_mistranslation_child/'+id, {
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
      onComplete: function(request){
      },
      evalScripts: true
    });
  },
  MistranslationResTwitter: function(id){

    if(top.$('adspace')){
      top.$('adspace').style.display='none';
    }
    this.dialog = new Window("twitter_window",{url:'/report/mistranslation_res/'+id,width:'670px',height:'265px',margin:'0px',padding:'0px',zIndex:10000});
    this.dialog.setDestroyOnClose();
    this.dialog.showCenter(true);
    this.dialog.setCloseCallback(this.MistranslationResTwitterClose);

  },
  MistranslationResTwitterClose: function(){
    if(top.$('adspace')){
      top.$('adspace').style.display='block';
    }
    return true;
  }
  
} /* }}} */


var FriendInvite = /* {{{ */ {
  
  Remove: function(id){
    if( !confirm(MSG_CONFIRM_DELETE_DESCRIPTION) ){
      return;
    }

    new Ajax.Request('/api/home/remove_friend_invite/'+id, {
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
      onComplete: function(request){
      },
      evalScripts: true
    });
  }
  
} /* }}} */


var Enquete = /* {{{ */ {
  
  AddItem: function(etc_jp,etc_cn){

    if(parseInt(jQuery("input:hidden[name='item_id[]']", document.body).length) >= parseInt(ENQUETE_ITEM_MAX_NUM)) {
      alert(MSG_ENQUETE_ITEM_MAX_OVER);
      return false;
    }
    if(!jQuery('#enquete_item_count').val()){
      jQuery('#enquete_item_count').val(0);
    }
    
    if(!etc_jp){
      etc_jp = "";
    }
    if(!etc_cn){
      etc_cn = "";
    }
    
    var before_index = parseInt(jQuery('#enquete_item_count').val());
    
    jQuery('#enquete_item_count').val(parseInt(jQuery('#enquete_item_count').val()) + 1);
    var index = parseInt(jQuery('#enquete_item_count').val());
    new Ajax.Request('/enquete/items/', {
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
      parameters: "index="+index+"&item_jp="+encodeURIComponent(etc_jp)+"&item_cn="+encodeURIComponent(etc_cn),
      onComplete: function(request){
		    jQuery("#item_insert").after(
		      jQuery("<div>").attr("id","enquete_item_zone_"+index).html(request.responseText)
		    );
		
    	  jQuery("#item_insert").remove();
    	
		    jQuery("#enquete_item_zone_"+index).after(
		      jQuery("<div>").attr("id","item_insert")
		    );
			
		    jQuery('a.enquete_item_pictogram_'+index).click(function (e) {
			    e.preventDefault();
			    basicModalContentShowPictogram(this.id+'_description_','','basicModalContent');
			
		    });
		    jQuery('a.enquete_item_pictogram_translate_'+index).click(function (e) {
			    e.preventDefault();
			    basicModalContentShowPictogram(this.id+'_description_','1','basicModalContentTranslate');
		    });
		
		    jQuery("#item_translate_insert").after(
		      jQuery("<div>").attr({
		  	    "id":"item_description_"+index+"_translate",
		  	    "className":"trans_confirm_item transcheck_text_item"
		      })
		    );
		
    	  jQuery("#item_translate_insert").remove();
    	
		    jQuery("#item_description_"+index+"_translate").after(
		      jQuery("<div>").attr("id","item_translate_insert")
		    );
		    
      },
      evalScripts: true
    });
  },
  
  RemoveItem: function(index){

    if( !confirm(MSG_CONFIRM_DELETE_DESCRIPTION) ){
      return;
    }
    
	  jQuery("#enquete_item_zone_"+index).remove();
	  jQuery("#item_description_"+index+"_translate").remove();
	
  }, 
  
  ShowMini: function(){

    new Ajax.Request('/enquete/show_mini/',{
      evalScripts: true,
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
	    parameters: '',
      onComplete: function(request){
        $('enquete_mini').update(request.responseText);
        Enquete.SetShowMiniPictogram();
      }
    });
  },
  
  SetShowMiniPictogram: function(){

    jQuery('a.enquete_mini_pictogram').click(function (e) {

	    e.preventDefault();
	    basicModalContentShowPictogram('enquete_mini_return_description_','','basicModalContentEnqueteMini');
	
    });

  },
  
  ReturnMini: function(language,id,is_comment){
    
    var enquete_item_id = jQuery("input[@name=enquete_item_id]:checked").val();
    if(!enquete_item_id){
      alert(MSG_ENQUETE_RETURN_REQUIRED);
      return;
    }
    var description = "";
    var description_key = 'description_'+language;
    if(is_comment){
      description = jQuery("#enquete_mini_return_description_"+language).val();
      description = encodeURIComponent(description);
    }
    var enquete_id = jQuery("#enquete_mini_return_enquete_id").val();
    
    new Ajax.Request('/enquete/show_mini/'+id,{
      evalScripts: true,
      method: "post",
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
	    parameters: 'id='+id+'&commit=1&enquete_return/enquete_id='+enquete_id+'&enquete_return/enquete_item_id='+enquete_item_id+'&enquete_return/'+description_key+'='+description,
      onComplete: function(request){
        $('enquete_mini').update(request.responseText);
        Enquete.SetShowMiniPictogram();
      }
    });
  },
  
  UpdateNew: function(id,mode){
    var target,change;
    if( mode=="jp_cn" ){
      target = $(id+"_jp").innerHTML;
      change = id+"_cn";
    }else if( mode=="cn_jp" ){
      target = $(id+"_cn").innerHTML;
      change = id+"_jp";
    }
    new Ajax.Updater(change,'/api/home/translate/'+id, {
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
      parameters: "mode="+mode+"&emoji=1",
      method: 'GET'
    });
    var img_html = $('translate_'+id).innerHTML;
    if( img_html.match(/left/) ){
      img_html = '/images/img_lobo01.gif';
    }else{
      img_html = '/images/img_lobo01.gif';
    }
    new Insertion.Before('translate_'+id,'<img src="'+img_html+'" />');
    Element.remove($('translate_'+id));
  },
  
  RemoveComment: function(id,url){
    if( !confirm(MSG_CONFIRM_DELETE_DESCRIPTION) ){
      return;
    }

    new Ajax.Request('/api/home/remove_enquete_comment/'+id, {
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
      onComplete: function(request){
//alert(request.responseText);
        if(url){
          self.location.replace(url);
        }
        $(id+'_jp').update(MSG_ENQUETE_NO_COMMENT_JP);
        $(id+'_cn').update(MSG_ENQUETE_NO_COMMENT_CN);
      },
      evalScripts: true
    });
  },
  // 2009-05-28 S.Onuma Add.
  ReturnEnquete: function(id){

    if(top.$('adspace')){
      top.$('adspace').style.display='none';
    }
    this.dialog = new Window("twitter_window",{url:'/enquete/return_enquete/'+id,width:'670px',height:'420px',margin:'0px',padding:'0px',zIndex:10000});
    this.dialog.setDestroyOnClose();
    this.dialog.showCenter(true);
    this.dialog.setCloseCallback(this.ReTwitterClose);

  },
  
  ReTwitterClose: function(){
    if(top.$('adspace')) { 
      top.$('adspace').style.display='block';
    }
    return true;
  }
  
} /* }}} */

var Oogiri = /* {{{ */ {
  
  ShowMini: function(){

    new Ajax.Request('/oogiri/show_mini/',{
      evalScripts: true,
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
	    parameters: '',
      onComplete: function(request){
        $('oogiri_mini').update(request.responseText);
        Oogiri.SetShowMiniPictogram();
      }
    });
  },
  
  SetShowMiniPictogram: function(){

    jQuery('a.oogiri_mini_pictogram').click(function (e) {

	    e.preventDefault();
	    basicModalContentShowPictogram('oogiri_mini_word_description_','','basicModalContentOogiriMini');
	
    });

  },
  
  ReturnMini: function(language,id,is_comment){
    
    var description = "";
    var description_key = 'description_'+language;
    if(is_comment){
      description = jQuery("#oogiri_mini_word_description_"+language).val();
      description = encodeURIComponent(description);
    }
    var oogiri_id = jQuery("#oogiri_mini_word_oogiri_id").val();
    
    new Ajax.Request('/oogiri/show_mini/'+id,{
      evalScripts: true,
      method: "post",
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
	    parameters: 'id='+id+'&commit=1&oogiri_word/oogiri_id='+oogiri_id+'&oogiri_word/'+description_key+'='+description,
      onComplete: function(request){
        $('oogiri_mini').update(request.responseText);
        Oogiri.SetShowMiniPictogram();
      }
    });
  },
  
  UpdateNew: function(id,mode){
    var target,change;
    if( mode=="jp_cn" ){
      target = $(id+"_jp").innerHTML;
      change = id+"_cn";
    }else if( mode=="cn_jp" ){
      target = $(id+"_cn").innerHTML;
      change = id+"_jp";
    }
    new Ajax.Updater(change,'/api/home/translate/'+id, {
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
      parameters: "mode="+mode+"&emoji=1",
      method: 'GET'
    });
    var img_html = $('translate_'+id).innerHTML;
    if( img_html.match(/left/) ){
      img_html = '/images/img_lobo01.gif';
    }else{
      img_html = '/images/img_lobo01.gif';
    }
    new Insertion.Before('translate_'+id,'<img src="'+img_html+'" />');
    Element.remove($('translate_'+id));
  },
  
  RemoveWord: function(id,url){
    if( !confirm(MSG_CONFIRM_DELETE_DESCRIPTION) ){
      return;
    }

    new Ajax.Request('/api/home/remove_oogiri_word/'+id, {
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
      onComplete: function(request){
//alert(request.responseText);
        if(url){
          self.location.replace(url);
        }
      },
      evalScripts: true
    });
  },
  
  RemoveRate: function(id,url){
    if( !confirm(MSG_CONFIRM_DELETE_DESCRIPTION) ){
      return;
    }

    new Ajax.Request('/api/home/remove_oogiri_rate/'+id, {
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
      onComplete: function(request){
//alert(request.responseText);
        if(url){
          self.location.replace(url);
        }
      },
      evalScripts: true
    });
  },
  
  
  // 2009-05-28 S.Onuma Add.
  ReturnOogiri: function(id){

    if(top.$('adspace')){
      top.$('adspace').style.display='none';
    }
    this.dialog = new Window("twitter_window",{url:'/oogiri/return_oogiri/'+id,width:'670px',height:'420px',margin:'0px',padding:'0px',zIndex:10000});
    this.dialog.setDestroyOnClose();
    this.dialog.showCenter(true);
    this.dialog.setCloseCallback(this.ReTwitterClose);

  },
  
  ReTwitterClose: function(){
    if(top.$('adspace')) { 
      top.$('adspace').style.display='block';
    }
    return true;
  },
  
  Clap: function(id, mode){
    new Ajax.Request('/api/home/oogiri_word_clap/'+id, {
      //parameters: "mode="+mode,
      method: 'GET',
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
      onComplete: function(request){
        var img_html = $('clap_'+id).innerHTML;
        var img_class = "";
        var small_js = "";
        var msg_clap_text = MSG_CLAPPING;
        if(mode == 'small'){
          img_class="ico_claped_small";
          img_html = '/images/common/ico_claped.gif';
          msg_clap_text = '';
          small_js = 'small';
        }else{
          img_class="ico_claped";
          img_html = '/images/common/ico_claped.gif';
        }
        new Insertion.After('clap_'+id,'<a href="#" id="clap_'+id+'" onClick="Oogiri.UnClap(\''+id+'\''+small_js+'); return false;">'+msg_clap_text+' <span class="clap_num">'+request.responseText+'</span> <img class="'+img_class+'" src="'+img_html+'" /> </a>');
        Element.remove($('clap_'+id));
      }
    });
  },
  
  UnClap: function(id, mode){
    new Ajax.Request('/api/home/oogiri_word_unclap/'+id, {
      //parameters: "mode="+mode,
      method: 'GET',
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
      onComplete: function(request){
        var img_html = $('clap_'+id).innerHTML;
        var img_class = "";
        var small_js = "";
        var msg_clap_text = MSG_CLAPPING;
        if(mode == 'small'){
          img_class="ico_clap_small";
          img_html = '/images/common/ico_clap.gif';
          msg_clap_text = '';
          small_js = 'small';
        }else{
          img_class="ico_clap";
          img_html = '/images/common/ico_clap.gif';
        }
        new Insertion.After('clap_'+id,'<a href="#" id="clap_'+id+'" onClick="Oogiri.Clap(\''+id+'\''+small_js+'); return false;">'+msg_clap_text+' <span class="clap_num">'+request.responseText+'</span> <img class="'+img_class+'" src="'+img_html+'" /> </a>');
        Element.remove($('clap_'+id));
      }
    });
  },
  UpdateNew: function(id,mode){
    var target,change;
    if( mode=="jp_cn" ){
      target = $(id+"_jp").innerHTML;
      change = id+"_cn";
    }else if( mode=="cn_jp" ){
      target = $(id+"_cn").innerHTML;
      change = id+"_jp";
    }
    new Ajax.Updater(change,'/api/home/translate/'+id, {
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
      parameters: "mode="+mode+"&emoji=1",
      method: 'GET'
    });
    var img_html = $('translate_'+id).innerHTML;
    if( img_html.match(/left/) ){
      img_html = '/images/img_lobo01.gif';
    }else{
      img_html = '/images/img_lobo01.gif';
    }
    new Insertion.Before('translate_'+id,'<img src="'+img_html+'" />');
    Element.remove($('translate_'+id));
  },
  
  ModifyRanking: function(id,ranking,out_text){
    
    out_text = (out_text) ? 1: "";
    new Ajax.Request('/api/home/oogiri_modify_ranking/'+id, {
      parameters: "ranking="+ranking+"&out_text="+out_text,
      method: 'GET',
      requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
      onComplete: function(request){ 
      },
      evalScripts: true
    });
  }
}; /* }}} */
