/*

   File Name: common.js
   Description: サイト共通スクリプト定義
   Last Update: 2011.05.25
   Index: [ NAVIGATION ]
          [ IMAGE ROLL OVER ]
            - OPACITY OVER
            - SLOW OPACITY OVER
          [ SMOOTH SCROLL ]
          [ CROSS SITE ]
          [ GOOGLE ANALYTICS ]

*/


/* -------------------------------------------------------
   NAVIGATION
------------------------------------------------------- */
$(function() {
	var globalNav = $('#globalNav');
	var localNav = $('body.craftmen #localNav, body.products #localNav');
	globalNav.css('left','-80px');
	localNav.css('left','-40px');
	globalNav.animate({'left':'0'},1000,'easeOutQuint',
		function() {
			localNav.css('left','0').animate({'left':'40px'},1000,'easeOutQuint');
		}
	);
});

$(function(){
	var target = '#globalNav a img[class!=current], body.craftmen #localNav a img[class!=current], body.products #localNav a img[class!=current]';
	$(target).each(function() {
		var src = $(this).attr('src');
		var srcOver = src.substr(0,src.lastIndexOf('.'))+'_over'+src.substring(src.lastIndexOf('.'));
		$('<img>').attr('src',srcOver);
		$(this).hover(
			function() {
				var imageOver = '<img src="'+srcOver+'" width="'+$(this).attr('width')+'" height="'+$(this).attr('height')+'" alt="'+$(this).attr('alt')+'" class="over" />';
				$(this).stop(true,false).css('position','absolute').after(imageOver).fadeTo(600,0);
			},
			function() {
				$(this).stop(true,false).css('position','static').fadeTo(0,1,
					function() {
						$('a img[src*=_over][class!=current]').remove();
					}
				)
			}
		);
	});
});


/* ------------------------------------------------------------
   IMAGE ROLL OVER
------------------------------------------------------------ */

/* OPACITY OVER
------------------------------------------------------------ */
$(function() {
	var target = 'p#pageTop a img, li#previous a img, li#next a img';
	$(target).hover(
		function() {
			$(this).css('opacity',0.3);
		},
		function() {
			$(this).css('opacity',1);
		}
	);
});

/* SLOW OPACITY OVER
------------------------------------------------------------ */
$(function() {
	var target = 'a.thickbox img';
	$(target).hover(
		function() {
			$(this).stop(true,false).fadeTo(200,0.5);
		},
		function() {
			$(this).fadeTo(200,1);
		}
	);
});

/* ------------------------------------------------------------
   SMOOTH SCROLL
------------------------------------------------------------ */
$(function() {
	$('a[href^=#]:not(a[href$=#])').click(
		function() {
			var target = $(this.hash);
			var top = target.offset().top;
			if (jQuery.support.checkOn) {
				$('html').stop(true,false).animate({scrollTop:top},800,'easeInOutQuart');
			}
			else {
				$('body').stop(true,false).animate({scrollTop:top},800,'easeInOutQuart');
			}
			return false;
		}
	);
})


/* -------------------------------------------------------
   CROSS SITE
------------------------------------------------------- */
jQuery.extend($.expr[':'],{
	external: function(a,i,m) {
		if(!a.href) {
			return false;
		}
		return a.hostname && a.hostname !== window.location.hostname;
	},
	internal: function(a,i,m) {
		if(!a.href) {
			return false;
		}
		return a.hostname && a.hostname == window.location.hostname;
	}
});

$(function() {
	$('a:external').addClass('external');
	$('a[href^="mailto"]').removeClass('external');
	$('a.external').click(
		function() {
			window.open(this.href,'_blank');
			return false;
		}
	);
});


/* -------------------------------------------------------
   GOOGLE ANALYTICS
------------------------------------------------------- */
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-18484571-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);
})();
