(function($){
	//title을 툴팁으로
	$.fn.titleTooltip = function() {			
		$("<span class=\"titleTooltip\"></span>").appendTo("body").hide();

		$(this).each(function(){
			var title = $(this).attr("title");
						
			if(title){
				$(this).removeAttr("title");
				$(this).attr("titleTooltip", title);
			
				$(this).mouseenter(function() {
					if($.browser.msie && ($.browser.version == 8.0))
						$(".titleTooltip").css("display","inline-block");
					else
						$(".titleTooltip").show();

					$(".titleTooltip").text(title);
			
				}).mousemove(function(e){
					var top = e.pageY;
					var left = e.pageX;
					$(".titleTooltip").offset({top: top+20, left: left+10});
					
				}).mouseleave(function(){
					$(".titleTooltip").hide();
				}); 
			}
		}); 
	};
  
	//comment: submit 후 작성한 댓글로 이동 (common2.js에서 커멘트 로드 후 작동)
	$.fn.backToComment = function() {		
		$('html,body').animate({scrollTop:$("#comment h3").offset().top}, 500);
	};


	//poutup의 daumview와 mixsh
	$.fn.slideTip = function(id, topValue, leftValue) {		
		var i = $(this).find("li"+id).index();
		var popup = $(this).find("li").eq(i+1);	
		var browser = $.browser;

		popup.css({position: "absolute", top: topValue, left: leftValue});
	
		$(this).find(id).mouseenter(function() {
			popup.dequeue();
			popup.css("display","block");	
		});

		popup.mouseenter(function(){	
			$(this).css("display","block");	
			popupENTER = true;
		}).mouseleave(function(){	
			$(this).hide();	
			popupENTER = false;
		});

		$(this).find(id).mouseleave(function(){
			popup.show(1).delay(500).queue(function(){
				popup.hide();		
			});
		});		
	};


  	//slideTab: popular post 와 favorite에 적용됨.
	 $.fn.slideTab = function() {
		var t = $(this);

		$(this).find(".slideTab span:first").addClass("currentTab").css("background-position", "left top")
			.end().find("ul:first").css({"opacity": 1, "z-index": 3});

			$(this).find(".slideTab span").mouseenter(function(){
				$(this).addClass("currentTab").css("background-position","left top");
				$(this).siblings("span").removeAttr("class").css("background-position","right top");

				var index = $(this).index();
				t.find("ul").eq(index).stop().fadeTo(400, 1).css("z-index", 3);
				t.find("ul").not("ul:eq("+index+")").stop().fadeTo(400, 0).css("z-index", 0);				 
			});
	};

})(jQuery);

if(($.browser.msie && ($.browser.version == 6.0))){
	$(document).ready(function($) {
		$("#popularPost ul:first").css("display","block");
		$("#content").prepend("<div class=\"warningIE6\">! Explorer 6에서는 정상적으로 보이지 않습니다.</div>");
	});

}else{
	$(document).ready(function($) {
		//smooth scroll
		$(".ticket").click(function(event){		
			event.preventDefault();
			$('html, body').animate({scrollTop: $(this.hash).offset().top}, 500);
		});

		if(!$.browser.safari)		$("a").not(".article a").titleTooltip();
		search();
		coverPage();	
		archive();
		popularPost();
		favorite();		
		article($(".entry .article"));
		fixHeight();
		$(".metaIcon").slideTip("#trigger1", -80, 43);
		$(".metaIcon").slideTip("#trigger2", 122, 43);
		$(".metaIcon").find(".popup:first").css({width: 72, overflow: "hidden"});

		$(window).load(function(){
			recentPost($("#recentPost li"), "none");
		});	
	});
}


	//coverPage 효과
	function coverPage() {
		$("#CoverPage").find("a.imgWrap").each(function(){
			$(this).mouseenter(function(){
			//	$(this).find("img").animate({opacity:0.3}, 300);
				$(this).find(".coverCnt").animate({"right": 30}, 250);

			
			}).mouseleave(function(){	
			//	$(this).find("img").animate({opacity:1}, 300);
				$(this).find(".coverCnt").animate({"right": -125}, 250);

			});
		});	
	}

	//search
	function search(search) {
		var f = false;

		$(".searchInput").focusin(function() {
			f = true;
			$(".searchSlide").css("top", 17);
		}).focusout(function() {
			f = false;
			$(".searchSlide").stop().animate({"top": 48}, 300);
		});			
	
		$(".search").find(".searchSlide").mouseenter(function(){
			$(this).stop().animate({"top": 17}, 300);
		}).mouseleave(function(){	
			if(f==false)
				$(this).stop().animate({"top": 48}, 300);				
		});	
	}


	//archive
	function archive() {

		for(var i=1;i<13;i++){
			$("#archivePage").find(".aM-"+i).mouseenter({index:i}, function(event) {	
				var k = event.data.index;
				$(".archiveRow").find(".aM-"+k).css("background-color", "#204663");		
				$(".archiveMonthRow").find(".aM-"+k).css('background-position', (4-43*(k-1))+'px -40px');
			}).mouseleave({index:i}, function(event) {
				var k = event.data.index;
				$(".archiveRow").find(".aM-"+k).css("background-color", "transparent");		
				$(".archiveMonthRow").find(".aM-"+k).css('background-position', (4-43*(k-1))+'px 0');
			});	
		}
	 }

	function calendar() {
		
		var tf = 0;
		$("#calendar").hide();

		$("#calToggle").click(function() {
			$("#calendar").slideToggle();

			if(tf==0){
				$("#calStratus").text("hide calendar");
				tf = 1;
			}else {
				$("#calStratus").text("show calendar");
				tf = 0;
			}
		});
	}



	//fixHeight:리스트부분 높이 맞추기
	function fixHeight() {

		if(!$("#ListPage ol").length)	return;
	
		//category 목록 부분
		var l = $("#ListPage li").length;
		var r = l%3;
		var q = Math.floor(l/3);
		if(r==0)	q = q-1;

		$("#ListPage li:even").addClass("even");			

		//새로운 height 및 top의 계산
		$("#ListPage li").each(function(index){		
			//2번째 줄부터 높이 계산
			if(index > 2){
				var prevbox = $("#ListPage li").eq(index-3);
				var h = prevbox.innerHeight();
				var top = prevbox.offset().top;
				var left = prevbox.offset().left;
		
		//		$(this).offset({top: top+h+1, left: left});	positoin: relative로 할 때
				$(this).css({"position": "absolute", top: top+h+1, left: left});	
			}
			//첫번째 줄
			else{
				var top = $(this).offset().top;			
				var left = $(this).offset().left;

				if(index == 1)	left = left + 233;
				else if(index == 2)	left = left + 466;				
			
				$(this).css({"position": "absolute", top: top, left: left});	
			}

			//마지막 줄에 last클래스 추가
			if(index+1 > q*3)	$(this).addClass("last");		
		}); 

		//마지막 줄 부분: 높이 계산
		lastHeight();
	
		//imgHover 때문에 따로 each문 돌려야 함(첫번째 줄의 left가 동일하게 잡히기 때문)
		$("#ListPage li").each(function(index){		
			//height정의: imgHover 실행 시 li의 높이를 고정시키기 위함
			var listH = $(this).height();
			$(this).height(listH);

			//img hover 효과
			imgHover(this, listH);
		}); 

		//검색 시 댓글, 트랙백 부분 
		$("#CommentList li:even").addClass("even");
		$("#TrackbackList li:even").addClass("even");
	};


	function lastHeight() {
	
		var totalH = 0;
		var lastTop = [], lastLeft = [];

		//totalH: 마지막 줄 중 가장 낮게 위치해 있는 요소의 (top+height)값 구함
		$("#ListPage li.last").each(function(index){
			lastTop[index] = $(this).offset().top;
			lastLeft[index] = $(this).offset().left;
			var lastHeight = $(this).innerHeight();

			if(totalH <= lastTop[index]+lastHeight){
				totalH = lastTop[index]+lastHeight;
			}	
		});
	
		//totalH에 의한 마지막 줄 요소의 height 계산(lastTop[index]: 여기서 each문 다시 실행 시 top과 left값이 일부 다르게 잡혀 첫번째 each문에서 가져왔던 값을 사용해야 함)
		$("#ListPage li.last").each(function(index){
			$(this).offset({top: lastTop[index], left: lastLeft[index]});
			$(this).height(totalH-lastTop[index]-50);		
		});

		//ol 높이: li의 요소 높이가 새로 계산되었기 때문
		var olTop = $("#ListPage ol").offset().top;
		$("#ListPage ol").height(totalH-olTop-18);
	}


	//li에 마우스오버되었을 때 커지는 효과
	function imgHover(a, h){
		var img = $(a).find("img");
		var listCnt = $(a).find(".listCnt");
		var link = $(a).find(".thumbIMGwrap");

		var left = $(a).offset().left;
		var top = $(a).offset().top;
		var hh = $(a).height();

		listCnt.css("opacity", 0); //ie8때문

		$(a).mouseenter(function() {
		//	$(this).find("h3").delay(100).animate({"font-size": "23px"}, 150);
		//	link.animate({width: 220, height: 275, "left": "-10px"}, 300);
			img.animate({width: 220, height: 275, "left": "-10px"}, 300);
			listCnt.animate({opacity: 1}, 300);

		}).mouseleave(function(){
		//	$(this).animate({width: 200, "left": left, "top": top, height: hh}, 300);
		//	$(this).find("h3").delay(50).animate({"font-size": "20px"}, 150);
			img.animate({width: 200, height: 250, "left": "0"}, 300);
			listCnt.animate({opacity: 0}, 300);
		}); 
	}




  //recent Post
  function recentPost(r,s) {

	if(!($.browser.msie && ($.browser.version == 7.0))){

  		$("#recentPost li:even p").css({"float": "left", opacity: 0});
		$("#recentPost li:even a").css("float", "left");
		$("#recentPost li:odd p").css({"float": "right", display: "none"});	
		$("#recentPost li:odd a").css("float", "right");	
	
		topCalculation(r,s);

	}
  }

	//본문에 slidetoggle 적용 시에도 부르기 위해 recent Post에 있던 내용을 따로 function으로 분리 
	function topCalculation(tc,c){

		tc.each(function(index){

			var img = $(this).find("img");
			var link = $(this).find("a");
			var p = $(this).find("p");

			if(c == "none"){
				var top = $(this).offset().top;
				var left = $(this).offset().left;
	
			}else if(c == "submit"){
				$(this).css("position", "inherit");
				var top = $(this).offset().top;
				var left = $(this).offset().left;
			}

			$(this).css({"top": top, "left": left, height: 116});

			link.mouseenter(function() {
				$(this).parent().stop().animate({height: 146, "top": top-15, "left": left-15}, 200);
				img.stop().animate({width: 130, height: 130}, 200);
				if(index%2 == 0)	p.stop().animate({opacity: 1},200);
				else	p.stop().fadeTo(200, 1);

			}).mouseleave(function(){
				$(this).parent().stop().animate({height: 116, "top": top, "left": left}, 200);
				img.stop().animate({width: 100, height: 100}, 200);
				if(index%2 == 0)	p.stop().animate({opacity: 0},200);
				else	p.stop().fadeTo(200, 0);
			}); 
		});

		tc.css("position", "absolute") 
	}


    //popular Post
    function popularPost() {
		$("#popularPost").slideTab();

		$("#popularPost ul").each(function(){
			var ul = $(this);
			$(this).find("li:first").addClass("open").find("div").css("left",162);
			$(this).find("li").not("li:first").css("left", 60);

			$(this).find("li").mouseenter(function() {					
				$(this).addClass("open").stop().animate({left: 0},300)
					.siblings('li').removeAttr("class");

				$(this).prevAll("li").stop().animate({"left": 0},300)				
					.end().nextAll("li").stop().animate({"left": 60},300);

				var div = $(this).find("div");
				div.stop().animate({"left": 162},300);
				ul.find("li div").not(div).stop().animate({"left": 0},300);	
			});
		}); 				
	}


	//favorites
    function favorite() {
		
		$("#favorite").slideTab();

		$("#favorite").find("li a").each(function(){	
			$(this).mouseenter(function(){
				$(this).animate({opacity: 0}, 150, function(){
					$(this).css("background-position","left bottom");
				});
				$(this).animate({opacity: 1}, 150);						
			}).mouseleave(function(){
				$(this).animate({opacity: 0}, 150, function(){
					$(this).css("background-position","left top");
				});
				$(this).animate({opacity: 1}, 150);						
			});
		
		}); 
	}



	//본문에 관련된 자바스크립트
	function article(article) {

		//download 서브메뉴
		if($(".downloadWrap .submenu").length){
			$(".downloadWrap").find(".download").click(function(event) {
				event.preventDefault();
				$(".submenu").slideToggle("slow", function(){
					var rp = $("#recentPost li");
					topCalculation(rp,"submit");
				});
			});
		}

		//tab기능
		$(".tabBox div").not(".tabBox div:first").hide();
		$(".tabBox").find("li").css({cursor: "pointer", "font-size": 20, display: "inline"});

		$(".tabBox").each(function(index){
			var currentBox = $(".tabBox").eq(index);

			$(this).find(".tab li").each(function(i){

				var currentDiv = currentBox.find("div").eq(i);

				$(this).click(function(){
					currentDiv.fadeIn(300);
					currentBox.find("div").not(currentDiv).fadeOut(300);
					currentBox.find("li").eq(i).css
				});
			});
		});

	}
