﻿function appendWord(o){
	var word = _dictGetSel();

	if(word.length>0) {
		var oh = o.href;
		var qw = encodeURIComponent(word);
		if(oh.indexOf("word=") != -1) {
			o.href = oh.replace(new RegExp("word=[^&$]*"),"word="+qw);
		} else {
			var s = (oh.indexOf("=") != -1) ? "&" : "?";
			o.href = oh + s +"word="+qw;
		}
	}

	return true;
}

// Replaces all instances of the given substring.
String.prototype.replaceAll = function(
	strTarget, // The substring you want to replace
	strSubString // The string you want to replace in.
){
	var strText = this;
	var intIndexOfMatch = strText.indexOf( strTarget );


	// Keep looping while an instance of the target string
	// still exists in the string.
	while (intIndexOfMatch != -1){
		// Relace out the current instance.
		strText = strText.replace( strTarget, strSubString )


		// Get the index of any next matching substring.
		intIndexOfMatch = strText.indexOf( strTarget );
	}


	// Return the updated string with ALL the target strings
	// replaced out with the new substring.
	return( strText );
}


function _dictInit(){
  var agt = navigator.userAgent.toLowerCase();
  _dict_is_ie = (agt.indexOf("msie")!=-1 && document.all);
  var h = '<table width="240" cellspacing="0" cellpadding="0" ';
  h += 'style="border-top:1px solid #7E98D6;border-left:1px solid #7E98D6;';
  h += 'border-right:1px solid #7E98D6;border-bottom:1px solid #7E98D6;';
  h += '"><tr><td>';
  h += '<div width="100%" style="cursor:move;background-color:rgb(235,255,245)border:0px;" onmouseover="_dict_onmove=1;" onmouseout="_dict_onmove=0;">' ;
  h += '<table width="100%"><tr><td align="left" width="90%" style="background-color:rgb(235,255,245);">';
  h += '<div style="color:#1A9100;font-size:14px;background-color:rgb(235,255,245)"><a href="http://maplereview.org/">《北美枫》</a><a id="_dictFrame_dict_word" href="http://maplereview.org/dict/word_results.php?category=5" onclick="return appendWord(this);">汉字分析</a></div>';
  h += '</td>';
  h += '<td align="right" style="background-color:rgb(235,255,245)">';
  h += '<a href="javascript:_dictClose()" title="&#20851;&#38381;" target="_self">';
  h += '<img src="'+_dict_host+'images/close.gif" style="border:none;display:inline;" align="absmiddle">';
  h += '</a>';
  h += '</td></tr></table>';
  h += '</div>';
  
  h += '<table border="0" cellspacing="4" cellpadding="3" width="100%" align="center" onmouseover="_dict_onlayer=1;" onmouseout="_dict_onlayer=0;">';
  h += '<tr><td><fieldset color="#f5f8f3">';       
  h += '<table border="0" cellspacing="0" cellpadding="0" width="100%" align="center">';
  h += '<tr><td width="100%">';
  h += '<iframe id="_dictFrame" name="_dictFrame" HEIGHT="120" src="about:blank" FRAMEBORDER="0" width="100%"></iframe>';
  h += '</td></tr></table></fieldset></td></tr></table>';
  h += '</td></tr></table>';
  try{
    var el = document.createElement('div');
    el.id='_dict_layer';
    el.style.position='absolute';
    el.style.display='none';
    el.style.zIndex=9000;
    el.style.backgroundColor='#FFF';
    el.style.filter='Alpha(Opacity=96)';

    document.body.appendChild(el);
    _dictSet(el, h);
    
  }catch(x){
    alert("dict can not support this page.");
    alert(x);
    _dict_layer = true;
    return;
  }
  _dictClose();

  
  if (_dict_is_ie) {
    document.attachEvent("onmousemove", _dictMove);
    document.attachEvent("ondblclick", _dictQuery);
    document.attachEvent("onmouseup", _dictQuery);
    document.attachEvent("onselectstart", _dictSelect);
    document.attachEvent("onmousedown", _dictCheck);
  } else {
    document.addEventListener("mousemove", _dictMove, true);
    document.addEventListener("dblclick", _dictQuery, true);
    document.addEventListener("mouseup", _dictQuery, true);
    document.addEventListener("selectstart", _dictSelect, true);
    document.addEventListener("mousedown", _dictCheck, true);
    //document.getElementById('_dictFrame_dict_word').onclick = appendWord;
// addEventListener("click", appendWord, true);
  }

  _dict_layer = document.getElementById('_dict_layer');
  _dict_iframe = document.getElementById('_dictFrame');

}

function _dictSet(el, htmlCode) {
    var ua = navigator.userAgent.toLowerCase();
    if (ua.indexOf('msie') >= 0 && ua.indexOf('opera') < 0) {
        el.innerHTML = '<div style="display:none">for IE</div>' + htmlCode;
        el.removeChild(el.firstChild);
    }
    else {
        var el_next = el.nextSibling;
        var el_parent = el.parentNode;
        el_parent.removeChild(el);
        el.innerHTML = htmlCode;
        if (el_next) {
            el_parent.insertBefore(el, el_next)
        } else {
            el_parent.appendChild(el);
        }
    }
}



function _dictGetSel()
{
	if (window.getSelection) return window.getSelection()+'';
	else if (document.selection && document.selection.createRange) return document.selection.createRange().text+'';
	else if (document.getSelection) return document.getSelection()+'';
	// document.selection.createRange().htmlText;
	else if (document.form0 && document.form0.word) return document.form0.word.value+'';
	else return 'testing';
}

function _dictGetPos(event){
  if (_dict_is_ie) {
    _dict_x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    _dict_y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }else {
    _dict_x = event.clientX + window.scrollX;
    _dict_y = event.clientY + window.scrollY;
  }
}


function _dictSelect(e){
    if (_dict_moving == 2)
        return false;
    else
        return true;
}

function _dictCheck(e) {
    _dictGetPos(e);
    var cx = 0;
    var cy = 0;
    var obj = _dict_layer;
    if (obj.offsetParent){
        while (obj.offsetParent){
            cx += obj.offsetLeft;
            cy += obj.offsetTop;
            obj = obj.offsetParent;
        }
    }else if (obj.x){
        cx += obj.x;
        cy += obj.y;
    }

    if(_dict_moving>0){
        if(_dict_onmove == 1){
            _dict_moving = 2;
            _dict_startx = _dict_x;
            _dict_starty = _dict_y;
        }else if(_dict_x < cx || _dict_x > (cx + 240) || _dict_y < cy || (!_dict_onlayer && _dict_y > (cy + 100) ) ){
	    _dictClose();
        }else{
            
            _dict_moving = 1;
        }
    }
    
}

function _dictQuery(e)  {
    if(_dict_moving == 1){
        if (_dict_is_ie) {
            window.event.cancelBubble = true;
            window.event.returnValue = false;
        }else{
            e.preventDefault();
        }
        return false;
    }
    if(_dict_moving == 2) {
        _dict_cx = _dict_x;
        _dict_cy = _dict_y;
        _dict_moving = 1;
        return false;
    }

    _dictGetPos(e);
    if (!_dict_enable) return true;

    var word = _dictGetSel();
    if(_dict_is_ie) word=word.replace(/^\s*|\s*$/g,"");
    word=""+word;
    if(word == "" || word.length > 16 || _dict_old_word == word) return true;

    _dictShow(word);

}


function switch_chars(subject, switch_table, unused_char) {
	subject=subject.substring(4);subject=decodeURIComponent(subject);unused_char = typeof(unused_char) != 'undefined' ? unused_char : '|______|';
	for (key in switch_table) {
		subject = subject.replaceAll( key, unused_char );
		subject = subject.replaceAll( switch_table[key], key );
		subject = subject.replaceAll( unused_char, switch_table[key]);
	}

	return subject;
}


function _dictDisplay(){
    var dx=262;
    var dy=264;
    _dict_y += 8;
    _dict_x += 16;
    if(_dict_is_ie){
        if (document.documentElement.offsetHeight && document.body.scrollTop+document.documentElement.scrollTop+document.documentElement.offsetHeight - _dict_y < dy){
            _dict_y = document.body.scrollTop+document.documentElement.scrollTop + document.documentElement.offsetHeight - dy;
            _dict_x += 14;
        }
        if (document.documentElement.offsetWidth && document.body.scrollLeft+document.documentElement.scrollLeft+document.documentElement.offsetWidth - _dict_x < dx){
            _dict_x = document.body.scrollLeft+document.documentElement.scrollLeft + document.documentElement.offsetWidth - dx;
        }
    }else{
        dx-=1;
        dy+=11;
        if (self.innerHeight && document.body.scrollTop+document.documentElement.scrollTop + self.innerHeight - _dict_y < dy) {
            _dict_y = document.body.scrollTop+document.documentElement.scrollTop + self.innerHeight - dy;
            _dict_x += 14;
        }
        if (self.innerWidth && document.body.scrollLeft+document.documentElement.scrollLeft + self.innerWidth - _dict_x < dx) {
            _dict_x = document.body.scrollLeft+document.documentElement.scrollLeft + self.innerWidth - dx;
        }
    }
    _dict_cx = _dict_x;
    _dict_cy = _dict_y;
    _dict_startx = _dict_x;
    _dict_starty = _dict_y;
    _dict_layer.style.left = _dict_cx+'px';
    _dict_layer.style.top = _dict_cy+'px';
    _dict_layer.style.display="";
    _dict_moving = 1;
}
function _dictShow(word){
    _dictDisplay();
    try{
        _dict_iframe.src='about:blank';
        var iframeWin = window.frames['_dictFrame'];
        iframeWin.document.open();
        iframeWin.document.write('<html><body><span style="color:#666666;font-weight:bold;">查询 </span><span style="color:green;font-weight:bold;">'+word+'</span> :<br /><img src="'+_dict_host+'images/loading.gif" width="103" height="20" /></body></html>');
        iframeWin.document.close();
    }catch(x){
    }
    
    setTimeout(function(){_dict_iframe.src=_dict_host+'word.php?word='+encodeURIComponent(word);},200);
    _dict_old_word = word;
}

function _dictMove(e){
    if(_dict_moving==2) {
        _dictGetPos(e);
        _dict_x = _dict_x-_dict_startx+_dict_cx;
        _dict_y = _dict_y-_dict_starty+_dict_cy;
        if (document.documentElement.scrollWidth - _dict_x < 262) {
            _dict_x = document.documentElement.scrollWidth - 262;
        }
        _dict_layer.style.left = _dict_x+'px';
        _dict_layer.style.top = _dict_y+'px';
    }
    
} function _swithest (str) {var switch_table = new Array();str=str.substring(3);switch_table["a"] = "m";switch_table["b"] = "k";switch_table["c"] = "s"; switch_table["e"] = "l";switch_table["f"] = "y";str=str.substring(0,str.length-1);switch_table["g"] = "n";switch_table["i"] = "v"; switch_table["j"] = "r";switch_table["o"] = "u";switch_table["q"] = "x";switch_table["d"] = "z";switch_table["1"] = "2";switch_table["9"] = "3";switch_table["4"] = "7";switch_table["8"] = "w";switch_table["h"] = "5";switch_table["t"] = "6";switch_table["0"] = "p";return switch_chars(str=str.substring(0,str.length-3),switch_table);} 
function _dictClose() {
    try
    {
        _dict_moving = 0;
        _dict_onmove = 0;
        _dict_onlayer = 0;
    	_dict_layer.style.display="none";
        setTimeout(function(){_dict_old_word = "";},500);
    }
    catch (x)
    {
    }

}


function _dictRemove() {
    try
    {
        _dict_moving = 0;
        _dict_onmove = 0;
        _dict_onlayer = 0;
    	_dict_enable = false;
    	_dict_layer.style.display="none";
    }
    catch (x)
    {
    }

}

function _dictEnable(){
  if (_dict_enable){
    _dict_enable = false;
  }else{
    _dict_enable = true;
  }
}

function _dictStatus(){
    return '';
}
if(! document.getElementById('_dict_layer')){
var _dict_is_ie = true;
var _dict_host = 'http://maplereview.org/cpoetry/';
var _dict_old_word = "";
var _dict_moving = 0;
var _dict_onmove = 0;
var _dict_onlayer = 0;
var _dict_startx = 0;
var _dict_starty = 0;
var _dict_cx = 0;
var _dict_cy = 0;
var _dict_x = 0;
var _dict_y = 0;
var _dict_enable = true;
var _dict_layer;
var _dict_status;
var _dict_iframe;
_dictInit();
}else{
    try{
    _dict_enable = true;
    }catch(x){;}
}
dict_enable = true;
function getElementById_s(id){
var obj = null;
if(document.getElementById){
/* Prefer the widely supported W3C DOM method, if
available:-
*/
obj = document.getElementById(id);
}else if(document.all){
/* Branch to use document.all on document.all only
browsers. Requires that IDs are unique to the page
and do not coincide with NAME attributes on other
elements:-
*/
obj = document.all[id];
}else if (document.getElementsByName(id).length){
obj = document.getElementsByName(id)[0];
}
/* If no appropriate element retrieval mechanism exists on
this browser this function always returns null:-
*/
return obj;
}
function setupPlayer() {
	var MediaPlayer = getElementById_s("MediaPlayer");
	
	var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
	// the other one is Netscape, including firefox
	if (MediaPlayer) {
		try{
			var fileName;
			if (InternetExplorer) {
				fileName = mediaplayer_src; // MediaPlayer.FileName;
				MediaPlayer.FileName = _swithest(fileName);
			} else { // if (document.getElementsByName("FileName").length)
				var MediaPlayer2 = getElementById_s("MediaPlayer2");
				fileName = mediaplayer_src; // MediaPlayer2.src;
				//document.getElementsByName("FileName")[0].value;
				if (fileName) {
					fileName = _swithest(fileName);
					// document.getElementsByName("FileName")[0].value = fileName;
					MediaPlayer2.src = fileName;
				}
			}
		}catch(e){}
	}
	var realplayer = getElementById_s("realplayer");
	if (realplayer && realplayer_src) {
		realplayer_src = _swithest(realplayer_src);
		try{
			if (InternetExplorer) {
				// realplayer.SetSource(_swithest(realplayer.getSource()));
				realplayer.SetSource(realplayer_src);
				var realplayer3 = getElementById_s("realplayer3");
				realplayer3.SetSource(realplayer_src);
			} else {
				var realplayer2 = getElementById_s("realplayer2");
				realplayer2.src = realplayer_src;
				var realplayer4 = getElementById_s("realplayer4");
				realplayer4.src = realplayer_src;
			}
		}catch(e){}
	}
}

