var timer;

/*-- def easing --*/
	jQuery.extend(jQuery.easing,
	{
		easeInQuart: function (x, t, b, c, d) {
			return c*(t/=d)*t*t*t + b;
		},
		easeInBack: function (x, t, b, c, d, s) {
			if (s == undefined) s = 1.70158;
			return c*(t/=d)*t*((s+1)*t - s) + b;
		},
		easeOutElastic: function (x, t, b, c, d) {
			var s=1.70158;var p=0;var a=c;
			if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
			if (a < Math.abs(c)) { a=c; var s=p/4; }
			else var s = p/(2*Math.PI) * Math.asin (c/a);
			return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
		},
		easeOutBack: function (x, t, b, c, d, s) {
			if (s == undefined) s = 1.70158;
			return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
		}
	});
/*-- //def easing --*/

jQuery(document).ready(function() {

	if(navigator.userAgent.indexOf('iPhone') > 0 || navigator.userAgent.indexOf('Android') > 0){
		jQuery('body').css('fontSize','3em');
	}

	soundPlay();
	
	//flick loading
	var s = true;	
	timer = setInterval(function(){
		if(s == true){
			jQuery('#load').hide();
			s = false;
		} else {
			jQuery('#load').show();
			s = true;
		}
	}, 50);
 
  //bg effects  
	$thumb = jQuery('.m a img');
	$index_meta = jQuery('.index_meta');
	$index_meta.hover(
  	function(){
  		jQuery(this).css({backgroundImage:'url(http://www.magma-web.jp/wp-content/themes/magma-web/_img/post_back.gif)'});
  		jQuery.sound.play("http://www.magma-web.jp/wp-content/themes/magma-web/_sound/post_over.wav");
  	},
  	function(){
  		jQuery(this).css('backgroundImage','none');
  });
  
  //menu effects
  $menu = jQuery('#menu ul li.s');
  $menu.hover(
  	function(){
   		jQuery(this).animate({"marginTop": "-53px"}, 100);
  	},
  	function(){
  		jQuery.sound.play("http://www.magma-web.jp/wp-content/themes/magma-web/_sound/menu_out.wav");
   		jQuery(this)
				.animate({opacity: 1,"marginTop": "-35px"}, 200, "easeInBack")
				.animate({"marginTop": "-48px"}, 300, "easeOutBack");
  	}
  );
  
  //for single page
  jQuery('.content p img').each(
  	function(i){jQuery(this).attr({'class': 'list'}); }
  );
  jQuery('.content p .list').wrap('<div class="left imgs"></div>');
  $imgs = jQuery('.content p div');
  var imgLength = $imgs.length;
  
  var pWidth = 0;
  var margin = 83;
  $imgs.each(function(i){
    jQuery(this).attr({'id': i+1});
    var img = jQuery(this).children('.list');
    var w = img.width();
    pWidth += w + margin;
    var n;
    if(i == imgLength-1) n = 0;
    else n = i+2;
    img.wrap('<a href="#' + n + '"></a>');
  });
  jQuery('.content p').css('width', pWidth + margin*2);
  	
	jQuery("#content").easySlider();
	
	
	//ticker
	jQuery('#ticker #ticker_inner_right').toggle(
		function(){
			jQuery(this).parent().animate({"left": "0"}, 500, "easeInQuart")
		},
		function(){
			jQuery(this).parent().animate({"left": "-451px"}, 500, "easeInQuart")
		}
	);
	
	//icon over action
	jQuery('#content_footer ul li ul li.icon').hover(
		function(){
			jQuery(this).animate({"paddingTop": "0"}, 100);
		},
		function(){
			jQuery(this).animate({"paddingTop": "3px"}, 100);
		}
	);

	jQuery('.magma').animate({"marginTop": "0", opacity: 1}, 1000);
	
	jQuery('.content p div a, #content_magma a').mouseup(function(){
		jQuery.sound.play("http://www.magma-web.jp/wp-content/themes/magma-web/_sound/content_slide.wav");
	});
	
	//center
	var fitHworksContent = new resizeH(jQuery('#content'));
	var fitHworksContent = new resizeH(jQuery('#content_wrapper_single'));
	var fitHworksContent = new resizeH(jQuery('#error'));
});



jQuery(window).load(function(){

	clearInterval(timer);
	jQuery('#load').hide();

	//intro effects
	$menu = jQuery('#menu ul li.item');
	$news = jQuery('.post');
	$n = jQuery('.post .index_meta a img');
	jQuery('#header_title').animate({opacity: 1}, 2000, "easeOutBack");
	
	function fade(num){
		var t = setTimeout(function(){fade(num);}, 150);
		jQuery($menu.get(num))
			.animate({opacity: 1,"marginTop": "-10px"}, 800, "easeInBack")
			.animate({"marginTop": "-48px"}, 1000, "easeOutElastic");
		num++;
		if(num == $menu.length){
			clearTimeout(t);
			news_fade(0);
		}
	}
	function news_fade(nn){
		var tt = setTimeout(function(){news_fade(nn);}, 100);
		jQuery($news.get(nn)).animate({opacity: 1}, 1000);
		nn++;
		if(nn == $news.length){
			clearTimeout(tt);
			news_mov(0);
		}
	}
	function news_mov(nnn){
		var nt = setTimeout(function(){news_mov(nnn);}, 100);
		jQuery($n.get(nnn))
			.animate({opacity: 0.1}, 200)
			.animate({opacity: 1}, 50);
		nnn++;
		if(nnn == $n.length){
			clearTimeout(nt);
		}
	}
	fade(0);
	
	$single_imgs = jQuery('.content p div a');
	$icons = jQuery('#content_footer ul li ul li.icon');
	function img_fade(num){
		var tm = setTimeout(function(){img_fade(num);}, 300);
		jQuery($single_imgs.get(num)).animate({opacity: 1,"paddingLeft": "80px"}, 1000);
		num++;
		if(num == $single_imgs.length){
			clearTimeout(tm);
			icon_fade(0);
		}
	}
	function icon_fade(nm){
		var tmtm = setTimeout(function(){icon_fade(nm);}, 100);
		jQuery($icons.get(nm)).animate({opacity: 1}, 50);
		nm++;
		if(nm == $icons.length){
			clearTimeout(tmtm);
		}
	}
	img_fade(0);

});





//adjusting #content_single to the middle
var resizeH = function(ob){
	var bodyHeight;
	var divHeight;
	
		bodyHeight = jQuery(window).height();
		divHeight = jQuery(ob).height();
		headerHeight = jQuery('.header_wrapper').height();
		jQuery(ob).css('paddingTop', (bodyHeight-divHeight+25)*0.5);

	var resizeTimer = null;
	jQuery(window).bind('resize', function() {
		if (resizeTimer) clearTimeout(resizeTimer);
		resizeTimer = setTimeout(function(){
			bodyHeight = jQuery(window).height();
			divHeight = jQuery(ob).height();
			var pdTop = (bodyHeight-divHeight+25)*0.5;
			jQuery(ob).css('paddingTop', pdTop);
			if(ob == jQuery('#content_wrapper_single')){
				jQuery('#ticker').css('paddingTop', pdTop);
			}
		}, 10);
	});
}



