var jes=ct=xy={};
(function(){
	//pidÃ¼Å©
	var pid='';
	if(typeof(xy_page_key)!='undefined') pid = xy_page_key;
	else if(typeof(ct_pid)!='undefined') pid = ct_pid;

	if(!pid) return;

	//jes	
	jes.$=function(s) { return document.getElementById(s) };
	jes.getEl=function(e){var E=jes.getE(e);return E.target || E.srcElement}
	jes.getE=function(e){return e || window.event}
	jes.addEvent=function(object, type, listener) {	
		if(object.addEventListener) {if(type=='mousewheel')type='DOMMouseScroll'; object.addEventListener(type, listener, false)}
		else { object.attachEvent("on"+type, listener); }
	};
	jes.stopEvent=function(event) {
		var e=event || window.event;
		if(e.preventDefault) {e.preventDefault(); e.stopPropagation(); }
		else {e.returnValue = false; e.cancelBubble = true;}
	};
	jes.DynamicScript=function(url,enc){
		this.url=url||'';
		this.enc=enc||'';
		this.head=document.getElementsByTagName("head").item(0);
		if(this.url) this.call(this.url);
	};
	jes.DynamicScript.prototype={
		noCacheParam:function(){
			var b=(this.url.indexOf('?')==-1) ? '?':'&';
			return b+'nOcAchE='+(new Date()).getTime();
		},
		call:function(url){
			try{this.head.removeChild(this.script)}catch(e){};
			this.url=url;
			this.script = document.createElement("script");
			this.script.setAttribute("type", "text/javascript");   
			this.script.setAttribute("src", this.url+this.noCacheParam());
			if(this.enc) this.script.setAttribute("charset", this.enc);
			this.head.appendChild(this.script);
			jes.sleep(100);
		}
	};
	jes.sleep=function(milliseconds) {
		var start = new Date().getTime();
		while (true) {
			if ((new Date().getTime() - start) > milliseconds){
			break;
			}
		}
	};

	//ct	
	ct.chkAtag=function(node){
		return (node.tagName=="A" && node.href!="") ? node : null;
	};
	ct.getNodeIndex=function(node,tag){
		if(node && node.parentNode)
		{
			var childs = node.parentNode.childNodes;

			for(var i=0,j=0; i<childs.length; i++)
			{
				if(childs[i].nodeType!=1 || childs[i].tagName!=tag) continue; //ff
				if(childs[i] == node )return j;
				j++;
			}
		}
		return -1;
	};
	ct.getPath=function(node){
		
		while(node)
		{
			if(node.nodeType!=1) continue;
			
			if(node.id && !/[0-9]{2,}/.test(node.id)) //id°¡ ³ª¿Ã¶§±îÁö && ¼ýÀÚ°¡ ¹Ýº¹µÇ´Â id´Â skip
			{
				tree.push('@'+node.id);
				break;
			}
			if(node.tagName=='BODY') //body ÅÂ±× ³ª¿Ã¶§±îÁö
			{
				tree.push('_BODY');
				break;
			}

			tree.push(node.tagName + ":" + ct.getNodeIndex( node ,node.tagName));
			node=node.parentNode;
		}
		
		tree.reverse();
		return tree.join('/');
	};
	ct.getZoneName = function(class_name){
		return class_name.replace(/(.+)?(CT_ZONE_.[^ ]+)(.+)?/,'$2');

	};
	ct.getZone=function(node){
		while(node)
		{
			if(node.nodeType!=1) continue;

			if(node.className.indexOf('YTN_CSA_') > -1)
			{
				return ct.getZoneName(node.className)
				break;
			}
			if(node.tagName=='BODY') break;

			node=node.parentNode;
		}
		return '';
	};
	ct.click=function(event){
		var node=jes.getEl(event);
		node=ct.chkAtag(node) || ct.chkAtag(node.parentNode); 
		if(!node) return;	
		ct.call(node);	
	
		//jes.stopEvent(event); //debug
	};
	ct.call=function(node){
		var nid = ct.getPath(node);
		var zid = ct.getZone(node);
		
//		alert(nid + "\n\n" + zid) //debug

		dynamic.call('http://mom2.ytn.co.kr/_comm/yct.php?callback=ct.callBack&pid='+pid+'&nid='+encodeURIComponent(nid)+'&zid='+zid);	

		tree=[];//ÃÊ±âÈ­
	};
	ct.callBack=function(){	};

	ct.pid=pid;

	xy=ct;
	var tree=[];
	var dynamic  = new jes.DynamicScript();

	if(typeof(ct_collect_stop) == 'undefined') jes.addEvent(document ,'click', ct.click);
})(); //end funciton
