// JavaScript

// Init
$(function(){
	EventBlogFormat.loadCalendar();
	$('#event-calendar :checkbox').click(function(){
		EventBlogFormat.getEvents({month:$('#month').val()});
	});
	
	// スタイル
	$('#event-calendar div.section label').each(function(){
		if ($(this).text().length>6)
			$(this).addClass('wide');
	});
	
	EventBlogFormat.current_month = new Date();
	$(EventBlogFormat.DATA_CONTAINER).data('month', EventBlogFormat.current_month.getFullYear()+'-'+(EventBlogFormat.current_month.getMonth()+1)+'-'+EventBlogFormat.current_month.getDate());
	
	EventBlogFormat.getEvents();
});


/**
 * ブログ形式のイベント情報
 */
var EventBlogFormat = function(){
};

EventBlogFormat.CALENDAR_CONTAINER = '#event-calendar .calendar-container';
EventBlogFormat.EVENT_CONTAINER = '#event-calendar .event-container';
EventBlogFormat.DATA_CONTAINER = '#event-calendar';
EventBlogFormat.ROOT = '../_ajax/';
EventBlogFormat.PLUGIN = 'JJC_eventCalendar';

EventBlogFormat.current_month = null;

/**
 * カレンダーを読み込む
 * オプションには nextmonthof と prevmonthof のいずれかを渡すことができる
 * @param Object option オプション
 */
EventBlogFormat.loadCalendar = function(options){
	
	// 条件
	var condition = EventBlogFormat.getConditionByStrings();
	
	if (options) {
		for (var key in options) {
			condition += '&'+key+'='+options[key]
		}
	}
	
	$(EventBlogFormat.CALENDAR_CONTAINER).load(EventBlogFormat.ROOT+EventBlogFormat.PLUGIN+'/getCalendar/?'+condition, null, function(){
		EventBlogFormat.bindClickEventOnCalendarsDate();
	});
	
};

/**
 * 検索条件をまとめてリクエストを投げる
 */
EventBlogFormat.getEvents = function(add_condition){
	
	// 条件
	var condition = EventBlogFormat.getConditionByStrings();
	
	for (var key in add_condition) {
		condition += '&'+key+'='+add_condition[key];
	}
	
	$(EventBlogFormat.EVENT_CONTAINER).load(EventBlogFormat.ROOT+EventBlogFormat.PLUGIN+'/getEvents/?'+condition, null, function(){
		var portal_detail_page = $('#portal_detail_page').val();
		var company_detail_page = $('#company_detail_page').val();
		var flag = false;
		$(EventBlogFormat.EVENT_CONTAINER+' a.portal.detail_page').each(function(){
			var href = $(this).attr("href");
			href = href.split(document.location.href).join('');
			$(this).attr("href", portal_detail_page+href);
		});
		$(EventBlogFormat.EVENT_CONTAINER+' a.company.detail_page').each(function(){
			var href = $(this).attr("href");
			href = href.split(document.location.href).join('');
			$(this).attr("href", company_detail_page+href);
		});
	});
	
};


/**
 * 条件を文字列で取得
 */
EventBlogFormat.getConditionByStrings = function(){
	
	var condition = '';
	
	// 業種をまとめる
	var business_category_condition = [];
	$(':checkbox[name="business_category[]"]:checked').each(function(){
		business_category_condition.push(this.value);
	});
	if (business_category_condition.length>0)
		condition += '&business_category_id='+business_category_condition.join(',');

	// 職種をまとめる
	var job_type_condition = [];
	$(':checkbox[name="job_type[]"]:checked').each(function(){
		job_type_condition.push(this.value);
	});
	if (job_type_condition.length>0)
		condition += '&job_type_id='+job_type_condition.join(',');
	
	return condition;
	
};


/**
 * カレンダーのa要素にイベントを設定
 */
EventBlogFormat.bindClickEventOnCalendarsDate = function(){
	
	$(EventBlogFormat.CALENDAR_CONTAINER+' a.date').click(function(){
		EventBlogFormat.getEvents({date:$(this).find('#calendar_date').val()});
	});
	$(EventBlogFormat.CALENDAR_CONTAINER+' a.next_month').click(function(){
		EventBlogFormat.loadCalendar({nextmonthof:$('#month').val()});
		EventBlogFormat.getEvents({nextmonthof:$('#month').val()});
	});
	$(EventBlogFormat.CALENDAR_CONTAINER+' a.prev_month').click(function(){
		EventBlogFormat.loadCalendar({prevmonthof:$('#month').val()});
		EventBlogFormat.getEvents({prevmonthof:$('#month').val()});
	});
	$(EventBlogFormat.CALENDAR_CONTAINER+' a.current_month').click(function(){
		EventBlogFormat.getEvents({month:$('#month').val()});
	});
};
