byMessageStatus = true;

function byMessageView(){
	var div = document.getElementById("byMessage");
	if(!byMessageStatus){
		byMessageStatus = true;
//		var iframe = document.getElementById("byMessageFrame");
//		iframe.src = "http://show.daum.net/by/message/list.daum?messageBean.by_main_id="+id;
		div.style.display = "";
	}else{
		div.style.display = "none";
		byMessageStatus = false;
	}
}

var byImages = new Array();
function byPlayImageSet(){
	var mainDiv = document.getElementById("byTemplate");
	if(mainDiv !=null){
		var divs = mainDiv.getElementsByTagName("DIV");
		var check = false;
		var imageCnt = 0;
		for(var i=0;i<divs.length;i++){
			var type = divs[i].getAttribute("type");
			if(type == "image"){
				var multi = divs[i].getAttribute("multi");
				var image = divs[i].getElementsByTagName("IMG");
				if(multi){
					byImages[imageCnt] = image;
					check = true;
					imageCnt++;
				}
				for(var j=0;j<image.length;j++){
					image[j].onclick = byImageClick;
					image[j].style.cursor ="pointer";
				}
			}
		}
		if(byImages.length >0 && check){
			setInterval(byPlayImage,3000);
		}
	}
}
function byImageClick(){
	var src = this.src;
	src = src.replace(/\/show_t_\w+\//,"/image/");
	src = src.replace(/\/S\d+x\d+\//,"/image/");
	hide_historyPopBox();
	show_historyPopBox(src);
	gLink(this, "BYTO", "1", "1");
	smartLog(this, "s=TO&a=BYTO&pg=1&r=1&p=1&rc=1",event); 
}

function byPlayImage(){

	for(var i=0;i<byImages.length;i++){
		var check = false;
		var changeImage = false;
		var images = byImages[i];
		
		for(var j=0;j<images.length;j++){
			if(images[j].style.display != "none"){
				images[j].style.display = "none";
				check = true;
				continue;
			}
			if(check){
				images[j].style.display = "";
				changeImage = true;
				break;
			}
		}
		if(changeImage == false && images.length>0){
			images[0].style.display = "";
		}
	}
}
function byRandomAuto(){
	setTimeout(goRandom,3000);
}
function goRandom(){
	location.href="http://show.daum.net/by/top/random.daum?show=auto";
}
// 팝업 위치 재설정
function replace_historyPopBox() {
    if (document.getElementById("history_popOriginImg")) {
        if (document.getElementById("history_popOriginImg").style.display != "none") {
            
            byImgW=document.getElementById('by_image_view').width+2;
	        byImgH=document.getElementById('by_image_view').height+1;
	        if (byImgW<250){
	        	byImgW=250;
	        }else if (byImgW>800){
	        	byImgW=800;
	        }
	        if (byImgH<200){
	        	byImgH=200;
	        }else if (byImgH>700){
	        	byImgH=700;
	        }
	        
	        document.getElementById("history_popOriginImg").style.top = (parseInt(document.body.scrollTop) + ((parseInt(document.body.clientHeight) - byImgH) / 2)) + "px";
            document.getElementById("history_popOriginImg").style.left = (parseInt(document.body.scrollLeft) + ((parseInt(document.body.clientWidth) - byImgW) / 2)) + "px";   
            
	        document.getElementById("history_popImgBox").style.width=byImgW;
	        document.getElementById("history_popImgBox").style.height=byImgH;
	        if (!document.all){
	        	byImgW =byImgW +2;
	        	byImgH = byImgH+2;
	       	}
	        document.getElementById("history_popOriginImg").style.width=byImgW;
	        document.getElementById("history_popOriginImg").style.height=byImgH+27;
	        document.getElementById("poTop").style.width=byImgW;
            
        }
    }
}

// 팝업 보이기
var byImgW=0;
var byImgH=0;
function show_historyPopBox(imgsrc) {
    if (!document.getElementById("history_popOriginImg")) {
        var popWrap = document.createElement("div");
        popWrap.id = "history_popOriginImg";
        document.body.appendChild(popWrap);
        var strHtml = "";
        strHtml += "<div class='poTop' id='poTop'>";
        strHtml += "    <div class='topL'></div>";
        strHtml += "    <div class='topLg'></div>";
        strHtml += "    <div class='topMain'><a href='javascript:;'><img src='http://imgsrc.search.daum.net/search_all/webtab/ic_popclose.gif' width='14' height='13' onclick='javascript:hide_historyPopBox();' /></a></div>";
        strHtml += "    <div class='clr'></div>";
        strHtml += "</div>";
        strHtml += "<div class='poMid' id='history_popImgBox' style='width:100px;'><a href='javascript:;' onclick='javascript:hide_historyPopBox();'><img src='" + imgsrc + "' id='by_image_view'/></a></div>";
        popWrap.innerHTML = strHtml;
        document.getElementById("history_popOriginImg").style.width=0;
        document.getElementById("history_popOriginImg").style.height=0;
        setTimeout('replace_historyPopBox()',1000); 
    }
}

// 팝업 감추기
function hide_historyPopBox() {
    if (document.getElementById("history_popOriginImg")) {
        document.body.removeChild(document.getElementById("history_popOriginImg"));
    }
}


/* ActiveX */
function showActiveX(obj,div){
	// generate html code
	// for ie obejct
	var html = '<object ';
	if (!obj.id && !obj.name){
		var r = Math.round(Math.random()*100);
		html += 'id="daumActiveXObject'+r+'" name="daumActiveXObject'+r+'" ';
	} else {
		if (obj.id) html += 'id="'+obj.id+'" ';
		else html += 'id="'+obj.name+'" ';
		if (obj.name) html += 'name="'+obj.name+'" ';
		else html += 'name="'+obj.id+'" ';
	}
	if (obj.type) html += 'type="'+obj.type+'" ';
	if (obj.classid) html += 'classid="'+obj.classid+'" ';
	if (obj.width) html += 'width="'+obj.width+'" ';
	if (obj.height) html += 'height="'+obj.height+'" ';
	if (obj.codebase) html += 'codebase="'+obj.codebase+'" ';
	// append events
	for (var i in obj.events){
		if (obj.events[i]){
			html += obj.events[i][0]+'="'+obj.events[i][1]+'" ';
		}
	}
	// end of object tag
	html += '>\n';
	// append params
	for (var i in obj.param){
		if(obj.param[i][0] != undefined ){
			html += '<param name="'+obj.param[i][0]+'" value="'+obj.param[i][1]+'"/>\n';
		}
	}

	// for ns embed
	html += '<embed ';
	if (!obj.id && !obj.name){
		var r = Math.round(Math.random()*100);
		html += 'id="daumActiveXObject'+r+'" name="daumActiveXObject'+r+'" ';
	} else {
		if (obj.id) html += 'id="'+obj.id+'" ';
		if (obj.name) html += 'name="'+obj.name+'" ';
	}
	if (obj.type) html += 'type="'+obj.type+'" ';
	if (obj.width) html += 'width="'+obj.width+'" ';
	if (obj.height) html += 'height="'+obj.height+'" ';
	// append params
	for (var i in obj.param){
		if (obj.param[i]){
			if (obj.param[i][0]=='movie' || obj.param[i][0]=='src'){
				var _src = obj.param[i][1];
			}
			html += obj.param[i][0]+'="'+obj.param[i][1]+'" ';
		}
	}
	html += '/>\n';
	html += '</object>';

	var isIE = (document.all)?true:false;
	if (isIE){
		document.getElementById(div).innerHTML = html;
	} else if (obj.type=='application/x-shockwave-flash' || obj.classid.toLowerCase()=='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000'){
		// ie외의 브라우저에서 activex가 flash 경우만 노출
		document.getElementById(div).innerHTML = html;
	} else if (navigator.platform.indexOf('Win')>=0 && obj.classid.toLowerCase()=='clsid:22d6f312-b0f6-11d0-94ab-0080c74c7e95'){
		// Windows Media Player의 경우 windows platform에서만 노출
		document.getElementById(div).innerHTML = html;
	}
}
function ShowFlash(src,fv,width,height,div){
	/*
	var obj = new Object();
	obj.id = div+"_movie";
	obj.type = 'application/x-shockwave-flash';
	obj.classid = 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000';
	obj.codebase = 'http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.c-ab#version=9,0,0,0';
	obj.width = width;
	obj.height = height;

	var param = [		
		['movie',src],
		['src',src],
		['quality','high'],
		['wmode','transparent'],
		['allowScriptAccess','always'],
		['bgcolor','#FFFFFF'],
		['FlashVars',fv]
	];
	obj.param = param;
	showActiveX(obj,div);
	*/
	embedSWF(src,fv,width,height,div);
}


function embedSWF(f, fv, w, h, div){
	var param = {
		id: "UIswf_" + f,
		quality: 'high',
		bgcolor: '#ffffff',
		allowScriptAccess: 'always',
		allowFullScreen: 'true',
		wmode: "transparent"
	}
	
	var id = 'id="' + param.id + '"';
	var name = 'name="' + param.id + '"';
	var p = '', e = '';
	
	for (i in param) {
		if (i == 'id') 
			continue;
		p += '<param name="' + i + '" value="' + param[i] + '">\n';
		e += i + '="' + param[i] + '" ';
	}
	var s = '<object ' + id + ' width="' + w + '" height="' + h + '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0">';
	s += '<param name="movie" value="' + f + '">' + p;
	s += '<embed ' + name + ' src="' + f + '" width="' + w + '" height="' + h + '" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" ' + e + '/>';
	s += '</object>';
	document.getElementById(div).innerHTML = s;
}


// 둥둥이 
function byEvent(){
	var r = parseInt(Math.random() * 10);
	var top = "150px";
	document.write("<div id='IconPop' style='position:absolute;top:"+top+";left:690px;display:;'><a href='http://search.daum.net/search?nil_suggest=btn&nil_ch=&rtupcoll=&w=tot&m=&lpp=&q=@%BC%D2%C1%F6%BC%B7+%BD%BA%C5%B8%C0%CF' target='_blank'><img src='http://imgsrc.search.hanmail.net/search_all/icon/icn_080411_vonin_1.gif' width='75' height='75'></a></div>");
}


function floatEvent(image,url){
	var r = parseInt(Math.random() * 10);
	var top = "125px";
	document.write("<div id='IconPop' style='position:absolute;top:"+top+";left:690px;display:;'><a href='"+url+"'><img src='"+image+"'></a></div>");
}

function floatEvents(events){
	if(events.length > 0 ){
		var r = parseInt(Math.random() * events.length);
		var image = events[r].image;
		var url = events[r].url;
		floatEvent(image,url);
	}	
}


var $A = Array.from = function(iterable) {
  if (!iterable) return [];
  if (iterable.toArray) {
    return iterable.toArray();
  } else {
    var results = [];
    for (var i = 0, length = iterable.length; i < length; i++)
      results.push(iterable[i]);
    return results;
  }
}
Function.prototype.bind = function() {
	var __method = this, args = $A(arguments), object = args.shift();
	return function() {
		return __method.apply(object, args.concat($A(arguments)));
	}
}
var XMLLoader = function() {
	this.request = null;
	this.onSuccess = showCallback;
	this.onFailure = null;
	this.onTimeout = null;
	this.t = null;
	this.target = "_top";
}
XMLLoader.prototype.getXMLHttpRequest = function () {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try	{
			return new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try	{				
				return new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e1) { return null; }
		}
	}
}

XMLLoader.prototype.callFailureHandler = function() {
	if (this.onFailure) this.onFailure();
}
XMLLoader.prototype.callSuccessHandler = function(objXML) {
	if (this.onSuccess) this.onSuccess(objXML);
}
XMLLoader.prototype.callTimeoutHandler = function() {
	if (this.onTimeout) this.onTimeout();
}

XMLLoader.prototype.processReqChange = function(){
	if (this.xmlHttpReq.readyState == 4) {
			if(this.xmlHttpReq.status == 200 || this.xmlHttpReq.status == 304){
				this.onSuccess(this.xmlHttpReq.responseText,this.target);
			}else{
				this.onFailure();
			}
	}
}
XMLLoader.prototype.load = function( url ) {
	this.xmlHttpReq = this.getXMLHttpRequest();
	if(!this.xmlHttpReq) {
		this.callFailureHandler();
		return;
	} 
	try{
    	this.xmlHttpReq.open("GET", url, true);
        this.xmlHttpReq.onreadystatechange = this.processReqChange.bind(this);
        this.xmlHttpReq.send(null);
        return false;
    }catch(e){
		alert(e);
    }
}

var showView = function(){
	this.Mx = null;
	this.My = null;
}

function showCallback(ret,target,nomsg){
	    var msg='';
	    if (ret == null|| ret.length <10){

	    	if(nomsg == undefined)
		        msg ='<div class="noKw">검색쇼가 없습니다.</div>'
		    else
		    	msg = '<div class="noKw">'+nomsg+'</div>';
	    }else{
			var show = ret.split("&&");
	        msg ='<ul>';
	        for (var i=0;i<show.length ;i++ ){
	        	var s = show[i].split("||");
                msg=msg+'<li onMouseOver="this.style.background=\'#ebebeb\';"  onMouseOut="this.style.background=\'#ffffff\';"><a href="http://search.daum.net/search?w=tot&q='+s[1]+'" onFocus="this.blur();" target="'+target+'">'+s[0]+'</a></li>';
	        }
	        msg +='</ul>';
	    }
	    document.getElementById('kwDiv').innerHTML = msg;
	    document.getElementById('kwDiv').style.display="block";
	    document.getElementById('kwDiv').style.left = this.Mx -2;
	    document.getElementById('kwDiv').style.top = this.My -document.getElementById('kwDiv').offsetHeight +30;
}
function viewKw(e,id,by_main_id,target){

	var show = new XMLLoader();
	if(target == "new")
		show.target="_new"
	show.Mx = (!document.all) ? e.pageX : event.clientX + document.body.scrollLeft;
	show.My = (!document.all) ? e.pageY : event.clientY + document.body.scrollTop;
	show.load("/by/message/show.daum?id="+id+"&by_main_id="+by_main_id);
}
function viewKwSearch(e,nomsg){
	var show = new XMLLoader();
	var target="_top";
	var c = document.getElementById("byShowFrame");
	var content = c.contentWindow.document.getElementById("msg").innerHTML;
	content = content.replace(/&amp;/g,"&");
	show.Mx = (!document.all) ? e.pageX : event.clientX + document.body.scrollLeft;
	show.My = (!document.all) ? e.pageY : event.clientY + document.body.scrollTop;
	show.onSuccess(content,target,nomsg);
}
	