(function () {
	S = {
		clicknoise:document.getElementById("clicknoise"),
		hovernoise:document.getElementById("hovernoise"),
		
		playclick:function(){
			//S.clicknoise.currentTime = 0;
			S.clicknoise.volume = 0.3;
			S.clicknoise.play();
		},
		playhover:function(){
			//S.hovernoise.currentTime = 0;
			S.hovernoise.volume = 0.3;
			S.hovernoise.play();
		},
	}
	F = {
		getActualDimension:function(image) {
			var run, mem, w, h, key = "actual";
		 
			// for Firefox, Safari, Google Chrome
			if ("naturalWidth" in image) {
				return {width: image.naturalWidth, height: image.naturalHeight};
			}
			if ("src" in image) { // HTMLImageElement
				if (image[key] && image[key].src === image.src) {return  image[key];}
				 
				if (document.uniqueID) { // for IE
					w = $(image).css("width");
					h = $(image).css("height");
				} else { // for Opera and Other
					mem = {w: image.width, h: image.height}; // keep current style
					$(this).removeAttr("width").removeAttr("height").css({width:"",  height:""});    // Remove attributes in case img-element has set width  and height (for webkit browsers)
					w = image.width;
					h = image.height;
					image.width  = mem.w; // restore
					image.height = mem.h;
				}
				return image[key] = {width: w, height: h, src: image.src}; // bond
			}
			 
			// HTMLCanvasElement
			return {width: image.width, height: image.height};
		},
	}
	P = {
		index:0,
		hash:null,
		prev:0,
		fadeDuration:2000,
		images:[],
		headerHeight:( $(window).height() - $("header").height() ),
		headerPosition:"def",
		animate:null,
		cont:null,
		fadeTimer:null,
		now:null,
		browser:BrowserDetect.browser,
		OS:BrowserDetect.OS,
		btnhover:function(){
			S.playhover();
			$(this).stop();
			$(this).fadeTo(500, 0.5);
		},
		btnout:function(){
			$(this).stop();
			$(this).fadeTo(500, 1.0);
		},
		navhover:function(){
			S.playhover();
			$(this).stop();
			$(this).animate( { color: '#ffffff'} , 500 );
		},
		navout:function(){
			$(this).stop();
			$(this).animate( { color: '#666666'} , 500 );
		},
		navclick:function(){
			S.playclick();
		},

		startup:function(){
			
			for( img in Preloader.imageSources ){
				P.images.push(Preloader.imageSources[img]);
			}
			
			
			var canvas = document.getElementById("bc01");
			var container = document.getElementById("background");
			if ( ! canvas || ! canvas.getContext ) { return false; }
			var context = canvas.getContext('2d');
			$(canvas).attr("width",F.getActualDimension(P.images[0]).width);
			$(canvas).attr("height",F.getActualDimension(P.images[0]).height);
			context.globalAlpha = 1;
			context.drawImage(P.images[0], 0, 0);
			
			$(container).css( { opacity:0 } );
			$(container).delay(2000).fadeTo( P.fadeDuration , 1 );
			P.onresize();
			P.onresizeBG();
			//P.crossfader();
			if($("video")){
				$("video").delay(2000).fadeTo( P.fadeDuration , 1 );
			}
			$(window).resize( P.onresize);
			$(window).resize( P.onresizeBG);
			$(window)
				.hashchange(function() {
					P.pageAction();
				});
	

			$("header").delay(10).animate( { opacity:1 } , 2000 ,"easeOutCubic"  );
			$("header nav li.news").delay(1100).animate( { opacity:1 } , 1000 ,"easeOutCubic"  );
			$("header nav li.photography").delay(1200).animate( { opacity:1 } , 1000 ,"easeOutCubic"  );
			$("header nav li.films").delay(1300).animate( { opacity:1 } , 1000 ,"easeOutCubic"  );
			$("header nav li.links").delay(1400).animate( { opacity:1 } , 1000 ,"easeOutCubic"  );
			$("header nav li.notice").delay(1600).animate( { opacity:1 } , 1000 ,"easeOutCubic"  );
			$("header nav li.biography").delay(1700).animate( { opacity:1 } , 1000 ,"easeOutCubic"  );
			$("header nav li.contact").delay(1800).animate( { opacity:1 } , 1000 ,"easeOutCubic"  );
			$(window).delay(3000).hashchange();
			P.setNav(); //hashchangeで呼ばれるからいらない
		
		},
		setNav:function(){
			$("nav li a").each( function(){
				$(this).unbind( "mouseover" , P.navhover );
				if( $(this).attr("href") == P.hash ){
					$(this).stop();
					$(this).css("color","#ffffff");
				}else{
					$(this).stop();
					$(this).css("color","#666666");
					$(this).bind( "mouseover" , P.navhover );
				}
			});
			$("nav li a").each( function(){
				$(this).unbind( "mouseout" , P.navout );
				if( $(this).attr("href") == P.hash ){
					$(this).stop();
					$(this).css("opacity","#ffffff");
				}else{
					$(this).stop();
					$(this).css("color","#666666");
					$(this).bind( "mouseout" , P.navout );
				}
			});
			$("nav li a").each( function(){
				$(this).unbind( "click" , P.navclick );
				$(this).unbind( "click" , P.clickVoid );
				if( $(this).attr("href") == P.hash ){
					$(this).bind( "click" , P.clickVoid );
				}else{
					$(this).bind( "click" , P.navclick );
				}
			});
		},
		clickVoid:function(){
			return false;
		},
		pageAction:function(){
			$("body").scrollTop(0);
			location.hash.replace('#', '');
			P.hash = location.hash;
			
			if( P.hash != P.now ){
				P.setNav();
				
				
				var oldpos = P.headerPosition;
				switch( P.hash ){
					case "#news":
						P.cont = new ContNews();
						$("#container").delay(10).animate( { opacity:0 }, 500  ,"easeOutCubic" ,function(){
							$("#container").load("./?cat=3 #content" ,{} , P.contLoaded);
						});
						P.restartFade();
						P.headerPosition = "bottom";
					break;
					case "#photography":
						P.cont = new ContPhotography();
						$("#pane").css("opacity",0);
						$("#container").delay(10).animate( { opacity:0 }, 500  ,"easeOutCubic" ,function(){
							$("#container").load("./?cat=4 #content" ,{} , P.contLoaded);
						});
						P.headerPosition = "bottom";
					break;
					case "#films":
						P.cont = new ContFilms();
						$("#pane").css("opacity",0);
						$("#container").delay(10).animate( { opacity:0 }, 500  ,"easeOutCubic" ,function(){
							$("#container").load("./?cat=6 #content" ,{} , P.contLoaded);
						});
						P.headerPosition = "bottom";
					break;
					case "#links":
						P.cont = new ContLinks();
						$("#container").delay(10).animate( { opacity:0 }, 500  ,"easeOutCubic" ,function(){
							$("#container").load("./?page_id=152 #content" ,{} , P.contLoaded );
						});
						P.restartFade();
						P.headerPosition = "bottom";
					break;
					case "#notice":
						P.cont = new ContNotice();
						$("#container").delay(10).animate( { opacity:0 }, 500  ,"easeOutCubic" ,function(){
							$("#container").load("./?page_id=1851 #content" ,{} , P.contLoaded );
						});
						P.restartFade();
						P.headerPosition = "bottom";
					break;
					case "#biography":
						P.cont = new ContBiography();
						$("#container").delay(10).animate( { opacity:0 }, 500  ,"easeOutCubic" ,function(){
							$("#container").load("./?page_id=16 #content" ,{} , P.contLoaded );
						});
						P.restartFade();
						P.headerPosition = "bottom";
					break;
					case "#contact":
						P.cont = new ContContact();
						$("#container").delay(10).animate( { opacity:0 }, 500  ,"easeOutCubic" ,function(){
							$("#container").load("./?page_id=155 #content" ,{} , P.contLoaded );
						});
						P.restartFade();
						P.headerPosition = "bottom";
					break;
					default:
						//P.headerPosition = "top";
						P.restartFade();
					break;
				}
				if(P.hash == "#photography"){
			var video = $("#video").get(0);
					if(video){
						video.pause();
						$(video).animate( { opacity:0 }, 500 ,"easeOutCubic");
					}
					//$("#bc,#bc01,#bc02").animate( { opacity:1 }, 1000 ,"easeOutCubic");
				}else if(P.hash == "#films"){
					$("#bc").animate( { opacity:0 }, 500 ,"easeOutCubic");
					$("#bc01").animate( { opacity:0 }, 500 ,"easeOutCubic");
					$("#bc02").animate( { opacity:0 }, 500 ,"easeOutCubic");
					
				}else{
					var video = $("#video").get(0);
					if(metavalues){
						if(video){
							if(BrowserDetect.browser != "Chrome"){
							video.load();
							}
							video.play();
							$(video).animate( { opacity:1 }, 500 ,"easeOutCubic");
						}
					}else{
						if(video){
							video.pause();
							$(video).animate( { opacity:0 }, 500 ,"easeOutCubic");
						}
					}
				}
				if(P.headerPosition == "bottom" && oldpos != "bottom"){
					$("header").stop();
					$("header").animate( { opacity:0 } , 500 ,"easeOutCubic" , function(){
						P.onresize();
						$("header").animate( { opacity:1 } , 500 ,"easeOutCubic" );
					} );
				}
				P.now = P.hash;
				
			}
			return false;
		},
		stopFade:function(){
			if(P.fadeTimer) clearInterval(P.fadeTimer);
		},
		restartFade:function(){
			if(P.fadeTimer) clearInterval(P.fadeTimer);
			var canvas = document.getElementById("bc");
			var context = canvas.getContext('2d');
			$("#bc").delay(10).animate( { opacity:0 }, 500 ,"easeOutCubic",function(){
				context.clearRect(0, 0, canvas.width, canvas.height);
				$("#bc").hide();
			});
			$("#bc01").show();
			$("#bc02").show();
			P.crossfader();
		},
		contLoaded:function(){
			if(P.cont) P.cont.preset();
			$("#container").delay(10).animate( { opacity:1 }, 500  ,"easeOutCubic" ,function(){
				if(P.cont) P.cont.initialize();
			});
		},
		crossfader:function(){
			P.fadeTimer = setInterval(function() {
				P.crossfade();
			}, 5000);
  		},
		crossfade:function(){
			if(P.images.length > 1){
				P.index++;
				if(!P.images[P.index]) P.index = 0;
				P.prev = P.index-1;
				if(!P.images[P.prev]) P.prev = P.images.length - 1;
				var canvas01 = document.getElementById("bc01");
				var context01 = canvas01.getContext('2d');
				context01.clearRect(0, 0, canvas01.width, canvas01.height);
				$(canvas01).attr("width",F.getActualDimension(P.images[P.index]).width);
				$(canvas01).attr("height",F.getActualDimension(P.images[P.index]).height);
				context01.drawImage(P.images[P.index], 0, 0);
				$(canvas01).css( { opacity:0 } );
				$(canvas01).animate( { opacity:1 }, P.fadeDuration ,"easeOutCubic" );
				
				var canvas02 = document.getElementById("bc02");
				var context02 = canvas02.getContext('2d');
				context02.clearRect(0, 0, canvas02.width, canvas02.height);
				$(canvas02).attr("width",F.getActualDimension(P.images[P.prev]).width);
				$(canvas02).attr("height",F.getActualDimension(P.images[P.prev]).height);
				context02.drawImage(P.images[P.prev], 0, 0);
				$(canvas02).css( { opacity:1 } );
				$(canvas02).animate( { opacity:0 }, P.fadeDuration ,"easeOutCubic" );
			}
			P.onresizeBG();
			
  		},
		loading:function(){
			if( document.getElementById("overlay") ){
				$("#overlay").animate( { opacity:0 }, 500 ,"easeOutCubic", function(){
					$("#overlay").remove();
				});
			}else{
				var div = $(document.createElement("div"));
				div.attr("id","overlay");
				div.css( {
					"background":"#000 url(./media/images/spinner.gif) center center no-repeat" ,
					"width":"100%" ,
					"height":"100%" ,
					"z-index":1000 ,
					"position":"absolute" ,
					"opacity":0
				} );
				$("body").append(div);
				$("#overlay").delay(10).animate( { opacity:0.8 }, 500 ,"easeOutCubic" );
			}
		},
		loaded:function(){
			if( document.getElementById("overlay") ){
				$("#overlay").delay(10).stop().animate( { opacity:0 }, 500 ,"easeOutCubic", function(){
					$("#overlay").remove();
				});
			}
		},
		onresize:function(){
			P.headerHeight = ( $(window).height() - $("header").height() );
			
			
			if( P.headerPosition == "bottom" ){
				$("header").css( "background-color" , "rgba(0,0,0,0.5)" );
				$("header").css( "top" , P.headerHeight + 'px' );
			}else if( P.headerPosition == "top" ){
				$("header").css( "top" , '0px' );
				$("header").css( "background-color" , "rgba(0,0,0,0.0)" );
			}
			
			$("#container").height(P.headerHeight);
		},
		onresizeBG:function(){
			var canvas = document.getElementById("bc01");
			
			$(canvas).width( $(window).width() );
			var scaleX = $(canvas).width() / F.getActualDimension(P.images[P.index]).width;
			$(canvas).height( F.getActualDimension(P.images[P.index]).height*scaleX );
			
			if( $(canvas).height() < $(window).height() ){
				$(canvas).height( $(window).height() );
				var scaleY = $(canvas).height() / F.getActualDimension(P.images[P.index]).height;
				$(canvas).width( F.getActualDimension(P.images[P.index]).width*scaleY );
				
			}
			$(canvas).css( { "top":( $(window).height() - $(canvas).height() ) / 2 , "left":( $(window).width() - $(canvas).width() ) / 2 } );
			
			
			var canvas = document.getElementById("bc02");
			var scaleX = $(canvas).width() / F.getActualDimension(P.images[P.prev]).width;
			$(canvas).height( F.getActualDimension(P.images[P.prev]).height*scaleX );
			
			if( $(canvas).height() < $(window).height() ){
				$(canvas).height( $(window).height() );
				var scaleY = $(canvas).height() / F.getActualDimension(P.images[P.prev]).height;
				$(canvas).width( F.getActualDimension(P.images[P.prev]).width*scaleY );
				
			}
			$(canvas).css( { "top":( $(window).height() - $(canvas).height() ) / 2 , "left":( $(window).width() - $(canvas).width() ) / 2 } );
			
			var video = $("#video").get(0);
			
			$(video).width( $(window).width() );
			var scaleX = $(video).width() / P.videoWidth;
			$(video).height( P.videoHeight*scaleX );
			
			if( $(video).height() < $(window).height() ){
				$(video).height( $(window).height() );
				var scaleY = $(video).height() / P.videoHeight;
				$(video).width( P.videoWidth*scaleY );
				
			}
			$(video).css( { "top":( $(window).height() - $(video).height() ) / 2 , "left":( $(window).width() - $(video).width() ) / 2 } );
			
		},
		videoWidth:$("video").width(),
		videoHeight:$("video").height()

			
	
	};
	Preloader = {
		assetsComplete:false,
		contentsComplete:false,
		finished:false,
		complete:false,
		assetsLoaded:0,
		assetsToLoad:0,
		loadfiles:backgroundfiles,
		imageSources:[],
		animation:{
			initialize:function(){
				
			},
			activate:function(){
				
			}

		},
		addAssetToPreloadQueue:function(asset){
			asset.load(Preloader.onAssetLoaded);
			asset.error(Preloader.onAssetLoaded);
		},
		onAssetLoaded:function( event ){
			if(++Preloader.assetsLoaded >= Preloader.assetsToLoad){
				Preloader.onAllAssetsLoaded();
			};
			Preloader.updateMeter();
		},
		onAllAssetsLoaded:function(){
			if(!Preloader.assetsComplete && Preloader.assetsLoaded >= Preloader.assetsToLoad ){
				Preloader.assetsComplete = true;
				Preloader.loaded();
			}
			Preloader.updateMeter();
		},
		updateMeter:function(){
			var segmentsTotal = Preloader.assetsToLoad;
			var segmentsComplete = Preloader.assetsLoaded;
			if(Preloader.contentsComplete){
				segmentsComplete++;
			}
			var progress = Math.min(segmentsComplete/segmentsTotal,1);
			$("#preloader .progress .fill").width(progress*$("#preloader .progress").width());
			$("#percent").html(Math.round(progress*100) + "%")
			if( progress >= 1 ){
				Preloader.complete = true;	
			}
		},
		onAllAssetsLoaded:function(){
			if( !Preloader.assetsComplete && Preloader.assetsLoaded >= Preloader.assetsToLoad ){
				Preloader.assetsComplete = true;
				Preloader.finish();
			}
			Preloader.updateMeter();
		},
		finish:function(){
			if( Preloader.contentsComplete && Preloader.assetsComplete && Preloader.complete && !Preloader.finished ){
				$("#preloader").delay(10).fadeTo(500 , 0 , Preloader.onComplete );
			}
			Preloader.updateMeter();
		},
		onComplete:function(){
			$("#preloader").stop();
			$(this).remove();
			Preloader.finished = true;
			P.startup();
			$(Preloader).remove();
		},
		onContentsLoaded:function(){
			if( !Preloader.contentsComplete ){
				Preloader.contentsComplete = true;
				Preloader.finish();
			}
			Preloader.updateMeter();
		},
		initialize:function(){
			//Preloader.assetsToLoad = Preloader.loadfiles.length + 1;
			Preloader.animation.initialize();
			
			//Preloader.animation.activate();
			Preloader.assetsToLoad = Preloader.loadfiles.length;
			$(Preloader.loadfiles).each( function ( i ){
				Preloader.imageSources[i] = new Image();
				Preloader.imageSources[i].src = Preloader.loadfiles[i];
				Preloader.addAssetToPreloadQueue( $(Preloader.imageSources[i]) );
			} );
			$("#preloader").show();
			$("#preloader .contents").css({"opacity":0})
			$("#preloader .contents").delay(0).animate({
				opacity:1
			},100);
		},
	}
	
	Preloader.initialize();

})(jQuery);
$( function(){
	$(document).bind("touchmove", function()
	{
		event.preventDefault();
	});
	Preloader.onContentsLoaded();
} );

//moveTo(0,0);
//resizeTo(screen.width ,screen.height);

function fakeClick(fn) {
	var $a = $('<a href="#" id="fakeClick"></a>');
		$a.bind("click", function(e) {
			var video = $("#video").get(0);
			video.play();
		});

	$("body").append($a);

	var evt, 
		el = $("#fakeClick").get(0);

	if (document.createEvent) {
		evt = document.createEvent("MouseEvents");
		if (evt.initMouseEvent) {
			evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
			el.dispatchEvent(evt);
		}
	}

	$(el).remove();
}


