/* jQuery */
$(function(){

	// IE version check
	var ie6=($.browser.msie && $.browser.version==6);
	var ie7=($.browser.msie && $.browser.version==7);
	var ie8=($.browser.msie && $.browser.version==8);

	// current directory display
	var navCat=['home','featured','products','info','outlet','charity','records','company'];
	for(var i=0,len=navCat.length;i<len;i++){
		if(_dir==navCat[i]){
			$('#ctg-'+navCat[i]).addClass('current');
		}
	};
	// current side display
	var navSide=['srg','reissue','classics','goods-accessories','custom-shop'];
	var path=location.pathname;
	for(var i=0,len=navSide.length;i<len;i++){
		if(path.match('/'+navSide[i])){
			$('#side-'+navSide[i]+' a').addClass('current');
		}
	};

	// magic line
	var $el,leftPos,newWidth,$mainNav=$("#global-navi ul");
	$mainNav.append("<li id='magic-line'></li>");
	var $magicLine=$("#magic-line");

	$magicLine
		.width($(".current").width())
		.css("left",$(".current a").position().left)
		.data("origLeft",$magicLine.position().left)
		.data("origWidth",$magicLine.width());

	$("#global-navi ul li").find("a").hover(function(){
		$el=$(this);
		leftPos=$el.position().left;
		newWidth=$el.parent().width();

		$magicLine.stop().animate({
			left:leftPos,
			width:newWidth
		});
	},function(){
		$magicLine.stop().animate({
			left:$magicLine.data("origLeft"),
			width:$magicLine.data("origWidth")
		});
	});

	/* window open */
	$('.entry-content a,.comment-content a,.page-content a').not('a[rel*=shadowbox],.more a,#goods-accessories h3 a,a.blank,#tm a').each(function(){
		var owUrl=$(this).attr('href');
		var owTxt=$(this).text();
		//$(this).before('&#9658;&#160;');
		$(this).after('&#160;<a href="'+owUrl+'" class="owimg" title="'+owTxt+'を別ウィンドウで開く"><img src="images/openwin.gif" alt="別ウィンドウで開く" width="14" height="13" /></a>&#160;');
	});
	$('a.owimg').click(function(){
		window.open(this.href);
		return false;
	});

	/* smooth scroll */
	$('a[href*=#]').click(function(){
		if(location.pathname.replace(/^\//,'')==this.pathname.replace(/^\//,'')&&location.hostname==this.hostname){
			var target=$(this.hash);
			target=target.length&&target;
			if(target.length){
				var sclpos=0;
				var scldurat=1500;
				var targetOffset=target.offset().top-sclpos;
				$('html,body').animate({scrollTop:targetOffset},{duration:scldurat,easing:'easeOutQuint'});
				return false;
			}
		}
	});

	/* png fix */
	$(document).pngFix();

	/* lazyload effect */
	$('#alpha img').not('#products-gallery img').lazyload({
		placeholder:"../images/clear.gif",
		effect:"fadeIn"
	});

	/* panel gallery */
	$('#products-gallery').panelGallery({
		boxSize:50,
		boxFadeDuration:1000,
		boxTransitionDuration:50,
		viewDuration:3000,
		FX:new Array('jackpot','panelZipperUp,true','jackpot','panelZipperDown,true')
	});

	/* index-navi */
	if($('#index-navi').length){
		$('#index-navi a img').each(function(){
			$("<img>").attr("src",$(this).attr("src").replace(/^(.+)(\.[a-z]+)$/, "$1-over$2"));
		})
		if($.browser.msie && $.browser.version<7){
			$('#index-navi a img').each(function(){
				$(this)
				.data("src",$(this).attr("src"))
				.attr("src","../images/clear.gif")
				.css("filter","progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+$(this).data("src")+"',sizingMethod='crop')")
			}).mouseover(function(){
				$(this).css("filter","progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+$(this).data("src").replace(/^(.+)(\.[a-z]+)$/, "$1-over$2")+"',sizingMethod='crop')")
			}).mouseout(function(){
				$(this).css("filter","progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+$(this).data("src")+"',sizingMethod='crop')")
			})
		}else{
			$('#index-navi a img').mouseover(function(){
				$(this).attr("src",$(this).attr("src").replace(/^(.+)(\.[a-z]+)$/, "$1-over$2"));
			}).mouseout(function(){
				$(this).attr("src",$(this).attr("src").replace(/^(.+)-over(\.[a-z]+)$/, "$1$2"));
			})
		}
	};

});

var scrolltotop={
	//startline: Integer. Number of pixels from top of doc scrollbar is scrolled before showing control
	setting: {startline:300},
	state: {isvisible:false, shouldvisible:false},

	togglecontrol:function(){
		var scrolltop=jQuery(window).scrollTop()
		this.state.shouldvisible=(scrolltop>=this.setting.startline)? true : false
		if (this.state.shouldvisible && !this.state.isvisible){
			jQuery("#pagetop-anchor").slideDown();
			this.state.isvisible=true;
		}
		else if (this.state.shouldvisible==false && this.state.isvisible){
			jQuery("#pagetop-anchor").slideUp();
			this.state.isvisible=false;
		}
	},
	init:function(){
		jQuery(document).ready(function($){
			jQuery("#pagetop-anchor").hide();
			var mainobj=scrolltotop;
			$(window).bind('scroll resize', function(e){
				mainobj.togglecontrol();
			})
		})
	}
}
scrolltotop.init();
