﻿// this is a common script for the website
var MAPLEREVIEW_ORG = 'http://maplereview.org/';
var BAIKEXUE_COM = 'http://yizitong.com/';
// BAIKEXUE_COM = 'http://baikexue.com/';
var MAPLEREVIEW_NET = 'http://maplereview.net';
var BAIKEXUE_COM2 = 'http://yizitong.com/';

// language
var ENGLISH = 1;
var CHINESE = 2;
var TRADITIONAL = 3;
var PINYIN = 4;
var YIZIWEN = 5;

// privileges of user:
var NOT_REGISTERED = 0;
var REGISTERED = 1;
var VIP = 2;
var EDITOR = 3;
var DEVELOPER = 4;
// var DELETE = 4;

// default, set to have no access!
// can be set to others as desired
var defaultPrivilege = NOT_REGISTERED; //NOACCESS;
var INVALID = -1;

function appendWord(o){
	var word = _analyzGetSel();

	if (USER_LANG_SELECT >= PINYIN) {
		word = trimPinyins(word);
	}

	if(word.length>0) {
		var oh = o.href;
		var qw = encodeURIComponent(word);
		if(oh.indexOf("o=") != -1) {
			o.href = oh.replace(new RegExp("o=[^&$]*"),"o="+qw);
		} else {
			var s = (oh.indexOf("=") != -1) ? "&" : "?";
			o.href = oh + s +"o="+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 tongFocused(){
   var frm = document.f;
   if (frm && frm.o && document.anchors.length == 0) {
   	frm.o.focus();
	// frm.o.select();
   }
}
function tongUnfocused(){
   var frm = document.f;
   if (frm) {
   	// frm.o.blur();
	// window.focus();
return false;
	if (window.getSelection) { // FF, Safari, Opera
		var sel = window.getSelection();
		if(sel.containsNode(frm.o, true)) {
			return true;
		}
	}  else if (document.selection) { // IE
		if (document.selection.type == 'Text') {
			return true;
		}
	}
   }

   return false;
}

var first_click = true;
function tongClicked(o){
/*
   if (first_click) {
   	o.value = '';
	first_click = false;
   }
   */
}

var is_google_map = -100;

function tongOnload(){
   if (is_google_map != -100) {
      if (is_google_map == 'google') {
         initialize();
      } else if (is_google_map == 'bing') {
         GetMap();
      } else if (is_google_map == 'abc') {
         mapInit();
      }
   }

   tongFocused();
}

function tongOnunload(){
   if (is_google_map != -100) {
      if (is_google_map) {
         GUnload();
      } else {
	while (maps && maps.length > 0) {
		map = maps.pop();
		if (map != null) {
			map.Dispose();
			map=null;
		}
	}

	if (map != null) {
		map.Dispose();
		map=null;
	}
      }
   }
}


		
function _tongInit(){
return;
   if (_analyz_is_ie) {
   	document.attachEvent('onload', tongfocused);
   	
   } else if (document.addEventListener) {
   	/* for Mozilla */
       document.addEventListener("DOMContentLoaded", tongfocused, null);
       
   } else {
   	window.onload = tongfocused;
   }
}

function _analyzInit(){
    if(typeof(USER_LANG_SELECT) == "undefined") {
	USER_LANG_SELECT = 2;
    }

  var agt = navigator.userAgent.toLowerCase();
  _analyz_is_ie = (agt.indexOf("msie")!=-1 && document.all);
  var h = '<table width="360" 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="_analyz_onmove=1;" onmouseout="_analyz_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="/">&#19968;&#23383;&#36890;</a> &nbsp; <a id="_analyzFrame_analyz_word" href="/china/baike.php" onclick="return appendWord(this);">&#30334;&#31185;&#36890;</a></div>';
  h += '</td>';
  h += '<td align="right" style="background-color:#C8DAF3;">';
  h += '<a href="'+_analyz_host+'help.php" target="_blank" title="&#24110;&#21161;" id="help">';
  h += '<img src="'+_analyz_host+'images/help.gif" style="border:none;display:inline;" align="absmiddle">';
  h += '</a> ';  
  h += '<td align="right" style="background-color:rgb(235,255,245)">';
  h += '<a href="javascript:_analyzClose()" title="&#20851;&#38381;" target="_self">';
  h += '<img src="'+_analyz_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="_analyz_onlayer=1;" onmouseout="_analyz_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="_analyzFrame" name="_analyzFrame" HEIGHT="auto" onload="calcHeight()" 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='_analyz_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);
    _analyzSet(el, h);
    
  }catch(x){
    alert("analyz can not support this page.");
    alert(x);
    _analyz_layer = true;
    return;
  }
  _analyzClose();

  
  if (_analyz_is_ie) {
    document.attachEvent("onmousemove", _analyzMove);
    document.attachEvent("ondblclick", _analyzQuery);
    document.attachEvent("onmouseup", _analyzQuery);
    document.attachEvent("onselectstart", _analyzSelect);
    document.attachEvent("onmousedown", _analyzCheck);
    document.attachEvent("onkeydown", keyWhat);
    
  } else {
    document.addEventListener("mousemove", _analyzMove, true);
    document.addEventListener("dblclick", _analyzQuery, true);
    document.addEventListener("mouseup", _analyzQuery, true);
    document.addEventListener("selectstart", _analyzSelect, true);
    document.addEventListener("mousedown", _analyzCheck, true);
    document.addEventListener("keypress",keyWhat,true);
    //document.getElementById('_analyzFrame_analyz_word').onclick = appendWord;
// addEventListener("click", appendWord, true);
  }

  _analyz_layer = document.getElementById('_analyz_layer');
  _analyz_iframe = document.getElementById('_analyzFrame');

}

function _analyzSet(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 _analyzGetSel()
{
	var range;

	if (document.selection) {
		range = document.selection.createRange();

		if (USER_LANG_SELECT >= PINYIN) {
			return range.htmlText;

		} else {
			return range.text;
		}

	} else if (window.getSelection) {
		var userSelection = window.getSelection();

		if (USER_LANG_SELECT < PINYIN) {
			return userSelection + '';
		}

		// Get the range:
		if (userSelection.getRangeAt) {
			range = userSelection.getRangeAt (0);
		} else {
			range = document.createRange ();
			range.setStart (userSelection.anchorNode, userSelection.anchorOffset);
			range.setEnd (userSelection.focusNode, userSelection.focusOffset);
		}

		// And the HTML:
		var clonedSelection = range.cloneContents ();
		var div = document.createElement ('div');
		div.appendChild (clonedSelection);

		return div.innerHTML;


	} else if (document.form0 && document.form0.word) {
		return document.form0.word.value+'';

	} else {
		return '';
	}
}


function _analyzGetPos(event){
  if (_analyz_is_ie) {
    _analyz_x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    _analyz_y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  } else {
    _analyz_x = event.clientX + window.scrollX;
    _analyz_y = event.clientY + window.scrollY;
  }
}


function _analyzSelect(e){
    if (_analyz_moving == 2)
        return false;
    else
        return true;
}

function _analyzCheck(e) {
    _analyzGetPos(e);
    var cx = 0;
    var cy = 0;
    var obj = _analyz_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(_analyz_moving>0){
        if(_analyz_onmove == 1){
            _analyz_moving = 2;
            _analyz_startx = _analyz_x;
            _analyz_starty = _analyz_y;
        }else if( _analyz_x < cx || _analyz_x > (cx + 240) || _analyz_y < cy || (!_analyz_onlayer && _analyz_y > (cy + 100)) ){
		if (_analyz_enable || USER_LANG_SELECT >= PINYIN) {
	    		_analyzClose();
		} else {
        		_analyz_moving = 1;
		}

        }else{

            _analyz_moving = 1;
        }
    }
    
}

function cancelKey(evt) 
{
	if (evt.preventDefault) 
	{
		evt.preventDefault();
		return false;
	}
	else 
	{
		//evt.keyCode = 0;
		evt.returnValue = false;
	}
}

function in_type_mode(e){
   e = (e) ? e : ((event) ? event : null);

   if (isNull(e)) {
	if (document.selection) {
		var type = document.selection.type;
		if (type == 'Text') {
			//return true;
		}
	}

   	return false;
   }

   var srcElement = e.srcElement;
   if (srcElement && (srcElement.type == "text" || srcElement.type == "textarea" || srcElement.type == "password" || srcElement.type == "button")) {
	return true;
   } else {
	if (!isNull(document.selection)) {
		var type = document.selection.type;
		if (type == 'Text') {
			// return true;
		}// else if (isNull(type)) {
			// alert('none here');
		//}
	}

      return false;
   }
}

function keyWhat(e){
   // var e=e? e : window.event;
   e = (e) ? e : ((event) ? event : null);
   if (e == null) {
   	return;
   }

   if (in_type_mode(e)) {
	return ;//false;
   }
   
   var keyCode = 0;
   if (_analyz_is_ie){
   	keyCode = e.keyCode;
   	if(keyCode == 16 || keyCode == 17 || keyCode == 18){
	    // doUnSelect(e);
	    return false;
   	}

   } else {
   	keyCode = e.which;
   	if(keyCode == 97 || keyCode == 99){
	    // doUnSelect(e);
	    return false;
   	}
   }
}
/*
function clearData(){
	window.clipboardData.setData('text','') 
}
function deldat(){
	if(clipboardData){
		clipboardData.clearData();
	}
}
setInterval("deldat();", 1000);
*/

copyToClipboard = function(txt) {
	if(window.clipboardData) {
	  window.clipboardData.clearData();
	  window.clipboardData.setData("Text", txt);
	} else if(navigator.userAgent.indexOf("Opera") != -1) {
	  window.location = txt;
	} else if (window.netscape) {
		try {
			netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
		} catch (e) {
			alert("You need set 'signed.applets.codebase_principal_support=true' at 'about:config'");
			return false;
		}
		var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
		if (!clip) return;
		var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
		if (!trans) return;
		trans.addDataFlavor('text/unicode');
		var str = new Object();
		var len = new Object();
		var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
		var copytext = txt;
		str.data = copytext;
		trans.setTransferData("text/unicode",str,copytext.length*2);
		var clipid = Components.interfaces.nsIClipboard;
		if (!clip) return false;
		clip.setData(trans,null,clipid.kGlobalClipboard);
	}
}

function isNull(element) {
	return (typeof(element) == "undefined" || !element || element == null || element == '');
}
function disableContextMenu(element) {
    if (typeof(element) == "undefined" || !element || element == null) {
    	return;
    }
    element.oncontextmenu = function() {
        return false;
    }
}
var allContextMenuDisabled = false;
function disableAllContextMenu(){
	if ((typeof(can_comment) == "undefined" || can_comment != 1) && (typeof(can_edit) == "undefined" || can_edit != 1)) {
		document.oncontextmenu=new Function("return false");
		return;
	}
	
	//if (event.button==2) return false;
	if(allContextMenuDisabled) return;
	allContextMenuDisabled = true;
	disableContextMenu(document.getElementById("maplereview_photo"));
}
// disable user's right click menu
// document.onmousedown=disableAllContextMenu;

function _unSelect()
{
    if (window.getSelection) { // FF, Safari, Opera
        var sel = window.getSelection();
        sel.removeAllRanges();
    } else if (document.selection) { // IE
        document.selection.empty();
    } else {
    	alert('Welcome to ' + BAIKEXUE_COM2 + '!');
    }
}

function doUnSelect(e) {
   	if ((typeof(can_comment) == "undefined" || can_comment != 1) && (typeof(can_edit) == "undefined" || can_edit != 1)) {
		_unSelect();
		cancelKey(e);
	}
}

function _analyzQuery(e)  {
   e = (e) ? e : ((event) ? event : window.event);
   if (in_type_mode(e)) {
	return;// false;
   }
    if(_analyz_moving == 1){
        if (_analyz_is_ie) {
            window.event.cancelBubble = true;
            window.event.returnValue = false;
        }else{
            e.preventDefault();
        }
        return false;
    }
    if(_analyz_moving == 2) {
        _analyz_cx = _analyz_x;
        _analyz_cy = _analyz_y;
        _analyz_moving = 1;
        return false;
    }

    _analyzGetPos(e);

   if (!_analyz_enable && USER_LANG_SELECT < PINYIN) {
	return;
   }

    var word = _analyzGetSel();

    if(_analyz_is_ie) word=word.replace(/^\s*|\s*$/g,"");

    // strip off html tags
    if (USER_LANG_SELECT >= PINYIN) {
	word = trimPinyins(word);
    }

    word = word.replace(/(<([^>]+)>)/ig,"");
    word = word.replace(/(\[([^\]]+)\])/ig,"");
    word=""+word;

    // this line will prevent the unfocus issue for a text or password field
    if(word == "") return true;
    
    // the maximum length allowed
    if ( (USER_LANG_SELECT < PINYIN && word.length > 32) || _analyz_old_word == word) {
    	// doUnSelect(e);
    	return true;
    }

    if (!_analyz_enable && USER_LANG_SELECT < PINYIN) return true;
    
    _analyzShow(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;
}


var diff_x = 0, diff_y = 0;
function _analyzDisplay(){
    var dx=262;
    var dy=264;
    _analyz_y += 8;
    _analyz_x += 16;
    if(_analyz_is_ie){
        if (document.documentElement.offsetHeight && document.body.scrollTop+document.documentElement.scrollTop+document.documentElement.offsetHeight - _analyz_y < dy){
            _analyz_y = document.body.scrollTop+document.documentElement.scrollTop + document.documentElement.offsetHeight - dy;
            _analyz_x += 14;
        }
        if (document.documentElement.offsetWidth && document.body.scrollLeft+document.documentElement.scrollLeft+document.documentElement.offsetWidth - _analyz_x < dx){
            _analyz_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 - _analyz_y < dy) {
            _analyz_y = document.body.scrollTop+document.documentElement.scrollTop + self.innerHeight - dy;
            _analyz_x += 14;
        }
        if (self.innerWidth && document.body.scrollLeft+document.documentElement.scrollLeft + self.innerWidth - _analyz_x < dx) {
            _analyz_x = document.body.scrollLeft+document.documentElement.scrollLeft + self.innerWidth - dx;
        }
    }


    if (!_analyz_enable && USER_LANG_SELECT < PINYIN) {
        _analyz_x = _analyz_x / 2;
        _analyz_y = _analyz_y / 2;
    }

    _analyz_cx = _analyz_x - diff_x;
    _analyz_cy = _analyz_y - diff_y;
    _analyz_startx = _analyz_x;
    _analyz_starty = _analyz_y;
    _analyz_layer.style.left = _analyz_cx+'px';
    _analyz_layer.style.top = _analyz_cy+'px';
    _analyz_layer.style.display="";
    _analyz_moving = 1;
}

function _analyzShow(word){
    _analyzDisplay();

    _analyz_iframe.src='about:blank';
    var iframeWin = window.frames['_analyzFrame'];
    iframeWin.document.open();

    if (USER_LANG_SELECT < PINYIN) {
        try{
            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="'+_analyz_host+'images/loading.gif" width="103" height="20" /></body></html>');
            iframeWin.document.close();
        }catch(x){}

        setTimeout(function(){_analyz_iframe.src=BAIKEXUE_COM2+'china/mini.php?o='+encodeURIComponent(word);},200);

    } else {
	// word = trimPinyins(word);

        iframeWin.document.write('<html><body><span style="font-size: 12px;color:#000000;line-height:17px;">'+word+'</span></body></html>');
    }

    _analyz_old_word = word;
}


function trimPinyins(word)
{
	// <span title=输>shū</span>
	word = word.replace(/(<SPAN\s+title=)/ig,"");
	word = word.replace(/(>[^><]+<\/SPAN>)/ig,"");
	word = word.replace(/(\")/ig,'');
	word = word.replace(/(\s+)/ig,"");

	return word;
}

function getHeight(o) {
	var h = 250;
	if(o.offsetHeight){
		h=o.offsetHeight;
	} else if (o.style.pixelHeight){ // o.style.height
		h=o.style.pixelHeight;
	} else if (o.clientHeight) {
		h=o.clientHeight;
	} else if (o.style.height) {
		h=o.style.height;
	}

	if (h > 250) {
		h = 250;
	} else if (h < 150) {
		h = 150;
	}

	return h;
}

function calcHeight()
{
	var iframeWin = document.getElementById('_analyzFrame');
	
	//change the height of the iframe
	iframeWin.height=getHeight(iframeWin.contentWindow.document.body);
}


function _setSize(w,h)
{
	var iframeWin = document.getElementById('_analyzFrame');
	
	//change the height of the iframe
	iframeWin.style.width=w;
	iframeWin.style.height=h;
}


function _analyzMove(e){
    if(_analyz_moving==2) {
        _analyzGetPos(e);
        _analyz_x = _analyz_x-_analyz_startx+_analyz_cx;
        _analyz_y = _analyz_y-_analyz_starty+_analyz_cy;
        if (document.documentElement.scrollWidth - _analyz_x < 262) {
            _analyz_x = document.documentElement.scrollWidth - 262;
        }
        _analyz_layer.style.left = _analyz_x+'px';
        _analyz_layer.style.top = _analyz_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 _analyzClose() {
    try
    {
        _analyz_moving = 0;
        _analyz_onmove = 0;
        _analyz_onlayer = 0;
	if (_analyz_layer.style.display == '') {
    		_analyz_layer.style.display="none";
	}
        setTimeout(function(){_analyz_old_word = "";},500);
    }
    catch (x)
    {
    }

}


function _analyzRemove() {
    try
    {
        _analyz_moving = 0;
        _analyz_onmove = 0;
        _analyz_onlayer = 0;
    	_analyz_enable = false;
    	_analyz_layer.style.display="none";
    }
    catch (x)
    {
    }

}

function _dictEnable(){
  if (_analyz_enable){
    _analyz_enable = false;
  }else{
    _analyz_enable = true;
  }
}

function _analyzStatus(){
    return '';
}
var shared_path='common';
var cshared_path="cpoetry";
var mshared_path="best";
if(!document.getElementById('_analyz_layer')){
var _analyz_is_ie = true;
var _analyz_host = BAIKEXUE_COM2 + 'dict/';
var _analyz_old_word = "";
var _analyz_moving = 0;
var _analyz_onmove = 0;
var _analyz_onlayer = 0;
var _analyz_startx = 0;
var _analyz_starty = 0;
var _analyz_cx = 0;
var _analyz_cy = 0;
var _analyz_x = 0;
var _analyz_y = 0;
var _analyz_enable = false;
var _analyz_layer;
var _analyz_status;
var _analyz_iframe;
_analyzInit();
}
/*
else{
    try{
    _analyz_enable = true;
    }catch(x){;}
}
*/
_tongInit();
_analyz_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;
}


