var timer;

jQuery(document).ready(function() {

	soundPlay();
	
	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');
	$index_meta = jQuery('.index_meta');
	$bgs = jQuery('.bgs');
	$thumb.hover(
  	function(){
  		var index = $thumb.index(this);
      $bgs.eq(index).show();
  		jQuery('#supersize').fadeIn(0);
  		$index_meta.not($index_meta.get(index)).fadeTo(0, 0.5);
  		$thumb.not(this).fadeTo(0, 0.5);
  	},
  	function(){
      $bgs.hide();
  		$index_meta.fadeTo(0, 1);
  		$thumb.fadeTo(0, 1);
  });
	$index_meta.hover(
  	function(){
  		var index = $index_meta.index(this);
      $bgs.eq(index).show();
  		jQuery('#supersize').fadeIn(0);	  		
  		$index_meta.not(this).fadeTo(0, 0.5);
  		$thumb.not($thumb.get(index)).fadeTo(0, 0.5);
  	},
  	function(){
      $bgs.hide();
  		$index_meta.fadeTo(0, 1);
  		$thumb.fadeTo(0, 1);
  });
  
  /*jQuery('#test .post').hover(
  	function(){
  		jQuery.sound.play("http://tymote.sakura.ne.jp/tymote_jp/wp-content/themes/tymote_jp/_sound/simple_click.wav");
  	},
  	function(){
  		return;
  });*/

  //menu effects
  $menu = jQuery('#menu ul li.s');
  $menu.hover(
  	function(){
  		jQuery.sound.play("http://tymote.sakura.ne.jp/tymote_jp/wp-content/themes/tymote_jp/_sound/simple_click.wav");
  		jQuery(this).children('img').show();
  		jQuery(this).children('img').fadeTo(0, 0);
   		jQuery(this).children('img').animate({"paddingTop": "10px", opacity : 1}, 400);
  	},
  	function(){
   		jQuery(this).children('img').animate({"paddingTop": "20px", opacity : 0}, 0);
  		$menu.children('img').hide();
  	}
  );
  
  //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 = 50
  $imgs.each(function(i){
    jQuery(this).attr({'id': i+1});
    var w = jQuery(this).children('.list').width();
    pWidth += w + margin; 
  });
  jQuery('.content p').css('width', pWidth + margin);
  
  var links = '';
  for(var i=1; i<imgLength+1; i++){
  	links += '<li><a href="#'+ i +'">'+ i +'</a></li>';
  }
  //links += '<li><a href="#credit">credit</a></li>';

  
  jQuery('.info').after('<div id="content_footer"><ul>'+ links +'</ul></div>');
  
  
	jQuery("#content").easySlider();
  jQuery('#supersize').supersized(); 

	var fitHworksContent = new resizeH(jQuery('#content'));
	var fitHworksContent = new resizeH(jQuery('#content_wrapper_single'));
	//adjustHeight(jQuery('#content_wrapper_single'));//ajusting #content_single to the middle
});



jQuery(window).load(function(){

	clearInterval(timer);
	jQuery('#load').hide();
	
	jQuery('#logo').hover(
		function(){
			var s = true;	
			timer = setInterval(function(){
				if(s == true){
					jQuery('#logo').fadeTo(0, 1);
					s = false;
				} else {
					jQuery('#logo').fadeTo(0, 0.1);
					s = true;
				}
			}, 50);
		},
		function(){
			clearInterval(timer);
			jQuery('#logo').fadeTo(0, 1);
		}
	);
	



	//intro effects
	jQuery('.m img').fadeIn(500);
	jQuery('#logo, #menu ul li ul').fadeIn(800);
	jQuery('.index_meta').show();
	jQuery('.index_meta').fadeTo(0, 0);
  setTimeout(function(){
		jQuery('#supersize').hide();
		jQuery('.index_meta').animate({
			opacity: 1,
			paddingLeft: "0"
		}, 800);
  }, 500);
  setTimeout(function(){
		jQuery('#cntrl, #music').fadeIn(500);
  }, 1000);
});





//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();	
			jQuery(ob).css('paddingTop', (bodyHeight-divHeight+25)*0.5);
		}, 10);
	});
}


//popup window for comic
function popWin(pUrl){
	var mWinName = 'tymote.music';
	var mOption = 'width=200, height=600, location=no, menubar=no, toolbar=no, scrollbar=yes, resizable=yes, status=no, directories=no, top=0, left=0';
	window.open(pUrl, mWinName, mOption);
}