//create array
var allContent = new Array();
var current = allContent.indexOf(getNearestElement());

function init(){
	allContent = new Array();
	jQuery.each( $('.post'), function(){
		allContent.push(this);
		$(this).fadeTo(0, 0.1);
		this.isShow = false;
		var imgs = $(this).find("img");
		for ( var j=0; j<imgs.size(); j++){
			allContent.push( imgs.get(j) );
			$(imgs.get(j)).fadeTo(0, 0.1);
			imgs.get(j).isShow = false;
		}
	});
	if($('.navigation')) allContent.push($('.navigation')[0]);
	changeAlpha();
}

function getNearestElement(){
	var sTop = $(window).scrollTop();
	var dist =0;
	var pDist=10000000000;
	var cCurrent;
	jQuery.each( allContent, function(){
		dist = Math.abs(sTop - this.offsetTop);
		if(dist<pDist) {
			pDist = dist;
			cCurrent = this;
		}
	});
	return cCurrent;
}


function changeAlpha(){
	cObj = getNearestElement();
	jQuery.each( allContent, function(){
		if(this == cObj){
			if(!this.isShow){
				$(this).stop();
				//$(this).fadeTo(500, 1.0);
				this.isShow = true;
				if($(this).is("img")){
					$(this).fadeTo(500, 1.0).animate({boxShadow: '0 0 40px #303030'}, 100);
				}else{
					$(this).fadeTo(500, 1.0);
				}
			}
		}else if( this!=cObj){
			
			if(jQuery.contains(this, cObj)){ // for div.post
				if(!this.isShow){
					$(this).stop();
					$(this).fadeTo(500, 1.0);
					this.isShow = true;
				}
			}else {
				if(this.isShow){
					$(this).stop();
					if($(this).is('div')){
						$(this).fadeTo(500, 0.30);
					}else{
						$(this).animate({boxShadow: '0 0 0 #303030'}, 0);
						$(this).fadeTo(500, 0.10)
					}
					this.isShow = false;
				}
			}
		}
	})
}



jQuery(document).ready(function(){
	
	////accordion
	//styles
	$('#header').css('position', 'fixed');
	$('#sidebar').css('position', 'fixed');
	 
	var handler = $('#sidebar h3');
    var menus = $('#sidebar div');
    
    menus.css('display', 'none');
    
    handler.click(function(){
        var _thisMenu = $(this).next();
        if(_thisMenu.is(':visible')){
	        _thisMenu.show();
        } else {
        	menus.slideUp();
	        _thisMenu.slideDown();
        }
    });

	//wp autopagerize
//	$(".navigation").css('display', 'none');
	
	//lazy load
	/*
	$("img").lazyload({
		threshold: 200,
		placeholder: "http://eiji.muroichi.info/wp-content/themes/eiji2/images/transparent.gif",
		effect: "fadeIn"
		
	});
	*/
	
	//adjust size
	$('#content').css('padding-bottom', $(window).height() - 185);
	$(window).resize( function(){
		$('#content').css('padding-bottom', $(window).height() - 185);
	})
	
	//space bar scrolling navigation
	$(window).keypress(function(e){
		
		if(e.keyCode==32){
			var duration = 250;
			
			var cObj = getNearestElement();
			if(current!=allContent.length-1) current = allContent.indexOf(cObj);
			var targetTop;
			if(!e.shiftKey ){
				if (current<allContent.length-1) {
					current++;
					targetTop = allContent[current].offsetTop;
					$(window).scrollTo({top:targetTop, left:0},duration);
				}else if(current==allContent.length-1){
					if($('.navigation .next a')[0]){
						location.href = $('.navigation .next a').attr('href');
					}
				}
			}else if(e.shiftKey){
				if(current>0){
					current--;
					targetTop = allContent[current].offsetTop;
					$(window).scrollTo({top:targetTop, left:0},duration);
				}else if(current==0){
					if($('.navigation .previous a')[0]){
						location.href = $('.navigation .previous a').attr('href');
					}
				}
			}
			
			event.returnValue = false;
			
		}
	});
	
	$(window).scroll( changeAlpha );
	
	init();

});
