// JavaScript Document
$(document).ready(function(){
	rollOver();
	pageTop();
	thumbView();
	tabChg();
	formFunc();
	$("#mainWrap").subMenuStoker("#sidebar");
});

/*	ロールオーバー
-------------------------------------------------------------------------- */
function rollOver() {
	$("img[src*='_off.']").filter(function() { return $(this).parent().hasClass("non") ? false : true; }).hover( 
		setRollOver, setRollOut
	);
}

function setRollOver() {
	$(this).attr("src", $(this).attr("src").replace("_off.", "_on."));
}

function setRollOut() {
	$(this).attr("src", $(this).attr("src").replace("_on.", "_off."));
}


/*	スムーズスクロール
-------------------------------------------------------------------------- */
function pageTop() {
	var speed = 700;
	$("a.pn[href^='#']").click(function() {
		var target = $(this).attr('href').substr(1);
		var targetY = 0;
		var h = Math.max( document.body.clientHeight , document.body.scrollHeight );  
		h = Math.max( h , document.documentElement.scrollHeight );  
		h = Math.max( h , document.documentElement.clientHeight );
		var inH = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
		if(target != '') {
			targetY = $("*[name='"+target+"'], #"+target).offset().top;
			if(targetY+inH > h) targetY = h-inH;
		}
		//if($('html').scrollTop() == 0) {
			//alert('test');
			$('html').animate({scrollTop:targetY}, speed, "easeInOutSine");
	//	} else{
			$('body').animate({scrollTop:targetY}, speed, "easeInOutSine");
		//}
		return false;
	});
}


function thumbView() {
	$("#thumbnail li a").click( function() {
		var imgTgt = $(this);
		$("#photo img").queue([]);
		$("#photo img").fadeTo(10, 0, function() {
			$("#photo img").attr("src", imgTgt.attr("href") );
			$("#photo img").fadeTo(1500, 1);
		});
		$("#text").html(imgTgt.attr("title"));
		return false;
	} );
}

function tabChg() {
	$(".taBox").hide();
	var page = "contact";
	var get = retrieveGETqs();
	if( get['type'] ) {
		var page = get['type'];
	}
	$("#"+page).show();
	$("#"+page+"Btn img").trigger("mouseenter").unbind("mouseenter").unbind("mouseleave");
	$("#tab a").click( function() {
		var index = $("#tab a").index($(this));
		$(".taBox").hide();
		$(".taBox:eq("+index+")").show();
		$("#tab a img").hover(setRollOver, setRollOut).trigger("mouseleave");
		$(this).find("img").trigger("mouseenter").unbind("mouseenter").unbind("mouseleave");
		return false;
	} );
}

 
function retrieveGETqs() {
	var qsParm = new Array();
	var query = window.location.search.substring(1);
	var parms = query.split('&');
	for (var i=0; i<parms.length; i++) {
		var pos = parms[i].indexOf('=');
		if (pos > 0) {
			var key = parms[i].substring(0,pos);
			var val = parms[i].substring(pos+1);
			qsParm[key] = val;
		}
	}
	return qsParm;
}

function formFunc() {
	$("input.inputer").each( function() {
		var reg = $(this).attr("rel");
		var tgt = $("input.checker[rel='"+reg+"']");
		if( !tgt.attr("checked") ) {
			$(this).attr("disabled", "disabled");
		}
	});
	$("input.checker").click( function() {
		var reg = $(this).attr("rel");
		var tgt = $("input.inputer[rel='"+reg+"']");
		if( $(this).attr("checked") == true ) {
			tgt.attr("disabled", "").val( tgt.attr("dval") );
		} else {
			tgt.attr("dval", tgt.val()).attr("disabled", "disabled").val("");
		}
	} );
}

jQuery.fn.extend({
	subMenuStoker : function(child) {
		var toPos = 20;
		var cBox = $(this);
		var lBox = $(this).find(child);
		var dTop = lBox.offset().top;
		var nowP = dTop;
		var scrInterval, chkInterval;
		if( !$("body.index").length ) {
			cBox.css("position", "relative");
			lBox.css( {"position":"absolute", "top":0, "right":0} );
			goMoving();
		}
		function chkMoving() {
			chkInterval = setInterval( function() {
				var sabun = getTargetPos()-nowP;
				if( Math.abs(sabun) > 5 ) {
					clearInterval(chkInterval);
					goMoving();
				}
			}, 700 );
		}
		function goMoving() {
			scrInterval = setInterval( function() {
				var sabun = ( getTargetPos()-nowP )/5;
				nowP = nowP + sabun;
				
				if( Math.abs(sabun) < 0.5 ) {
					clearInterval(scrInterval);
					nowP = getTargetPos();
					lBox.css('top', nowP );
					chkMoving();
				} else {
					lBox.css('top', nowP);
				}
			}, 10);
		}
		function getTargetPos() {
			var nHeight = lBox.outerHeight({margin:true});
			var cHeight = cBox.outerHeight({margin:true});
			var scr = $(window).scrollTop()+toPos;
			var tgt = scr-dTop;
			tgt = tgt<0 ? 0 : tgt;
			if( tgt+nHeight > cHeight )
				tgt = cHeight-nHeight;
			return tgt;
		}
	}
});


