$(function(){
	//scroll
	$('a[href*=#]').click(function () {
		if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) + ']');
			if ($target.length) {
				var targetOffset = $target.offset().top;
				$('html,body').animate({
					scrollTop: targetOffset
				}, 1000);
				return false;
			}
		}
	});
	
	//rollover
	var conf = {
		className: 'btn',
		postfix: '_on'
	};
	$('.' + conf.className).each(function () {
		this.originalSrc = this.src;
		this.rolloverSrc = this.originalSrc.replace(/(\.gif|\.jpg|\.png)/, conf.postfix + "$1");
		preloadImage(this.rolloverSrc);
	}).hover(function () {
		this.src = this.rolloverSrc;
	}, function () {
		this.src = this.originalSrc;
	});
	
	
	if ($.browser.msie) {
		$('label[for^=menu]').click(function () {
			$('#' + $(this).attr('for')).focus().click();
		});
    }
	$('.listAbout li:even').addClass('even');
	$('input[name=relation],input[name=contact]').change(function() {
		var name = $(this).attr('id');
		if(name == 'relation05' || name == 'designate') {
			$('.check_otherinput').attr('disabled',false);
		}else {
			$('.check_otherinput').attr('disabled',true);
		}
	})
	
	$('label[for^=menu],label[for^=about]').click(function() {
		$(this).parent().toggleClass('on');
	});
	$('input[name^=menu],input[name^=about]').each(function(){
		if($(this).attr('checked') == 'checked'){
			$(this).parent().addClass('on');
		}
	});
	
});

preloadedImages = [];
function preloadImage(url){
	var p = preloadedImages;
	var l = p.length;
	p[l] = new Image();
	p[l].src = url;
}

//form validation
$(function(){
    $("form").submit(function(){
    
        //エラーの初期化
        $(".error").remove();
        
        $(":text,textarea").filter(".validate").each(function(){
            
            //必須項目のチェック
            $(this).filter(".req").each(function(){
                if($(this).val()==""){
                    $(this).after("<span class='error'><br />必須項目です</span>")
                }
            })
            
            //数値のチェック
            $(this).filter(".number").each(function(){
                if(isNaN($(this).val())){
                    $(this).after("<span class='error'><br />数値のみ入力可能です</span>")
                }
            })
            
            //メールアドレスのチェック
            $(this).filter(".mail").each(function(){
                if($(this).val() && !$(this).val().match(/.+@.+\..+/g)){
                    $(this).after("<span class='error'><br />メールアドレスの形式が異なります</span>")
                }
            })
            
            //メールアドレス確認のチェック
            $(this).filter(".mail_check").each(function(){
                if($(this).val() && $(this).val()!=$("input[name="+$(this).attr("name").replace(/^(.+)_check$/, "$1")+"]").val()){
                    $(this).after("<span class='error'><br />メールアドレスと内容が異なります</span>")
                }
            })
            
        })
        
        //ラジオボタンのチェック
        $(":radio").filter(".validate").each(function(){
            $(this).filter(".req").each(function(){
                if($("input[name="+$(this).attr("name")+"]:checked").size() == 0){
                    $(this).parent().prepend("<p class='error'>選択してください</p>")
                }
            })
        })
        
        //チェックボックスのチェック
        $(".checkboxRequired").each(function(){
            if($(":checkbox:checked",this).size()==0){
                $(this).prepend("<p class='error'>選択してください</p>")
            }
        })
        
        // その他項目のチェック
        $(".validate.add_text").each(function(){
            if($(this).attr("checked")==true && $("input[name="+$(this).attr("name").replace(/^(.+)$/, "$1_text")+"]").val()==""){
                $(this).parent().prepend("<p class='error'>その他の項目を入力してください。</p>")
            }
        })
        
        //エラーの際の処理
        if($(".error").size() > 0){
                $('html,body').animate({ scrollTop: $(".error:first").offset().top-40 }, 'slow');
                //$(".error").parent().addClass("error");
                return false;
        }
    })
})
