/* ブラウザ判別 */
var ie=document.all ? 1 : 0;
var ns6=document.getElementById&&!document.all ? 1 : 0;
var opera=window.opera ? 1 : 0;

/* 子メニューの表示・非表示切替 */
function openFolder(childObj, number){
    $(childObj).toggle();

    // 全て空欄だった場合のみ全てチェック
    var name_reg = new RegExp('^subjob\\[' + number + '\\]');
    var checkboxes = $A(document.getElementsByTagName('input')).findAll(function(element) {
        return element.name.match(name_reg);
    });
    if (checkboxes.find(function (element) { return element.checked; }) === undefined) {
        checkboxes.each(function (element) {
            element.checked = true;
        });
    }
}

function setcolor(obj0,sw1) {
  if (sw1 == 0) {
    bd = '1px solid #F5F5F5';
    bgc = '';
  } else {
    bd = '1px solid #71AFEE';
    bgc = '#EEF7FB';
  }
  obj0.style.border = bd;
  obj0.style.backgroundColor = bgc;
}

/* 写真切り替え */
var photo_index = {};
function nextPhoto(id) {
	if (photo_index[id] === undefined) photo_index[id] = 0;
	photo_index[id]++;
	if (image_exists[id].length-1 < photo_index[id]) {
		photo_index[id] = 0;
	}
    if (/^h/.match(id)) {
        $('photo_' + id).src = image_exists[id][photo_index[id]];
    } else {
        $('photo_' + id).src = "/img/job_pict/" + id + "/" + image_exists[id][photo_index[id]] + ".jpg"; 
    }
}

function backPhoto(id) {
	if (photo_index[id] === undefined) photo_index[id] = 0;
	photo_index[id]--;
	if (photo_index[id] == -1) {
		photo_index[id] = image_exists[id].length-1;
	}
    if (/^h/.match(id)) {
        $('photo_' + id).src = image_exists[id][photo_index[id]];
    } else {
        $('photo_' + id).src = "/img/job_pict/" + id + "/" + image_exists[id][photo_index[id]] + ".jpg"; 
    }
}

function slidePhoto(id, value) {
	if (photo_index[id] === undefined) photo_index[id] = 0;
	photo_index[id] += value;
	if (photo_index[id] == -1) {
		photo_index[id] = image_exists[id].length-1;
	}
	if (image_exists[id].length-1 < photo_index[id]) {
		photo_index[id] = 0;
	}
	if (/^h/.match(id)) {
		$('photo_' + id).src = image_exists[id][photo_index[id]];
	} else {
		$('photo_' + id).src = "http://j-sen.jp/img/job_pict/" + id + "/" + image_exists[id][photo_index[id]] + ".jpg"; 
	}
}

function setObserver() {
	// new Form.Observer('search_form', 0.3, updateCounter);
	if ($('search_form')) {
		var inputs = $('search_form').getInputs();
		[].push.apply(inputs, $('search_form').getElements('select'));
		inputs.each(function (element) {
			element.observe('change', setUpdateCounter);
		});
	}
	//setUpdateCounter();
}
Event.observe(window, 'load', setObserver);

function setUpdateCounter() {
	setTimeout(updateCounter, 100);
}

function updateCounter() {
	var timer = setInterval(setRandom, 30);
	var value = $('search_form').serialize();
	new Ajax.Request('/js/search_count.php?' + value, {
		method: 'get',
		onSuccess: function(transport) {
			clearInterval(timer);
			var n = Number(transport.responseText);
			$('match_number').update(isNaN(n) ? 0 : n);
		}
	});
}

function setRandom() {
	$('match_number').update((Math.random()*1000).floor());
}
