function I_(keywordList, allKeywordCnt, removeList, pCnt){$_(keywordList, allKeywordCnt, removeList, pCnt);}
function P_(reTry){$__(reTry);}
function vS_(keyword){$searchDiv(keyword)}
function beA_(title,description,siteHost,ClickUrl,keyword){
	var html = '<div id="lasOut"><div id="lasIn" onclick="if(!_isIconClick){$openUrl(\'_blank\',\'' + ClickUrl + '\');return false;}else{_isIconClick=false;}"><a href="' + ClickUrl + '" target="_blank" title="'+title+'">';
	html += title + '</a> ';
	html += '<a href="' + ClickUrl + '" target="_blank" title="'+description+'"';
	html += ' class="desc">' + description + '</a> ';
	html += '<a href="' + ClickUrl + '" target="_blank" title="'+siteHost+'"';
	html += ' class="host">' + siteHost + '</a>';
	html += '<p id="icon"><a href="http://www.keywordsconnect.com/sub/ad.asp" target="_blank" onfocus="blur();" onclick="_isIconClick=true;"><img src="http://img.keywordsconnect.com/icon1.gif" /></a><img src="http://img.keywordsconnect.com/icon0.gif" /><a href="http://www.keywordsconnect.com/" target="_blank" onfocus="blur();" onclick="_isIconClick=true;"><img src="http://img.keywordsconnect.com/icon2.gif" /></a><img src="http://img.keywordsconnect.com/icon0.gif" /><img src="http://img.keywordsconnect.com/icon3.gif" onclick="_isIconClick=true;$hidetip();" /></p></div></div>';
	__adLayer.innerHTML=html;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
function $getobj(obj){
    return document.getElementById? __window.document.getElementById(obj) : eval(__window.obj);
}

function $encodeBURL(input) {
    var output = "";
    var retVal = "";
    var chr1, chr2, chr3;
    var enc1, enc2, enc3, enc4;
    var i = 0;
    var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+_="; // '/' -> '_'
    do {
	chr1 = input.charCodeAt(i++);
	chr2 = input.charCodeAt(i++);
	chr3 = input.charCodeAt(i++);
	
	enc1 = chr1 >> 2;
	enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
	enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
	enc4 = chr3 & 63;
		
	if (isNaN(chr2))
	    enc3 = enc4 = 64;
	else if (isNaN(chr3))
	    enc4 = 64;
	
	output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + 
	    keyStr.charAt(enc3) + keyStr.charAt(enc4);
    } while (i < input.length);
    if(output.length > 230){
	retVal += output.substr(0, 114);
	retVal += output.substring(-114, 114);
    }else
	retVal = output;
    return retVal;
}

function $getNumResults(s){
    try{
	return parseInt(s.match(/numResults=\"([\d])\"/)[1]);
    }catch(e){
	return 0;
    }
}

function $getURL(s, keyword) {
    var retUrl = 'http://click.keywordsconnect.com/';
    var url = s.match(/<ClickUrl type=\"body\">(.*?)<\/ClickUrl>/)[1];
    retUrl += '?url=' + escape(url)  + "&keyword=" + keyword + "&articleUrl=" + escape(__URL) + "&media=" + __media;
    return retUrl;
}

function $getBrowser(){
    // getType == IFRAME then used
    // IE : true --> Opera : false || other : false;
    // window.attachEvent : IE, Opera
    // window.addEventListener : FF, Chroem, safari
    return (window.attachEvent)?((navigator.appName.indexOf("Opera") != -1)?false:true):false;
}

function $iframeInit(obj){
    // getType == IFRAME then used
    var w = obj.contentWindow.document.body.scrollWidth;
    var h = obj.contentWindow.document.body.scrollHeight;
    var addHeight = (navigator.appVersion.indexOf("Safari") != -1)?16:0;

    obj.width = w;
    obj.height = h+addHeight;
}

function $importIFRAME(keyword){
    var url = 'http://get.keywordsconnect.com/xml.asp?getType=IFRAME&articleUrl=' + escape(__URL) + "&media=" + __media + "&keyword=" + escape(keyword) + "&searchUrl="+ escape(__internalSearchUrl) +"&searchWords=" + escape(__searchWords) + "&cssUrl=" + escape(__cssURL);
    var ifrHTML = "<iframe id='__adIframe' name='__adIframe' src='"+ url +"' frameborder='0' marginwidth='0' marginheight='0' allowTransparency='true'";			
    ifrHTML += ($getBrowser())?"":" width='0' height='0'";
    ifrHTML += " style='margin:0px; padding:0px;' onload='$iframeInit(this)'></iframe>";
    __adLayer.innerHTML = ifrHTML;
}

function $importSWF(keyword, rtNum){
    var url = 'http://get.keywordsconnect.com/xml.asp?getType=SWF&rtNum=' + rtNum + '&articleUrl=' + escape(__URL) + "&media=" + __media + "&keyword=" + escape(keyword);
    $$SWFRequest.request(url, keyword, "$changeOV");
}

function $importJSONP(keyword, rtNum){
    var url = 'http://get.keywordsconnect.com/xml.asp?getType=JSONP&rtNum=' + rtNum + '&articleUrl=' + escape(__URL) + "&media=" + __media + "&keyword=" + escape(keyword);
    $createScript(url, 'uScript');
}

function $createScript(url, jName){

    var oldJsonp = $getobj(jName);
    if (oldJsonp != null) {
	oldJsonp.parentNode.removeChild(oldJsonp);
	delete oldJsonp;
    }
    var head = document.getElementsByTagName("head")[0];
    var script = document.createElement('script');
    script.id = jName;
    script.type = 'text/javascript';
    script.src = url;
    head.appendChild(script);
}

function $changeOV(xmlHttp, keyword) {
    _cache[keyword] = xmlHttp;
    if($getNumResults(xmlHttp) == 1)
	$renderDiv(xmlHttp, keyword);
    else{
	if(_Data && $getNumResults(xmlHttp) == 0)
	    $____(keyword);
	else if($getNumResults(xmlHttp) == 2){
	    if(__getType == 'JSONP')
		$importJSONP(keyword, '3');
	    else
		$importSWF(keyword, '3');
	}else
	    $searchDiv(keyword);
    }
}

function $renderDiv(s, keyword){
    try{
	var title = s.match(/title=\"(.*?)\"/)[1];
	var description = s.match(/description=\"(.*?)\"/)[1];
	var siteHost = s.match(/siteHost=\"(.*?)\"/)[1];
	var ClickUrl = $getURL(s, keyword);
	var html = '<div id="lasOut"><div id="lasIn" onclick="if(!_isIconClick){$openUrl(\'_blank\',\'' + ClickUrl + '\');return false;}else{_isIconClick=false;}"><a href="' + ClickUrl + '" target="_blank" title="'+title+'">';
	html += title + '</a> ';
	html += '<a href="' + ClickUrl + '" target="_blank" title="'+description+'"';
	html += ' class="desc">' + description + '</a> ';
	html += '<a href="' + ClickUrl + '" target="_blank" title="'+siteHost+'"';
	html += ' class="host">' + siteHost + '</a>';
	html += '<p id="icon"><a href="http://www.keywordsconnect.com/sub/ad.asp" target="_blank" onfocus="blur();" onclick="_isIconClick=true;"><img src="http://img.keywordsconnect.com/icon1.gif" /></a><img src="http://img.keywordsconnect.com/icon0.gif" /><a href="http://www.keywordsconnect.com/" target="_blank" onfocus="blur();" onclick="_isIconClick=true;"><img src="http://img.keywordsconnect.com/icon2.gif" /></a><img src="http://img.keywordsconnect.com/icon0.gif" /><img src="http://img.keywordsconnect.com/icon3.gif" onclick="_isIconClick=true;$hidetip();" /></p></div></div>';
	__adLayer.innerHTML=html;
    } catch(e){
    	$searchDiv(keyword);
    }
}

function $searchDiv(keyword){
    if(__isInternalSearch){
	var html = '<div id="lasOut"><div id="lasIn">';
	html += '<a href="' + __internalSearchUrl + escape(keyword) + '" target="_blank"> \'';
	html += keyword + '\' ' + __searchWords + '</a><p id="icon"><a href="http://www.keywordsconnect.com/" onclick="_isIconClick=true;" target="_blank" onfocus="blur();"><img src="http://img.keywordsconnect.com/icon2.gif" /></a><img src="http://img.keywordsconnect.com/icon0.gif" /><img src="http://img.keywordsconnect.com/icon3.gif" onclick="_isIconClick=true;$hidetip();" /></p></div></div>';
    }else
	html = '';
    __adLayer.innerHTML=html;
}

function $getposOffset(what, offsettype){
    var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
    var parentEl=what.offsetParent;
    while (parentEl!=null){
        totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
        parentEl=parentEl.offsetParent;
    }
    return totaloffset;
}

function $showhide(obj, e, visible, hidden, tipwidth){
    if (tipwidth!=""){
        __adLayer.widthobj=__adLayer.style;
        __adLayer.widthobj.width=tipwidth;
    }
    if (e.type=="click" && obj.visibility==hidden || e.type=="mouseover")
        obj.visibility=visible;
    else if (e.type=="click")
        obj.visibility=hidden;
}

function $iecompattest(){
    return (__window.document.compatMode && __window.document.compatMode!="BackCompat")? __window.document.documentElement : __window.document.body;
}

function $clearbrowseredge(obj, whichedge){
    var edgeoffset=(whichedge=="rightedge")? parseInt('0px')*-1 : parseInt('6px')*-1;
    if (whichedge=="rightedge"){
        var windowedge=__window.document.all && !__window.opera? $iecompattest().scrollLeft+$iecompattest().clientWidth : __window.pageXOffset+__window.innerWidth;
        __adLayer.contentmeasure=__adLayer.offsetWidth;
        if (windowedge-__adLayer.x < __adLayer.contentmeasure)
            edgeoffset=__adLayer.contentmeasure-obj.offsetWidth;
    }
    else{
        var windowedge=__window.document.all && !__window.opera? $iecompattest().scrollTop+$iecompattest().clientHeight : __window.pageYOffset+__window.innerHeight;
        __adLayer.contentmeasure=__adLayer.offsetHeight;
        if (windowedge-__adLayer.y < __adLayer.contentmeasure)
            edgeoffset=__adLayer.contentmeasure+obj.offsetHeight;
    }
    return edgeoffset
}

function $setPosLAS(obj, e, tipwidth){
    var mX;
    if (window.event)
        event.cancelBubble=true;
    else if (e.stopPropagation)
        e.stopPropagation();

    $clearhidetip();
    __adLayer.innerHTML = '';
    __adLayer.x=$getposOffset(obj, "left");
    __adLayer.y=$getposOffset(obj, "top");
    mX = e.pageX ? e.pageX : __adLayer.x - obj.offsetLeft + e.x;
    if(mX + 3 < __adLayer.x){ //ie8 bug(+3)
    	if((__adLayer.x - obj.offsetLeft + 50) > mX){
	    var w = e.pageX ? 10 : 0;
	    __adLayer.x = __adLayer.x - obj.offsetLeft + w;
    	}else
	    __adLayer.x = mX - 20;
    	__adLayer.y = obj.offsetHeight / 2 + 2 + __adLayer.y;
    }else if(obj.offsetHeight > 20)
    	if(e.x < 200)
	    __adLayer.y = obj.offsetHeight / 2 + 2 + __adLayer.y;
    __adLayer.style.left=__adLayer.x-$clearbrowseredge(obj, "rightedge")-10+"px";
    __adLayer.style.top=__adLayer.y-$clearbrowseredge(obj, "bottomedge")+10+"px";
    $showhide(__adLayer.style, e, "visible", "hidden", tipwidth);
}

function $hidetip(){
    if (typeof __adLayer!="undefined")
    	__adLayer.style.visibility="hidden";
}

function $delayhidetip(){
    delayhide=setTimeout("$hidetip()", __lhdt);
}

function $clearhidetip(){
    if (typeof delayhide!="undefined")
        clearTimeout(delayhide);
}

function $addEvent(obj, event ,listener)
{
    if (obj.addEventListener)
        obj.addEventListener(event, listener, false);
    else if (obj.attachEvent)
        obj.attachEvent('on' + event, listener);
}

function $getElementsByClassName() {
    var my_array = __window.document.getElementsByTagName("a");
    var retvalue = new Array();
    for (var i=0, j=0;i<my_array.length;i++) {
        var c = " " + my_array[i].className + " ";
        if (c.indexOf(" adlink ") != -1  || c.indexOf(" dklink ") != -1) retvalue[j++] = my_array[i];
    }
    return retvalue;
}

function $getOverture(e){
    if(!_mouseState){
	var obj = e.srcElement ? e.srcElement : e.target;
	var keyword = obj.childNodes.item(0).nodeValue;
	obj.title = '';
	$setPosLAS(obj, e, '');
	if(__getType == 'IFRAME')
	    $importIFRAME(keyword);
	else if(_cache[keyword])
	    $renderDiv(_cache[keyword], keyword);
	else
	    _delayGet=setTimeout("$import" + __getType + "('" + keyword + "','2');", 100);
    }
}

function $hideOverture(){
    if (typeof _delayGet != "undefined")
        clearTimeout(_delayGet);
    $delayhidetip();
}

function $openUrl(keyword, url){
    $hidetip();
    window.open(url, keyword);
}

function $endReceive(keyword, url){
    if(_cache[keyword]){
	clearInterval(_tid);
	if($getNumResults(_cache[keyword]) == 1)
	    url = $getURL(_cache[keyword], keyword);
        $openUrl(keyword, url);
    }
}

function $sendOverture(e){
    var obj = e.srcElement ? e.srcElement : e.target;
    var keyword = obj.childNodes.item(0).nodeValue;
    var url='';
    if(_cache[keyword]){
	if($getNumResults(_cache[keyword]) == 1){
	    obj.href = $getURL(_cache[keyword], keyword);
	    $hidetip();
	    return true;
	}else{
	    if(!__iskeywordClick){ //edaily _cache Check**Click ON
		if (window.event)
		    event.cancelBubble=true;
		else if (e.preventDefault)
		    e.preventDefault();
		return false;
	    }else{
		url = __internalSearchUrl + escape(keyword);
	    }
	}
    }else{	    
	if(!__iskeywordClick){ //edaily _cache Check**Click ON
	    if (window.event)
		event.cancelBubble=true;
	    else if (e.preventDefault)
		e.preventDefault();
	    return false;
	}else{
	    if(url == '')
		obj.href = __searchUrl + escape(keyword);
	    else
		obj.href = url
	}
	//obj.target = keyword;
	//obj.href = 'about:blank';
	//f = "$endReceive('" + keyword + "','" + url + "')";
	//_tid=setInterval(f ,200);
    }
}

function $getCSS(csshref){
    __cssURL = csshref;

    var CSS   = document.createElement('link');
    CSS.rel   = 'stylesheet';
    CSS.type  = 'text/css';
    CSS.href  = csshref;
    CSS.media = 'screen';
    __window.document.getElementsByTagName('head')[0].appendChild(CSS);
}

function $addImg(tempArray){
    preloadImg = new Array();
    for(var i=0; i<tempArray.length;i++){
	preloadImg[i] = new Image;
	preloadImg[i].src = tempArray[i];
    }
}

function $init(){
    var objs = $getElementsByClassName();
    if(objs.length > 0 && __media != 'pressian')
	$addAction();
    else{
	_Data = true;
	var url = 'http://data.keywordsconnect.com/Js/' + __media + '/' + __pCode + '/' + __bUrl + '.js';
	$createScript(url, 'uScript');
    }
}

function $addAction(){
    var objs = $getElementsByClassName();
    if(objs.length > 0)
	for(var i=0; i < objs.length;i++){
	    $addEvent(objs[i], 'mouseover', $getOverture);
	    $addEvent(objs[i], 'mouseout', $hideOverture);
	    $addEvent(objs[i], 'click', $sendOverture);
	}
}	

function $__(reTry){
    if(!reTry)
	reTry = '0';
    var url = 'http://cf.keywordsconnect.com/?reTry=' + reTry + '&articleFolder=' + __pCode + '&bUrl=' + __bUrl + "&media=" + __media + "&articleUrl=" + escape(__URL);
    $createScript(url, 'aScript');
}

function $___(){
    return
}

function $____(keyword){
    var url = 'http://rk.keywordsconnect.com/?articleFolder=' + __pCode + '&bUrl=' + __bUrl + "&media=" + __media + "&articleUrl=" + escape(__URL) + '&keyword=' + keyword + '&maxCount=' + __maxCount;
    $createScript(url, 'aScript');
}

function $_sub(html, tmpIndex, keyword, i){
    var frontWord3 = html.substr(tmpIndex-3,3);
    
    if(frontWord3 == "$]]" || html.substr(tmpIndex + keyword.length,3) == "[[$" || frontWord3.search(/[\s|\W|\w]{1}[\uac00-\ud7a3|\w]{1}/) > 0){
	html = html.replace(keyword,'[[$]]');
	tmpIndex = html.indexOf(keyword,tmpIndex);
	if(tmpIndex > -1)
	    html = $_sub(html, tmpIndex, keyword, i);
	html = html.replace('[[$]]', keyword);
    }else{
	html = html.replace(keyword, '[[$'+ i + '$]]');
    }
    return html
}

function $_(keywordList, allKeywordCnt, removeList, pCnt){
    var keywords = keywordList.split(',');
    if(Math.floor(allKeywordCnt / 8) > keywords.length && pCnt >= -5)
	$__('True');
    
    if(keywordList != ''){
	var j = 0;
	var flag=true;
        var tmpIndex;
	var contents = $getobj(__contentsDiv);
	var html = contents.innerHTML;
	if(__media == 'pressian'){
	    var pptn=/(<TD.*?>กใ(.*?)\s*<\/TD>)/g;
	    try{
		if(pptn.test(html)) {
		    html=html.replace("name=img_resize","name=img_resize id=id='emato_ad'");	
		    html=html.replace(RegExp.$1,RegExp.$1+"</tr><tr><td><div id='divBox' class='box'></div></td>");
		}
	    }catch(e){
		flag=true;
	    }
	}
	var tagList = html.match(__pattern);
	html = html.replace(__pattern, "[[^@]]");
	for(var i = 0; i < keywords.length; i++){
	    if(keywords[i] == "KT")
		continue;
	    if( j >= __maxCount)
		break;
            tmpIndex = html.indexOf(keywords[i]);
            if(tmpIndex > -1){
                html = $_sub(html, tmpIndex, keywords[i], i);
		if(html.indexOf("[[$" + i + "$]]") != -1)
		    j += 1;
		flag=false;
            }
	}
	for(var i = 0; i < tagList.length; i++)
	    html = html.replace("[[^@]]",tagList[i]);
	for(var i = 0; i < keywords.length; i++){
	    if(html.indexOf('[[$') == -1)
		break;
	    html = html.replace('[[$' + i + '$]]', '<a href="' + __URL + '" class="dklink" target="_blank">' + keywords[i] + '</a>');
	}
	if(flag)
	    $__('True');
	else{
	    contents.innerHTML = html;
	    $addAction();
	}
    }else if(((__media == 'idomin' || __media == 'newsway21') && allKeywordCnt == 0) || (__media == 'frontiertimes' && allKeywordCnt == 3) || removeList != '')
	$__('True');
    if(__media == 'hankooki'){
	try{
	    LeftScroll();
	}catch(e){
	    return;
	}
    }	    
}

function $isIframe(flag){
    return flag ? window.parent : window;
}

function $initGlobalModule(){
    if(!__window.document.getElementById("divLAS")){
	__adLayer = document.createElement("div");
	__adLayer.id = "divLAS";
	__window.document.body.appendChild(__adLayer);
    }
    __adLayer = $getobj("divLAS");
    _cache = new Array;
    _mouseState = 0;
    _isIconClick = false;
    $addEvent(__adLayer, 'mouseover', $clearhidetip);
    $addEvent(__adLayer, 'mouseout', $delayhidetip);

    if($getobj(__contentsDiv)){
	$addEvent($getobj(__contentsDiv), 'mousedown', function(){_mouseState=1;});
	$addEvent($getobj(__contentsDiv), 'mousemove', function(){if(_mouseState){_mouseState=2}else{_mouseState=0;}});
	$addEvent($getobj(__contentsDiv), 'mouseup', function(){_mouseState = 0;});
    }
    __internalSearchUrl = 'http://internalsearch.keywordsconnect.com/?articleUrl=' + escape(__URL) + '&media=' + __media + '&keyword=';
    __searchUrl = 'http://search.keywordsconnect.com/?articleUrl=' + escape(__URL) + '&media=' + __media + '&keyword=';
    if(__getType == "SWF"){
	var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
	var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
	var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
	$$SWFRequest = {
	    tmpid : 'tmpSwf'+(new Date).getMilliseconds()+Math.floor(Math.random()*100000),
	    write : function(swf_path){
		if(isIE)
		    document.write('<div style="position:absolute;top:-100px;left:-100px"><object id="'+$$SWFRequest.tmpid+'" width="1" height="1" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0"><param name="movie" value="'+swf_path+'"><param name = "allowScriptAccess" value = "always" /><embed name="'+$$SWFRequest.tmpid+'" src="'+swf_path+'" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="1" height="1" allowScriptAccess="always" swLiveConnect="true"></embed></object></div>');
		else
		    document.write('<div style="position:absolute;top:-100px;left:-100px"><embed id="'+$$SWFRequest.tmpid+'" name="'+$$SWFRequest.tmpid+'" src="'+swf_path+'" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="1" height="1" allowScriptAccess="always" swLiveConnect="true"></embed></div>');
	    },
	    request : function(url, keyword, callback){
		var swfObj = $getobj($$SWFRequest.tmpid);
		swfObj.requestToFlash(url, keyword, callback);
	    }
	};
	$$SWFRequest.write("http://js.keywordsconnect.com/DKL.swf");
    }
}
//-----------------
$completeInitLas();