﻿/*
	통합검색 관련 스크립트
*/

//header 자동완성기능 레이어 관련
function autoComFunc(){
	if(!document.getElementById("autoComLayer")) return false; //id 없을 경우 체크
	var gID = document.getElementById("autoComLayer");
	var gIcon = document.getElementById("autoComplete");
	var source;
	var gImgSrc = gIcon.lastChild.lastChild.getAttribute("src");

	if(gID.style.display == "none"){
		gID.style.display = "block";
		source = gImgSrc.replace("down.gif","up.gif");
		gIcon.lastChild.lastChild.setAttribute("alt","자동완성 접기");
		gIcon.lastChild.lastChild.setAttribute("src",source);
	} else {
		gID.style.display = "none";
		source = gImgSrc.replace("up.gif","down.gif");
		gIcon.lastChild.lastChild.setAttribute("alt","자동완성 펼치기");
		gIcon.lastChild.lastChild.setAttribute("src",source);
	}
}

//header 실시간 인기 검색어
function realTimeFunc(flag){
	if(!document.getElementById("realTimeSrhLayer")) return false; //id 없을 경우 체크
	var gID = document.getElementById("realTimeSrhLayer");
	if(flag == "open"){
		gID.style.display = "block";
	} else {
		gID.style.display = "none";
	}
}

//header 실시간 인기 검색어 롤링
function textScroll(scroll_el_id) {
    this.objElement = document.getElementById(scroll_el_id);
    this.objElement.style.position = 'relative';
    this.objElement.style.overflow = 'hidden';

    this.objLi = this.objElement.getElementsByTagName('li');
    this.height = this.objElement.offsetHeight; // li 엘리먼트가 움직이는 높이(외부에서 변경가능)
    this.num = this.objLi.length; // li 엘리먼트의 총 갯수
    this.totalHeight = this.height*this.num; // 총 높이
    this.scrollspeed = 2; // 스크롤되는 px
    this.objTop = new Array(); // 각 li의 top 위치를 저장
    this.timer = null;
    
    for(var i=0; i<this.num; i++){
        this.objLi[i].style.position = 'absolute';
        this.objTop[i] = this.height*i;
        this.objLi[i].style.top = this.objTop[i]+"px";
    }
}

textScroll.prototype.move = function(){
    for(var i=0; i<this.num; i++) {
        this.objTop[i] = this.objTop[i] - this.scrollspeed;
        this.objLi[i].style.top = this.objTop[i]+"px";
    }
    if(this.objTop[0]%this.height == 0){
        this.jump();
    }else{
        clearTimeout(this.timer);
        this.timer = setTimeout(this.name+".move()",50);
    }
}

textScroll.prototype.jump = function(){
    for(var i=0; i<this.num; i++){
        if(this.objTop[i] == this.height*(-2)){
            this.objTop[i] = this.objTop[i] + this.totalHeight;
            this.objLi[i].style.top = this.objTop[i]+"px";
        }
    }
    clearTimeout(this.timer);
    this.timer = setTimeout(this.name+".move()",3000);
}

textScroll.prototype.start = function() {
    this.timer = setTimeout(this.name+".move()",3000);
}


//검색된 목록 정렬
function srhOrderFunc(t,v){
	if(!document.getElementById(v)) return false; //id 없을 경우 체크
	
	var gID = document.getElementById(v);
	var gNum = gID.getElementsByTagName("dd");
	var gParent = t.parentNode;

	for(var i=0; i<gNum.length; i++){
		gNum[i].className = "off";
	}
	gParent.className = "on";
}

//상세검색열기
function detailSrhViewFunc(t,v){
	if(!document.getElementById(v)) return false; //id 없을 경우 체크

	var gImg = t.getElementsByTagName("img")[0];
	var gImgSrc = gImg.getAttribute("src");
	var source;
	var gID = document.getElementById(v);

	if(gID.style.display == "none"){
		source = gImgSrc.replace("open.gif","close.gif");
		gImg.setAttribute("alt","상세검색닫기");
		gID.style.display = "block";
	} else {
		source = gImgSrc.replace("close.gif","open.gif");
		gImg.setAttribute("alt","상세검색열기");
		gID.style.display = "none";
	}
	gImg.setAttribute("src",source);
}

//오늘의 인기기사
function todayTabChangeFunc(t,showElement,hideElement){
	var gParent = t.parentNode;
	var gGrand = gParent.parentNode;
	var gGrandLI = gGrand.getElementsByTagName("li");
	
	var showElement = document.getElementById(showElement);
	var hideElement = document.getElementById(hideElement);
	if(gParent.className == "tabOff"){
		showElement.style.display = "block";
		hideElement.style.display = "none";
	} else {
		showElement.style.display = "block";
		hideElement.style.display = "none";
	}
	
	if(gGrandLI[1] == gParent){
		gParent.className = "tabOn";
		gParent.style.marginLeft = "-1px";
		gGrandLI[0].className = "tabOff";
		gGrandLI[0].style.marginLeft = "0";
	} else {
		gParent.className = "tabOn";
		gParent.style.marginLeft = "0";
		gGrandLI[1].className = "tabOff";
		gGrandLI[1].style.marginLeft = "-1px";
	}
		
	
}














