//**********************************************************
//    トップページに関するJavaScript
//            - require jquery.js
//            - require jcarousellite_1.0.1.min.js
//                                    muraki@triax.jp
//**********************************************************

//--------------------------------------------------
//
// クイックサーチのプルダウンで選択された値を、
// 正式なパラメータ名に分解してhiddenフィールドに
// セットする処理
// [詳細]
//  - 正式には、上記の処理を行う関数を返す
//
//--------------------------------------------------
function set_quick_search_hidden_fields(type_name){
	return function(){
		value = $('#quick_' + type_name).val();
		min = '';
		max = '';
		if(value.indexOf(':') != -1){
			ary = value.split(':');
			min = ary[0];
			max = ary[1];
		}
		$('#' + type_name + '_t_min').val(min);
		$('#' + type_name + '_t_max').val(max);
	};
}

//--------------------------------------------------
//
// CloseUpのページ送り処理
// [詳細]
//  - CloseUpの要素の最大の高さを
//    取得し、表示されているli要素にセットする
//    高さ調整の処理も含む。
//
//--------------------------------------------------
function regist_close_up_pager_event(){
	function fill_label(obj) {
	        var closeup_office = $('.closeup_office');
		var count = closeup_office.size();
		var page_num = (obj ? closeup_office.parent('li').index(obj) : 0) + 1;
		var label = page_num;
		if (count > 1) {
		    label += '～' + (page_num + 1);
		} else {
		    count = 2;
		}
		$('.current_page_label').text(label);
		if (page_num == 1) {
		    $('a.prev_page').hide();
		} else if (page_num == 2) {
		    $('a.prev_page').show();
		}
		if (page_num + 1 == count) {
		    $('a.next_page').hide();
		} else if (page_num + 2 == count) {
		    $('a.next_page').show();
		}
	}

	fill_label();

	$('a.prev_page,a.next_page').click(function(){return false;});

        // ターゲットとなるul liの子孫にもul li が含まれているため、
        // jCarouselLiteの挙動がおかしくなる。
        // それを避けるためにコンテンツをコピーしておいていったん空にする。
        var clone = $('.closeup_office').clone();
        $('.closeup_office').empty().wrapAll('<ul>').wrap('<li>');

	$('.wrapB_office').jCarouselLite({
		btnNext: 'a.next_page',
		btnPrev: 'a.prev_page',
		visible: 2,
		circular: false,
		afterEnd: fill_label
	}).css('width', ''); // jCarouselLiteの実行に伴って幅が崩れるので対応

	// とっておいたコピーでコンテンツを埋め、高さを最大の要素に揃える
	var list = [];
	$('.closeup_office').each(function(i){
		var height = $(this).append(clone.eq(i).children()).height();
		list.push(height);
	});
	var max_height = Math.max.apply(null, list);
	$('.closeup_office').parent('li').height(max_height);
}

// execute function on document ready
$(function(){
	// クイックサーチのプルダウンを、正式なパラメータ名に分解
	$('#quick_search_button').click(set_quick_search_hidden_fields('footprint'));
	$('#quick_search_button').click(set_quick_search_hidden_fields('rent'));
	// CloseUpのページ送り
	regist_close_up_pager_event();
});

