/* 배경 리프레시 방지 */
try{
	document.execCommand("BackgroundImageCache", false, true);
}
catch(err) {}

/* 팝업 중앙에 띄우기 */
var win= null;
function NewWindow(mypage,myname,w,h,scroll){
  var winl = (screen.width-w)/2;
  var wint = (screen.height-h)/2;
  var settings  ='height='+h+',';
      settings +='width='+w+',';
      settings +='top='+wint+',';
      settings +='left='+winl+',';
      settings +='scrollbars='+scroll+',';
      settings +='resizable=no';
  win=window.open(mypage,myname,settings);
  if(parseInt(navigator.appVersion) >= 4){win.window.focus();}
}

/* IMAGE_ROLLOVER 스크립트 */
function menuOn(imgEl)
{
	imgEl.src = imgEl.src.replace(".gif", "_ov.gif");
}

function menuOut(imgEl)
{
	imgEl.src = imgEl.src.replace("_ov.gif", ".gif");
}

/* 기본 설정값
className: "selectbox", //클래스명
animationSpeedDown: 0, //펼쳐질때 스피드
animationSpeedUp: 0, //접힐때 스피드
listboxMaxSize: 10, //스크롤바 없이 나오는 option수
replaceInvisible: false,
direction: "down", //방향 위로는("up")
lineHeight: 18 //option 하나당 line-height값
*/

/*
$(function(){
	$(".selectBox").selectbox({className:"selbox"});
});
*/
jQuery.fn.chgImg = function(on,options){
	if(this[0] == null) return false;
	var settings = {
		imgType : "gif",
		replaceTxt : "_on"
	}
	settings = $.extend(settings, options || {});
	settings.imgType = "." + settings.imgType;
	settings.replaceTxt = settings.replaceTxt+settings.imgType;
	
	var chkOn = $(this).attr("src").indexOf(settings.replaceTxt) != -1;
	if(on){
		if(chkOn){
			$(this).unbind("mouseout");
			return;
		}else{
			$(this).attr("src",$(this).attr("src").replace(settings.imgType,settings.replaceTxt));
		}
	}else if(chkOn){
		$(this).attr("src",$(this).attr("src").replace(settings.replaceTxt,settings.imgType));
	}
}

jQuery.fn.rolloverImg = function(options){
	var obj = $(this).attr("src") == null ? $("img",this) : this;
	$(obj).hover(function(){
		$(this).chgImg(1,options);
	},function(){
		$(this).chgImg(0,options);
	});
}

jQuery.fn.topNavi = function(){
	var o = $(this);
	var o_d1 = o.find(".depth1:lt(6)");
	var o_d1a = o.find(".depth1 a");
	var o_d2 = o.find(".depth2");
	var o_d2u = o.find(".depth2 ul:lt(6)");
	var o_d2a = o.find(".depth2 a");
	var idx,idxPrev,tgX,tgXDef,def=0,active=0,defOn=0;
	
	/* setting */
	function setAttr(){
		o_d1a.attr("idx",function(i){
			return i;
		});
		o_d2u
			.attr("idx",function(i){
				return i;
			});
	}
	
	function copyMenuImg(){
		o_d1.find("img").each(function(){
			$(this).clone().insertAfter(this);
			$(this).next().addClass("on")
				.attr("src",function(){
					return $(this).attr("src").split(".gif")[0]+"_on.gif"
				});
		});
	}
	
	/* menu animate */
	function menuOn(){
		if(idxPrev == idx && active){return false;}
		tgX = o_d2u.eq(idx).attr("tgX");
		tgXDef = o_d2u.eq(idx).attr("tgXDef");
		if(!def){
			o_d2.filter("[off!='true']").css({left:tgXDef});
		}
		o_d2.show().removeAttr("off").gx('pause').gx({left:tgX},600,"Quart:Out");
		o_d2u.eq(idx).css({display:"block"}).gx('pause').gx({opacity:1},600,"Quart:Out");
		//$("#aaa").text($("#aaa").text()+" "+o_d2u.eq(idx).css("opacity"));
		imgOn(o_d1a.eq(idx).find("img:eq(1)"));
			imgOff(o_d1a.eq(idxPrev).find("img:eq(1)"));
			menuOff();
	}
	
	function menuOff(){
			o_d2u.eq(idxPrev).hide();
			/*
		o_d2u.eq(idxPrev).gx('pause').gx({opacity:0},100,"",function(){
		})
		*/
	}
	
	var tm;
	function menuDef(){
		//alert(defOn);
		clearTimeout(tm);
		tm = setTimeout(function(){
			if(!defOn){
				if(active){return false;}
				o_d2.attr("off","true");
				activeMenu();
				o_d2u.eq(idxPrev).gx('pause').css({opacity:0});
					idxPrev = idx;
					//$(this).hide().removeAttr("off");
			}
		},1000);
	}
	
	/* img on off */
	function imgOn(el){
		el.show().css({opacity:0});
		el.gx("pause").gx({opacity:1},400);
	}
	
	function imgOff(el){
		el.gx("pause").gx({opacity:0},300,"",function(){el.hide();});
	}
	
	function imgOn2(el){
		el.addClass("over");
	}
	
	function imgOff2(el){
		if(!el.parents("li").eq(0).hasClass("on")){
			el.removeClass("over");
		}
	}
	
	/* default menu */
	function activeMenu(){
		idx = parseInt(o_d1.find("li.on a").attr("idx"));
		menuOn();
		imgOn2(o_d2.find("li.on a"));
	}
	
	/* first run */
	function init(){
		setAttr();
		copyMenuImg();
		o_d2u.css({left:0,opacity:0});
		activeMenu();
		def = o_d1.find("li.on").length;
	}
	
	/* event */
	o_d1a.mouseover(function(){
		idxPrev = idx;
		if($(this).parents("li.on").eq(0).length){
			defOn = 1;
		}else{
			defOn = 0;
		}
		active=1;
		idx = parseInt($(this).attr("idx"));
		menuOn();
	});
	
	o_d1a.mouseout(function(){
		if($(this).parents("li.on").eq(0).length){
			defOn = 1;
		}else{
			defOn = 0;
		}
		active=0;
		idxPrev = parseInt($(this).attr("idx"));
	});
	
	o_d2a.mouseover(function(){
		active=1;
		if(!$(this).parent().hasClass("on")){
			imgOn2($(this));
		}
	});
	
	o_d2a.mouseout(function(){
		active=0;
		if(!$(this).parent().hasClass("on")){
			imgOff2($(this));
		}
	});
	
	o_d2u.mouseover(function(){
		active=1;
	});
	
	o.mouseover(function(){
		if(o_d1.find("li.on img.on:visible").length){
			active=1;
		}
	});
	
	o.mouseout(function(){
		if(o_d1.find("li.on img.on:hidden").length){
			active=0;
		}
		menuDef();
	});
	
	init();
}

function dep1On(){
	var url = location.href;
	var href;
	var isOn=0;
	$("#topNavi .depth1 li").each(function(){
		href = $.trim($(this).find("a").attr("href"));
		href = "/"+href.split("/")[1]+"/";
		if(!href.length) return
		isOn = url.indexOf(href)+1;
		if(isOn){
			$(this).addClass("on");
			return false;
		}
	});
	dep2On(href);
	dep3On();
}
function dep2On(categ){
	var url = location.href;
	var href,page=[];
	var isOn=0;
	function classOn(el,i,i2){
		el.addClass("on");
		$("#topNavi .depth1 li").eq(i).removeClass("on").addClass("on");
		//leftmenu
		var tmp = 
		$("#left_menu h2").eq(i).show()
		 .next().show()
		 .find("h3").eq(i2).parent();
		
		alert(tmp.src);

		
		
		$("#left_menu h2").eq(i).show()
		 .next().show()
		 .find("h3").eq(i2).parent().addClass("on")
		 .next().find("ul").show()
		 .find("li").each(function(i3){
			href = $.trim($(this).find("a").attr("href"));
			isOn = url.indexOf(href)+1;
			if(isOn){
				$(this).addClass("on");
				return false;
			}
		 });
		 
	}
	$("#topNavi .depth2 ul").each(function(i){
		$(this).find("li").each(function(i2){
			href = $.trim($(this).find("a").attr("href"));
			isOn = href.indexOf(categ)+1;
			if(!isOn) return false;
			if(!href.length) return
			isOn = url.indexOf(href)+1;
			if(isOn){
				classOn($(this),i,i2);
				return false;
			}else if($(this).find("a").is("[page]")){
				page = $(this).find("a").attr("page").split(",");
				for (var j in page){
					isOn = url.indexOf(page[j])+1;
					if(isOn){
						classOn($(this),i,i2);
						return false;
					}
				}
			}
		});
	});
	$("#topNavi").topNavi();
}


/* 장학생 주요활동 */
$.fn.tabSelector = function(){
	var i = 0;
	var tabClass = ">.jqTab img";
	var contClass = ">.big";
	var objCont = $(this).find(contClass);

	function openTab(el,i){
		$(el).parents("div").eq(0).find(contClass).hide();
		$(objCont).eq(i).show();
	}
	$(this).find(tabClass).each(function(i){
		$(this).mouseover(function(){
			$("img",$(this).parents("ul").eq(0)).each(function(i){
				$(this).attr("src",$(this).attr("src").replace("_ov.jpg", ".jpg"));
			});
			$(this).attr("src",$(this).attr("src").replace(".jpg", "_ov.jpg"));
			
			$(this).parents("ul").eq(0).find("li").removeClass("on");
			$(this).parents("li").eq(0).addClass("on");
			openTab(this,i);
			return false;
		});
	});
	$(this).each(function(){
		$(this).find(tabClass).eq(0).mouseover();
	});
};

jQuery.fn.hoverToggle = function(el){
	$(this).each(function(i){
		var o = $(this);
		o.mouseover(function(){
			o.stop().animate({opacity:0},300,"easeOutSine",function(){$(this).hide();});
		});
		$(el).eq(i).mouseover(function(){
			$(this).removeAttr("on");
		});
		$(el).eq(i).mouseout(function(){
			$(this).attr("on","1");
			setTimeout(function(){
				if($(el).eq(i).is("[on]"))
					o.stop().show().animate({opacity:1},300,"easeInSine");
			},100);
		});
	});
}


var loc_info = null;
var left_info = null;
function displayMenu(loc_info) {
	var ar = loc_info.split(',');
	var top_m        = toInt(ar[0]);
	var left_idx     = toInt(ar[1], 1) - 1;
	var left_sub_idx = toInt(ar[2], 100) - 1;
	var o = $('#topNavi > .depth2 > .m0' + top_m + ' > li');
	//dep3On (o.eq(left_idx), top_m - 1, left_idx, left_sub_idx);
	classOn(o.eq(left_idx), top_m - 1, left_idx, left_sub_idx);

	function classOn(el,i,i2,i3){
		el.addClass("on");
		$("#topNavi .depth1 li").eq(i).removeClass("on").addClass("on");
		
		if (left_info != null) {
			i2 = left_info; // 상영관을 위한 임시
		}
		
		$("#left_menu h2").eq(i).show()
		 .next().show()
		 .find("h3").eq(i2).parent().addClass("on")
		 .next().find("ul").show()
		 .find("li").eq(i3).addClass("on");
		
		
		//상단네비 호출
		$("#topNavi").topNavi();
		$("#left_menu").topNavi();
	}

}

jQuery(function(){
	if (loc_info != null) {
		displayMenu(loc_info);
	} else {
		dep1On();
	}
	jQuery(".activity").tabSelector();
});

