/**
* 画像を指定したサイズに合わせる。
* @param {Element} elem img タグ
* @param {Number} wsize ピクセル単位の大きさ (縦)
* @param {Number} hsize ピクセル単位の大きさ (横)
*/
function fitImage(elem, wsize, hsize) {
	var s = getActualDimension(elem);

	var w = s.width;
	var h = s.height;

	if (w == 0 || h == 0) {
		return;
	}

	//var ratio = size / w;
	var top = 0;
	var left = 0;

	/*if (w < h) {
		ratio = size / h;
		w = w * ratio;
		h = h * ratio;
		left = (size - w) / 2;
	} else {
		ratio = size / w;
		w = w * ratio;
		h = h * ratio;
		top = (size - h) / 2;
	}*/
    if(w > wsize){
	    h = (h * wsize) / w;
	    w = wsize;
    }
    else if(h > hsize){
	    w = (w * hsize) / h;
	    h = hsize;
    }

	var e = $(elem);
	e.css('width', w);
	e.css('height', h);
	e.css('margin-top', top);
	e.css('margin-left', left);
	// e.css('margin-right', left);
	e.css('visibility', 'visible');
}

/**
* 画像の実際のサイズを取得する。
* @param {String} image img要素
*/
function getActualDimension(image) {
	var run, mem, w, h, key = "actual";

	// for Firefox, Safari, Google Chrome
	if ("naturalWidth" in image) {
		return {width: image.naturalWidth, height: image.naturalHeight};
	}
	if ("src" in image) { // HTMLImageElement
		if (image[key] && image[key].src === image.src) {return image[key];}
		if (document.uniqueID) { // for IE
			run = image.runtimeStyle;
			mem = {w: run.width, h: run.height};
			run.width = "auto";
			run.height = "auto";
			w = image.width;
			h = image.height;
			run.width = mem.w;
			run.height = mem.h;
		} else { // for Opera and Other
			mem = {w: image.width, h: image.height}; // keep current style
			// Remove attributes in case img-element has set width and height (for
			//webkit browsers)
			$(this).removeAttr("width").removeAttr("height").css({width:"", height:""});
			w = image.width;
			h = image.height;
			image.width = mem.w; // restore
			image.height = mem.h;
		}
		return image[key] = {width: w, height: h, src: image.src}; // bond
	}
	// HTMLCanvasElement
	return {width: image.width, height: image.height};
}

function livesInitOrig() {
	// 検索結果ページの地域リスト表示・非表示切り替え
	$('p#area_toggle a').ready(function(){
		$('p#area_toggle a').click(function() {
			var area_toggle = $('p#area_toggle');
			var area = $('div.areabox');
			if(area.css('display') == 'block') {
			  area.css('display', 'none');
			  area_toggle.attr('class', 'close');
			}
			else{
			  area.css('display', 'block');
			  area_toggle.attr('class', 'open');
			}
			return false;
		});
	});

	// 検索結果ページの地域リスト表示・非表示切り替え
	$('select.search_type_selector').ready(function(){
		$('select.search_type_selector').change(function() {
			$(this).parents('form').submit();
		});
	});
}
function livesInit() {
	// 検索結果ページの地域リスト表示・非表示切り替え
	$('p#area_toggle a').ready(function(){
		$('p#area_toggle a').click(function() {
			var area_toggle = $('p#area_toggle');
			var area = $('div.areabox');
			if(area.css('display') == 'block') {
			  area.css('display', 'none');
			  area_toggle.attr('class', 'close');
			}
			else{
			  area.css('display', 'block');
			  area_toggle.attr('class', 'open');
			}
			return false;
		});
	});

	// 検索結果ページの表示件数切り替え
	$('select.disp_num_selector').ready(function(){
		$('select.disp_num_selector').change(function() {
			$(this).parents('form').submit();
		});
	});

	// 検索結果ページの並び替え変更
	$('select.search_type_selector').ready(function(){
		$('select.search_type_selector').change(function() {
			$(this).parents('form').submit();
		});
	});
}

livesInit();

