﻿var mouseX = 0,mouseY = 0;
var outDelay = 200, inDelay = 300;
var nMax = 90, nMin = 30, loadByte = Math.floor(Math.random()*(nMax-nMin+1))+nMin;
var gflag = 0;
var isIE = (navigator.appName.toLowerCase().indexOf('internet explorer')+1?1:0);

/*Loader*/
$(function()
{
	$(document).one("ready", function()
	{
		$("body").append("<div id=\"loading\"><div id=\"loader\"></div><p><img src=\"images/logo_about.png\" alt=\"now Loading\" /></p><h2>GeluBde</h2></div>");
		$("#loading p img").load(function(){
			$("#wrapper").fadeIn(600);
			$("#loader").delay(300).animate({width:loadByte + '%'}, 600, "easeOutQuint");
		});
	});
});
$(window).load(function()
{
	$("#loader").animate({width:"100%"}, 300, "easeOutQuint", function()
	{
		$("#loading").delay(300).fadeOut(600, function(){$("#loading").remove();});
	});
	$("header").delay(inDelay).fadeIn(600);
});

/*HashChange*/
$(function(){
	$(window).hashchange(function(){
		
		var hash = location.hash;
		if(hash != ""){
			document.title = hash.replace( '#', '' ) + ' | GeluBde';
		} else {
			document.title = 'GeluBde';
		}

		var page = ["#top","#memo","#about","#gallery","#bookmark"];
		
		$(page[0]).each( function(){

			if(hash == "")
			{	
				pageChange(0);
			}
			
		});

		$(page[1]).each( function(){

			if(hash == "#MEMO")
			{
				pageChange(1);
			}
			
		});
		
		$(page[2]).each( function(){

			if(hash == "#ABOUT")
			{
				pageChange(2);
			}
			
		});
		
		$(page[3]).each( function(){

			if(hash == "#GALLERY")
			{
				pageChange(3);

				if(gflag==0){
					gflag = 1;
					gallery();
				}
			}
			
		});
		
		$(page[4]).each( function(){

			if(hash == "#BOOKMARK")
			{
				pageChange(4);
			}
			
		});

		function pageChange(p){

			if(page!=3){clearInterval(siv),(siv = null);}

			if(p!=0){$(page[0]).fadeOut(outDelay);}
			if(p!=1){$(page[1]).fadeOut(outDelay);}
			if(p!=2){$(page[2]).fadeOut(outDelay);}
			if(p!=3){$(page[3]).fadeOut(outDelay);}
			if(p!=4){$(page[4]).fadeOut(outDelay);}

			if(p==0){$("#header h1").hide();}
			else{$("#header h1").delay(inDelay).fadeIn(600);}

			$(page[p]).delay(inDelay).fadeIn(600);
		}

	});
	$(window).hashchange();
});

//GALLERY

var count;
var siv;

function gallery(){
	
	var n = 1;//はじめに表示させる画像ナンバー
	var imgArray = [];
	var imgSource = $("#imgList li a.imgView");
	var activeImg = imgSource.eq(n);
	var imgFadeIn = 500, imgFadeOut = 300;
	var imgMaxHeight = 550;
	var imgWidth,imgHeight,marginTop, wh=$(window).height(),hh=$("#headerWrap").height(),fh=$("footer").height();
	//var ilw = parseInt( $("#imgList").width() / ( $("#imgList li a").width() + $("#imgList li a").outerWidth(true) + 2 ) );
	//var ilh=( $("#imgList li a").height() + $("#imgList li a").outerHeight(true) )*( parseInt($("#imgList li a").size() / ilw) + 1);
	var usedArea = hh + fh;
	
	//画像を配列に追加
	for( var i=0; i < imgSource.length; i++ ){
		imgArray.push(new Array(imgSource[i].getAttribute("href"),imgSource[i].getAttribute("title")));
		//imgArray[画像ナンバー][0=title,1=href]
	}
	
	//画像リストアイコンの透明度
	$("#imgList").hover(function(){
		$(this).stop(true,true).fadeTo(300, 1);
	},
	function(){
		$(this).stop(true,true).delay(2400).fadeTo(600, 0.1);
	});


	//自動画像切り替え
	function imgAutoHandler(flag){
		count=flag;
		siv = setInterval(function(){
			count--;
			if(count==0){
				imgAutoChange();
			}
		},3000);
	}
	function imgAutoChange(){
		if(n == imgSource.length - 1){
			n = 0;
			activeImg = imgSource.eq(n);
			imgLoad(activeImg);
		}else{
			n += 1;
			activeImg = imgSource.eq(n);
			imgLoad(activeImg);
		}
	}
	
	//htmlの設定
	var changeColor = "#000";//表示中の画像アイコンの枠色
	var imgBox = $("#imgArea");
	imgBox.append("<p id=\"imgTitle\"></p><img id=\"imgObject\" />");
	var imgTarget = $("#imgArea #imgObject");
	var titleTarget = $("#imgArea p#imgTitle");
	
	
	var imgTrueSize = function(image){
		var w = image.width ,
			h = image.height ;
	 
		if ( typeof image.naturalWidth !== 'undefined' ) {  // for Firefox, Safari, Chrome
			w = image.naturalWidth;
			h = image.naturalHeight;
	 
		} else if ( typeof image.runtimeStyle !== 'undefined' ) {    // for IE
			var run = image.runtimeStyle;
			var mem = { w: run.width, h: run.height };  // keep runtimeStyle
			run.width  = "auto";
			run.height = "auto";
			w = image.width;
			h = image.height;
			run.width  = mem.w;
			run.height = mem.h;
	 
		} else {         // for Opera
			var mem = { w: image.width, h: image.height };  // keep original style
			image.removeAttribute("width");
			image.removeAttribute("height");
			w = image.width;
			h = image.height;
			image.width  = mem.w;
			image.height = mem.h;
		}
	 
		return {width:w, height:h};
	}
	
	function hc()
	{
		imgHeight = (wh - usedArea)*0.85;
		if(imgHeight>imgMaxHeight){imgHeight=imgMaxHeight;}
		marginTop = (wh - usedArea - imgHeight)/3;

		return {height:imgHeight, mt:marginTop};
	}

	$(window).resize(function()
	{
		wh = $(window).height();
		imgTarget.stop(false,false).animate({height:hc().height, marginTop:hc().mt}, 600, "easeOutQuint");
		$("#imgList").stop().animate({bottom:hc().mt/2}, 600, "easeOutQuint");
	});
	
	function imgLoad(activeImg)
	{
		
		//アイコン枠色変更
		$(imgSource).css({borderColor:"#fff"});
		$(activeImg).css({borderColor:changeColor});

		if(isIE){
			imgTarget.css({opacity:"0"})
			.attr({
				src:imgArray[n][0],
				title:imgArray[n][1],
				alt:imgArray[n][1]
			});
			IE();
			function IE(){
				imgTarget.stop().animate({opacity:"1"},{queue:false, duration:imgFadeIn/2, easing:"easeInCubic", complete:function(){
					clearInterval(siv),(siv = null);
					imgAutoHandler(2);
				}})
				.animate({height:hc().height, marginTop:hc().mt},
				imgFadeIn, "easeOutQuint", function(){
				});
				titleTarget.hide().text(imgArray[n][1]).fadeIn(imgFadeIn);
			}
		}
		else{
			imgTarget.stop(false,false).animate({opacity:"0"},{duration:imgFadeOut, easing:"easeInCirc", complete: function()
			{
				imgTarget.attr({
					src:imgArray[n][0],
					title:imgArray[n][1],
					alt:imgArray[n][1]
				}).load(function(){
					$(this).stop().animate({opacity:"1"},{queue:false, duration:imgFadeIn/2, easing:"easeInCubic", complete:function(){
						
						clearInterval(siv),(siv = null);
						imgAutoHandler(2);
						titleTarget.text(imgArray[n][1]).fadeIn(imgFadeIn);
					}})
					.animate({height:hc().height, marginTop:hc().mt},
					imgFadeIn, "easeOutQuint", function(){
					});
				});
				titleTarget.hide();
			}
			});
		}
	}
	imgLoad();

	imgSource.bind("click", function()
	{
		var clickedObj = this;
		
		imgSrc = $(clickedObj).attr("href");
		//imgTitle = $(clickedObj).attr("title");
		
		//現在表示している画像とは別かを判定
		if( imgSrc != imgArray[n][0] /*&& imgTitle != imgArray[n][1]*/ )
		{
			n = imgSource.index(clickedObj);//何番目の要素か調べて代入
			imgLoad(clickedObj);
		}
		
		return false;
	});
	
	///*画像クリックでのページ送り
	/*function getWPoint()
	{
		var isWin9X = (navigator.appVersion.toLowerCase().indexOf('windows 98')+1);
		var isIE = (navigator.appName.toLowerCase().indexOf('internet explorer')+1?1:0);
		var isOpera = (navigator.userAgent.toLowerCase().indexOf('opera')+1?1:0);
		if (isOpera) isIE = false;
		var isSafari = (navigator.appVersion.toLowerCase().indexOf('safari')+1?1:0);
		
		var obj = new Object();
		if (!isSafari && !isOpera) {
		obj.x = document.documentElement.clientWidth || document.body.clientWidth || document.body.scrollWidth;
		//obj.y = document.documentElement.clientHeight || document.body.clientHeight || document.body.scrollHeight;
		} else {
		obj.x = window.innerWidth;
		//obj.y = window.innerHeight;
		}
		obj.hx = parseInt((obj.x)/2);
		//obj.hy = parseInt((obj.y)/2);
		
		return obj;
	}
	*/
	imgBox.mousemove(function(e){
		mouseX=e.clientX;
		//mouseY=e.clientY;
	});
	imgTarget.bind("click", function()
	{
		var winWidth = $(window).width()/2;
		//alert(mouseX);//IE(window)から取得不可
		if( mouseX > winWidth)
		{
			if(n == imgSource.length - 1)
			{
				n = 0;
				activeImg = imgSource.eq(n);
				imgLoad(activeImg);
			}else
			{
				n += 1;
				activeImg = imgSource.eq(n);
				imgLoad(activeImg);
			}
		}else{
			if(n == 0)
			{
				n = imgSource.length - 1;
				activeImg = imgSource.eq(n);
				imgLoad(activeImg);
			}else
			{
				n -= 1;
				activeImg = imgSource.eq(n);
				imgLoad(activeImg);
			}
		}
	});
	//*/
	
}

/*
$(window).scroll(function(){
	//alert(getScroll().y);
	var sclY = document.documentElement.scrollTop || document.body.scrollTop;
    $("#headerWrap").animate({top:sclY}, 600, "easeOutQuint");
});

function getScroll(){
	var scl = new Object();
	scl.x = document.documentElement.scrollLeft || document.body.scrollLeft;
	scl.y = document.documentElement.scrollTop || document.body.scrollTop;
	return scl;
}*/
