//$(document).ready(function(){
/*$(window).bind("load",function(){
});*/
$(function(){

/*	$('#menu a').each(function(){
		var tgturi = $(this).attr("href").split('/');
		if(tgturi[tgturi.length-2] == $('body').attr('id')){
			$(this).addClass('sel');
		}
	});*/
	
	$("#btn_pagetop").click(function(){
		pageScroll();
		return false;
	});
	$("#btn_contact").click(function(){
		pageScroll($('#contact').offset().top);
		return false;
	});

//////////////////////

	//ボーダー
	if($('body#home').size()){
		
		pencilBorder($('.photoPane'));
		pencilBorder($('.shopPane'));
		pencilBorder($('.recentPane'));
		pencilBorder($('.textPane'));
		
		$('.pencilLeft').each(function(){
			var num1 = 5 + Math.floor( Math.random() * 16 );
			var num2 = 5 + Math.floor( Math.random() * 21 );
			
			$(this).css({'margin-top':num1});
			$(this).css({'margin-left':num2, 'width':$(this).width()-num1});//,{'width':$(this).width()-num2*2}
		});
		
		$('#shopmenu').hover(function(){
			$('#shopmenu ul').show();
		},function(){
			$('#shopmenu ul').hide();
		});
		
		$('.photobox').each(function(){
			var tW = $('img', this).width();
			var tH = $('img', this).height();
			var pW = $(this).width();
			var pH = $(this).height();
			if(tW > pW || tH > pH){
				$('img', this).css('margin', Math.floor((pH-tH)/2) + "px " + Math.floor((pW-tW)/2)+"px");
			}
		});

		//////////////////////
	
		//TOPページ組み替え
		$('.shortcut#set2').vgrid({
			easeing: "easeOutQuint",
			useLoadImageEvent: true,
			time: 400,
			delay: 20,
			fadeIn: {
				time: 500,
				delay: 50
			}
		});
	
		//////////////////////
	
		//サブメニューの位置（現状TOPのみ）
		$(".submenu ul").each(function(n){
			var pos = 0-($(this).width()/2) + ($(this).parent().width()/2);//+40
			$(this).css({'margin-left': pos});
		});
		
		//////////////////////
		
			
	}else{
		pencilBorder($('.post'));
		
		if($("#menu li.current-cat-parent").size()==0){
			if($("#menu ul.children").size()) {
				$("#menu ul.children").each(function(){
					$(this).parent().hover(function(){
						$("ul.children" , this).show();
					},function(){
						$("ul.children" , this).hide();
					});
				});
			}
		}else{
			$("ul.children" , this).show();
		}
		
		/*if($('#welcome-cocco').size()) {
			pencilBorder($('#om_shortcut li'));
			$('#welcome-cocco .photobox').each(function(){
				var tW = $('img', this).width();
				var tH = $('img', this).height();
				var pW = $(this).width();
				var pH = $(this).height();
				if(tW > pW || tH > pH){
					$('img', this).not(".month_tab").css('margin', Math.floor((pH-tH)/2) + "px " + Math.floor((pW-tW)/2)+"px");
				}
				
				if($('a', this).size()){
				$(this).hover(function(){
					$(this).fadeTo(100,0);
				},function(){
					$(this).fadeTo(0,1);
				});
				}
			});
		}*/
		
	}

//////////////////////

	//2分割の時に画像削除
	$(".textbox img").each(function(n){
		$(this).remove();
		if($(this).parent().children().size()<2){
			$(this).parent().remove();
		}
	});

//////////////////////


	if($('body#welcome-cocco').size() && $("#mcs_container").size()){
		$("#mcs_container").mCustomScrollbar("vertical",400,"easeOutCirc",1.05,"fixed","yes","yes",10);
		$('#topphoto').each(function(){
			var tW = $('img', this).width();
			var tH = $('img', this).height();
			var pW = $(this).width();
			var pH = $(this).height();
			if(tW > pW || tH > pH){
				$('img', this).css('margin', Math.floor((pH-tH)/2) + "px " + Math.floor((pW-tW)/2)+"px");
			}
		});
		
		$('#bookviewer img').hide();//ページ上の画像を隠す
	}

});


function pencilBorder(tgt) {
	$(tgt).wrapInner('<div class="pencilRight"></div>');
	$(tgt).wrapInner('<div class="pencilLeft"></div>');
	$('.pencilRight', tgt).prepend('<div class="pencilTop"><div class="pencilTopRight"></div></div>');
	$('.pencilRight', tgt).append('<div class="pencilBottom"><div class="pencilBottomRight"></div></div>');
}

function slideViewer(n) {
	var $active = $('#slideshow article.active');
	if ( $active.length == 0 ) $active = $('#slideshow article:last');
	
	// use this to pull the images in the order they appear in the markup
	var $next =  $active.next().length ? $active.next()
	: $('#slideshow article:first');
	
	// uncomment the 3 lines below to pull the images in random order
	// var $sibs  = $active.siblings();
	// var rndNum = Math.floor(Math.random() * $sibs.length );
	// var $next  = $( $sibs[ rndNum ] );
	
	$active.addClass('last-active');
	
	$next.css({opacity: 0.0})
	.addClass('active')
	.animate({opacity: 1.0}, 2000, function() {
	$active.removeClass('active last-active');
	});
}


//Welcome Cocco画像遅延ロード
var i = 0;
var int=0;
$(window).bind("load", function() {//ページコンテンツのロードが完了後、機能させる
     var int = setInterval("doThis(i)",500);//フェードするスピード
});
 
function doThis() {
     var images = $('#bookviewer img').length;//画像の数を数える
     if (i >= images) {// ループ
          clearInterval(int);//最後の画像までいくとループ終了
    }
    $('img:hidden').eq(0).fadeIn(500);//一つずつ表示する
    i++;
}



function pageScroll(o){
	if(!o) o=0;
	$.scrollTo(o,250);
}
