$(document).ready(function(){
	$global_search="";
	// left menu mouseover,out
	$("ul.gnb li ul li img").mouseover(function(){
		$(this).attr("src", $(this).attr("src").replace('off','over'));
		$(this).children('div').show();
	}).mouseout(function(){
		$(this).attr("src", $(this).attr("src").replace('over','off'));
		$(this).children('div').hide();
	});

	var menuPos;
	var pagePath = location.pathname;
	pagePath = $.trim(pagePath.match(/^.*\//));
	switch (pagePath) {
		case "/story/" : $("#gnb01").attr("src",$("#gnb01").attr("src").replace("off.gif","over.gif")); break;
		case "/menu/" : $("#gnb02").attr("src",$("#gnb02").attr("src").replace("off.gif","over.gif")); break;
		case "/store/" : $("#gnb03").attr("src",$("#gnb03").attr("src").replace("off.gif","over.gif")); break;
		case "/news/" : $("#gnb04").attr("src",$("#gnb04").attr("src").replace("off.gif","over.gif")); break;
		case "/center/" : $("#gnb05").attr("src",$("#gnb05").attr("src").replace("off.gif","over.gif")); break;
	}

	$("img")
	  .error(function(){
		$(this).hide();
	  });

	// datepicker
	$('#datepicker').datepicker({
		inline: true
	});
	/*
	$(".calendar").click(function(){
		$('#datepicker').show();
	});
	*/

	// form
	$('.jqform').jqTransform({imgPath:'/images/jqtransform/'});


	// light box
    $('#gallery a').lightBox({
	maxHeight: $(window).height()* 0.9,
	maxWidth: $(window).width() * 0.9
	});

    $('.imgbox').lightBox({
	maxHeight: $(window).height()* 0.9,
	maxWidth: $(window).width() * 0.9
	});


	var img_tag = $('td.tbl_content p img').parent().html();
	var img_src = $('td.tbl_content p img').attr('src');

	$('td.tbl_content p img').parent().html('<a href="'+img_src+'">'+img_tag+'</a>');

	$('td.tbl_content a').lightBox({
		maxHeight: $(window).height()* 0.9,
		maxWidth: $(window).width() * 0.9
	});

	// text input value delete
	$('.search_ip_txt').focusin(function(){
		ip_value = $(this).val();
		if($global_search==ip_value){
			$(this).val("");
		}
	});
	$('.search_ip_txt').focusout(function(){
		ip_value = $(this).val();
		if(ip_value==""){
			$(this).val($global_search);
		}
	});

	// quick menu rollover
	$('#quick ul li').mouseover(function(){
		var img = $(this).find("img");
		img.attr("src",img.attr("src").replace("off.gif","ov.gif"));
	}).mouseout(function(){
		var img = $(this).find("img");
		img.attr("src",img.attr("src").replace("ov.gif","off.gif"));
	});

	//img view rolling
	$(".thumbs li:first").addClass("on");
	var thumbArrow = $("<span class='arrow'></span>");
	thumbArrow.appendTo(".thumbs li.on");
	$(".thumbs a").click(function(){

		var largePath = $(this).attr("href");
		var largeAlt = $(this).attr("alt");
		$("#largeImg").attr({ src: largePath, alt: largeAlt });
		$("#largeAtag").attr({ href: largePath });

		//delete border
		$(".thumbs li").each(function(){
			$(this).removeClass("on");
			$("span").remove(".arrow");
		});

		//over border
		var list = $(this).parent();
		$(list).addClass("on");

		//insert arrow
		thumbArrow.appendTo(".on");
		return false;
	});


	// faq
	$(".tbFaq .subject a").click(function(){
		//$(this).next().attr("src","../images/content/bul_faq_close.gif");
		var answer = $(this).parent().parent().next('.line');
		answer.toggleClass('open');
		//if(answer.hasClass('open')) $(this).next().attr("src","../images/content/bul_faq_open.gif");
	});

	// special cake preview hide
	$(".pre .imgPre").click(function(){
		$(this).hide();
	});

	// special cake preview hide
	$(".preview .btn").click(function(){
		$(".preview").hide();
		$(".preview").parent().removeClass('height_control');
		$(document).scrollTop(0);
	});


});

window.onload = function(){
  initMoving(document.getElementById("quick"), 16, 16, 40);
}

// quick menu
function initMoving(target, position, topLimit, btmLimit) {
	if (!target)
		return false;

	var obj = target;
	obj.initTop = position;
	obj.topLimit = topLimit;
	obj.bottomLimit = Math.max(document.documentElement.scrollHeight, document.body.scrollHeight) - btmLimit - obj.offsetHeight;

	obj.style.position = "absolute";
	obj.top = obj.initTop;
	obj.left = obj.initLeft;

	if (typeof(window.pageYOffset) == "number") {	//WebKit
		obj.getTop = function() {
			return window.pageYOffset;
		}
	} else if (typeof(document.documentElement.scrollTop) == "number") {
		obj.getTop = function() {
			return Math.max(document.documentElement.scrollTop, document.body.scrollTop);
		}
	} else {
		obj.getTop = function() {
			return 0;
		}
	}

	if (self.innerHeight) {	//WebKit
		obj.getHeight = function() {
			return self.innerHeight;
		}
	} else if(document.documentElement.clientHeight) {
		obj.getHeight = function() {
			return document.documentElement.clientHeight;
		}
	} else {
		obj.getHeight = function() {
			return 500;
		}
	}

	obj.move = setInterval(function() {
		if (obj.initTop > 0) {
			pos = obj.getTop() + obj.initTop;
		} else {
			pos = obj.getTop() + obj.getHeight() + obj.initTop;
			//pos = obj.getTop() + obj.getHeight() / 2 - 15;
		}

		if (pos > obj.bottomLimit)
			pos = obj.bottomLimit;
		if (pos < obj.topLimit)
			pos = obj.topLimit;

		interval = obj.top - pos;
		obj.top = obj.top - interval / 3;
		obj.style.top = obj.top + "px";
	}, 30)
}

function validate(ele,alertstr,type){
	var retb = false;
	if(type == "select"){
		if($.trim(ele.val()).length<1){
			alert(alertstr+" 선택해 주세요.");
			ele.focus();
			retb = true;
		}
	}else if(type == "check"){
		var cnt = 0;
		$('input[id='+ele+']').each(function(){
			if(!$(this).is(':checked')){
				cnt++;
			}
		});

		if($('input[id='+ele+']').length==cnt) {
    		alert(alertstr+" 선택해 주세요.");
			$('input[id='+ele+']').focus();
			retb = true;
		}

	}else{
		if($.trim(ele.val()).length<1){
			alert(alertstr+" 입력해 주세요.");
			ele.focus();
			retb = true;
		}

	}
	return retb;
}

function validateLen(ele,alertstr,fir,thi){
	var retb = false;
	if($.trim(ele.val()).length>=fir && $.trim(ele.val()).length<=thi){
		retb = false;
	}else{
		alert(alertstr+' '+fir+"자이상 "+thi+" 이하로 입력해 주세요.");
		ele.focus();
		retb = true;
	}

	return retb;
}

function debugInput(){
	var txt = new Array();
	$(":input").each(function($key){
		txt[$key]="(name:"+$(this).attr('name')+")(id:"+$(this).attr('id')+")(value:"+$(this).attr('value')+")(type:"+$(this).attr('type')+")"+"<br />";
	});
	//alert(txt.sort());
	$('html>body').append("<div id='debugwindow' style='background-color:#fff;_position:absolute;width:700px;height:400px;margin:0px 0px 0px 100px;overflow:auto;'>"+txt.sort()+"</div>");
}
function sc_pop(param,target,size){
	var pop = window.open(param,target,size);
	if(pop == null){
		alert('팝업차단으로 서비스 이용이 불가능 합니다. 해제후 이용해 주십시오.');
	}else {
		if(!pop){
			pop.opener= self;
			pop.close();
		}
	}
}
