var mantou_is_ie = true;
var mantou_host = 'http://www.mantou.co.kr/cndb/';
var mantou_help = "http://www.mantou.co.kr/pen.html";
var mantou_banner = "http://www.mantou.co.kr/";
var mantou_enable = true;
var mantou_old_word = "";
var mantou_moving = 0;
var mantou_onmove = 0;
var mantou_onlayer = 0;
var mantou_startx = 0;
var mantou_starty = 0;
var mantou_cx = 0;
var mantou_cy = 0;
var mantou_x = 0;
var mantou_y = 0;
var mantou_layer;
var mantou_iframe;

function mantouInit(){
var agt = navigator.userAgent.toLowerCase();
  mantou_is_ie = (agt.indexOf("msie")!=-1 && document.all);
  var h = '<div id="mantou_layer" style="position:absolute;z-index:6000;display:none;background-color:#FFF;';
  if (mantou_is_ie) h += 'filter:Alpha(Opacity=96);';
  h += '"><table width="268" cellspacing="0" cellpadding="0" ';
  h += 'style="border-top:1px solid #d43f4a;border-left:1px solid #d43f4a;';
  h += 'border-right:1px solid #d43f4a;border-bottom:1px solid #d43f4a;';
  h += '"><tr><td>';
  h += '<div width="100%" style="cursor:move;background-color:#e96f79;border:0px;" onmouseover="mantou_onmove=1;" onmouseout="mantou_onmove=0;">';
  h += '<table width="100%"><tr><td align="left" width="60%" style="background-color:#e96f79;">';
  h += '<div style="color:#ffffff;font-size:12px;background-color:#e96f79;font-family:µ¸¿ò;" align="center"><b>www.mantou.co.kr</b></div>';
  h += '</td>';
  h += '<td align="right" style="background-color:#e96f79;">';
  h += '<a href="'+mantou_help+'" target="_blank" title="Help">';
  h += '<img src="'+mantou_host+'img/mini_help.gif" style="border:none;display:inline;" align="absmiddle">';
  h += '</a> ';
  h += '<a href="javascript:mantouClose()" title="Close" target="_self">';
  h += '<img src="'+mantou_host+'img/mini_close.gif" style="border:none;display:inline;" align="absmiddle">';
  h += '</a>';
  h += '</td></tr></table>';
  h += '</div>';

  h += '<table border="0" cellspacing="1" cellpadding="1" width="100%" align="center" onmouseover="mantou_onlayer=1;" onmouseout="mantou_onlayer=0;">';
  h += '<tr><td><fieldset color="#00c0ff">';
  h += '<table border="0" cellspacing="0" cellpadding="0" width="100%" align="center">';
  h += '<tr><td width="100%">';
  h += '<iframe id="mantouFrame" name="mantouFrame" height="150" src="about:blank" frameborder="0" width="100%"></iframe>';
  h += '</td></tr><tr align="center"><td width="100%" height="20">';
  h += '<font color="#000000" size="2" face="µ¸¿ò" size="8px">(C)&nbsp;2010 ';
  h += '<a href="http://www.mantou.co.kr" target="_blank"><font color="#ff7200" size="2" face="arial">mantou</font></a></font>';
  h += '</td></tr></table>';
  h += '<table border="0" cellspacing="0" cellpadding="0" width="100%" align="center">';
  h += '<tr><td width="100%">';
  h += '<iframe height="60" src="'+mantou_banner+'banner_s.html" frameborder="0" width="100%"></iframe>';
  h += '</td></tr></table>';  
  h += '</fieldset></td></tr></table>';
  h += '</td></tr></table></div>';
  document.write(h);

  mantou_layer = document.getElementById('mantou_layer');
  mantou_iframe = document.getElementById('mantouFrame');
  mantouClose();
  if (mantouRCookie("mantoustate") == '1' && mantou_enable) mantou_enable = false;
  mantouUpdateStatus();

  
  if (mantou_is_ie) {
    document.attachEvent("onmousemove", mantouMove);
    document.attachEvent("ondblclick", mantouQuery);
    document.attachEvent("onmouseup", mantouQuery);
    document.attachEvent("onselectstart", mantouSelect);
    document.attachEvent("onmousedown", mantouCheck);
    window.attachEvent("onload", mantouUpdateStatus);
  }else {
    document.addEventListener("mousemove", mantouMove, true);
    document.addEventListener("dblclick", mantouQuery, true);
    document.addEventListener("mouseup", mantouQuery, true);
    document.addEventListener("selectstart", mantouSelect, true);
    document.addEventListener("mousedown", mantouCheck, true);
    window.addEventListener("load", mantouUpdateStatus, true);
  }
  var img = new Image();
  img.src = mantou_host+"img/mini_loading.gif";
}

function mantouGetSel()
{
	if (window.getSelection) return window.getSelection();
	else if (document.getSelection) return document.getSelection();
	else if (document.selection)
	{
	    var oText=document.selection.createRange().text; 
	    /*
        if(oText.text.length>0) 
        { 
        str=oText.text; 
        oText.text="BuB"+oText.text+"EuE"; 
        } 

        oText.select(); 
        event.srcElement.innerHTML=event.srcElement.innerHTML.replace("BuB","<u style='FONT-WEIGHT: bold;COLOR: #ff3366'>").replace("EuE","</u>"); 
        */
	    return oText;
	    
	} 
	
	else return '';
}

function mantouGetPos(event){
  if (mantou_is_ie) {
    mantou_x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    mantou_y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }else {
    mantou_x = event.clientX + window.scrollX;
    mantou_y = event.clientY + window.scrollY;
  }
}


function mantouSelect(e){
    if (mantou_moving == 2)
        return false;
    else
        return true;
}

function mantouCheck(e) {
    mantouGetPos(e);
    var cx = 0;
    var cy = 0;
    var obj = mantou_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(mantou_moving>0){
        if(mantou_onmove == 1){
            mantou_moving = 2;
            mantou_startx = mantou_x;
            mantou_starty = mantou_y;
        }else if(mantou_x < cx || mantou_x > (cx + 240) || mantou_y < cy || (!mantou_onlayer && mantou_y > (cy + 100) ) ){
	    mantouClose();
        }else{
            
            mantou_moving = 1;
        }
    }
    
}

function mantouQuery(e)  {
    if(mantou_moving == 1){
        if (mantou_is_ie) {
            window.event.cancelBubble = true;
            window.event.returnValue = false;
        }else{
            e.preventDefault();
        }
        return false;
    }
    if(mantou_moving == 2) {
        mantou_cx = mantou_x;
        mantou_cy = mantou_y;
        mantou_moving = 1;
        return false;
    }

    mantouGetPos(e);
    if (!mantou_enable) return true;

    var word = mantouGetSel();
    if(mantou_is_ie) word=word.replace(/^\s*|\s*$/g,"");
    word=""+word;
    if(word == "" || word.length > 16 || word == mantou_old_word) return true;
    //var dicID=0;

    //mantouShow(dicID, word);
    mantouShow(word);

}

function mantouDisplay(){
    var dx=262;
    var dy=264;
    mantou_y += 8;
    mantou_x += 16;
    if(mantou_is_ie){
        if (document.documentElement.offsetHeight && document.body.scrollTop+document.documentElement.scrollTop+document.documentElement.offsetHeight - mantou_y < dy){
            mantou_y = document.body.scrollTop+document.documentElement.scrollTop + document.documentElement.offsetHeight - dy;
            mantou_x += 14;
        }
        if (document.documentElement.offsetWidth && document.body.scrollLeft+document.documentElement.scrollLeft+document.documentElement.offsetWidth - mantou_x < dx){
            mantou_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 - mantou_y < dy) {
            mantou_y = document.body.scrollTop+document.documentElement.scrollTop + self.innerHeight - dy;
            mantou_x += 14;
        }
        if (self.innerWidth && document.body.scrollLeft+document.documentElement.scrollLeft + self.innerWidth - mantou_x < dx) {
            mantou_x = document.body.scrollLeft+document.documentElement.scrollLeft + self.innerWidth - dx;
        }
    }
    mantou_cx = mantou_x;
    mantou_cy = mantou_y;
    mantou_startx = mantou_x;
    mantou_starty = mantou_y;
    mantou_layer.style.left = mantou_cx+'px';
    mantou_layer.style.top = mantou_cy+'px';
    mantou_layer.style.display="";
    mantou_moving = 1;
}

function mantouShow(word){
    mantouDisplay();
    try{
        mantou_iframe.src='about:blank';
        iframeWin = window.frames.mantouFrame;
        iframeWin.document.open();
        iframeWin.document.write('<html><body><span style="color:#666666;font-weight:bold;">search </span><span style="color:green;font-weight:bold;">'+word+'</span> :<br /><img src="'+mantou_host+'img/mini_loading.gif" width="70" height="15" /></body></html>');
        iframeWin.document.close();
    }catch(x){
    }

    setTimeout(function(){mantou_iframe.src=mantou_host+'mini_output.php?search=yes&q='+encodeURIComponent(word);},200);
    mantou_old_word = word;
}

function mantouMove(e){
    if(mantou_moving==2) {
        mantouGetPos(e);
        mantou_x = mantou_x-mantou_startx+mantou_cx;
        mantou_y = mantou_y-mantou_starty+mantou_cy;
        if (document.documentElement.scrollWidth - mantou_x < 262) {
            mantou_x = document.documentElement.scrollWidth - 262;
        }
        mantou_layer.style.left = mantou_x+'px';
        mantou_layer.style.top = mantou_y+'px';
    }    
}

function mantouClose() {
    try
    {
        mantou_moving = 0;
        mantou_onmove = 0;
        mantou_onlayer = 0;
    	mantou_layer.style.display="none";
        setTimeout(function(){mantou_old_word="";},500);
    	if(window.sf) sf();
    }
    catch (x)
    {
    }

}

function mantouWCookie(name,value)
{
    var date=new Date();
    var now=date.getTime();
    date.setTime(now+365*24*60*60*1000);
    document.cookie=name+"="+value+"; path=/; expires="+date.toGMTString();
}

function mantouRCookie(name)
{
    var cookie=String(document.cookie);
    var pos=cookie.indexOf(name+"=");
    if(pos!=-1){
        var end=cookie.indexOf("; ",pos);
        return cookie.substring(pos+name.length+1,end==-1?cookie.length:end);
    }
    return "";
}

function mantouEnable(){
  if (mantou_enable){
    mantou_enable = false;
    mantouWCookie("mantoustate", '1');
  }else{
    mantou_enable = true;
    mantouWCookie("mantoustate", '0');
  }
  mantouUpdateStatus();
}

function mantouUpdateStatus(){
  var el = document.getElementById('mantou_status');
  if(el){
    el.innerHTML = mantouStatus();
  }
}

function mantouStatus(){
    if (mantou_enable){
     	return '<a href="javascript:mantouEnable()" title="start" target="_self" style="color:#1A9100;font-size:14px;background-color:transparent;">stop</a>';
    }else{
      	return '<a href="javascript:mantouEnable()" title="stop" target="_self" style="color:#1A9100;font-size:14px;background-color:transparent;">start</a>';
    }
}
