var openClose = $('<span class="open-close"></span>');
var openButton = $('<a href="javascript:" class="open"><img src="/exhibition/common/img/icn_open_01.gif" width="19" height="19" alt="[open]"></a>');
var closeButton = $('<a href="javascript:" class="close"><img src="/exhibition/common/img/icn_close_01.gif" width="19" height="19" alt="[close]"></a>');
var closeAction = function(){
	var box = $(this).parent().parent().parent();
	var content = box.find('.ex-content');
	content.slideUp(250, function(){
		var list = box.parent().parent();
		list.hide();
		list.show();
		box.find('.close').hide();
		box.find('.open').show();
		if($('#ex-container ul.ex-list div.ex-box .ex-content:visible').size() == 0){
			$('#full-open a.close').hide();
			$('#full-open a.open').show();
		}
	});
}
var openAction = function(){
	var box = $(this).parent().parent().parent();
	var content = box.find('.ex-content');
	content.slideDown(50, function(){
		var list = box.parent().parent();
		list.hide();
		list.show();
		box.find('.open').hide();
		box.find('.close').show();
		if($('#ex-container ul.ex-list div.ex-box .ex-content:hidden').size() == 0){
			$('#full-open a.open').hide();
			$('#full-open a.close').show();
		}
	});
}

var today = new Date();
function getStatus(_obj){
	var startDate = getDate(_obj.find('.start').text());
	var endDate = getDate(_obj.find('.end').text());
	
	if(endDate.getFullYear() < 2009){
		endDate.setFullYear(startDate.getFullYear());
	} else if(startDate.getFullYear() < 2009){
		startDate.setFullYear(endDate.getFullYear());
	}
	endDate.setHours(23);
	endDate.setMinutes(59);
	endDate.setSeconds(59);
	var status = 0;
	if(startDate > today){
		status = 2;
	} else if(today < endDate){
		status = 1;
	}
	return status;
}



var allCloseAction = function(){
	$('#ex-container ul.ex-list div.ex-box .ex-content').hide();
	$('#ex-container ul.ex-list div.ex-box').find('.close').hide();
	$('#ex-container ul.ex-list div.ex-box').find('.open').show();
	$('#full-open a.close').hide();
	$('#full-open a.open').show();
}
var allOpenAction = function(){
	$('#ex-container ul.ex-list div.ex-box .ex-content').show();
	$('#ex-container ul.ex-list div.ex-box').find('.open').hide();
	$('#ex-container ul.ex-list div.ex-box').find('.close').show();
	$('#full-open a.open').hide();
	$('#full-open a.close').show();
}
var filterReset = function(){
	$('#ex-container ul.ex-list>li').hide().show();
	$('#ex-container .month-block').hide().show();
	disableButton($('#no-filter a'));
	notFoundNotice.hide();
	$('#full-open a').removeClass('disabled');
	allCloseAction();
}
var filterRunning = function(_this){
	$('#ex-container .month-block').show();
	$('#ex-container ul.ex-list>li').each(function(){
		var status = getStatus($(this));
		if(status == 1 || status == 2){
			$(this).show();
		} else {
			$(this).hide();
		}
	});
	$(_this).hide();
	$(_this).parent().find('span').show();
	$('#ex-container .month-block').each(function(){
		$(this).hide().show(); // for IE
		var displayItemCount = $(this).find('.ex-list>li:visible').size();
		if(displayItemCount == 0){
			$(this).hide();
		}
	});
	notFoundNotice.hide();
	$('#full-open a').removeClass('disabled');

	if($('#ex-container .month-block:visible').size() == 0){
		$('#ex-container').append(notFoundNotice);
		notFoundNotice.show();
		$('#full-open a').addClass('disabled');
	}
	disableButton(_this);
	allOpenAction();
}
var disableButton = function(_obj){
	$('.filter-nav a.enabled').show();
	$('.filter-nav span').hide();
	$(_obj).hide();
	$(_obj).parent().find('span').show();
}


$(function(){

	$('ul.ex-list div.ex-box div.ex-title').each(function(){
		var status = getStatus($(this));
		$(this).find('p.date').prepend(statusIconList[status].clone());
		$(this).find('p.date').find('img').show();
	});

	if($('#ex-container .month-block').size() > 0){
		$('ul.ex-list div.ex-box div.ex-content').hide();
		$('ul.ex-list div.ex-box')
		.find('div.ex-title')
		.append(openClose)
		.find('.open-close')
		.append(closeButton)
		.find('.close').click(closeAction).hide().end()
		.append(openButton)
		.find('.open').click(openAction).end();

		var filterNavs = $('<ul class="filter-nav"></ul>');
		$('#ex-container').prepend(filterNavs);
		filterNavs.append(filterNavList);
		$('#full-open a.open').click(allOpenAction);
		$('#full-open a.close').click(allCloseAction).hide();
		$('#no-filter a').click(function(){filterReset(this);});
		$('#filter-running a').click(function(){filterRunning(this);});
		$('.filter-nav span').hide();
		disableButton($('#no-filter a'));
		filterRunning($('#filter-running a'));
	}


});



