/**************************************
* WRITER : iNum, 2008-02-27, dcinside corp.
***************************************/

function include_adscript(src) { 
	var script = document.createElement("script"); 
	script.type = 'text/javascript'; 
	//script.defer = true; 
	//alert(src);
	script.src = src; 
	//script.charset = 'euc-kr'; 	
	document.getElementsByTagName('head')[0].appendChild(script); 		
	//return script; 
} 


function DcinsideBar(varName){
	if(varName==null){
		alert("변수명을 입력해 주세요");
		return;
	}

	var dBar = this;
	dBar.varName = varName;
	dBar.overSrc = "";
	dBar.overAdSrc = "";

	dBar.isViewAd = false;
	dBar.isViewRun = false;
	dBar.AdSet = {
		scUpLen : 245,
		height : 40,
		adUpTermTime : 500,
		adAutoCloseTime : 5000
	};

	dBar.moveAdTimeId = [];
	dBar.isLoadedBottomBlank = false;




	return this;
}

DcinsideBar.prototype = {
	init : function(){
		//GEID('dcBar_BG'),GEID('dcBar_Ad')
		this.BarObj = {
			bg :null, 
			udAd :GEID('dcBar_Ad')
		}




		try{
			if(dBar.overAdSrc) {
				this.BBT = GEID('BrandBlankTable');
				this.BBT.style.height="55px";
				this.isLoadedBottomBlank = true;
			}
		}catch(e){
			this.isLoadedBottomBlank = false;
		}
	},
	positionReset : function(){
		BarObj = this.BarObj;
		var hInfo = getHtmlInfo(BarObj.bg);
		var barWidth = hInfo.offsetWidth;
		if (barWidth < 1024) barWidth = 1024;

		BarObj.bg.style.width = barWidth - 20 + "px";
		GEID('bar_ad').style.width = BarObj.bg.style.width;
		var height = parseInt(BarObj.bg.style.height);
		GEID('bar_ad').style.height = BarObj.bg.style.height;
		//alert(1);
		//GEID('mainBackGround').style.height = parseInt(hInfo.offsetHeight)-0 + "px";

		//BarObj.bg.style.top   = parseInt(hInfo.clientHeight + hInfo.scrollTop - height) + "px";
		//BarObj.bg.style.left  = hInfo.scrollLeft + "px";
		if(true && this.isViewAd){
			this.clearTimeObj(this.moveAdTimeId);
			this.moveAdTimeId=[];
			var oAd = BarObj.udAd;
			this.setOpacityObj(oAd, 10);
			var oAdheight = parseInt(oAd.style.height.replace(/px/,''));
			oAd.style.top         = parseInt(hInfo.offsetHeight  -  this.AdSet.scUpLen - height) + "px";
			if(parseInt(oAd.style.left) >  hInfo.offsetWidth-20 - parseInt(oAd.style.width)) 
				oAd.style.left = GEID('dcMainAll').offsetWidth - parseInt(oAd.style.width)-20 + "px";
		}
		//disOn(BarObj.bg);
		return "";
	},

	pReset : function(){
		var temp = this.positionReset();
	},

	closeAd : function(){
		this.clearTimeObj(this.moveAdTimeId);
		this.moveAdTimeId=[];
		var oAd = BarObj.udAd;
		disOff(oAd);
		this.isViewAd=false;
		this.isViewRun=false;
		this.cancelAutoClose();
	},

	adUp : function(e){
		if(this.isViewAd) return;
		var flag = this.callOverSrc();		
		if(!flag) return;
		
		var o = this.BarObj.udAd;
		var hInfo = getHtmlInfo();
		var mInfo = getMouseInfo(e);
		var height = parseInt(this.BarObj.bg.style.height);
		o.style.top = parseInt(hInfo.offsetHeight - height) + "px";
		o.style.left = parseInt(mInfo.pageX - parseInt(parseInt(o.style.width)/2)) + "px";
		if(parseInt(o.style.left) < 0 ) o.style.left = "0px";
		else if(parseInt(o.style.left) >  hInfo.offsetWidth - parseInt(o.style.width)) o.style.left = hInfo.offsetWidth - parseInt(o.style.width) -20+ "px";			
		this.scrollUp(o, this.AdSet.scUpLen);
	},

	callOverSrc : function(){
		//this.overAdSrc;
		include_adscript(dBar.overAdSrc)
//		alert(GEID('div_overSrc').innerHTML)
		return true;
	},

	clearTimeObj : function(ids){
		for(var i = 0 , m = ids.length; i < m ; i++){
			try{
				window.clearTimeout(ids[i]);
			}catch(e){

			}
		}
	},

	outOver : function(){
		if(this.isViewRun){
			this.closeAd();
		}
	},

	isViewRunFunc : function(val){
		this.isViewRun=false;
	},


	scrollUp : function(obj, px){	
		var cPx = px;
		var pos = parseInt(obj.style.top.replace(/px/i, ''));
		var targetPos = pos - px;
		var movePos = 0;
		var maxMove = 5;
		var maxcount = 100;
		var time = 1500;
		var timeC = time;

		this.setOpacityObj(obj, 0);
		//alert(obj.id);
		disOn(obj);
		this.moveAdTimeId = [];
		this.isViewAd=true;
		this.isViewRun=true;
		this.movePatternArr = [];
		while(pos > targetPos && maxcount){
			var moveGap = parseInt((pos - targetPos) / 2);
			var gap = targetPos - pos;
			if(gap < 0) gap = gap * -1;
			var opaValue = 100 - parseInt((gap) / (px / 100)) ;
			opaValue = Math.round(opaValue)/10;
			timeC = parseInt(timeC / 1.15);
			if(moveGap > maxMove) moveGap = maxMove;
			pos = pos - moveGap - 1;
			//obj.innerHTML+=pos+";";
			this.movePatternArr[this.movePatternArr.length] = {
				objId : obj.id,
				pos   : pos,
				opaValue : opaValue
			}
/*	moveAdTimeId[moveAdTimeId.length] = setTimeout( new Function(
							"document.getElementById('" + obj.id + "').style.top = '"+pos+"';"
						+   "setOpacityObj(document.getElementById('" + obj.id + "'), "+opaValue+");"
						), time - timeC + 1);
*/
			maxcount--;
		}
		var timeGap = parseInt(time / this.movePatternArr.length);
		var mnsTime = 0;		
		for(var i = 0, m = this.movePatternArr.length; i < m ; i++){
			mnsTime += timeGap;
			if(i==0){
				setTimeout( new Function(
							this.varName + ".isViewRunFunc();"
						), this.AdSet.adUpTermTime);
			}			
			this.moveAdTimeId[this.moveAdTimeId.length] = setTimeout( new Function(
							"document.getElementById('" + this.movePatternArr[i].objId + "').style.top = '"+this.movePatternArr[i].pos+"px';"
						+   "setOpacityObj(document.getElementById('" + this.movePatternArr[i].objId + "'), "+this.movePatternArr[i].opaValue+"); "
						), mnsTime + this.AdSet.adUpTermTime);
		}

		//this.autoCloseId = setTimeout( new Function(this.varName+".closeAd();"), mnsTime + this.AdSet.adAutoCloseTime);
		this.runAutoClose(mnsTime + this.AdSet.adAutoCloseTime);		
	},

	runAutoClose : function(time) {
		if(!time){
			time = this.AdSet.adAutoCloseTime;
		}
		if(this.autoCloseId){
			this.cancelAutoClose();
		}
		this.autoCloseId = setTimeout( new Function(this.varName+".closeAd();"), time);
	},

	cancelAutoClose : function(){
			window.clearTimeout(this.autoCloseId);
			this.autoCloseId=null;
	},

	setOpacityObj : function(obj, value){
		obj.style.opacity = value/10;
		obj.style.filter = 'alpha(opacity=' + value*10 + ')';
	},

	barOn : function(){
		if(!dBar.overAdSrc) return;
		this._c();
		this._c2();
		var temp = this.positionReset();
		window.scrollTo(0,0);
		window.scrollbar="no";
		var b = document.body;
		b.scroll = 'no'; 
		b.style.overflow="hidden";
		b.style.padding="0";

		//var b = document.body;
		//b.appendChild(BarObj.bg);
		disOn(this.BarObj.bg);
		var func_pReset = new Function(this.varName+".pReset();");
		func_pReset.prototype.name = "pResetFunc";
//		alert(func_pReset);
		//this.addEvent(window,'scroll', func_pReset);
		this.addEvent(this.BarObj.bg,'mouseover',new Function("event", this.varName+".adUp(event);"));
		this.addEvent(this.BarObj.bg,'mouseout',new Function("event", this.varName+".outOver();"));
		this.addEvent(window,'resize', func_pReset);
		//this.BarObj.bg.onmouseover = new Function("event", this.varName+".adUp(event);");
		//this.BarObj.bg.onmouseout = new Function(this.varName+".outOver();");
//		alert("등록");
//		this.barOff();
//		alert("해제");
	},

	barOff : function(){
		this.clearTimeObj(this.moveAdTimeId);
		var func_pReset = new Function(this.varName+".pReset();");
		func_pReset.prototype.name = "pResetFunc";
		//this.removeEvent(window, 'scroll', func_pReset);
		this.removeEvent(window,'resize', func_pReset);
		document.body.removeChild(this.BarObj.bg);
		document.body.removeChild(this.BarObj.udAd);
		if(this.isLoadedBottomBlank) this.BBT.style.height="0px";
	},


	_c : function(){
		var o = document.createElement('div');
		o.id='dcBar_BG';
		//o.style.display="none";
		o.style.position="absolute";
		o.style.bottom = "-1px";
		o.style.left   = "0px";
		o.style.width  = "500px";
		o.style.height = this.AdSet.height + "px";
		o.style.color  = "white";
		o.style.zIndex = 5000;
		document.body.appendChild(o);
		this.BarObj.bg = GEID('dcBar_BG');
		this.BarObj.bg.appendChild(GEID('bar_ad'));
		disOn(GEID('bar_ad'));

	},

	_c2 : function (){
		var o = document.createElement('div');
		o.id='dcBar_Ad';
		o.style.display="none";
		o.style.position="absolute";
		//o.style.bottom = "0px";
		//o.style.left   = "0px";
		o.style.width  = "320px";
		o.style.height = "245px";
		o.style.color  = "white";
		o.style.zIndex = 4999;
		document.body.appendChild(o);
		this.BarObj.udAd = GEID('dcBar_Ad');	
		this.BarObj.udAd.appendChild(GEID('barScroll_ad'));
		this.addEvent(this.BarObj.udAd,'mouseover',new Function(this.varName+".cancelAutoClose();"));
		this.addEvent(this.BarObj.udAd,'mouseout',new Function(this.varName+".runAutoClose();"));
		disOn(GEID('barScroll_ad'));
	},
	addEvent : function( obj, type, fn ) {
	  if ( obj.attachEvent ) {
		obj['e'+type+fn] = fn;
		obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
		//alert(obj[type+fn]);
		obj.attachEvent( 'on'+type, obj[type+fn] );
	  } else if(obj.addEventListener){
		obj.addEventListener( type, fn, false );
	  }else{
		obj['e'+type+fn] = fn;
	  }
	},

	/*
	addEvent : function(object, type, handler, phase)
	{
		if(phase==null) phase=false;
		if (object.addEventListener) {
			object.addEventListener(type, handler, phase);
		} else if (object.attachEvent) {
			object.attachEvent(['on',type].join(''),handler);
		} else {
			object[['on',type].join('')] = handler;
		}
	},
*/
	removeEvent : function ( obj, type, fn ) {
	  if ( obj.detachEvent ) {
	    obj.detachEvent( 'on'+type, obj[type+fn] );
	    obj[type+fn] = null;
	  } else if(obj.removeEventListener) {
	    obj.removeEventListener( type, fn, false );
	  }else{
		obj['e'+type+fn] = null;
	  }
	}


}



		function setOpacity(value)
		{
			//alert("!");
			setOpacityObj(targetObj, value);
//			targetObj.style.opacity = value/10;
//			targetObj.style.filter = 'alpha(opacity=' + value*10 + ')';
		}

		function setOpacityObj(obj, value){
			obj.style.opacity = value/10;
			obj.style.filter = 'alpha(opacity=' + value*10 + ')';
		}

		function resetOpacityObj(obj){
			obj.style.opacity = "";
			obj.style.filter = "";

		}
