var _ = jQuery.noConflict();

// PNG24
function setPng24(obj) {
	obj.width=obj.height=1;
	obj.className=obj.className.replace(/\bpng24\b/i,''); 
	obj.style.filter =
	"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+ obj.src +"',sizingMethod='image');"
	obj.src='about:blank;';
	return '';
}

function topRanking(target){
	this.dropListNum;
	this.oldActiveNum;
	this.activeNum=0;
	
	this.source = document.getElementById(target);
	this.sourceLi = this.source.getElementsByTagName("LI");
	this.sourceLiHeight= this.sourceLi[0].offsetHeight;
	this.sourceA = this.source.getElementsByTagName("A");
		
	var func = this;
	for(i=0; i<this.sourceLi.length; i++){
		this.sourceLi[i].style.top = this.sourceLiHeight + "px";
	
	}

	var sourceLiHeight = this.sourceLiHeight;
	var actionFunc = setInterval(function(){
		func.action();
	}, 50);
	var changeFunc = setInterval(function(){
		func.change();
	}, 3000);

}
topRanking.prototype.action = function(){
	var source = this.source;
	var sourceLi = this.sourceLi;
	var sourceLiHeight = this.sourceLiHeight;

	var activeNum = this.activeNum;
	var dropListNum = this.dropListNum;
	var oldActiveNum = this.oldActiveNum;

	if(oldActiveNum !== undefined){
		var gap1 = 0.2*(-sourceLiHeight - (parseFloat(sourceLi[oldActiveNum].style.top)));
		sourceLi[oldActiveNum].style.top = parseFloat(sourceLi[oldActiveNum].style.top) + Math.floor(gap1) + "px"; 
	}
	var gap2 = 0.2*(-parseInt(sourceLi[activeNum].style.top, 10));
	sourceLi[activeNum].style.top = parseFloat(sourceLi[activeNum].style.top) + Math.floor(gap2) + "px"; 
}
topRanking.prototype.change = function(){
	var sourceLi = this.sourceLi;
	var sourceLiHeight = this.sourceLiHeight;

	if(this.dropListNum !== undefined) sourceLi[this.dropListNum].style.top = sourceLiHeight + "px"; 
	this.oldActiveNum = this.activeNum;
	this.dropListNum = this.oldActiveNum;
	if(sourceLi.length-1 == this.activeNum) this.activeNum=0; 
	else ++this.activeNum;
}

function mnScroller(target) {
    this.dropListNum;
    this.oldActiveNum;
    this.activeNum = 0;

    this.source = document.getElementById(target);
    this.sourceLi = this.source.getElementsByTagName("LI");
    this.sourceLiHeight = this.sourceLi[0].offsetHeight;
    this.sourceA = this.source.getElementsByTagName("A");

    var func = this;
    for (i = 0; i < this.sourceLi.length; i++) {
        this.sourceLi[i].style.top = this.sourceLiHeight + "px";

    }

    var sourceLiHeight = this.sourceLiHeight;
    var actionFunc = setInterval(function() {
        func.action();
    }, 50);
    var changeFunc = setInterval(function() {
        func.change();
    }, 5000);
}
mnScroller.prototype.action = function() {
    var source = this.source;
    var sourceLi = this.sourceLi;
    var sourceLiHeight = this.sourceLiHeight;

    var activeNum = this.activeNum;
    var dropListNum = this.dropListNum;
    var oldActiveNum = this.oldActiveNum;

    /*
    if (sourceLi[oldActiveNum] !== undefined && sourceLi[oldActiveNum].style.display != "none")
        sourceLi[oldActiveNum].style.display = 'none';
    if (sourceLi[activeNum] !== undefined && sourceLi[activeNum].style.display != "inline")
        sourceLi[activeNum].style.display = 'inline';
    */
    
    if (oldActiveNum !== undefined) {
        var gap1 = 0.2 * (-sourceLiHeight - (parseFloat(sourceLi[oldActiveNum].style.top)));
        sourceLi[oldActiveNum].style.top = parseFloat(sourceLi[oldActiveNum].style.top) + Math.floor(gap1) + "px";
    }
    var gap2 = 0.2 * (-parseInt(sourceLi[activeNum].style.top, 10));
    sourceLi[activeNum].style.top = parseFloat(sourceLi[activeNum].style.top) + Math.floor(gap2) + "px";
}
mnScroller.prototype.change = function() {
    var sourceLi = this.sourceLi;
    var sourceLiHeight = this.sourceLiHeight;

    if (this.dropListNum !== undefined) sourceLi[this.dropListNum].style.top = sourceLiHeight + "px";
    this.oldActiveNum = this.activeNum;
    this.dropListNum = this.oldActiveNum;
    if (sourceLi.length - 1 == this.activeNum) this.activeNum = 0;
    else ++this.activeNum;
}

//----------------------------------------
// TOGGLE
//----------------------------------------
function toggle(id){ 
  var element = document.getElementById(id); 
  element.style.display = (element.style.display == 'block') ? "none" : "block"; 
} 

// Show/Hidden
function displayLayer(id, disp) {
	_("#"+id).css('display',disp);
}

var turnOff;
function combo(sId, ImgId, imgOut) {
    var stat = _("#" + sId).css('display');
	
	if (stat == "none"){
	    displayLayer(sId, "block");
	    if (turnOff) { _("#" + ImgId)[0].src = _("#" + ImgId)[0].src.replace("_out.", "_on."); turnOff = false; }
		_("#"+ImgId)[0].src = _("#"+ImgId)[0].src.replace("_off.", "_on.");
	}else{		
		displayLayer(sId, "none");
		if (imgOut) { _("#" + ImgId)[0].src = _("#" + ImgId)[0].src.replace("_on.", "_out."); turnOff = true; }
		_("#"+ImgId)[0].src = _("#"+ImgId)[0].src.replace("_on.", "_off.");
	}

	if (sId == "AllLyr") {
	    //SetOverFlash(sId, stat);
	}
}

/**
* 전체 메뉴 레이어와 겹치는 flash 영역을 숨기거나 z-index 설정을 변경한다.
*/
function SetOverFlashZIndex(sId) {
    // 신문홈
    obj = document.getElementById("paperviewer");
    if (obj != null)
        obj.style.zIndex = 0;

    // 잡지홈
    obj = document.getElementById("magazineviewer");
    if (obj != null)
        obj.style.zIndex = 0;
}

function SetOverFlash(sId, opt) {
    var obj;
    
    // 신문/잡지 썸네일보기
    obj = document.getElementById("mz_list2");
    if (obj != null)
        obj.style.minHeight = 700;
        
    obj = document.getElementById("PreeViewer");
    if (obj != null) {
        var o = _("#" + obj.id);
        o.parents(".mz_list2").css('height', o.css('height'));
        obj.style.display = opt;
    }

    // 기사 뷰어
    //if (sId == 'AllLyr') {
        obj = document.getElementById("articleViewer");
        if (obj != null)
            obj.style.display = opt;
    //}
    
    SetOverFlashZIndex(sId);
}

function comboMd(open) {

    var stat = _("#mdPrev2").css('display');
    if (open == "open") { stat = "none"; }
    else if (open == "close") { stat = "block"; }

    if (stat == "none") {
        _("#mdPrev2").show();
        _("#mwrap").height("173px");
        _("#opmd")[0].src = _("#opmd")[0].src.replace("_on.", "_off.");
    } else {
        _("#mdPrev2").hide();
        _("#mwrap").height("43px");
        _("#opmd")[0].src = _("#opmd")[0].src.replace("_off.", "_on.");
    }
}


var refer=false;
function combo2(sId){
	if (refer) {
	_("#"+sId)[0].src = _("#"+sId)[0].src.replace("_off.", "_on.");
	refer=false;
	}else {
	_("#"+sId)[0].src = _("#"+sId)[0].src.replace("_on.", "_off.");
	refer=true;
	}
}

function tabView(tabSize, tabId, idx, targetId) {
	
	for ( var i = 0; i < tabSize; i++ ) {
		if ( i == idx ) {
		    _("#" + tabId + i)[0].className = _("#" + tabId + i)[0].className.replace("off", "on");

		    if (_("#" + targetId + i)) {
		        _("#" + targetId + i).show();
			}
		} else {
		    _("#" + tabId + i)[0].className = _("#" + tabId + i)[0].className.replace("on", "off");

			if ( _("#"+targetId + i) ) {
				_("#"+targetId + i).hide();
			}
		}
	}
}

function tabImg(tabSize, tabId, idx) {

    for (var i = 0; i < tabSize; i++) {
        if (i == idx) {
            _("#" + tabId + i)[0].src = _("#" + tabId + i)[0].src.replace("_off.", "_on.");

        } else {
            _("#" + tabId + i)[0].src = _("#" + tabId + i)[0].src.replace("_on.", "_off.");
        }
    }
}

function tabViewId(tabSize, idx, targetId) {

    for (var i = 0; i < tabSize; i++) {
        if (i == idx) {
            if (_("#" + targetId + i)) {
                _("#" + targetId + i).show();
            }
        } else {
            if (_("#" + targetId + i)) {
                _("#" + targetId + i).hide();
            }
        }
    }
}


function tabImgView(tabSize, tabId, idx, targetId) {
	

	for ( var i = 0; i < tabSize; i++ ) {
		if ( i == idx ) {
			_("#"+tabId + i)[0].src = _("#"+tabId + i)[0].src.replace("_off.", "_on.");
			
			if ( _("#"+targetId + i) ) {
				_("#"+targetId + i).show();
			}
		} else {
			_("#"+tabId + i)[0].src = _("#"+tabId + i)[0].src.replace("_on.", "_off.");

			if ( _("#"+targetId + i) ) {
				_("#"+targetId + i).hide();
			}
		}
	}
}

// image rollover
_(function() {    
	_("img.rollover").hover(
	function() {
	try{_("img.rolloversel")[0].src = _("img.rolloversel")[0].src.replace("_on", "_off");}catch(e){}
	this.src = this.src.replace("_off", "_on");
	},
	function() {
	this.src = this.src.replace("_on","_off");
	try{_("img.rolloversel")[0].src = j("img.rolloversel")[0].src.replace("_off", "_on");}catch(e){}
	}); 
});

// JavaScript 플래시 관련 공통함수 Util 
function SWFLoader() {
	this.id = "";
	this.title = undefined;
	this.wmode = "window";
	this.flashvars = "";
	this.classId = 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000';
	this.codeBase = 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0';
	this.pluginSpage = 'http://www.macromedia.com/go/getflashplayer';
	this.embedType = 'application/x-shockwave-flash';
}
SWFLoader.prototype.setting = function(obj){
	
	for(var property in obj){
		this[property] = obj[property];
	}
	
	if(this.alternate && document.getElementById(this.alternate)){
		var node = document.getElementById(this.alternate);
		node.style.display = "none";
		var tmp = node.cloneNode(true);
		tmp = node.innerHTML;
		this.alternate = tmp;
	}
	
	this.parameter = "";
	this.parameter += "<param name='allowScriptAccess' value='always' />";
	this.parameter += "<param name='allowFullScreen' value='false' />";
	this.parameter += "<param name='movie' value='"+this.url+this.flashvars+"' />";
	this.parameter += "<param name='wmode' value='"+ this.wmode +"' />";
	this.parameter += "<param name='quality' value='best'/>";
	this.parameter += "<param name='base' value='.'>";
	this.parameter += "<param name='scale' value='noscale'/>";
}
SWFLoader.prototype.addParameter = function(name, value){
	this.parameter += "<param name='"+name+"' value='"+value+"'/>";
}
SWFLoader.prototype.show = function(){
	var str = "";
	var title = (this.title) ? ' title="'+this.title+'"' : "";
	str += '<object id="'+this.id+'" width="'+this.width+'" height="'+this.height+'" classid="'+this.classId+'" codebase="'+this.codeBase+'"'+ title+'>';
		str += this.parameter;
		str += '<!--[if !IE]>-->';
		str += '<object type="application/x-shockwave-flash" data="'+this.url+this.flashvars+'" width="'+this.width+'" height="'+this.height+'" name="'+this.id+'"'+ title +'>';
		str += this.parameter;
		str += '<!--<![endif]-->';
			//str += this.alternate // 대체택스트
		str += '<!--[if !IE]>-->';
		str += '</object>';
		str += '<!--<![endif]-->';
	str += '</object>';
	
	document.write(str);
}




//----------------------------------------
// 이미지 Rolling
//----------------------------------------
function ImageRotation() {
	var scroll = {time:1, start:0, change:0, duration:50, timer:null};
	var originaltime = scroll.time;
	var objWidth = 0;
	var currentNum = 0;
	var tmpName, tmpWrap, tmpListId, tmpNext, tmpPrev, tmpNum, tmpTime, moveEvent, restNum, objNum;
	this.GoodsSetTime = null;
	var cloneElement = [];

	this.setScrollType = function (obj) {}
	var func = this;

	this.initialize = function () {
		tmpNum = this.listNum;
		tmpTime = this.GoodsSetTime;
		tmpWrap = document.getElementById(this.wrapId);
		tmpListId = document.getElementById(this.listId);
		tmpNext = document.getElementById(this.btnNext);
		tmpPrev = document.getElementById(this.btnPrev);

		objNum = tmpListId.getElementsByTagName('li').length;

		//for (var j=0; j<objNum; j++) {
			//tmpListId.getElementsByTagName('li')[j].className = 'num' + (j + 1);
		//}

		//tmpNext.onmouseover = function () { if (!this.getElementsByTagName('img')[0].src.match('_off.gif')) this.getElementsByTagName('img')[0].src = this.getElementsByTagName('img')[0].src.replace('_off.gif', '_on.gif'); }
		//tmpPrev.onmouseover = function () { if (!this.getElementsByTagName('img')[0].src.match('_off.gif')) this.getElementsByTagName('img')[0].src = this.getElementsByTagName('img')[0].src.replace('_off.gif', '_on.gif'); }

		//tmpNext.onmouseout = function () { this.getElementsByTagName('img')[0].src = this.getElementsByTagName('img')[0].src.replace('_on.gif', '_off.gif'); }
		//tmpPrev.onmouseout = function () { this.getElementsByTagName('img')[0].src = this.getElementsByTagName('img')[0].src.replace('_on.gif', '_off.gif'); }
		
		
		objWidth = tmpListId.getElementsByTagName('li')[0].offsetWidth * tmpNum;
		tmpListId.style.width = (objNum * tmpListId.getElementsByTagName('li')[0].offsetWidth) + 'px';
	
		tmpWrap.style.width = objWidth + 'px';

		tmpListId.style.overflow = 'hidden';
		tmpWrap.style.overflow = 'hidden';

		tmpNext.onclick = setPrev;
		tmpPrev.onclick = setNext;

		if (this.autoScroll == 'none') {
			// do nothing.
		} else {
			moveEvent = 'setPrev';
			clearInterval(tmpTime);
			tmpTime = setInterval(function () { eval(moveEvent + '();'); }, this.scrollGap);
		}
	}

	var setNext = function() {
	    func.NextEvent();

	    if (objNum <= tmpNum) return false;
	    moveEvent = 'setNext';

	    for (var i = 0; i < tmpNum; i++) {
	        var objLastNode = tmpListId.removeChild(tmpListId.getElementsByTagName('li')[objNum - 1]);
	        tmpListId.insertBefore(objLastNode, tmpListId.getElementsByTagName('li')[0]);
	    }

	    tmpWrap.scrollLeft = objWidth;

	    var position = getActionPoint('indirect');
	    startScroll(position.start, position.end, 'next');
	    return false;
	}

	var setPrev = function() {
	    func.PrevEvent();
	    
	    if (objNum <= tmpNum) return false;
	    moveEvent = 'setPrev';
	    var position = getActionPoint('direct');
	    startScroll(position.start, position.end, 'prev');
	    return false;
	}

	var startScroll = function (start, end, location) {
		if (scroll.timer != null) {
			clearInterval(scroll.timer);
			scroll.timer = null;
		}

		scroll.start = start;
		scroll.change = end - start;
		scroll.timer = setInterval(function () {
			scrollHorizontal(location);
		}, 15);
	}

	var scrollHorizontal = function (location) {
		if (scroll.time > scroll.duration) {
			clearInterval(scroll.timer);
			scroll.time = originaltime;
			scroll.timer = null;
			if (location == 'prev') {
				for (var i=0; i<tmpNum; i++) {
					var objFirstNode = tmpListId.removeChild(tmpListId.getElementsByTagName('li')[0]);
					tmpListId.appendChild(objFirstNode);
				}

			}
			tmpWrap.scrollLeft = 0;
		} else {
			tmpWrap.scrollLeft = sineInOut(scroll.time, scroll.start, scroll.change, scroll.duration);
			scroll.time++;
		}
	}

	var getActionPoint = function (dir) {
		var end;

		if (dir == 'direct') end = tmpWrap.scrollLeft + objWidth;
		else end = tmpWrap.scrollLeft - objWidth;

		var start = tmpWrap.scrollLeft;

		var position = {start:0, end:0};
		position.start = start;
		position.end = end;

		return position;
	}

	var sineInOut = function (t, b, c, d) { return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b; }

	var findElementPos = function (elemFind) {
		var elemX = 0;
		elemX = tmpWidth*(elemFind/tmpNum);
		return elemX;
	}
}
ImageRotation.prototype.NextEvent = function() { }
ImageRotation.prototype.PrevEvent = function() { }
