var mainTime = 0;
var mainTimer = null;
function setFreeChalHome(url){
	fcMainUtil.ajax("GET", function(){}, url, true);
}
function mTimer(){
	mainTime++;
	newsCtl.autoNews();
	if(mainTime >= 30){
		top.location.href = g_mainsvr;
	}else{
		mainTimer = window.clearTimeout(mainTimer);
		mainTimer = window.setTimeout(function(){mTimer()}, 18000);
	}
}
var ScriptLoader = {
	require: function(libraryName) {
		var s = document.createElement("script");
		s.type="text/javascript";
		s.src=libraryName;
		$('fchead').appendChild(s);
	},
	load: function(arr_script) {
		if((typeof arr_script=='object') && arr_script.length > 0){
			arr_script.each( function(sc) {
				ScriptLoader.require(sc)
			});
		}
	}
}
var fcMainUtil = {
	NT_SERVICE : "FCM",
	NT_SECTION : "000",
	objAjax: {},
	onFunction: [],
	observe: function(func){
		this.onFunction[this.onFunction.length] = func;
	},
	onLoad: function(){
		this.onFunction.each(function(onFunc,idx){
			onFunc();
		});
	},
	URLEncode: function(clearString){
		var output = '';
		var x = 0;
		clearString = clearString.toString();
		var regex = /(^[a-zA-Z0-9]*)/;
		while (x < clearString.length) {
			var match = regex.exec(clearString.charAt(x));
			if (match != null && match.length > 1 && match[1] != '') {
				output += match[1];
				x += match[1 ].length;
			} else {
				if (clearString.charAt(x) == ' '){
					output += '+';
				}else if(clearString.charCodeAt(x)>127){
					output += escape(clearString.charAt(x));
				}else {
					var charCode = clearString.charCodeAt(x);
					var hexVal = charCode.toString(16);
					output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
				}
				x++;
			}
		}
		return output;
	},
	ReplaceUrlForMain20: function(strUrl, strSection, strNetCode){
		var strCode;
		if(strNetCode){
		    if(/\?/.test(strUrl)){
		        strUrl = strUrl + "&";
		    }else{
		        strUrl = strUrl + "?";
		    }
		    strCode = strNetCode.length > 10  ? strNetCode : this.NT_SERVICE + this.NT_SECTION + "_" + strNetCode;
		   	strUrl = strUrl + "LogSection=" + this.URLEncode(strCode);
		}
		return g_gatesvr + "/common/FcCommonLogGate.asp?gbn=N&RedirUrl=" + this.URLEncode(strUrl) + "&section=" +this.URLEncode(strSection);
	},
	onTab: function(obj, total, idx, func) {
		for(var i=0;i<total;i++){
			$(obj+"["+i+"]").style.display = "none";
		}
		$(obj+"["+idx+"]").style.display = "block";

		if(func) func();
	},
	onClass: function(obj, total, idx, func){
		for(var i=0;i<total;i++){
			$(obj+"["+i+"]").className = $(obj+"["+i+"]").className.replace("_on", "");
		}
		$(obj+"["+idx+"]").className = $(obj+"["+idx+"]").className + "_on";
	},
	toggle: function(obj, total) {
		var visibleIdx;
		for(var i=0;i<total;i++){
			if($(obj+"["+i+"]").visible()) visibleIdx = i;
			$(obj+"["+i+"]").hide();
		}
		if(++visibleIdx>=total) visibleIdx=0;
		$(obj+"["+visibleIdx+"]").show();
	},
	ajax: function(method, callback, url, data, async){
		try{
			this.objAjax = {};
			this.objAjax = new Ajax.Request(url, {
				method: method,
				asynchronous: async ? async : true,
				contentType: 'application/x-www-form-urlencoded',
				encoding: 'euc-kr',
				parameters: data ? data : "",
				onSuccess: function(xmlHttp) {
					callback(xmlHttp);
        		}
        	});
		}catch(e){}
	},
	next: function(){
		this.Z();
	},
	Z: function(){
		return void(0);
	},
	imageAbort: function(event){
		Event.element(event).src = Event.element(event).src;
	},
	flashTag: function(url, w, h, id, wmode, fvar, bg){
		var bgcolor = bg ? bg : "#000000";
		var flashStr = "<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='" + location.protocol + "//fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' width='"+w+"' height='"+h+"' id='"+id+"' name='"+ id +"'align='middle'>"+
		             "<param name='allowScriptAccess' value='always' />"+
		             "<param name='movie' value='"+url+"' />"+
		             "<param name='wmode' value='"+wmode+"' />"+
		             "<param name='FlashVars' value='"+fvar+"' />"+
		             "<param name='menu' value='false' />"+
		             "<param name='quality' value='high' />"+
		             "<param name='bgcolor' value='" + bgcolor +"' />"+
		             "<param name='base' value='.'>"+
		             "<embed src='"+url+"' wmode='"+wmode+"' menu='false' quality='high' width='"+w+"' height='"+h+"' id='"+id+"' name='"+id+"' FlashVars='"+fvar+"' align='middle' allowScriptAccess='always' type='application/x-shockwave-flash' pluginspage='" + location.protocol + "//www.macromedia.com/go/getflashplayer' />"+
		             "</object>";
		return flashStr;
	},
	flash: function(url, w, h, id, win){
		var flashStr = this.flashTag(url, w, h, id, win)
		document.write(flashStr);
	},
	visual: function(vId){
		if((/(embed)/igm.test($(vId).innerHTML) || /object/igm.test($(vId).innerHTML)) && /.swf/igm.test($(vId).innerHTML)){
			var flashTagText = /(embed)/igm.test($(vId).innerHTML) ? "embed" : "object";
			var selectNodeText = "#"+vId+" "+flashTagText;
			$$(selectNodeText).each(function(flashObj,idx){
				var movieUrl = /object/img.test($(vId).innerHTML) ? flashObj.getAttribute("data") : flashObj.getAttribute("src");
				var movieWidth = flashObj.getAttribute("width");
				var movieHeight = flashObj.getAttribute("height");
				$(vId).innerHTML = this.flashTag(movieUrl, movieWidth, movieHeight, vId+"_flash", "transparent");
			}.bind(this));
		}
	}
}
var fcMainRank = {
	NET_NAME : "LRNK",
	objRank: {},
	rankDivName: "rnkLink",
	Rank: ["TOTAL", "NEWS", "ENT", "HUMOR", "GAME", "PEPOLE"],
	indexRank: 0,
	timeoutId: null,
	timeInterval: 7000,
	contentUrl: "/common/FcMain/80/FmSvcRankList.asp",
	qRankingUrl: "/common/FcMain/80/FmSvcQRanking.asp",
	initialize: function(){
		this.timeoutId = window.setTimeout(function(){this.autoNext()}.bind(this), this.timeInterval);
	},
	getBlankData: function(){
		var url		= this.blankUrl;
		fcMainUtil.ajax("get", function(){}, url, "");
	},
	getRankData: function(rank, auto){
		if(!this.objRank[rank]){
			var url		= rank != "PEPOLE" ? this.contentUrl : this.qRankingUrl;
			fcMainUtil.ajax("get", function(xmlHttp){this.callBack(rank, xmlHttp)}.bind(this), url, "rank=" + rank);
		}else{
			//if(!auto) this.getBlankData();
			this.rankSelector(rank);
		}
	},
	callBack: function(rank, xmlHttp){
		this.objRank[rank] = xmlHttp.responseText.isJSON() ? xmlHttp.responseText.evalJSON() : xmlHttp.responseText;
		this.rankSelector(rank);
	},
	rankSelector: function(rank){
		if(rank != "PEPOLE"){
			if(!$('rnkList').visible()){
				$('rnkList').show();
				$('pepoleList').hide();
			}
			this.display(this.objRank[rank]);
		}else{
			$('rnkLink').href = fcMainUtil.ReplaceUrlForMain20(g_qsvr + "/Base/Base30/QbTodayPeopleList.asp?sid=20", "QTVRANKING>TEXT", this.NET_NAME + "01_006");
			$('rnkLink').target = "_self";
			$('rnkTitle').innerHTML = "ÇÇÇÃ ·©Å·";
			$('rnkList').hide();
			$('pepoleList').innerHTML = this.objRank[rank];
			$('pepoleList').show();
		}
	},
	display: function(objRank){
		if(objRank){
			$('rnkLink').href = fcMainUtil.ReplaceUrlForMain20(objRank.LINK, objRank.TAG, objRank.NET);
			$('rnkLink').target = objRank.TARGET;
			$('rnkTitle').innerHTML = objRank.TITLE;
			for(var i=0;i<10;i++){
				var target = $(this.rankDivName+'['+i+'][0]');

				target.innerHTML= objRank.DATA[i][0];
				target.title	= objRank.DATA[i][0];
				target.target	= objRank.TARGET;
				target.href		= fcMainUtil.ReplaceUrlForMain20(objRank.CONTENTLINK+objRank.DATA[i][1], objRank.TAG, objRank.CNET);
			}
		}
	},
	next: function(auto){
		this.indexRank = (this.indexRank+1<this.Rank.length) ? ++this.indexRank:this.indexRank = 0;
		this.getRankData(this.Rank[this.indexRank], auto);
	},
	previous: function(auto){
		this.indexRank = (this.indexRank-1>=0) ? --this.indexRank:this.indexRank = this.Rank.length-1;
		this.getRankData(this.Rank[this.indexRank], auto);
	},
	autoNext: function(){
		this.next(true);
		if(this.timeoutId) this.timeoutId = window.clearTimeout(this.timeoutId);
		this.timeoutId = window.setTimeout(function(){this.autoNext()}.bind(this), this.timeInterval);
	},
	stop: function(){
		if(this.timeoutId)
			this.timeoutId = window.clearTimeout(this.timeoutId);
	},
	start: function(){
		if(!this.timeoutId)
			this.timeoutId = window.setTimeout(function(){this.autoNext()}.bind(this), this.timeInterval);
	}
}
var fcMainHotMovie = {
	totalCnt : 17,
	queryUrl : "/common/FcMain/80/FmSvcHotMovieView.asp",
	nIndex: 0,
	sTime: null,
	runTime: 0,
	blnEffect: true,
	timmer: null,
	btnEvent: {},
	query: function(index){
		this.nIndex = index;
		fcMainUtil.ajax("get", function(xmlHttp){this.callback(xmlHttp)}.bind(this), this.queryUrl, "seqno="+index+"&LogSection="+fcMainUtil.URLEncode('FCM000_CQTH02_002'));
	},
	callback: function(xmlHttp){
		var serverData = xmlHttp.responseText;
		this.view(serverData);
	},
	view: function(data){
		if($('hotview'+this.nIndex)) $('hotview'+this.nIndex).hide();
		$('btn_hot_left').onclick = function(event){this.prev()}.bind(this);
		$('btn_hot_right').onclick = function(event){this.next()}.bind(this);
		$('MovieHotView').innerHTML = data;
		$('MovieHotList').hide();
		$('hot_title').addClassName('tit_ul');
		$('MovieHotView').show();
		$('btn_close').show();
		if(this.blnEffect){
			var win = $('MovieHotView');
			win.setOpacity(0);
			new Effect.Opacity(win, {
									duration:0.5,
									from:0,
									to:1,
									beforeStart:function(){
										this.sTime = new Date();
									}.bind(this),
									afterFinish:function(){
										this.runTime = (new Date()).getTime() - this.sTime.getTime();
										this.blnEffect = this.runTime > 600 ? false : true;
									}.bind(this)
								});
		}
	},
	close: function(){
		if(this.blnEffect){
			var win = $('MovieHotView');
			new Effect.Opacity(win, {
									duration:0.2,
									from:1,
									to:0,
									afterFinish:function(){
										$('hot_title').removeClassName('tit_ul');
										$('MovieHotList').show();
										$('MovieHotView').hide();
										$('btn_close').hide();
									}.bind(this)
								});
		}else{
			$('hot_title').removeClassName('tit_ul');
			$('MovieHotList').show();
			$('MovieHotView').hide();
			$('btn_close').hide();
		}
		$('btn_hot_left').onclick = function(event){ hotCtl.next(-1); };
		$('btn_hot_right').onclick = function(event){ hotCtl.next(1); };
	},

	prev: function(){
		if(this.nIndex-1 >= 0){
			--this.nIndex;
		}else{
			this.nIndex = this.totalCnt;
		}
		this.query(this.nIndex);
	},
	next: function(){
		if(this.nIndex+1 <= this.totalCnt){
			++this.nIndex;
		}else{
			this.nIndex = 0;
		}
		this.query(this.nIndex);
	},
	over: function(tIndex){
		$('hotview'+tIndex).show();
		this.timmer = window.setTimeout(function(){this.query(tIndex);}.bind(this), 1200);
	},
	out: function(tIndex){
		if(this.timmer) this.timmer = window.clearTimeout(this.timmer);
		$('hotview'+tIndex).hide();
	}
}
var fcMainCtl = Class.create();
fcMainCtl.prototype = {
	oData : {},
	url: null,
	nPage: 0,
	tPage: 0,
	tab: null,
	tDom: null,
	initialize: function(url, tdom, npage, tpage, blnView){
		this.oData = {};
		this.url = url;
		this.nPage = npage;
		this.tPage = tpage;
		this.tDom = tdom;
		if(blnView) this.randNext();
	},
	getData: function(){
		if(!this.oData[this.nPage.toString()]){
			fcMainUtil.ajax("GET", function(xml){this.setData(xml);}.bind(this), this.url + "?page=" + this.nPage);
		}else{
			this.display();
		}
	},
	setData: function(res){
		if(!this.oData[this.nPage.toString()])
			this.oData[this.nPage.toString()] = res.responseText;
		this.display();
	},
	display: function(){
		document.getElementById(this.tDom).innerHTML = this.oData[this.nPage.toString()];
	},
	next: function(step){
		this.nPage +=  step;
		if(this.nPage > this.tPage) this.nPage = 1;
		if(this.nPage < 1) this.nPage = this.tPage;
		this.getData();
	},
	randNext: function(){
		this.nPage = parseInt(Math.random() * this.tPage) + 1;
		this.getData();
	}
}
var fcMainShopCtl = Class.create(fcMainCtl,{
	getData: function(){
		if(!this.oData[this.tab.toString() + this.nPage.toString()]){
			fcMainUtil.ajax("GET", function(xml){this.setData(xml);}.bind(this), this.url + "?page=" + this.nPage + "&tab="+this.tab);
		}else{
			this.display();
		}
	},
	setData: function(res){
		if(!this.oData[this.tab.toString() + this.nPage.toString()])
			this.oData[this.tab.toString() + this.nPage.toString()] = res.responseText;
		this.display();
	},
	display: function(){
		document.getElementById(this.tDom).innerHTML = this.oData[this.tab.toString() + this.nPage.toString()];
	},
	next: function(step, tab){
		this.nPage +=  step;
		this.tab = tab;
		if(this.nPage > this.tPage[tab-1]) this.nPage = 1;
		if(this.nPage < 1) this.nPage = this.tPage[tab-1];
		this.getData();
	},
	randNext: function(){
		this.nPage = parseInt(Math.random() * this.tPage[0]) + 1;
		this.tab = 1;
		this.getData();
	},
	tabNext: function(tab){
		if(this.tab != tab){
			this.tab = tab;
			var iPage = this.nPage=0 ? 1 : this.nPage;
			if(iPage > this.tPage[tab-1]) iPage = 1;
			if(iPage < 1) iPage = this.tPage[tab-1];
			this.next(iPage, tab);
		}
	}
});
var fcMainNewsCtl = Class.create(fcMainCtl,{
	blnStop: false,
	nImgPage: 0,
	tImgPage: 0,
    TagName : "NEWS",
    NET_NAME: "CNWS",
	initNews: function(nimgpage, timgpage, blnView){
		this.oData = null;
		this.blnStop = false;
		this.nImgPage = nimgpage;
		this.tImgPage = timgpage;
		if(blnView) this.randNext();
	},
	getData: function(){
		if(!this.oData){
			fcMainUtil.ajax("GET", function(xml){this.setData(xml);}.bind(this), this.url);
		}else{
			this.display();
		}
	},
	setData: function(res){
		if(res.responseText.isJSON()){
			this.oData = res.responseText.evalJSON();
			this.display();
		}
	},
	display: function(){
		if(this.oData){
			for(var i=0;i<this.oData.txt[this.nPage].htxt.length;i++){
				var title = this.oData.txt[this.nPage].htxt[i].title;
				$("hlink."+i).href = fcMainUtil.ReplaceUrlForMain20(this.oData.txt[this.nPage].htxt[i].link,this.TagName+">HALFTEXT",this.NET_NAME+"03_001");
				if(this.oData.txt[this.nPage].htxt[i].bold=="Y"){
					title = "<strong>"+title+"</strong>";
				}
				$("hlink."+i).innerHTML = title;
				if((i == 0 || i == 2) && this.oData.txt[this.nPage].htxt[i+1].title != ""){
					$("news_bar."+i).style.display = '';
				}else if(i == 0 || i == 2){
					$("news_bar."+i).style.display = 'none';
				}
			}
			for(var i=0;i<this.oData.txt[this.nPage].ltxt.length;i++){
				var title = this.oData.txt[this.nPage].ltxt[i].title;
				$("llink."+i).href = fcMainUtil.ReplaceUrlForMain20(this.oData.txt[this.nPage].ltxt[i].link,this.TagName+">LONGTEXT",this.NET_NAME+ (i<3 ? "03_003" : "03_004"));
				if(this.oData.txt[this.nPage].ltxt[i].bold=="Y"){
					title = "<strong>"+title+"</strong>";
				}
				$("llink."+i).innerHTML = title;
			}

			$("ilink_img").href  = fcMainUtil.ReplaceUrlForMain20(this.oData.img[this.nImgPage].link,this.TagName+">IMG",this.NET_NAME+"03_002");
			$("ilink_img").alt  = this.oData.img[this.nImgPage].title;
			$("isrc").src   	= this.oData.img[this.nImgPage].image;

			if(this.oData.img[this.nImgPage].bold == "Y"){
				$("iplay_").href	= fcMainUtil.ReplaceUrlForMain20(this.oData.img[this.nImgPage].link,this.TagName+">IMG",this.NET_NAME+"03_002");
				$("iplay_").title   = this.oData.img[this.nImgPage].title;
				$("iplay_").style.display = 'block';
				$("iplay").style.display = 'block';
			}else{
				$("iplay_").style.display = 'none';
				$("iplay").style.display = 'none';
			}
			$("ilink_txt").href			= fcMainUtil.ReplaceUrlForMain20(this.oData.img[this.nImgPage].link,this.TagName+">IMG",this.NET_NAME+"03_002");
			$("ilink_txt").innerHTML	= this.oData.img[this.nImgPage].title;
			$("ilink_img").innerHTML 	= $("ilink_img").innerHTML;
		}
	},
	next: function(step){
		this.nPage +=  step;
		this.nImgPage += step;
		if(this.nPage >= this.tPage) this.nPage = 0;
		if(this.nPage < 0) this.nPage = this.tPage-1;
		if(this.nImgPage >= this.tImgPage) this.nImgPage = 0;
		if(this.nImgPage < 0) this.nImgPage = this.tImgPage-1;
		this.blnStop = true;
		this.getData();
	},
	randNext: function(){
		this.nPage =  parseInt(Math.random() * this.tPage) + 1;
		this.nImgPage = parseInt(Math.random() * this.tImgPage) + 1;
		this.blnStop = true;
		this.getData();
	},
	autoNews: function(){
		if(!this.blnStop){
			this.next(1);
		}else{
			this.blnStop = false;
		}
	}
});
var fcMainUccCtl = Class.create(fcMainCtl,{
	setData: function(res){
		if(res.responseText.isJSON()){
			if(!this.oData[this.nPage.toString()]){
				this.oData[this.nPage.toString()] = res.responseText.evalJSON();
			}
			this.display();
		}
	},
	display: function(){
		this.oDataE = this.oData[this.nPage.toString()];
		if(this.oDataE.CODE == "image"){
			$('adImg_Image').src		=  this.oDataE.IMAGE;
			$('adImg_Image').alt		=  this.oDataE.TITLE;
			$('adImg_link').href		=  this.oDataE.LINK;
			$('adImg_link').target		=  this.oDataE.TARGET;
			$('adImg_link').title		=  this.oDataE.TITLE;
			$('adImg_textLink').href	=  this.oDataE.LINK;
			$('adImg_textLink').target	=  this.oDataE.TARGET;
			$('adImg_textLink').title	=  this.oDataE.TITLE;
			$('adImg_textLink').innerHTML= this.oDataE.TITLE;
			$('adTV').hide();
		   	$('adImg').show();
		}else{
			$('adTV_link').href		= this.oDataE.LINK
			$('adTV_link').target	= this.oDataE.TARGET;
			$('adTV_link').title	= this.oDataE.TITLE;
			$('adTV_link').innerHTML= this.oDataE.TITLE;
			if (window.ActiveXObject)	$("FLVPlayer").movie		= this.oDataE.MOVIE;
			else						$("player").innerHTML	=  '<embed src="'+this.oDataE.MOVIE+'" bgcolor="#000000" allowScriptAccess="always" allowFullScreen="true" wmode="opaque" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="198" height="148"></embed>';
		   	$('adImg').hide();
			$('adTV').show();
		}
	}
});