
// POSTするフォームで重複送信防止のためボタンを非表示にする
// 重複送信を防止したいform要素に preventDuplicateForm クラスを付ける
// 重複送信を防止するために隠したい要素に preventDuplicateTarget クラスを付ける
function preventDuplicatePost() {
    $('form.preventDuplicateForm').submit(function(){
        $('.preventDuplicateTarget').css('visibility', 'hidden');
    });
}

function setClassBtn() {
	$("ul#nav a img, ul#subNav a img").each(function() {
		$(this).addClass("btn");
	});
}


function setTableFormAddMust() {
	$("table.inputDetail th").each(function() {
		var th_text = jQuery(this).html();
		th_text = th_text.replace(/（必須）/g, "<img src='/img/common/ico_must.gif' alt='必須' width='25' height='11' />");
		jQuery(this).html(th_text);
	});
}

function initRollOverImages() {
	var image_cache = new Object();
	$("a img.btn,input[type=image].btn").not("[@src*='_o.'],[@src*='_d.']").each(function(i) {
		var imgsrc = this.src;
		var dot = this.src.lastIndexOf('.');
		var imgsrc_on = this.src.substr(0, dot) + '_o' + this.src.substr(dot, 4);
		image_cache[this.src] = new Image();
		image_cache[this.src].src = imgsrc_on;
		$(this).hover(
			function() { this.src = imgsrc_on; },
			function() { this.src = imgsrc; }
		);
	});
}




function pageScroll() {
	$('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}, 500);
				return false;
			}
		}
	});
}


// 左上のサイトロゴをクリックした場合、職種をリセットする
function attachResetJobType() {

    // 職種をリセットする
    function resetJobType() {
        var options, parsed;
        parsed = location.host.match(/^([a-z0-9-]+?\.)([a-z0-9-.]+)$/);
        options = {
            path: '/',
            expires: 1
        };
        if (parsed && parsed.length == 3) {
            options.domain = '.' + parsed[2];
        }

        $.cookie('CakeCookie[recruit_job_type]', '', options);
    }
    $('div#header h1 a').click(resetJobType)
}

function popUpForSupportSite() {
    $(function(){
        $('a').each(function(){
            var support_site_pattern = /^(https?:\/\/[-a-zA-Z.]*)?\/support/;
            if (!(this.href.match(support_site_pattern))) {
                return;
            }
            $(this).click(function(){
                window.open(this.href, "support","width=780,height=600,menubar=yes,scrollbars=yes,resizable=yes");
                return false;
            });
        });
    });
}

/**
 * ページ最上部の会員登録促進メッセージの表示制御
 */
function hideRegisterNotice() {
    if (jQuery("div#headerAdd").size() == 0) {
        return false;
    }
    if (!($(jQuery.cookie))) {
        return false;
    }

    if (jQuery.cookie('hide_register_notice') === '1') {
        jQuery("div#headerAdd").remove();
    }

    jQuery("div#headerAdd a.close").click(function(){
        jQuery("div#headerAdd").remove();
        jQuery.cookie('hide_register_notice', '1', {
            path: '/'
        });
    });
}

//
// 利用規約確認をチェックするまでフォームに入力できないようにする
// @param {Object} conf 設定 処理対象のjQuery Expression
//     conf.checkAgreement 規約確認のためのチェックボックス
//     conf.targetBlock    ブロックする領域
//     conf.targetForm     ブロックするフォーム要素
//     conf.targetButton   ブロックするsubmitボタン
//
function attachBlockUI2Form(conf){
    var checkAgreement, targetBlock, targetForm, targetButton;
    checkAgreement = $(conf.checkAgreement);
    targetBlock = $(conf.targetBlock);
    targetForm = $(conf.targetForm);
    targetButton = $(conf.targetButton);
    $.blockUI.defaults.css = {
        padding:        10,
        margin:         0,
        width:          '55%',
        top:            '10%',
        left:           '35%',
        textAlign:      'center',
        color:          '#000',
        border:         '3px solid #FF6600',
        backgroundColor:'#fff',
        cursor:         'default'
    };
     $.blockUI.defaults.overlayCSS = {
        backgroundColor: '#fff',
        opacity:         '0.7',
        cursor:         'default'
    };
    $.blockUI.defaults.centerY = false;

    function blockForm(){
        targetBlock.block({message:false});
        // submitボタンを無効に
        targetForm.bind('submit.requireAgreement', function(){
            return false;
        })
        targetButton.css('opacity', '0.5');
    }
    function unblockForm(){
        targetBlock.unblock();
        // submitボタンを有効に
        targetForm.unbind('submit.requireAgreement');
        targetButton.css('opacity', '1');
    }
    checkAgreement.bind('click', function(){
        if ($(this).attr('checked')) {
            unblockForm();
        } else {
            blockForm();
        }
    }).triggerHandler('click');
}


$(document).ready(setClassBtn);
$(document).ready(setTableFormAddMust);
$(document).ready(initRollOverImages);
$(document).ready(pageScroll);
$(document).ready(attachResetJobType);
$(document).ready(popUpForSupportSite);
$(document).ready(hideRegisterNotice);
$(document).ready(preventDuplicatePost);
