var _mouse=true;
var _rolling=true;
var _notice=true;
var _event=true;
var _idx=1;
var _block=5;

$(function()
{
	// focus dotted line
	/*
	$("#baseWrap").mouseover(function() {
		if (_mouse) {
			$("a").focus(function() {
				$(this).blur();
			});
			_mouse=false;
		}
	});
	*/

	// insert flash movie
	var _main=new SWFObject("/main/swf/main.swf","main_flash","454","199","8","#FFFFFF");
	_main.addParam("quality","high");
	_main.addParam("wmode","transparent");
	_main.write("id_flash_area");

	// link description
	linkDescription();

	// form description
	formDescription();

	//createPopupLayer();
	//createPopupLayer();

    // recommend ranking rolling
	setInterval('recommendRolling()', 3000);	
	$("li.li_rec_rank").mouseover(function() {
		_rolling=false;
	});	
	$("li.li_rec_rank").mouseout(function() {
		_rolling=true;
	});

    // top notice rolling
	setInterval('noticeRolling()', 6000);	
	$("li.li_notice_top").mouseover(function() {
		_notice=false;
	});	
	$("li.li_notice_top").mouseout(function() {
		_notice=true;
	});

    // top evnet rolling
	setInterval('eventRolling()', 6000);	
	$("li.li_event").mouseover(function() {
		_event=false;
	});	
	$("li.li_event").mouseout(function() {
		_event=true;
	});
});

// recommend ranking rolling
function recommendRolling()
{
	if (_rolling) {
		$("ul#ul_rank_area").animate({marginTop:"-26px"},500,null,function() {
			$("ul#ul_rank_area").css("marginTop","0px").append($("ul#ul_rank_area").find("> li:first"));
		});
	}
}

// top notice rolling
function noticeRolling()
{
	if (_notice) {
		$("ul#ul_notice_top").animate({marginTop:"-26px"},500,null,function() {
			$("ul#ul_notice_top").css("marginTop","0px").append($("ul#ul_notice_top").find("> li:first"));
		});
	}
}

// top notice rolling
function eventRolling()
{
	if (_event) {
		$("ul#ul_event").animate({marginLeft:"-209px"},1500,null,function() {
			$("ul#ul_event").css("marginLeft","0px").append($("ul#ul_event").find("> li:first"));
		});
        _idx++;
	    if (_idx>_block){
            _idx=1;
        }
        $(".view_number").css({'background':'url(/images/left_event/number_'+_idx+'.gif) no-repeat'});
    }
}

// link description
function linkDescription()
{

    // view event 
    $(".btn_event").click(function() {
        _event=false;
        _no=$(this).attr('id').split('_');
        if(_no[2]!=_idx) {
            if (Number(_no[2])>Number(_idx)){
                _break=Number(_no[2])-Number(_idx);
                _margin=209*Number(_break);
                $("ul#ul_event").animate({marginLeft:"-"+_margin+"px"},1500,null,function() {
                    for(i=1;i<=_break;i++) {
                        $("ul#ul_event").css("marginLeft","0px").append($("ul#ul_event").find("> li:first"));
                    }
                });
                _idx=_no[2];
                $(".view_number").css({'background':'url(/images/left_event/number_'+_idx+'.gif) no-repeat'});
            } else {
                _break=_block-(Number(_idx)-Number(_no[2]));
                _margin=209*Number(_break);
                $("ul#ul_event").animate({marginLeft:"-"+_margin+"px"},1500,null,function() {
                    for(i=1;i<=_break;i++) {
                        $("ul#ul_event").css("marginLeft","0px").append($("ul#ul_event").find("> li:first"));
                    }
                });
                _idx=_no[2];
                $(".view_number").css({'background':'url(/images/left_event/number_'+_idx+'.gif) no-repeat'});
            }
        }
        _event=false;
    });
    $(".btn_event").mouseover(function() {
		_event=false;
	});
    $(".btn_event").mouseout(function() {
		_event=true;
	});

	// write/edit acc link
	$("a.link_find_mem").click(function() {
		find_pass();
		return false;
	});

	// view ranking link
	$("#link_ranking_view").click(function() {
		openLink('/recommend/ranking.php','view_ranking',650,690);
		return false;
	});

	// view ranking link
	$("#link_view_ranking").click(function() {
		openLink('/recommend/ranking.php','view_ranking',650,690);
		return false;
	});

	// view ranking link
	$("#view_ranking").click(function() {
		openLink('/recommend/ranking.php','view_ranking',650,690);
		return false;
	});

	// buy textbook
	$("#link_buy_textbook").click(function() {
		openLink('/textbook/write.php','buy_textbook',600,650);
		return false;
	});

	// buy textbook - not login
	$("#link_buy_textbook_login").click(function() {
		alert("·Î±×ÀÎ ÈÄ ±³Àç½ÅÃ»ÀÌ °¡´ÉÇÕ´Ï´Ù");
		return false;
	});

    // view scholarship
	$("#link_view_scholarship").click(function() {
		openLink('/policy/scholarship.php','view_scholarship',677,649,0,0,0,0,1);
		return false;
	});

    // ibook link
    $("#link_go_ibook").click(function() {
		var _ibook=window.open("http://ibook.bugsbook.com/lecture/","ibookland","");
		if (!_ibook) {
			alert("ÆË¾÷ÀÌ Â÷´ÜµÇ¾î °­ÀÇ½Ç·Î ÀÌµ¿ÇÒ ¼ö ¾ø½À´Ï´Ù. ÆË¾÷Â÷´Ü ÇØÁ¦¸¦ ÇØÁÖ¼¼¿ä");
            return false;
        }
	});

    // wjtb link
    $("#link_go_wjtb").click(function() {
		var _wjtb=window.open("http://wjtb.bugsbook.com/lecture/","wjtb","");
		if (!_wjtb) {
			alert("ÆË¾÷ÀÌ Â÷´ÜµÇ¾î °­ÀÇ½Ç·Î ÀÌµ¿ÇÒ ¼ö ¾ø½À´Ï´Ù. ÆË¾÷Â÷´Ü ÇØÁ¦¸¦ ÇØÁÖ¼¼¿ä");
            return false;
        }
	});

    // event layer remove
    $(".link_cls_event").click(function() {
        var a_id=$(this).attr("id").split("link_remove_");
        var _id="eventArea"+a_id[1];

        $("#"+_id).css("display","none");

        return false;
    });

    // check escrow
    $("#link_escrow").click(function() {        
        var status="width=500 height=450 menubar=no,scrollbars=no,resizable=no,status=no";
        var obj=window.open('','kcp_pop',status);

        document.shop_check.method="post";
        document.shop_check.target="kcp_pop";
        document.shop_check.action="http://admin.kcp.co.kr/Modules/escrow/kcp_pop.jsp";

        document.shop_check.submit();
    });

}

// form description
function formDescription()
{
	// login form
	$("form#id_login_form").submit(function() {
		if (!checkForm("m_id","text","¾ÆÀÌµð¸¦ ÀÔ·ÂÇØ ÁÖ¼¼¿ä")) {
			return false;
		}
		if (!checkForm("m_pwd","text","ºñ¹Ð¹øÈ£¸¦ ÀÔ·ÂÇØ ÁÖ¼¼¿ä")) {
			return false;
		}

		return true;
	});

    // select change event
	$("select#id_m_coinf").change(function() {
		$("select#id_m_coinf option:selected").each(function() {
			if ($(this).val()) {
				$("#id_co_info").css("display","block");
			} else {
				$("#id_co_info").css("display","none");
            }
		});
	});

    $(".bbs_tab2").click(function() {
        $("#newsArea").css({"display":"block"});
        $("#noticeArea").css({"display":"none"});
    });

    $(".bbs_tab1").click(function() {
        $("#newsArea").css({"display":"none"});
        $("#noticeArea").css({"display":"block"});
    });

        
}

// main flash link 1
function viewFlashLink1()
{
    location.href="/guide/news.php?action=read&seq=486";
}

// main flash link 2
function viewFlashLink2()
{
    openLink('/recommend/ranking.php','view_ranking',650,690);
    return;
}

// create popup layer
function createPopupLayer()
{
    var _layer=new Array('1');
    var _width=new Array('324');
    var _height=new Array('600');
    var _top=new Array('300');
    var _left=new Array('214');
    var _img=new Array('/popup/images/bugsbook.jpg');
    var _text=new Array('»çÀÌÆ® Á¢¼ÓÀå¾Ö »ç°ú¹®');
    var _map=new Array('');
    var _map_cont="<MAP name='map1'><br /><AREA href='/2010/promote/news/view.php?no=462' shape='rect' coords=39,267,220,298 /><AREA href='/2010/promote/news/view.php?no=463' shape='rect' coords=39,314,231,344 /></MAP><MAP name='map2'><br /><AREA href='/2010/data/certList.php?no=11' shape='rect' coords=44,296,226,330 /></MAP>";
    var _source="";
    var _cookie="";

    for (var i=0;i<_layer.length;i++) {
        _cookie=getCookie("qlightkr_popup_"+_layer[i]);
        if (_cookie==null || _cookie=="") {
            _source="<div id=\"popup_"+_layer[i]+"\" style=\"width:"+_width[i]+"px; height:"+(parseInt(_height[i])+50)+"px; position:absolute; top:"+_top[i]+"px; left:"+_left[i]+"px; font-size:0px; border:2px solid #0A246A; display:block;\"><span style=\"width:"+_width[i]+"px; height:25px; position:absolute; top:0px; left:0px; background:#3D5A96; cursor:move; display:block;\"><span style=\"position:relative; top:5px; left:10px; font-weight:bold; color:#FFFFFF;\">"+_text[i]+"</span></span><img src=\""+_img[i]+"\" style=\"position:absolute; top:25px; left:0px; border:0px; \" alt=\""+_text[i]+"\" "+_map[i]+" /><span style=\"width:"+_width[i]+"px; height:25px; position:absolute; top:"+(parseInt(_height[i])+25)+"px; left:0px; background:#0a246a; display:block;\"><span style=\"position:relative; float:right; margin-left:10px; margin-right:10px; top:6px;\"><a href=\"#top_popup\" onclick=\"closeLayer("+_layer[i]+")\"><span style=\"color:#FFFFFF;\">[CLOSE]</span></a></span><input type=\"checkbox\" onclick=\"closeCookie("+_layer[i]+")\" style=\"width:15px; height:15px; position:relative; float:right; top:5px; display:block;\" /></span>"+_map_cont+"</div>";
            $("#top_popup").append(_source);
            $("#popup_"+_layer[i]).draggable();
        }
    }
}

// close popup layer
function closeLayer(_layer)
{
    $("#popup_"+_layer).css("display","none");
    return false;
}

// close popup layer & set cookie during 1 day
function closeCookie(_layer)
{
    setCookie("qlightkr_popup_"+_layer,"N",1);
    $("#popup_"+_layer).css("display","none");
    return false;
}

function getCookie(name) { 
	var Found = false 
	var start, end 
	var i = 0 
	 
	while(i <= document.cookie.length) { 
	start = i 
	end = start + name.length 
	 
	if(document.cookie.substring(start, end) == name) { 
	Found = true 
	break 
	} 
	i++ 
	} 
	 
	if(Found == true) { 
	start = end + 1 
	end = document.cookie.indexOf(";", start) 
	if(end < start) 
	end = document.cookie.length 
	return document.cookie.substring(start, end) 
	} 
	return "" 
} 

	// ¸®»çÀÌÁî
function reSize(id,wd)
{

var BoardMainFrame = eval(id+".document.body");
var ContentFrame = document.all[id];

ContentFrame.style.height = BoardMainFrame.scrollHeight + (BoardMainFrame.offsetHeight - BoardMainFrame.clientHeight);
ContentFrame.style.width = BoardMainFrame.scrollWidth + (BoardMainFrame.offsetWidth - BoardMainFrame.clientWidth); 

var ht = ContentFrame.style.height.replace("px","");

}

function CreateLayer(imgsrc,nm,t,l,w,h,id,link) {
    if (link) {
        linkimg="<a href='"+link+"'>"+imgsrc+"</a>";
    } else {
        linkimg=imgsrc;
    }
	var ly = "<div id='" + id + "' style='position:absolute; display:block; top:" + t + "px; left:" + l + "px; width:" + (w*1) + "px; height:" + (h*1) + "px; border:2px solid #0A246A; z-index:1000' onSelectStart=\"return false\">";
	ly += "<table border=0 cellpadding=0 cellspacing=0 width=100%>";
	ly += "<tr height='25' bgcolor='#3D5A96'>";
	ly += "<td id='ifrpopup"+id+"_top_00' width='"+w+"' style='padding-left:5px; font-weight:bold; color:#FFFFFF; cursor:default; '>";
	ly += nm;
	ly += "</td>";
	ly += "<td id='ifrpopup"+id+"_top_01' align='right' style='padding:2px' background='/2010/popup/images/pop_top_bg.jpg'>&nbsp;</td>";
	ly += "</tr>";
	ly += "<tr>";
	ly += "<td colspan='2'>"+linkimg;
	ly += "</td>";
	ly += "</tr>";
	ly += "<tr>";
	ly += "<td height='25' align='right' colspan='2' background='/2010/popup/images/pop_bottom.jpg' style='padding-right:10px;'>";
	ly += "<input type='checkbox' name='CK_"+id+"' id='CK_"+id+"' style='border:none' value='y'> <a href='#none' onClick=\"closeWin('"+id+"')\"><img src='/2010/popup/images/pop_bottom_txt.jpg' border=0></a>";
	ly += "</td>";
	ly += "</tr>";
	ly += "</table>";
	ly += "</div>";

	document.write(ly);
}

function setCookie( name, value, expiredays ) 
{ 
	var todayDate = new Date(); 
    todayDate.setDate( todayDate.getDate() + expiredays ); 
    document.cookie = name + "=" + escape( value ) + "; path=/; expires=" + todayDate.toGMTString() + ";" 
}

function closeWin(id) 
{ 
	var ly = document.getElementById(id);
    
	if ( document.getElementById("CK_" + id).checked == true ) {
		setCookie( "QPop" + id, "no" , 1);
	}
	
	ly.style.display = "none";
} 

function parent_lc(url){
	parent.location.replace(url);
}


//¸ÖÆ¼ ÆË¾÷
function openPopup1() 
{ 
    var imgsrc = new Array('<img src=\"./popup/images/20120131.jpg\" style=\"vertical-align:bottom; z-index:1000;\">'); //ÀÌ¹ÌÁö°æ·Î
    //var link = new Array('http://www.bugsbook.com');  //¸µÅ©ÁÖ¼Ò
    var link ='';
	var nm = new Array('0');											//ÆÄÀÏ¼ø¹ø
	var width = new Array('400');									//ÆÄÀÏ°¡·Î
	var height = new Array('550');									//ÆÄÀÏ¼¼·Î
	var Lleft = new Array('5');									//ÆÄÀÏ¿ÞÂÊÀ§Ä¡
	var Ltop = new Array('75');
	var PopName = new Array('»çÀÌÆ® Á¢¼ÓÀå¾Ö »ç°ú¹®');  //ÆË¾÷Á¦¸ñ

	var limitTime = new Array();										// Á¦ÇÑ¼³Á¤ new Date(¿¬µµ,¿ù-1,ÀÏ,½Ã°£(0~23),ºÐ(0~59)) ·Î ¼³Á¤ ÇØÁÖ¼¼¿ä, °ªÀ» 0À¸·Î ¼¼ÆÃ ½Ã Á¦ÇÑ¾øÀ½
		limitTime[0] = new Date(2012,2,05,23,59);
		//limitTime[1] = new Date(2009,9,5,23,59);

	var ThisTime = new Date();

	var w_width=window.screen.width;
	var w_height=window.screen.height;
		
	var win = ""; var Lx = 0; var Ly = 0; var Dx = 0; var Dy = 0; var Limit = "n";

	for(var i=0;i<nm.length;i++){

		if(limitTime[i] == 0) {
			Limit = "n"; 
		}
		else if(limitTime[0] !=0 ) {
			if( limitTime[0] > ThisTime ) Limit = "n"; else Limit = "y";
		}

		if(Limit=="n") {

			var CookieName = "QPop"+nm[i];
			var norCookie=getCookie(CookieName);  // ÄíÅ°³×ÀÓ ÁöÁ¤

			if (norCookie != "no") {
				if(i >0) {
					Lx=Lx*1+width[i]*1+13;
					if(Lx>(w_width*1-width[0])) { 
						Dx=Dx*1+20; Dy=Dy*1+20; Lx=Dx; Ly=Dy; 
					}
				}
				// ¾ÆÀÌÇÁ·¹ÀÓ¿¡ ÆÄ¾÷ ÆÄÀÏÀ» ¿­¾î ºÙÀÎ´Ù.
				//ifrpopup.location.replace(Url[i]);
				// ·¹ÀÌ¾î ÆË¾÷ ¿­±â
				// alert("1");
				CreateLayer(imgsrc[i],PopName[i],Ltop[i],Lleft[i],width[i],height[i],CookieName,link[i]); 

				//window.open(Url[i]+'?nid='+nm[i],'WinPop'+nm[i],'width='+width[i]+', height='+height[i]);
					
			}
		}

		win="";
	}

}


var PopId = p_PopId = "";
var ie5=document.all&&document.getElementById;
var ns6=document.getElementById&&!document.all;

// ÆË¾÷·¹ÀÌ¾î ÀÌµ¿ ÇÔ¼ö
function drag_drop(e){
if (ie5&&dragapproved&&event.button==1){
document.getElementById(PopId).style.left=tempx+event.clientX-offsetx
document.getElementById(PopId).style.top=tempy+event.clientY-offsety
}
else if (ns6&&dragapproved){
document.getElementById(PopId).style.left=tempx+e.clientX-offsetx
document.getElementById(PopId).style.top=tempy+e.clientY-offsety
}
}

function initializedrag(e,id){
offsetx=ie5? event.clientX : e.clientX
offsety=ie5? event.clientY : e.clientY

PopId = id;

if(p_PopId) document.getElementById(p_PopId).style.zIndex = 100;
document.getElementById(PopId).style.zIndex = 200;

if (ie5)
//document.getElementById("saver").style.display=''

tempx=parseInt(document.getElementById(PopId).style.left)
tempy=parseInt(document.getElementById(PopId).style.top)

p_PopId = id;

dragapproved=true
document.onmousemove=drag_drop
}

if (ie5||ns6)
document.onmouseup=new Function("dragapproved=false;document.onmousemove=null;");
