var Prart = {};

Prart.util = function(){
	return {
		addEvent: function(elm, type, fn){
			try{
				elm.addEventListener(type, fn, false);
			}catch(e){
				elm.attachEvent('on' + type, function() {
					fn.call(elm, window.event);
				});
			}
		}
	}
}();

Prart.rollOver = function(){
	var over = "_on.", out = "_off.";
	var overImgList = document.images;
	var overIptList = document.getElementsByTagName("input");
	var main = function() {
		if(overImgList.length) {
			for(var i = 0, len = overImgList.length; i < len; i++){
				if(overImgList[i].getAttribute("src") && overImgList[i].getAttribute("src").lastIndexOf(out) != -1) {
					var preload = new Image();
					preload.src = overImgList[i].src.replace(out, over);
					Prart.util.addEvent(overImgList[i], "mouseover", function(event) {
						this.setAttribute("src", this.getAttribute("src").replace(out, over));
					});
					Prart.util.addEvent(overImgList[i], "mouseout", function(event) {
						this.setAttribute("src", this.getAttribute("src").replace(over, out));
					});
				}
			}
		}
		if(overIptList.length) {
			for(var i = 0, len = overIptList.length; i < len; i++){
				if(overIptList[i].getAttribute("src") && overIptList[i].getAttribute("src").lastIndexOf(out) != -1) {
					var preload = new Image();
					preload.src = overIptList[i].src.replace(out, over);
					Prart.util.addEvent(overIptList[i], "mouseover", function() {
						this.setAttribute("src", this.getAttribute("src").replace(out, over));
					});
					Prart.util.addEvent(overIptList[i], "mouseout", function() {
						this.setAttribute("src", this.getAttribute("src").replace(over, out));
					});
				}
			}
		}
	}();
}
Prart.util.addEvent(window, "load", Prart.rollOver);

Prart.pageScroller = function() {
	var targetX, targetY, scrollX, scrollY, timer;
	var delay = 7, ms = 10;
	var html = document.documentElement;
	var body = document.body;
	var scrollBtns = document.links;
	var run = function(){
		var X = window.pageXOffset || html.scrollLeft || body.scrollLeft || 0,
			Y = window.pageYOffset || html.scrollTop || body.scrollTop || 0;
		scrollX += (targetX - X) / delay;
		scrollY += (targetY - Y) / delay;
		if(! ((targetX == scrollX && targetY == scrollY) || (Math.abs(targetX - X) < 1 && Math.abs(targetY - Y) < 1))) {
			window.scrollTo(scrollX, scrollY);
			timer = setTimeout(run, ms);
		} else {
			clearTimeout(timer);
		}
	}
	var main = function() {
		for(var i = 0, len = scrollBtns.length; i < len; i++){
			var btn = scrollBtns[i];
			if (btn.getAttribute("href",2).indexOf("#") != -1) {
				Prart.util.addEvent(btn, "click", function(event) {
					var targetId = this.getAttribute("href",2).substr(1);
					if (targetId != "" && document.getElementById(targetId)) {
						(event.preventDefault) ? event.preventDefault() : event.returnValue = false;
						var target = document.getElementById(targetId),
							windowHeight = html.clientHeight || body.clientHeight,
							windowWidth = html.clientWidth || body.clientWidth,
							pageHeight = html.scrollHeight || body.scrollHeight,
							pageWidth = html.scrollWidth || body.scrollWidth;
						if (document.compatMode == "BackCompat") {
							pageHeight = body.scrollHeight, pageWidth = body.scrollWidth;
						}
						try {
							var position = target.getBoundingClientRect();
							targetX = position.left + (body.scrollLeft || html.scrollLeft) - html.clientLeft;
							targetY = position.top + (body.scrollTop || html.scrollTop) - html.clientTop;
						} catch(e) {
							targetX = target.offsetLeft, targetY = target.offsetTop;
						}
						if (windowHeight + targetY > pageHeight) targetY -= (windowHeight + targetY) - pageHeight;
						if (windowWidth + targetX > pageWidth) targetX -= (windowWidth + targetX) - pageWidth;
						scrollX = window.pageXOffset || html.scrollLeft || body.scrollLeft || 0;
						scrollY = window.pageYOffset || html.scrollTop || body.scrollTop || 0;
						run();
					}
				});
			}
		}
	}();
}
Prart.util.addEvent(window, "load", Prart.pageScroller);


/* jQuery function (for v1.4.2) */

$(function(){

	if($("#visual").length){

		var visualCcontainer = $('#visual ul');
		var visualNav = $('#visualNav li');
	
		visualCcontainer.cycle({
			fx: 'fade', 
			speed: 500,
			timeout: 5000,
			before: function(){
				visualNav.removeClass("current");
				visualNav.eq($(this).index()).addClass("current");
			}
		});
		visualNav.click(function(){
			visualNav.not($(this)).removeClass("current");
			$(this).addClass("current");
			visualCcontainer.cycle($(this).index());
			visualCcontainer.cycle('pause');
		});

	}

	if($('#subStaffList').length){

		var staffTimer;

		$("#subStaffList li").mouseover(function(){

			if(staffTimer) clearTimeout(staffTimer);
			if($('.staffTips').length){
				$('.staffTips').remove();
			}

			var title = $(this).find('img').attr('alt');
			var href = $(this).find('a').attr('href');
			var tip = $("<span class='staffTips'><span><span><span><a href='" + href + "'>" + title + "</a></span></span></span></span>");
			var l = $(this).offset().left;
			var t = -13 + $(this).offset().top;
			$("#page").append(tip);
			var w = (20 - tip.width() / 2) + 'px';
			tip.css({left: l, top: t, marginLeft: w});
			tip.mouseover(function(){
				if(staffTimer) clearTimeout(staffTimer);
			}).mouseout(function(){
			if(staffTimer) clearTimeout(staffTimer);
			staffTimer = setTimeout(function() {
				staffTimer = null;
				if($('.staffTips').length){$('.staffTips').remove()}
			},500);
			});
		}).mouseout(function(){
			if(staffTimer) clearTimeout(staffTimer);
			staffTimer = setTimeout(function() {
				staffTimer = null;
				if($('.staffTips').length){$('.staffTips').remove()}
			},500);
		});

	}

	if($('#media').length){

		var timer;
		var mediaCarousel = $("#media ul").moodular({
			speed: 300,
			auto: false,
			api: true
		});

		$("#media li").each(function(){
			var src = $(this).find("img").attr("src");
			var newSrc = src.replace(".jpg","_on.jpg");
			$(this).css("background","url('" + newSrc + "')");
		});

		$("#media li").live("mouseover", function(){
			
			if(timer) clearTimeout(timer);
			$("#media li").not($(this)).find("img").fadeTo(0,1);
			$(this).find("img").fadeTo(200,0);
			if($('.mediaTips').length){
				$('.mediaTips').remove();
			}

			var title = $(this).find('img').attr('alt');
			var href = $(this).find('a').attr('href');
			var tip = $("<span class='mediaTips'><span><span><span><a href='" + href + "'>" + title + "</a></span></span></span></span>");
			var l = $(this).offset().left;
			var t = 89 + $(this).offset().top;

			$("#page").append(tip);
			var w = (48 - tip.width() / 2) + 'px';
			tip.css({left: l, top: t, marginLeft: w});
			tip.mouseover(function(){
				if(timer) clearTimeout(timer);
			});
		}).live("mouseout", function(){
			if(timer) clearTimeout(timer);
			timer = setTimeout(function() {
				timer = null;
				$("#media li").find("img").fadeTo(0,1);
				if($('.mediaTips').length){$('.mediaTips').remove()}
			},500);
		});

		$('#mediaNext img').click(function(){
			mediaCarousel.next();
		}).mouseover(function(){
			$("#media li").find("img").fadeTo(0,1);
			if($('.mediaTips').length){$('.mediaTips').remove()}
		});
		$('#mediaPrev img').click(function(){
			mediaCarousel.prev();
		}).mouseover(function(){
			$("#media li").find("img").fadeTo(0,1);
			if($('.mediaTips').length){$('.mediaTips').remove()}
		});
	}

	if($('.works').length){
		$('.works .title a').hover(function(){
			$(this).append("<span>click</span>");
			if(jQuery.support.opacity){
				$(this).find("span").animate({opacity: 1}, {queue: false});
			}
		},function(){
			if(jQuery.support.opacity){
				$(this).find("span").fadeOut("fast",function(){
					$(this).remove();
				});
			} else {
				$(this).find("span").add("shape").remove();
			}
		});
	}
	if($('.recommend').length || $('.recommendM').length){
		$('.recommend a.title').add('.recommendM a.title').hover(function(){
			$(this).append("<span>click</span>");
			if(jQuery.support.opacity){
				$(this).find("span").animate({opacity: 1}, {queue: false});
			}
		},function(){
			if(jQuery.support.opacity){
				$(this).find("span").fadeOut("fast",function(){
					$(this).find("span").remove();
				});
			} else {
				$(this).add("shape").remove();
			}
		});
	}
	if($("#share").length){
		var img = $("#share p").find("img"),
			src = img.attr("src"),
			next = $("#share div");
		$("#share p").toggle(function(){
			img.attr("src",src.replace(".gif","_on.gif"));
			if(jQuery.support.opacity){
				next.fadeIn();
			} else {
				next.show();
			}
		},function(){
			img.attr("src",src.replace("_on.gif",".gif"));
			if(jQuery.support.opacity){
				next.fadeOut("fast");
			} else {
				next.hide();
			}
		});
	}
	if($("#contactFoot").length){
		$("#contactFoot img").hover(function(){
			$(this).animate({opacity: 0.6}, {duration: "fast", queue: false});
		},function(){
			$(this).animate({opacity: 1}, {duration: "fast", queue: false});
		});
	}
	if($("#pageTop").length){
		$("#pageTop img").hover(function(){
			$(this).animate({opacity: 0.8}, {duration: "fast", queue: false});
		},function(){
			$(this).animate({opacity: 1}, {duration: "fast", queue: false});
		});
	}
	if($("#introduction a").length){
		$("#introduction a img").hover(function(){
			$(this).animate({opacity: 0.8}, {duration: "fast", queue: false});
		},function(){
			$(this).animate({opacity: 1}, {duration: "fast", queue: false});
		});
	}
	if($("#staffContact a").length){
		$("#staffContact a img").hover(function(){
			$(this).animate({opacity: 0.8}, {duration: "fast", queue: false});
		},function(){
			$(this).animate({opacity: 1}, {duration: "fast", queue: false});
		});
	}
	if($(".contentsWrapper").length){
		$(".contentsWrapper:last").addClass("marginZero");
	}

	//PPC
	if($(".ppcNav").length){
		$(".ppcNav .inner").find("div").click(function(){
			var href = $(this).find("h3 a").attr("href");
			location.href = href;
		});
	}
	if($("#dataWrapper").length){
		$(".dataPopUp").hide();
		$("#dataWrapper dt img").click(function(){
			var target = $(this).parents("li").find(".dataPopUp");
			if(! target.find(".title").length) {
				target.prepend("<p class='title'>" + $(this).prev().text() + "</p>");
			}
			var l = $(this).offset().left + 23;
			var t = $(this).offset().top - 15;
			if ($(this).hasClass("show")){
				if(jQuery.support.opacity){
					target.fadeOut();
				} else {
					target.hide();
				}
				$(this).removeClass("show");
			} else {
				target.css({
					"left": l + "px",
					"top": t + "px"
				});
				if(jQuery.support.opacity){
					target.fadeIn("fast");
				} else {
					target.show();
				}
				$(this).addClass("show");
			}
		});
		$(".dataPopUp .btn").click(function(){
			if(jQuery.support.opacity){
				$(this).parent().fadeOut();
			} else {
				$(this).parent().hide();
			}
			$(this).parents("li").find("dt img").removeClass("show");
		});
	}

	if($("#autherEntries").size()){
		var autherEntries = $('#blog #sub .news dt').length;
	
		function searchPageselectCallback(index, jq) {
			var itemsPerPage = 10;
			var maxElem = Math.min((index + 1) * itemsPerPage, autherEntries);
	
			$('#blog #sub .news dt:visible').add('#blog #sub .news dd:visible').hide();
			for (var i=index*itemsPerPage; i<maxElem; i++){
				$('#blog #sub .news dt:eq(' + i + ')').add('#blog #sub .news dd:eq(' + i + ')').show();
			}
		}
		$('#blog #sub .news').append('<div id="pagerSub"></div>');
		$("#pagerSub").pagination(autherEntries, {
			callback: searchPageselectCallback,
			link_to:"",
			num_display_entries:0,
			prev_text:"&lt; Prev",
			next_text:"Next &gt;",
			items_per_page:10
		});
	}

	/* for Ranking */
	var rankingTabsQuery = 0;
	if (window.location.search && window.location.search.match(/tab=/)) {
		rankingTabsQuery = window.location.search.replace(/^\?.*?tab=(\d)/,"$1");
	}
	
	var rankingNavLarge = $(".newsCategoryTitle2 > .nav > ul > li");
	var rankingLarge = $("#rankingLarge ul");
	rankingLarge.eq(rankingTabsQuery).siblings().css({"display": "none"});
	
	
	rankingNavLarge.each(function(i){
		var target = $(this).find("a");
		var html = target.html();
		$(this).append("<span>" + html + "</span>");
		if(i == rankingTabsQuery) $(this).find("span").addClass("cr");
		target.remove();
	});
	
	var rankingNavLargeTabs = rankingNavLarge.find("span");
	rankingNavLargeTabs.click(function(){
		if($(this).hasClass("cr")) return false;
		var index = $(this).parent().index();
		rankingLarge.not(rankingLarge.eq(index)).hide();
		rankingLarge.eq(index).show();
		rankingNavLargeTabs.not($(this)).removeClass("cr");
		$(this).addClass("cr");
	});
	if(!$.support.opacity){
		rankingLarge.each(function(){
			$(this).find("li:even").css({"background": "#f9f9f9"});
		});
	}
	
	var rankingNavSmall = $("#sub > .ranking > .nav > ul > li");
	var rankingSmall = $("#sub > .ranking > .list");
	rankingSmall.eq(0).nextAll("ul").css({"display": "none"});
	
	rankingNavSmall.each(function(i){
		var target = $(this).find("a");
		var html = target.html();
		$(this).append("<span>" + html + "</span>");
		if(i == 0) $(this).find("span").addClass("cr");
		target.remove();
	});
	
	var rankingNavSmallTabs = rankingNavSmall.find("span");
	rankingNavSmallTabs.click(function(){
		if($(this).hasClass("cr")) return false;
		var index = $(this).parent().index();
		rankingSmall.not(rankingSmall.eq(index)).hide();
		rankingSmall.eq(index).show();
		rankingNavSmallTabs.not($(this)).removeClass("cr");
		$(this).addClass("cr");
	});

	var otherEntriesChange = function(){
		var target = $('.otherEntries .tabs');
		$('.otherEntries .tabs:gt(0)').hide();
		var nav = $('.otherEntries .nav li');
		nav.each(function(i){
			var html = $(this).find("a").html();
			$(this).append("<span>" + html + "</span>");
			if(i == 0) $(this).find("span").addClass("cr");
			$(this).find("a").remove();
		});
		var btn = nav.find('span');
		btn.click(function(){
			var index = $(this).parent().index();
			target.not(target.eq(index)).hide();
			target.eq(index).show();
			btn.not($(this)).removeClass("cr");
			$(this).addClass("cr");
		});
		if(!jQuery.support.opacity){
			$('.otherEntries .tabs li:odd').addClass('odd');
			$('.otherEntries .tabs li:even').addClass('even');
		}
	}

	if($('.otherEntries').size()) {
		otherEntriesChange();
	}

});
