/* IE 6.0 이미지 리로드 버그 방지 */
try {doument.execCommand("BackgroundImageCache", false, true);} catch (ignored) {}
/* 쿼리 읽는 함수 출처 : http://papermashup.com/read-url-get-variables-withjavascript/ */
function getUrlVars() {
	var vars = {};
	var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi,function(m, key, value) {vars[key] = value;});
	return vars;
}

function Slideshow(originals_sel, thumbs_sel, time) {
	var currentImage;
	var currentIndex = -1;
	var interval;
	var myTimer;
	var originals = $(originals_sel + ' img');
	var thumbs = $(thumbs_sel + ' li');
	function showImage(index) {
		if (index < originals.length) {
			var indexImage = originals[index];
			if (currentImage) {
				if (currentImage !== indexImage) {
					$(currentImage).css('z-index', 2);
					clearTimeout(myTimer);
					$(currentImage).fadeOut(250, function() {
						myTimer = setTimeout(showNext, time);
						$(this).css({'display' : 'none','z-index' : 1});
					});
				}
			}
			$(indexImage).css({'display' : 'block','opacity' : 1});
			currentImage = indexImage;
			currentIndex = index;
			thumbs.removeClass('active');
			$(thumbs[index]).addClass('active');
		}
	}
	function showNext() {
		var len = originals.length;
		var next = currentIndex < (len - 1) ? currentIndex + 1 : 0;
		showImage(next);
	}
	function start() {
		myTimer = setTimeout(showNext, time);
		showNext(); // Load first image
		thumbs.bind('click', function(e) {
			var count = parseInt($(this).attr('rel'), 10) - 1;
			showImage(count);
		});
	}
	start();
}

function scrollY() {
	var de = document.documentElement;
	return self.pageYOffset || (de && de.scrollTop) || document.body.scrollTop;
}
function pageHeight() {
	return document.body.scrollHeight;
}
function resize() {
	$('iframe').attr('width',$(window).width()+'px');
	$('iframe').attr('height','300px');
}

$(document).ready(function() {
	/* 하이라이트 메뉴 */
	var currenturl = window.location.href;
	$('a').each(function() {
		var linkhref = $(this).attr("href");
		if (currenturl.indexOf(linkhref) !== -1) {
			if(!$(this).hasClass('all')) {
				$(this).addClass('now');
				$(this).parents('li').addClass('now');
				$(this).parents('ul').show();
			}
		}
	});
	/* 하이라이트 카테고리 */
	currenturl = "/?c=" + getUrlVars().c;
	$('#category a').each(function() {
		var linkhref = $(this).attr("href");
		if (linkhref.indexOf(currenturl) !== -1) {
			$(this).addClass('now');
			$(this).parents('li').addClass('now');
		}
	});
	
	/* 메뉴바 */
	$('nav.layout > ul > li').hover(function() {
		if (!$(this).hasClass('now')) {
			$('nav.layout > ul > li').siblings().find('ul').css('zIndex','1').slideUp('10');
			$('nav.layout > ul > li').removeClass('now');
		}
		$(this).find('ul').css('zIndex','4').slideDown('10');
		$(this).addClass('now');
	});
	$('nav.layout > ul > li > ul > li').hover(function() {
		$('nav.layout > ul > li > ul > li').removeClass('now');
		$(this).addClass('now');
	});

	/* 큰 디스플레이에서만 작동 */
	if ($(window).width() > 480) {
		/* lightbox */
		$('div.about_items a').lightBox();
		/* 단축 메뉴  */
		var h2count = $('article.layout h2').size();
		if (h2count > 1) {
			var aside = $('aside.layout').get(0);
			aside.innerHTML += '<section id="quick"><h1>목록</h1><ul></ul></section>';
			var section = $('section#quick');
			var count = 1;
			$('article.layout h2').each(function() {
				var qlinkname = 'q_link_'+count;
				$(this).attr('id',qlinkname);
				var title = $(this).text();
				var li = $('<li><a href="#'+qlinkname+'">'+title+'</a></li>');
				section.find('ul').append(li);
				count += 1;
			});
			section.insertBefore($('#schedule'));
		}
		/* 탑버튼 */
		var top_button = $('<span class="top_button">▲ 메뉴</span>').click(function() {$(document).scrollTop(0);});
		$('aside.layout').append(top_button);
		var aside_height = $('aside.layout').height() + 440;
		$(window).scroll(function() {
			if (scrollY() > aside_height) {
				if (top_button.css('display') === 'none') {
					top_button.slideDown('1000');
				}
				var sY = (scrollY() - 217) + 'px';
				top_button.addClass('on');
				top_button.css('top',sY);
			}
			if (scrollY() < aside_height) {
				top_button.slideUp('1000');
				top_button.removeClass('on');
			}
		});	
	}
	
	/* 모바일용 */
	if ($(window).width() < 481) {
		resize();
		window.onorientationchange = function() {
			resize();
			$('body').css('width',$(window).width());
		};
		window.addEventListener('load', function() {
			setTimeout(scrollTo, 0, 0, 1);
		}, false);
		$('#home p br').remove();
		$('body').css('width',$(window).width());
		
	}
	
	$('#schedule ul').load('/widget/calendar/ li');
	$('#news_it ul').load('/widget/news/it/ li');
	$('#news_game ul').load('/widget/news/game/ li');
	var slideshow = new Slideshow('section.images div.originals', 'section.images ul.thumbs', 4000);
	
	$('#history.images ul.thumbs li').each(function() {
		var rel = parseInt($(this).attr('rel'), 10);
		if (rel % 5 === 0) {
			$(this).addClass('eol');
		}
	});
	

});





