var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1472194-1']);
_gaq.push(['_trackPageview']);

(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();



var h_prop = $.browser.msie && $.browser.version < 7 ? "height" : "min-height";
// pngfix
if($.browser.msie && $.browser.version < 7) DD_belatedPNG.fix('img, #contentsInner');

$(function() {
	smartRollover();
	opacityRollover();
	externalLink();
	smoothScroll();

	$("a[rel=pop]").fancybox();
});

/* smartRollover
============================================================================================================ */
function smartRollover() {
	var imgCount = 0;
	var images_pre = new Array();
	$('img[src*="_off."],input[src*="_off."]').each (function(){
		images_pre[imgCount] = new Image();
		images_pre[imgCount].src = $(this).attr("src").replace("_off.", "_on.");
		$(this).hover(
			function () {
				$(this).attr("src", $(this).attr("src").replace("_off.", "_on."));
			},
			function () {
				$(this).attr("src", $(this).attr("src").replace("_on.", "_off."));
			}
		);
		imgCount ++;
	});
}

/* opacityRollover
============================================================================================================ */
function opacityRollover() {
	$('img[src*="_op."],input[src*="_op."], #caseParts a img').hover(
		function () {
			$(this).css('opacity', 0.7);
		},
		function () {
			$(this).css('opacity', 1);
		}
	);
}

/* externalLink
============================================================================================================ */
function externalLink() {
	var notBlank = new Array("");

	var n = "";
	for (var i = 0; i < notBlank.length; i ++) if(notBlank[i]) n += ":not([href*='" + notBlank[i] + "'])";
	if(document.domain) n += ":not([href*='" + document.domain + "'])";

	$("a[rel='external'], a[href$='.pdf']").attr("target", "_blank");
	$("a[href^=http]"+n).attr("target", "_blank");
}

/* smoothScroll
============================================================================================================ */
function smoothScroll() {
	$('a[href^="#"], a[href^="' + location.pathname + '#"]').each (function(){
		var hash = this.hash;
		if(hash.length > 1 && !this['rel']){
			$(this).click(function() {
				goScroll(hash);
				return false;
			})
		}
	});
}
function goScroll(hash) {
	var target = $(hash).offset().top;

	$(($.browser.safari) ? 'body' : 'html')
		.animate({scrollTop: target >= 15 ? target - 15 : target}, 600, 'swing', function(){$(this).unbind("mousewheel DOMMouseScroll");})
		.bind("mousewheel DOMMouseScroll",function(){
			$(this).queue([]).stop();
			$(this).unbind("mousewheel DOMMouseScroll");
		})
}


/* boxhiehgt
============================================================================================================ */
function boxhiehgt(wrap, ele){
	$(wrap).each( function(){
		var h = 0;
		$(ele, this).each( function(){
			h = Math.max(h, $(this).height());
		});
		$(ele, this).css(h_prop, h)
	});
}




/* phChange
============================================================================================================ */
function phChange(l, s){

	var viewTime = 7000;

	var timerID;
	var nowNum;
	var hover_flg = false;
	var cnt = 10;

	$(s).each( function(i){
		$(this).mouseover( function(){
			if(nowNum != i) phView(i, 200, true);
		})
	}).css('cursor', 'pointer');

	$(l + ',' + s).hover(
		function(){
			hover_flg = true;
			clearTimeout(timerID);
		},
		function(){
			hover_flg = false;
			clearTimeout(timerID);
			timerID = setTimeout(timerFunc, viewTime);
		}
	)

	function phView(num, time, hf){
		clearTimeout(timerID);
		cnt ++;
		nowNum = num;

		$(s).stop();

		$(l).stop().each( function(i){
			if(num == i) {
				$(this).show().css({'z-index':cnt, opacity:0}).animate({opacity: 1}, time, 'linear');
				if(hf) {
					$(s + ':eq(' + i + ')').css('opacity', 1);
				}else {
					$(s + ':eq(' + i + ')').animate({opacity: 1}, time, 'linear');
				}
			}else if($(this).css('opacity') > 0) {
				$(this).animate({opacity: 0}, time, 'linear');
				if(hf) {
					$(s + ':eq(' + i + ')').css('opacity', 0.6);
				}else {
					$(s + ':eq(' + i + ')').animate({opacity: 0.6}, time, 'linear');
				}
			}
		})
		timerID = setTimeout(timerFunc, viewTime);
	}


	function timerFunc(){
		if(hover_flg) return;

		var len = $(s).size();
		phView((nowNum+1)%len, 400);
	}

	phView(0, 0);
}




/* contentsSlide
============================================================================================================ */
function contentsSlide(wc, c, p, n, w){

	var viewTime = 7000;

	var timerID;
	var nowNum;
	var hover_flg = false;
	var s = $(c).size();

	$(wc).hover(
		function(){
			hover_flg = true;
			clearTimeout(timerID);
		},
		function(){
			hover_flg = false;
			clearTimeout(timerID);
			timerID = setTimeout(timerFunc, viewTime);
		}
	)

	$(p + ',' + n).hover(
		function(){
			$(this).addClass('hover');
		},
		function(){
			$(this).removeClass('hover');
		}
	)

	function slide(num){
		clearTimeout(timerID);
		nowNum = num;

		$(wc).stop().animate({left: -num * w}, 500, 'swing', function(){
			timerID = setTimeout(timerFunc, viewTime);
		});

		$(p + ',' + n).unbind('click')
		if(num <= 0){
			$(p).css('cursor', 'default').attr("src", $(p).attr("src").replace("_on", "_off")).attr("src", $(p).attr("src").replace("_off", "_no"));
		}else {
			$(p).css('cursor', 'pointer').attr("src", $(p).attr("src").replace("_no", "_off")).click( function(){slide((nowNum-1)%s);});
		}
		if(num >= s - 1){
			$(n).css('cursor', 'default').attr("src", $(n).attr("src").replace("_on", "_off")).attr("src", $(n).attr("src").replace("_off", "_no"));
		}else {
			$(n).css('cursor', 'pointer').attr("src", $(n).attr("src").replace("_no", "_off")).click( function(){slide((nowNum+1)%s);});
		}
	}


	function timerFunc(){
		if(hover_flg) return;

		slide((nowNum+1)%s);
	}

	slide(0);
}





