/*--------------------------------------
file Name:
URI: 
Description: project antique
Version: 1.0
Original: 2011.02.06
Modifier:
Author: 
Author URI: 
--------------------------------------*/



if(!/^http:\S+contact/.test(location.href)){
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-25054638-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 lib={
	// ロールオーバー
	rollover: function(target, hover, active, focus) {
		$(target).each(function() {
			var isActive = active && new RegExp(active + '(\.gif|\.jpg|\.png)([\?].*|$)').test(this.src);
			if (isActive && !focus) return;
			this.src.match('(\.gif|\.jpg|\.png)([\?].*|$)');
			var ext = RegExp.$1;
			var search = (isActive && focus) ? active + ext : ext;
			var replace = (isActive && focus) ? focus + ext : hover + ext;
			var out = this.src;
			var over = this.src.replace(search, replace);
			new Image().src = over;
			$(this).bind('mouseout', function() {
				this.src = out;
			}).bind('mouseover', function() {
				this.src = over;
			});
		});
	},
	// カレントページ
	gNaviCurrent: function(){
		var uri = location.href;
		var target='';
		if((/\/about\//i).test(uri)){
			target='#gNavi01';
		}else if((/\/world\//i).test(uri)){
			target='#gNavi02';
		}else if((/\/items\//i).test(uri)){
			target='#gNavi03';
		}else if((/\/shop\//i).test(uri)){
			target='#gNavi04';
		}else if((/\/company\//i).test(uri)){
			target='#gNavi07';
		}else{
		}
		if(target!=''){
			$(target).addClass('current');
		}
		
		var base = new Image();
		var link = new Image();
		var uri = location.href;
		var num=uri.lastIndexOf("/");
		var str=uri.substring(0,num+1);
		base.src= str;
		$('#sideBar .sideNavi dd a').each(function(){
			str=$(this).attr('href');
			link.src=str;
			re = new RegExp(link.src, "i");

			if(link.src==base.src){
//			if(re.test(base.src)){
//			if(link.src.match(re)){
				$(this).addClass('current');
			}
		});
	}
}


var toronama={
	init:function(){
		var self=this;
		self.gNaviCtrl();
		lib.gNaviCurrent();
		lib.rollover('.rollover','_ov','_ov');
		self.getAmount();
		self.scroller();
	},
	gNaviCtrl:function(){
		$('#gNavi .sub').stop().animate({height: "1"}, 0 );		/* IE6 overflow bug */
		
		$('#gNavi>li img,#gNavi>li img').mousemove(function(){
			$(this).parents('li').addClass('hover');
			$(this).parents('li.hover').find('.sub').stop().animate(
				{
					height: "130"
				}, 300 );
		}).mouseout(function(){
			var elm=$(this).parents('li');
			if($(this).parents('li').find('.sub').length==0){
				elm.removeClass('hover');
			}
			$(this).parents('li').find('.sub').stop().animate(
				{
					height: "1"
				}, 300,function(){elm.removeClass('hover');});
		});

		$('.sub').mousemove(function(){
			$(this).parents('li').addClass('hover');
			$(this).parents('li.hover').find('.sub').stop().animate(
				{
					height: "130"
				}, 300 );
		}).mouseout(function(){
			var elm=$(this).parents('li');
			$(this).stop().animate(
			{
				height: "1"
			}, 300,function(){elm.removeClass('hover');});
		});

	},

	getAmount:function(){
		if($('#amount').length>0){
			var rand=0;
			var timerId=setInterval(function(){
				rand += Math.floor(Math.random()*100000);
				$('#amount').html('<em>'+funcNum(rand)+'</em>円');}
			,100);
			
			setTimeout(function(){
			$.ajax({
				type: 'get',
				url: '/dat/CrCount.txt',
				dataType : 'text',
				error:function(msg){
					clearInterval(timerId);
					$('#amount').html('<em>--------</em>円');
				},
				success: function(msg){
					clearInterval(timerId);
					$('#amount').html('<em>'+msg+'</em>円');
				}
			});
			},1000);

			var now = new Date();
			var date = funcDay(now.getFullYear(), now.getMonth(), now.getDate(), -1);
			$('dd#date').html('('+ (date.getMonth()+1)*1 +'月'+date.getDate()+'日現在)');

		}

		function funcDay(y, m, d, n) {
			var dt = new Date(y, m, d);
			var myTime = dt.getTime() + (n * 1000*60*24);
			dt.setTime(myTime);
			return dt;
		}

		function funcNum(num) {
			var n = new String(num).replace(/,/g, "");
			while(n != (n = n.replace(/^(-?\d+)(\d{3})/, "$1,$2")));
			return n;
		}
	},

	scroller:function(){
		move();
		var m=0;
		var TimerId=null;
		$('#scrollCtrl p').click(function(){
			m=$(window).scrollTop();
			TimerId=setInterval(function(){
				var next=$(window).scrollTop()-($(window).scrollTop()/8);
				if(next<=m){
					m=next;
					$(window).scrollTop(next);
					if($(window).scrollTop()<4){
						$(window).scrollTop(0);
						clearInterval(TimerId);
					}
				}else{
					clearInterval(TimerId);			// リバウンド防止
				}
			}
			,1000/60);
		});

		$(window).scroll(function(){move();}).
			resize(function(){move();});

		function move(){
			var btm=$(window).scrollTop()+$(window).height()-200;
			var maxBtm=$(document).height()-420;
	
			var tgt= (btm < maxBtm) ? btm : maxBtm;	// 下限
			tgt=(tgt<515) ? 515:tgt;		// 上限
			$('#scrollCtrl').stop().animate({top: tgt+'px'},1500);

		}
		
	}
}

$(function(){
	toronama.init();
});



