// JavaScript Document
var grid = false;
var isScrolling = false;

$(document).ready(function(){
	init();
});

function init(){	


	$("#header a[href*='#']").easingScroll({
		easing:"easeOutExpo",
		duration:2000,
		delta:$("#header").height()
	});
	
	$("#footer .pagetop a[href*='#']").easingScroll({
		easing:"easeOutExpo",
		duration:2000,
		delta:$("#header").height()
	});

	subNavigation();
	pageNavigation();
		
	//Screen transition
	screenTransition();
	
	//Slide show
	slideShow();
	
	//Grid for debug
	/*$("body").append( '<div id="grid_button"><a href="#">Show Grid</a></div>');
	$('#grid_button').click(function(){
		showGrid();
		return false;
	});*/
	
}


function screenTransition(){
	var c = $.support.boxModel?navigator.appName.match(/Opera/)?"html":"html,body":"body";
	if(location.hash){
		var target = $(location.hash).offset().top - ($("#header").height());
		$(c).scrollTop(0);
		$(c).animate({scrollTop:target},1200,"easeOutExpo");	
	}else{
		$(c).scrollTop($(document).height());
		$(c).animate({scrollTop:0},2400,"easeOutExpo");
	}
}

function subNavigation(){
	if(jQuery.browser.msie){
		//for IE
		$("#navi .products").hover(
			function(){$("#navi .products_sub").show()}, 
			function(){$("#navi .products_sub").hide()} 
		);
		
		$("#navi .gallery").hover(
			function(){$("#navi .gallery_sub").show()}, 
			function(){$("#navi .gallery_sub").hide()} 
		);
		
		$("#navi .about").hover(
			function(){$("#navi .about_sub").show()}, 
			function(){$("#navi .about_sub").hide()} 
		);
		
	} else {
		//Others
		$("#navi .products").hover(
			function(){$("#navi .products_sub").stop(true,true).fadeIn(400)},
			function(){$("#navi .products_sub").stop(true,true).fadeOut(400)}
		);
		
		$("#navi .gallery").hover(
			function(){$("#navi .gallery_sub").stop(true,true).fadeIn(400)},
			function(){$("#navi .gallery_sub").stop(true,true).fadeOut(400)}
		);
		
		$("#navi .about").hover(
			function(){$("#navi .about_sub").stop(true,true).fadeIn(400)},
			function(){$("#navi .about_sub").stop(true,true).fadeOut(400)}
		);
		
	}
}

function pageNavigation(){
	var h = $("#pagenavi").height() + 36;
	$("#pagenavi").hover(
			function(){$("#pagenavi").stop(true,true).animate({top:0},1200,"easeOutExpo")},
			function(){$("#pagenavi").stop(true,true).animate({top:-h},1200,"easeOutExpo")}
	);
	
	$("#pagenavi")
	.animate({top:-h},0)
	.show()
	.animate({top:0},1200,"easeOutExpo")
	.animate({top:-h},1200,"easeOutExpo");
}

function slideShow(){
	//alert("slideShow")
	
	$('.slideshow').each(function(i){			  
		$(this).attr( 'id', "container" + i );
		//$(this).css('width',$('img', this).attr("width"));
		//$(this).css('height',$('img', this).attr("height"));
		$('li:gt(0)', this).hide();
		switchImage( this );
    });

	
	function switchImage( target ){
		setInterval(
			function(){
				$('li:first-child', target).fadeOut( 2000 )
				.next('li', target).fadeIn( 2000 )
				.end()
				.appendTo(target)},
			5000);
	}
}


function showGrid(){
	if( !grid ){
		$("#wrapper").append( '<div class="grid"></div>');
		$('#grid_button a').html("Hide Grid");
		grid = true;
	} else {
		$("#wrapper div.grid").remove();
		$('#grid_button a').html("Show Grid");
		grid = false;
	}
}


