var et_editor={};
function is(o, t) {var n = typeof(o); if (!t) return n != 'undefined'; if (t == 'array' && (o.hasOwnProperty && o instanceof Array)) return true; return n == t;}
function getViewWindow(id)
{
    return $(id).contentWindow;
}
function getViewDoc(id)
{
    return getViewWindow(id).document;
}
function getEditorHTML(sContent) {
    var html="";
    html+="<html>";
    html+="<head>";
    html+='<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
    html+="<style>p {*margin:0.2em auto;}body {margin:0;padding:0;word-wrap: break-word;overflow: scroll;overflow-x: auto;scrollbar-face-color: #ffffff;scrollbar-highlight-color: #ffffff;scrollbar-shadow-color: #c0c1bb;scrollbar-3dlight-color: #c0c1bb;scrollbar-arrow-color: #c9cbb6;scrollbar-track-color: #f4f5f0;scrollbar-darkshadow-color: #ffffff;scrollbar-base-color: #ffffff;}body, td, textarea, input, br, div, span{font-size: 12px;font-family: '宋体', Verdana,Arial, Helvetica, sans-serif;line-height:1.8;}img{border: 0;}html{height: 100%;cursor: text;}ul li{list-style:none;float:none;background:url('/img/blog/round.gif') no-repeat left center;padding-left:10px;}form{margin: 0;}</style>";
    html+="</head>";
    html+='<body>';
    html+=sContent;
    html+="</body>";
    html+="</html>";
    return html;
}
function loadEditorContent(id, sContent) {
    if (typeof et_editor[id] != 'undefined') {
        // prevent ie from stack overflow 
        if (et_editor[id]) {
            return;
        }
    }
    et_editor[id]=true;
    setEditorContent(id, sContent);
}
function setEditorContent(id, sContent) {
  var ed=getViewDoc(id);
  ed.open();
  ed.write(getEditorHTML(sContent));
  ed.close();
  if (isIE()) {
    ed.body.contentEditable=true;
  } else {
    ed.designMode="on";
  }
  ed.ef={ ed_id:id ,rang:null,node:null
    ,is:function(n, patt){return n.nodeName == patt}
    ,getRoot:function(){return getViewDoc(this.ed_id).body}
    ,getRng:function(){
      try { 
        if (s = this.getSel()) {
          r = s.rangeCount > 0 ? s.getRangeAt(0) : (s.createRange ? s.createRange() : getViewDoc(this.ed_id).createRange());
        }
      } catch (ex) {
      }
      if (!r) {
        r = isIE() ? getViewDoc(this.ed_id).body.createTextRange() : getViewDoc(this.ed_id).createRange();
      }
      return r;
    }
    ,getSel:function(){var w = getViewWindow(this.ed_id);return w.getSelection ? w.getSelection() : w.document.selection;}
    ,getParent:function(n, f, r){
      var e = this,na;
      r = r || e.getRoot();
      if(is(f, 'string')) {
        na = f;
        if(f === '*') {
          f = function(n){return n.nodeType==1};
        } else {
          f = function(n){return e.is(n, na)};
        }
      }
      while(n) {
        if(n == r || !n.nodeType || n.nodeType === 9) break;
        if(!f || f(n)) {
          return n;
        }
        n = n.parentNode;
      }
      return null;
    }
    ,getNode:function(){
      var r = this.getRng(), s = this.getSel(), a;
      if(!isIE()) {
        if(!r)
          return this.getRoot();
        a = r.commonAncestorContainer;

        if (!r.collapsed) {
          if (r.startContainer == r.endContainer) {
            if (r.startOffset - r.endOffset < 2) {
              if (r.startContainer.hasChildNodes())
                a = r.startContainer.childNodes[r.startOffset];
            }
          }
        }
        return this.getParent(a, '*');
      }
      return r.item ? r.item(0) : r.parentElement();
    }
    ,save:function(){
      if (isIE()) {
        this.rang=getViewDoc(this.ed_id).selection.createRange();
      } else {
        /* do nothing for FF? */
        return;
      }
    }
    ,focus:function(){
      if (!isIE()) {
        getViewWindow(this.ed_id).focus();
        return;
      }
      if (this.rang!=null) {
        try {
          ed.body.focus();
          this.rang.select();
        } catch (e) {
          ed.body.focus();
        }
      } else {
        ed.body.focus();
      }
    }
    ,pasteHTML:function(src){
      if (!isIE()){
        getViewDoc(this.ed_id).execCommand("insertHTML", false, src);
      } else {
        try {
          if(this.rang== null) {
            this.save();
          }
          this.rang.pasteHTML(src);
        } catch (e) {
          alert(e);
        }
      }
    }
  };
  Element.observe(ed.body, "beforedeactivate", ed.ef.save.bind(ed.ef), true);
}//setEditorContent()

function showInsertURLMenu(e) {
  var b=Event.element(e);
  var ef = getViewDoc(b.editor_id).ef;
  var anchor = ef.getParent(ef.getNode(), 'A');
  var btn = $(b.editor_id + '_url_btn');
  btn.onclick = function(){insertURL(b.editor_id,anchor)};
  var href = 'http://';
  if(anchor) {href = anchor.href;btn.onclick = function(){insertURL(b.editor_id,anchor, true)}}
  var menu=$(b.editor_id+'_urlToInsert');
  menu.value = href;
  showMenu(e);
  menu.focus();
}
function insertVideo(src) {
  hideGlobalDialog();
  var ef=getViewDoc(et_editor.editor_id).ef;
  ef.focus();
  ef.pasteHTML(src);
  save_msg = false;	//更改正文样式不提示保存
}

function showInsertVideoMenu(e) {
  $('upload_video_selector').src="/community/editor/upload_video";
  showMenu(e,false);
}
function showInsertGraphMenu(e){
  showMenu(e,false);
}
function showMenu(e,auto_hide) {
  var b=Event.element(e);
  var selector=b.menu_bar;
  var pos=selector.cumulativeOffset();
  et_editor.editor_id=b.editor_id;
  if (typeof auto_hide=='undefined') {
    auto_hide=true;
  }
  showGlobalDialog(b.menu, (pos[0]+b.menu_offsetx)+"px", (pos[1]+b.menu_offsety)+"px", auto_hide);
  save_msg = false;	//更改正文样式不提示保存
}
function insertGraph(id, url) {
  hideGlobalDialog();
  /*graph iframes is troublesome,we must keep compatibility with old editors*/
  if (typeof url == 'undefined') {
    url=id;
    id=et_editor.editor_id;
  }
  var d = getViewDoc(id);
  d.ef.focus();
  url = url.replace(/&amp;/g,"&");
  d.execCommand("InsertImage", false, url);
  save_msg = false;	//更改正文样式不提示保存
}
function insertFace(id, serial) {
  insertGraph(id, "/img/personal/face/"+serial+".gif");
  save_msg = false;	//更改正文样式不提示保存
}
function insertURL(id, a, edit) {
  edit = edit || false;
  var url=$(id+"_urlToInsert");
  var result = url.value.replace(/^(http:\/\/)*/,'');
  var txt = '';
  var d = getViewDoc(id);
  result = 'http://' + result;
  if(edit && a) {
    var t = a.innerHTML;
    if(result != 'http://') { t = '<a href="'+result+'" target="_blank">'+a.innerHTML+'</a>'}
    a.parentNode.removeChild(a);
    d.ef.pasteHTML(t);
  } else {
    if (window.getSelection) {
      txt = getViewWindow(id).getSelection();
    } else if (document.getSelection) {
      txt = d.getSelection();
    } else if (document.selection) {
      txt = d.ef.rang.text;
    }
    if(txt == ''){
      alert('请选择要插入链接的文字');
    } else {
      d.ef.focus();
      d.ef.pasteHTML('<a href="'+result+'" target="_blank">'+txt+'</a>');
    }
  }
  hideGlobalDialog();
  save_msg = false;	//更改正文样式不提示保存
}
function toggleFontStyle(id, style) {
  save_msg = false;	//更改正文样式不提示保存
  var d = getViewDoc(id);
  d.ef.focus();
  d.execCommand(style, false, true);
}
function setFontSize(id,size) {
  hideGlobalDialog();
  var d = getViewDoc(id);
  d.ef.focus();
  if (typeof size != 'undefined') {
    d.execCommand("FontSize", false, size);
  } else {
    d.execCommand("FontSize", false, $("font_size_selector").selectedIndex);
  }
  save_msg = false;	//更改正文样式不提示保存
}
function setFontColor(id,color) {
  hideGlobalDialog();
  var d=getViewDoc(id);
  d.ef.focus();
  d.execCommand("ForeColor",false,color);
  save_msg = false;	//更改正文样式不提示保存
}
function getEditorContent(id) {
  var content = getViewDoc(id).body.innerHTML;
  var newcontent = content.replace(/<\/?[^(img|>)]+>/gi, '');
  newcontent = newcontent.replace(/(&nbsp;|\s)/gi,'');
  if(newcontent == '') {
    return '';
  }
  if(is_moz) {
    var reguler = /<([^>]*)(src|href)="([^(http:\/\/|\/|javascript|#|mailto:)]([^"]*))"([^>]*)>/gi;
    var res;
    while((res = reguler.exec(content)) != null) {
      str = res[0];
      relativepath = res[3];
      absolute = /\/[^\/]*$/;
      host = window.location.host;
      path = window.location.pathname;
      path = path.replace(absolute,'/');
      toreplace = '<'+res[1]+res[2]+'="http://'+host+path+relativepath+'"'+res[5]+'>';
      content = content.replace(str,toreplace);
    }
  }
  return content;
}
function initEditorMenu(b,menu_bar,menu,editor_id,callback,offsetx,offsety) {
  Element.observe(b, "click", callback, true);
  b.menu_bar=menu_bar;
  b.menu=menu;
  b.editor_id=editor_id;
  b.menu_offsetx=offsetx;
  b.menu_offsety=offsety;
}
function format_editor_content(content){
  var newcontent = content.replace(/<\/?[^(img|>)]+>/gi, '');
  newcontent = newcontent.replace(/(&nbsp;|\s)/gi,'');
  newcontent = newcontent.replace(/<br>/g,'');
  newcontent = newcontent.replace(/<p>/g,'');
  newcontent = newcontent.replace(/<\/p>/g,'');
  if(newcontent == '') {
    return '';
  }
  if(is_moz) {
    var reguler = /<([^>]*)(src|href)="([^(http:\/\/|\/|javascript|#|mailto:)]([^"]*))"([^>]*)>/gi;
    var res;
    while((res = reguler.exec(content)) != null) {
      str = res[0];
      relativepath = res[3];
      absolute = /\/[^\/]*$/;
      host = window.location.host;
      path = window.location.pathname;
      path = path.replace(absolute,'/');
      toreplace = '<'+res[1]+res[2]+'="http://'+host+path+relativepath+'"'+res[5]+'>';
      content = content.replace(str,toreplace);
    }
  }
  return content;
}
function check_current_url(){
	var flags = $("flag").value;
	var result;
	switch(flags){
		case 'gp':
			result = 1;
			break;
		case 'gc':
			result = 2;
			break;	
		case 'my_blog':
			result = 3;
			break;	
		case 'blog_comment':
		default:
			result = 4;
			break;	
	}
	return result;
}
