$(document).ready( function() {
	// 保存の登録
	$(".save_clip").click(function(event) {
		var clip_job_ids = new Array();
		if ( $.cookie('clip_job_ids') != null ) {
			clip_job_ids = $.cookie('clip_job_ids').split(",");
		}
		if ( clip_job_ids.length < 20 ) {
			clip_job_ids.push(event.currentTarget.name);
			//$.cookie('clip_job_ids', '', { expires: -1 });
			$.cookie('clip_job_ids', $.unique(clip_job_ids).join(","), { path: '/' });
			//alert($.unique(clip_job_ids).join(","));
			$.ajax({
				  dataType: 'html'
				, url: '/index.php/job/getClipWithAjax'
				, data: { clip_job_ids: $.cookie('clip_job_ids').split(",") }
				, cache: false
				, success: function(html) {
					//alert(html);
					$(".sideClip").html(html);
				}
			});
			alert("保存が完了しました。\n後で「保存した求人一覧」から一括問い合わせができます。");
		} else {
			alert("20件までしか保存できません。");
		}
	});
	
	$(".all_save_clip").click(function(event) {
		var clip_job_ids = new Array();
		if ( $.cookie('clip_job_ids') != null ) {
			clip_job_ids = $.cookie('clip_job_ids').split(",");
		}
		//alert($(".clip_job_ids:checked").length);
		
		if ( clip_job_ids.length + $(".clip_job_ids:checked").length < 20 ) {
			$(".clip_job_ids").each(function(){
				if ( $(this).attr('checked') == 'checked' ) {
					clip_job_ids.push($(this).attr("name"));
				}
			});
			
			//$.cookie('clip_job_ids', '', { expires: -1 });
			$.cookie('clip_job_ids', $.unique(clip_job_ids).join(","), { path: '/' });
			//alert($.unique(clip_job_ids).join(","));
			$.ajax({
				  dataType: 'html'
				, url: '/index.php/job/getClipWithAjax'
				, data: { clip_job_ids: $.cookie('clip_job_ids').split(",") }
				, cache: false
				, success: function(html) {
					//alert(html);
					$(".sideClip").html(html);
				}
			});
			alert("保存が完了しました。\n後で「保存した求人一覧」から一括問い合わせができます。");
		} else {
			alert("20件までしか保存できません。");
		}
	});
	
	// 保存の削除
	$(".delete_clip").click(function(event) {
		var clip_job_ids = new Array();
		var new_clip_job_ids = new Array();
		if ( $.cookie('clip_job_ids') != null ) {
			clip_job_ids = $.cookie('clip_job_ids').split(",");
		}
		
		for (i = 0; i < clip_job_ids.length; i++) {
			if ( clip_job_ids[i] != event.currentTarget.name ) {
				new_clip_job_ids.push(clip_job_ids[i]);
			}
		}
		$.cookie('clip_job_ids', $.unique(new_clip_job_ids).join(","), { path: '/' });
		//alert($.unique(clip_job_ids).join(","));
		$("#clip_job_" + event.currentTarget.name).css("display", "none");
		alert("削除が完了しました。");
	});
	
	// 一括応募は5件まで
	$(".hope_job_ids").click(function(event) {
		//alert($(".hope_job_ids:checkbox:checked").length);
		if ( $(".hope_job_ids:checkbox:checked").length > 5 ) {
			$(this).attr("checked", false);
			alert("一括で応募できるのは5件までです。");
		}
	});
	
	// 最低1件はチェックして応募
	$(".apply_jobs").click(function(event) {
		if ( $(".hope_job_ids:checkbox:checked").length == 0 ) {
			alert("最低1件は選択してください。");
			return false;
		}
	});
	
	// 一括チェック
	$(".all_check").click(function(event) {
		$(".clip_job_ids").attr("checked", true);
	});
	
	// 検索条件の引き継ぎ
	var prefecture = '';
	$(".selectPref").each(function() { 
		if ( $(this).val() != '' ) {
			prefecture = $(this).val();
		}
	});
	var type = '';
	$(".selectType").each(function() { 
		if ( $(this).val() != '' ) {
			type = $(this).val();
		}
	});
	if ( prefecture != '' ) {
		getLine(prefecture);
		getCity(prefecture);
	}
	var selected_line = '';
	$(".selected_line").each(function() { 
		if ( $(this).val() != '' ) {
			selected_line = $(this).val();
		}
	});
	if ( selected_line != '' ) {
		getStation(selected_line);
	}
	if ( type == '0' ) {
		$(".h4TrainImage").attr("src", "/common/img/h4_searchtools_01_current.jpg");
		$(".h4CityImage").attr("src", "/common/img/h4_searchtools_02.jpg");
		$(".selectType").val(0);
	} else if ( type == '1' ) {
		$(".h4TrainImage").attr("src", "/common/img/h4_searchtools_01.jpg");
		$(".h4CityImage").attr("src", "/common/img/h4_searchtools_02_current.jpg");
		$(".selectType").val(1);
	}
	
	// 都道府県の変更
	$('div.selectable div.mapSelect map area').click(function(){
		getLine($(this).attr("alt"));
		getCity($(this).attr("alt"));
	});
	$('div.selectable div.mapSelect select').change(function(){
		getLine($(this).val());
		getCity($(this).val())
	});
		
	// 検索条件のリセット
	$(".reset").click(function() {
        $("form").find(':input').each(function() {
            switch (this.type) {
            case 'password':
            case 'select-multiple':
            case 'select-one':
            case 'text':
            case 'textarea':
                $(this).val('');
                break;
            case 'checkbox':
            case 'radio':
                this.checked = false;
                break;
            case 'hidden':
                break;
            }
        });
        return false;
    });
});

function getLine (prefecture) {
	//alert($(".lineOption").html());
	$(".lineSelect").html('<option>読み込み中・・・</option>');
	var selected_line = '';
	$(".selected_line").each(function() { 
		if ( $(this).val() != '' ) {
			selected_line = $(this).val();
		}
	});
	$.ajax({
		  dataType: 'html'
		, url: '/index.php/job/getLine'
		, data: { prefecture: prefecture, selected_line: selected_line }
		, cache: false
		, success: function(html) {
			//alert(html);
			$(".lineLi").html(html);
			//$(".lineSelect").attr("disabled", "disabled");
			// 路線の変更
			$('.lineSelect').change(function(){
				//alert($(this).val());
				getStation($(this).val());
				$(".h4TrainImage").attr("src", "/common/img/h4_searchtools_01_current.jpg");
				$(".h4CityImage").attr("src", "/common/img/h4_searchtools_02.jpg");
				$(".selectType").val(0);
			});
		}
	});
}

function getStation (line) {
	$(".stationOption").html("読み込み中・・・");
	//$(".stationSelect").html('<option>読み込み中・・・</option>');
	var selected_start_station = '';
	$(".selected_start_station").each(function() { 
		if ( $(this).val() != '' ) {
			selected_start_station = $(this).val();
		}
	});
	var selected_end_station = '';
	$(".selected_end_station").each(function() { 
		if ( $(this).val() != '' ) {
			selected_end_station = $(this).val();
		}
	});
	$.ajax({
		  dataType: 'html'
		, url: '/index.php/job/getStation'
		, data: { line: line, selected_start_station: selected_start_station, selected_end_station: selected_end_station }
		, cache: false
		, success: function(html) {
			//alert(html);
			$(".stationLi").html(html);
			//$(".startStationSelect").attr("disabled", "disabled");
			//$(".endStationSelect").attr("disabled", "disabled");
		}
	});
}

function getCity (prefecture) {
	$(".checkTable").html("読み込み中・・・");
	var selected_city = '';
	$(".selected_city").each(function() { 
		if ( $(this).val() != '' ) {
			selected_city = $(this).val();
		}
	});
	$.ajax({
		  dataType: 'html'
		, url: '/index.php/job/getCity'
		, data: { prefecture: prefecture, selected_city: selected_city }
		, cache: false
		, success: function(html) {
			//alert(html);
			$(".checkTable").html(html);
			$(".allCityCheck").click(function(event){
				allCityCheck($(this));
			});
			$(".cityCheck").click(function(event){
				$(".h4TrainImage").attr("src", "/common/img/h4_searchtools_01.jpg");
				$(".h4CityImage").attr("src", "/common/img/h4_searchtools_02_current.jpg");
				$(".selectType").val(1);
			});
			//$(".allCityCheck").attr("disabled", "disabled");
			//$(".cityCheck").attr("disabled", "disabled");
		}
	});
}

function allCityCheck(obj) {
	//alert(obj.attr("checked"));
	if ( obj.attr("checked") == "checked" ) {
		$(".cityCheck").attr("checked", true);
	} else {
		$(".cityCheck").attr("checked", false);
	}
}
