// JavaScript Document

var $maxNum = 0;
var $ID = 0;

$(document).ready(
	function (){		
		bodyFlg = $("body").attr('id');
		
		switch(bodyFlg){
		case "all":
			gridInit();
			break;
		case "wedding":
			gridInit();
			break;
		case "giftcafe":
			gridInit();
			cafeInit();
			break;
		}

		//*ga track
		$('a[href^=http]')
		.not("[href*="+location.hostname+"]").attr('onclick','pageTracker._link(this.href); return false;')
		.end()
		.not("[href*="+location.protocol +"]").attr('onclick','pageTracker._link(this.href); return false;')
		
		if( $("body").attr('class') == "page_top" ){
			$maxNum = $(".mainImg").length;
			var $mainThumb = $(".mainThumb li a");
			$mainThumb.click(function(){
				var indexNum = $mainThumb.index(this);
				topInit(indexNum);
			});
			
			animeInit();
			topInit(0);
		}
		
});

$(window).load(function() {
	var ie = (function(){
	var undef,v = 3,div = document.createElement('div'),all = div.getElementsByTagName('i');
	while (div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',all[0]);
	return v > 4 ? v : undef;
	}());
	if(ie===6){ $("#footer").css('margin-top','1px') }
});

function gridInit(){
	if($nongrid == true) return;
	
	//setup
	var vg = $("#grid_content").vgrid({
		useLoadImageEvent: true,
		useFontSizeListener: true,
		time: 0,
		fadeIn: {time: 50},
		onStart: function(){
			$("#message1")
				.css("visibility", "visible")
				.fadeOut("slow",function(){
					$(this).show().css("visibility", "hidden");
				});
		},
		onFinish: function(){
			$("#message2")
				.css("visibility", "visible")
				.fadeOut("slow",function(){
					$(this).show().css("visibility", "hidden");
				});
		}
	});
}

function topInit(num){
	clearInterval(fadeTimer)
	if( $maxNum == num){
		num = 0;
	}
	$ID = num;
	
	for(i=0; i<=$maxNum; i++){
		if(i==num){
			$($(".mainImg")[i]).fadeIn("slow");
		}else{
			$($(".mainImg")[i]).fadeOut("slow");
		}
	}
	animeInit();
}
function animeInit(){
	fadeTimer = setInterval(function loopFead(){
		topInit($ID+1);
		
	}, "3000");
}

initRollOverImages = function(elm){ // rollover共通処理  _on.拡張子
    $(elm).mouseover(function(){
        $(this).attr("src",$(this).attr("src").replace(/^(.+)(\.[a-z]+)$/, "$1_on$2"))
    }).mouseout(function(){
        $(this).attr("src",$(this).attr("src").replace(/^(.+)_on(\.[a-z]+)$/, "$1$2"));
    })
} 
initRollOverFead = function(elm2){ // rollover共通フェード処理 
    $(elm2).mouseover(function(){
//		console.log($(this).find("img"));	$(this).children("img")
        $(this).fadeTo("fast", 0.7);
    }).mouseout(function(){
       $(this).fadeTo("fast", 1);
    })
} 
function gotoTop(){
	$("html, body").animate({scrollTop: "0px"}, 500);
}
function gotoBottom(){
	$("html, body").animate({scrollTop: $('#main').height()+200 }, 500);
}

/////////////////////////////////////////////////////////////////////////////////
////	CAFE GIFT

var _popFlg = false;
var _itemID = 0;
var $list;
var $MAX;

function cafeInit(){
	
	bodyFlg = $("body").attr('class');
	
	if( bodyFlg == "page_chocolat_basic"){
		$list = "choco";
		$MAX  = 14;
	}else if( bodyFlg == "page_champagne_basic"){
		$list = "champagne";
		$MAX  = 30;
	}else{
		return;
	}
	
	$pop = $("#popupBox");
	$item = $("#innerBox");
	$bg = $("#bgLayer");
	$bg.click(function(){
		popupItem(-1);
	});
	
	var $gridThumb = $(".grid .popupItem");
	$gridThumb.click(function(){
		var indexNum = $gridThumb.index(this);
		indexNum = indexNum+1;
		popupItem(indexNum);
	});
	
	/* PREV BTN */
	$("#prevBtn img").click(function(){
		_itemID--;
		if(_itemID == 0){ _itemID = $MAX; }
		next(_itemID);
	});
	
	/* NEXT BTN */
	$("#nextBtn img").click(function(){
		_itemID++;
		if(_itemID == $MAX+1){ _itemID = 1; }
		next(_itemID);
	});
	
	/* CLOSE BTN */
	$("#closeBtn img").click(function(){
		popupDel();
	});
	
	$(window).resize(function(){
		boxSet();
	});
	
}

function popupItem(itemNum){
	
	_itemID = itemNum;
	
	if(_popFlg){
		popupDel();
	}else{
		itemShow(_itemID);
	}
	
}

function next(itemNum){
	$pop.fadeTo("slow", 0 , function(){
		$item.load( "item/"+$list+itemNum+".html", null, function(){
			
			$pop.fadeTo("slow", 1);
			//$itemH2 = $item.height()/2 - 25;
			
			_popFlg = true;
			boxSet();
		});
		
	});
}
function itemShow(itemNum){
	$bg.css("background-color","#ac964e");
	
	$bg.css("opacity", 0);
	$pop.css("opacity", 0);
	$("#prevBtn").css("opacity", 0);
	$("#nextBtn").css("opacity", 0);
	$("#closeBtn").css("opacity", 0);

	
	$bg.fadeTo("slow", 0.6);
	$pop.fadeTo("slow", 1);
	$("#prevBtn").fadeTo("slow", 1);
	$("#nextBtn").fadeTo("slow", 1);
	$("#closeBtn").fadeTo("slow", 1);
	
	$item.load( "item/"+$list+itemNum+".html", null, function(){
		
		//$itemH2 = $item.height()/2 - 25;
		_popFlg = true;
		boxSet();
	});
	
}

function popupDel(){
	_popFlg = false;
	$pop.fadeTo("slow", 0, function(){
		$pop.css("display","none");
	});
	$bg.fadeTo("slow", 0, function(){
		$bg.css("display","none");
	});
	
	$("#prevBtn").fadeTo("slow", 0, function(){ $("#prevBtn").css("display","none"); });
	$("#nextBtn").fadeTo("slow", 0, function(){$("#nextBtn").css("display","none"); });
	$("#closeBtn").fadeTo("slow", 0, function(){$("#closeBtn").css("display","none"); });
	
}

function boxSet(){
	if(!_popFlg) return;
	
    // ページ全体を表す要素を取得
	$topY = $(window).height()
	
	$centerX = ($("body").width() - $pop.width())/2;
	$centerY = ($(window).height() - $pop.height())/2 + getScrollPosition(); //スクロール量追加;
	
	$pop.css("left", $centerX);
	$pop.css("top", $centerY);
	
	var arrY = Math.round($centerY+$pop.height()*0.5-20);
	$("#prevBtn").css({"left":($centerX-40+"px"),"top":(arrY+"px")});
	$("#nextBtn").css({"left":($centerX+790+"px"),"top":(arrY+"px")});	
	$("#closeBtn").css({"left":($centerX+790+"px"),"top":($centerY-40+"px")});	
	
	$bg.css("height", $("body").height() );
}

function getScrollPosition(){
　　return (document.documentElement.scrollTop || document.body.scrollTop);
}

