var imgArray = ["imgTv", "imgRadio", "imgNews", "imgMovie", "imgMusic", "imgGame", "imgNeTv", "imgS", "imgSbsU", "imgQuick", "img8News", "imgUporter", "imgBbsPlus"];
for (var i=1; i<=58; i++) {
	imgArray.push("imgSub" + i);
}
function netv_popup() {
	window.open('http://netv.sbs.co.kr/box/box_index.jsp?loganal1=MENU_netv_box&loganal2=blank','netv_popup','width=420 height=250,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes');
}

var Rolling2 = Class.create();
Rolling2.prototype = {

	elementId: null,
	interval: 1,

	initialize: function(elementId, interval) {
		this.elementId = elementId;
		if (interval) {
			this.interval = interval;
		}

		new PeriodicalExecuter(this.roll.bind(this), this.interval);
	},

	roll: function() {
		if ($(this.elementId)) {
			var firstElement = $(this.elementId).childElements().first();
			firstElement.style.display="";
			firstElement.remove();
			firstElement.style.display="none";
			var secondElement = $(this.elementId).childElements().first();
			secondElement.style.display="";

			$(this.elementId).appendChild(firstElement);
		}
	}
}

var shouldRole = true;
Event.observe(window, "load", function() {
	$("layerTabBottom").observe("click", function() {
		MainTab.activate("layerTv");
	});
	$("layerTabBottom").setStyle({ cursor: "hand" });

	new PeriodicalExecuter(function(pe) {
		if (shouldRole) changeMainCon("right");
	}, 8);

	new Rolling2("mktlist23", 2);
	new Rolling2("mktlist26", 2);

	if ($("layerMainTitle")) {
		$("layerMainTitle").observe("mouseover", function() {
			//window.status = 'mouseover';
			shouldRole = false;
		});
		$("layerMainTitle").observe("mouseout", function() {
			//window.status = 'mouseout';
			shouldRole = true;
		});
	}

	attachMouseHandler();
});

function attachMouseHandler() {
	imgArray.each(function(imgName) {
		var imgLayer = $(imgName);
		if (imgLayer && !imgLayer.outSrc) {
			imgLayer.outSrc = imgLayer.src;
			imgLayer.overSrc = imgLayer.src.substring(0, imgLayer.src.lastIndexOf(".")) + "On" + imgLayer.src.substring(imgLayer.src.lastIndexOf("."));
			imgLayer.observe("mouseover", function() {
				this.src = this.overSrc;
			}.bind(imgLayer));
			imgLayer.observe("mouseout", function() {
				this.src = this.outSrc;
			}.bind(imgLayer));
		}
	});
}

function OpenMainContent(chLayer){
    var opt = {
    	// Use POST
    	method: 'get',
    	// Send this lovely data
    	onSuccess: function(t) {
			//alert("success");
     	},
		onComplete: function(t) {
			attachMouseHandler();
		},
    	// Handle 404
    	on404: function(t) {
        	alert('Error 404: location "' + t.statusText + '" was not found.');
    	},
    	// Handle other errors
    	onFailure: function(t) {
        	alert('Error ' + t.status + ' -- ' + t.statusText);
    	}
    }

	var layerPrefixUrl = null;
	switch (window.location.hostname) {
		case "admin.sbs.co.kr":
		case "testadmin.sbs.co.kr":
			layerPrefixUrl = "http://" + window.location.hostname + "/wsg/";
			break;
		default:
			layerPrefixUrl = "http://www.sbs.co.kr/newcms/admin/wsg/";
	}

	var layerHtml = null;
	switch (chLayer) {
		case "layerTv":
			layerHtml = "wsg_main_tv.html?loganal1=MENU_tap&loganal2=tv";
			break;
		case "layerRadio":
			layerHtml = "wsg_main_radio.html?loganal1=MENU_tap&loganal2=radio";
			break;
		case "layerNews":
			layerHtml = "wsg_main_news.html?loganal1=MENU_tap&loganal2=news";
			break;
		case "layerMovie":
			layerHtml = "wsg_main_movie.html?loganal1=MENU_tap&loganal2=movie";
			break;
		case "layerMusic":
			layerHtml = "wsg_main_music.html?loganal1=MENU_tap&loganal2=music";
			break;
		case "layerGame":
			layerHtml = "wsg_main_game.html?loganal1=MENU_tap&loganal2=game";
			break;
		case "layerNeTv":
			layerHtml = "wsg_main_netv.html?loganal1=MENU_tap&loganal2=netv";
			break;
		case "layerS":
			layerHtml = "wsg_main_s.html?loganal1=MENU_tap&loganal2=frees";
			break;
		case "layerSbsU":
			layerHtml = "wsg_main_u.html?loganal1=MENU_tap&loganal2=sbsu";
			break;
	}

	if (layerHtml) {
		//alert(layerHtml);
		new Ajax.Updater($(chLayer), layerPrefixUrl + layerHtml, opt);
	}

	/*
	new Ajax.Request(layerPrefixUrl + layerHtml, {
		method: 'get',
		onSuccess: function(t) {
			alert(t.responseText);
		}
	});

	alert("done");
	*/
}


var MainTab = {

	animating: false,

	// È­¸éÀÌ ÆîÃÄÁ³´ÂÁö ¿©ºÎ
	activated: false,

	activatedLayerName: "",

	tabHeight: 295,

	tabArray: ["layerTv", "layerRadio", "layerNews", "layerMovie", "layerMusic", "layerGame", "layerNeTv", "layerS", "layerSbsU"],

	activate: function(layerName) {
		if (this.animating) return;

		if (!this.activated) {
			var layer = $(layerName);
			var effects = [];

			var options = $H({
				duration: 0.5,
				scaleX: false,
				scaleY: true,
				scaleContent: false,
				scaleMode: {
					originalHeight: this.tabHeight
				},
				scaleFrom: 0,
				transition: Effect.Transitions.sinoidal,
				beforeStart: function() {
					this.animating = true;
				}.bind(this),
				afterFinish: function() {
					this.animating = false;
					this.activated = true;
					this.activatedLayerName = layerName;

					$("layerMainTitle").style.top = "100px";
					$("layerCenterBanner").style.top = "8px";

					if(document.getElementById("newBanner")) {
						if(document.getElementById("newBanner").style.display != "none") {
							$("layerCenterBanner").style.top = "213px";
						} else {
							$("layerCenterBanner").style.top = "8px";
						}
					} else {
						$("layerCenterBanner").style.top = "8px";
					}

					//	divProg.style.top = "92px";
					$("layerTabBottom").style.visibility = "hidden";
				}.bind(this)
			});

			effects.push(
				new Effect.Scale(layer, 100, options)
			);
		} else {
			// ¼±ÅÃµÈ layer°¡ ÇöÀç activatedµÈ layer¶ó¸é...
			if (this.activatedLayerName == layerName) {
				this.deactivate();
				return;
			} else {
				this.tabArray.each(function(tabLayerName) {
					var tabView = $(tabLayerName);
					if (tabView.id == layerName) {
						tabView.style.height = this.tabHeight + "px";
					} else {
						tabView.style.height = "0px";
					}
				}.bind(this));

				this.activatedLayerName = layerName;
			}
		}

		OpenMainContent(layerName);
	},

	deactivate: function() {
		//
	//	var layer2 = document.getElementById("divProg");
	//	layer2.style.top = "0px";
		//
		var layer = null;
		this.tabArray.each(function(tabLayerName) {
			var tabView = $(tabLayerName);
			if (tabView.style.height != "0px") {
				layer = tabView;
			}
		});
		if (layer) {
			var effects = [];

			var options = $H({
				duration: 0.5,
				scaleX: false,
				scaleY: true,
				scaleContent: false,
				scaleMode: {
					originalHeight: this.tabHeight
				},
				scaleFrom: 100,
				transition: Effect.Transitions.sinoidal,
				beforeStart: function() {
					this.animating = true;
				}.bind(this),
				afterFinish: function() {
					this.animating = false;
					this.activated = false;

					$("layerMainTitle").style.top = "8px";
					$("layerCenterBanner").style.top = "223px";

					$("layerTabBottom").style.visibility = "";
				}.bind(this)
			});

			effects.push(
				new Effect.Scale(layer, 0, options)
			);
		}
	}

}


	var caution = false;

	function wsg_getCookie(name) {
		var prefix = name + "=";
		var cookieStartIndex = document.cookie.indexOf(prefix);
		if (cookieStartIndex == -1) return null;
		var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
		if (cookieEndIndex == -1) cookieEndIndex = document.cookie.length;
		return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
	}

	function wsg_setCookie(name, value, expires, path, domain, secure) {
		var curCookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "")
		if (!caution || (name + "=" + escape(value)).length <= 4000)
		document.cookie = curCookie
		else
		if (confirm("Cookie exceeds 4KB and will be cut!"))
		document.cookie = curCookie
		return true;
	}

	// DIV ¹«ºù È¿°ú ¹× À§Ä¡Á¤º¸ ÄíÅ° ¼³Á¤

	function swapDIV(div1, div2)  {

		var d1 = $(div1);
		var d2 = $(div2);
		var b1 = $("btnDivDown");
		var b2 = $("btnDivUp");
		var b1src = b1.src;
		var b2src = b2.src;
		b1.src=b2src;
		b2.src=b1src;
		if(d1.offsetTop < d2.offsetTop) {
			var topY = parseFloat(d2.getHeight() || 0);
			var bottomY = 0-parseFloat(d1.getHeight() || 0);
			new Effect.Move (d1,{ x: 0, y: topY+9, mode: 'relative'});
			new Effect.Move (d2,{ x: 0, y: bottomY-10, mode: 'relative'});

			var exp = new Date();
			var oneYearFromNow = exp.getTime() + (365*24 * 60 * 60 * 1000);
			exp.setTime(oneYearFromNow);

	  		if(wsg_getCookie("SBS_DIV_ALIGN")=="12") cvalue="21";
	  		else cvalue="12";

	    		var sc = wsg_setCookie("SBS_DIV_ALIGN",cvalue,0,"/","www.sbs.co.kr",0);
	    		var sc = wsg_setCookie("SBS_DIV_ALIGN",cvalue,exp,"/","www.sbs.co.kr",0);
		}
		else {
			var topY = parseFloat(d1.getHeight() || 0);
			var bottomY = 0-parseFloat(d2.getHeight() || 0);
			new Effect.Move (d2,{ x: 0, y: topY+9, mode: 'relative'});
			new Effect.Move (d1,{ x: 0, y: bottomY-10, mode: 'relative'});

			var exp = new Date();
			var oneYearFromNow = exp.getTime() + (365*24 * 60 * 60 * 1000);
			exp.setTime(oneYearFromNow);

	  		if(wsg_getCookie("SBS_DIV_ALIGN")=="12") cvalue="21";
	  		else cvalue="12";

	    		var sc = wsg_setCookie("SBS_DIV_ALIGN",cvalue,0,"/","www.sbs.co.kr",0);
	    		var sc = wsg_setCookie("SBS_DIV_ALIGN",cvalue,exp,"/","www.sbs.co.kr",0);
		}
	}

	var mainConNum = 1;

	function changeMainCon(arrow)  {

		if (arrow == "left") {

			var d = $("layerMainTitle" + mainConNum);
			d.hide();
			if(eval($("layerMainTitle" + 3))){
				if (mainConNum == 1) {
					mainConNum = 3;
				} else {
					mainConNum = mainConNum - 1;
				}
			}else if(eval($("layerMainTitle" + 2))){
				if (mainConNum == 1) {
					mainConNum = 2;
				} else {
					mainConNum = mainConNum - 1;
				}
			}

			var d = $("layerMainTitle" + mainConNum);
			if(!eval(d)){		//1,3¸¸ Á¸ÀçÇÒ °æ¿ì 3¿¡¼­ 1·Î ³Ñ¾î°¡±â
				mainConNum=mainConNum-1;
			}

			d = $("layerMainTitle" + mainConNum);
			d.show();

		} else {

			var d = $("layerMainTitle" + mainConNum);
			d.hide();
			if(eval($("layerMainTitle" + 3))){
				if(mainConNum == 3) {
					mainConNum = 1;
				} else {
					mainConNum = mainConNum + 1;
				}
			}else if(eval($("layerMainTitle" + 2))){
				if(mainConNum == 2) {
					mainConNum = 1;
				} else {
					mainConNum = mainConNum + 1;
				}
			}

			var d = $("layerMainTitle" + mainConNum);
			if(!eval(d)){		//2,3¸¸ Á¸ÀçÇÒ °æ¿ì 3¿¡¼­ 2·Î ³Ñ¾î°¡±â
				mainConNum=mainConNum+1;
			}

			d = $("layerMainTitle" + mainConNum);
			d.show();

		}
	}

		///////// °Ë»öÆû ¾×¼Ç ½ºÅ©¸³Æ® ½ÃÀÛ
	function onEnter2(form)
	{
		if (event.keyCode==13)
		{
			if (!form.keyword.value) //°ø¹é½Ã ¹«¹ÝÀÀ Ã³¸® À§ÇÔ
			{
				form.action = "http://search.sbs.co.kr/search.jsp";
	     		form.target = "_top";
	    		form.searchText = "±è¿¬¾Æ";
	     		form.submit();
			} else {
				onSearch2(form);
			}

		}
	}

//	function onSearch2(form)
//	{
//		var sur01 = 0;
////
////		if (!form.keyword.value) //°ø¹é½Ã ¹«¹ÝÀÀ
////		return false;
////
//		if (!form.keyword.value) //°ø¹é½Ã ¹«¹ÝÀÀ
//		{
//			form.action = "http://find.sbs.co.kr?loganal1=MENU_search&loganal2=&q=";
//			form.target = "_top";
//			form.submit();
//		}
//
//		var searchText = form.keyword.value;
//
//		form.query.value = form.keyword.value;
//		form.q.value = form.keyword.value;
//
//		for(i =0 ;i <form.rdoChk.length;i++)
//		{
//			if(form.rdoChk[i].checked == true)
//			{
//				sur01 = form.rdoChk[i].value;
//				if (sur01 == "0")
//				{
//					for (var i=0; i<search_text.length; i++) {
//						if (search_text[i] == searchText) {
//							form.q.value = search_keyword[i];
////							if (form.q.value == "") {
////								form.q.value = form.query.value;
////							}
//						}
//					}
//				     form.action = "http://find.sbs.co.kr/index.jsp?loganal1=MENU_search&loganal2="+form.q.value+"&q="+form.q.value;
//				     form.target = "_top";
//				     form.submit();
//				} else if (sur01 == "1"){
//					for (var i=0; i<search_text.length; i++) {
//						if (search_text[i] == searchText) {
//							form.query.value = search_keyword[i];
//						}
//					}
//				     form.action = "http://search.sbs.co.kr/search.jsp?loganal1=MENU_search&loganal2="+form.query.value+"&query="+form.query.value;
//				     form.target = "_top";
//				     form.submit();
//			    }
//			}
//		}
//	}

function onSearch2(form)
	{
		if (!form.keyword.value) //°ø¹é½Ã ¹«¹ÝÀÀ
	    	form.searchText = "±è¿¬¾Æ";
		return false;

	     form.query.value = form.keyword.value;
	     form.loganal2.value = form.keyword.value;
	     form.action = "http://search.sbs.co.kr/search.jsp?loganal1=MENU_search&loganal2="+form.query.value+"&query="+form.query.value;
	     form.target = "_top";
	     form.submit();
	}

function fun_pop3(){
	document.location.href="http://tv.sbs.co.kr/tokyo";
	window.open('http://spv.sbs.co.kr/index.jsp?code=10000001490','event_br','scrollbars=yes,status=no,width=620,height=820,top=2,left=7');
}

