(function($){
    $(document).ready(function(){
        
        $("input.wage_type").click(changeWageType);
        var kyuboBox = $("#kyubo_box");
        var list = {
            "kyubo_tohoku":"東北",
            "kyubo_tokyo":"東京",
            "kyubo_kanto":"関東",
            "kyubo_sinetsu":"信越",
            "kyubo_tokai":"東海関西"
        }
        
        var switchImage = function(target,onoff){
            if(onoff == 'on'){
                target.attr('src',target.attr('src').replace(/off_/,'on_'));
            }else{
                target.attr('src',target.attr('src').replace(/on_/,'off_'));
            }
        }
        
        for(key in list){
            $("#" + key).bind('click',{region:list[key]},function(e){
                e.preventDefault();
                getKyubo(kyuboBox,e.data.region);
                for(key2 in list){
                    switchImage($('#' + key2 + ' img'),'off');
                }
                switchImage($('img',this),'on');
            });
        }

        
        
        getIchoshi($("#ichioshi_box"));
        $("#kyubo_tokyo").trigger('click');
        
        
    });
    
    var getIchoshi = function(target){
        $.ajax({
            type: "POST",
            dataType: "html",
            data: {},
            cache: false,
            url: "/app/default/ichioshi/pc/",
            error: function(XMLHttpRequest, textStatus, errorThrown){alert(XMLHttpRequest.responseText)},
            success: function(res){target.html(res)},
            complete:function(){}
        }); 
    };

    var getKyubo = function(target,region){
        $.ajax({
            type: "POST",
            dataType: "html",
            data: {"region":region},
            cache: false,
            url: "/app/default/ichioshi/kyubo/",
            error: function(XMLHttpRequest, textStatus, errorThrown){alert(XMLHttpRequest.responseText)},
            success: function(res){target.html(res)},
            complete:function(){}
        }); 
    };


    var changeWageType = function(){
        var wageType = $("input[name=wage_type]:checked").val();
        var hourlyWage = $("#hourly_wage");
        if(wageType == "月給"){
            $("#yen").text("万円");
            hourlyWage.attr("maxlength","2");
        }else{
            $("#yen").text("円");
            hourlyWage.attr("maxlength","4");
        }
    };
})(jQuery);
