
function initPressReleaseLoader(item, url){
	document.observe("dom:loaded", function(){
		new Ajax.Updater(item, url);
	});
}

_pr_doc = null;
_pr_onload = false;

function initPressReleaseSelector(url, selections, elems){

	document.observe("dom:loaded", function(){
		_pr_onload = true;
		if(_pr_doc != null){
			_buildPressReleaseSelector(_pr_doc, selections, elems);
		}
	});

	new Ajax.Request(url, {
		method:"get",
		onSuccess: function(transport){
			_pr_doc = transport.responseXML;
			if(_pr_onload){
				_buildPressReleaseSelector(_pr_doc, selections, elems);
			}
		}
	});
}

function _buildPressReleaseSelector(doc, selections, elems){
	var nl_category_group = doc.getElementsByTagName("category-group");
	var elem_company_select = $(elems.categorySelector);
	for(var i=0; i<nl_category_group.length; i++){
		var elem_category_group = nl_category_group.item(i);
		var option = new Option(
				elem_category_group.getAttribute('label'),
				elem_category_group.getAttribute('url')
		);
		elem_company_select.options[elem_company_select.options.length] = option;
		if(selections.category != "" && selections.category == elem_category_group.getAttribute('id')){
			elem_company_select.selectedIndex = elem_company_select.options.length - 1;
		}
	}
	elem_company_select.show();
	
	
	//year select
	var elem_current_year_group = null;
	var nl_date_group_year = null;
	
	if(selections.category != ""){
		var elem_category_date_groups = doc.getElementsByTagName("category-date-groups").item(0);
		var nl_date_group_year = [];
		for(var i=0; i<elem_category_date_groups.childNodes.length; i++){
			var node = elem_category_date_groups.childNodes.item(i);
			if(node.nodeType == 1 && node.tagName == 'category' && node.getAttribute('id') == selections.category){
				nl_date_group_year = node.getElementsByTagName("date-group-year");
				break;
			}
		}
	}
	else{
		var elem_date_groups = doc.getElementsByTagName("date-groups").item(0);
		var nl_date_group_year = elem_date_groups.getElementsByTagName("date-group-year");
	}

	
	var elem_year_select = $(elems.yearSelector);
	for(var i=0; i<nl_date_group_year.length; i++){
		var elem_date_group_year = nl_date_group_year.item(i);
		var option = new Option(
				elem_date_group_year.getAttribute('label'),
				elem_date_group_year.getAttribute('url')
		);
		elem_year_select.options[elem_year_select.options.length] = option;
		if(selections.year != "" && selections.year == elem_date_group_year.getAttribute('year')){
			elem_year_select.selectedIndex = elem_year_select.options.length - 1;
			elem_current_year_group = elem_date_group_year;
		}
	}
	
	if(elem_current_year_group == null){
		elem_current_year_group = nl_date_group_year.item(0);
	}
	elem_year_select.show();
	
	//year_indicate
	$(elems.yearIndicate).update(elem_current_year_group.getAttribute('year'));

	var nl_date_group_month = elem_current_year_group.getElementsByTagName('date-group-month');
	
	var elem_month_container = $(elems.monthContainer);

	var elem_month_cell_current = elem_month_container.select(elems.monthCellCurrent)[0];
	var elem_month_cell = elem_month_container.select(elems.monthCell)[0];

	var docfrag = document.createDocumentFragment();
	if(selections.month != ""){
		var cell = elem_month_cell.cloneNode(true);
		cell.childElements()[0].update('すべて');
		cell.childElements()[0].setAttribute('href', elem_current_year_group.getAttribute('url'));
		cell.show();
		docfrag.appendChild(cell);
	}
	else{
		var cell = elem_month_cell_current.cloneNode(true);
		cell.childElements()[0].update('すべて');
		cell.show();
		docfrag.appendChild(cell);
	}
	
	for(var i=0; i<nl_date_group_month.length; i++){
		var elem_date_group_month = nl_date_group_month.item(i);
		if(selections.month != "" && selections.month == elem_date_group_month.getAttribute('month')){
			var cell = elem_month_cell_current.cloneNode(true);
			cell.childElements()[0].update(elem_date_group_month.getAttribute('label'));
			cell.show();
			docfrag.appendChild(cell);
		}
		else{
			var cell = elem_month_cell.cloneNode(true);
			cell.childElements()[0].update(elem_date_group_month.getAttribute('label'));
			cell.childElements()[0].setAttribute('href', elem_date_group_month.getAttribute('url'));
			cell.show();
			docfrag.appendChild(cell);
		}
	}
	elem_month_container.appendChild(docfrag);
	elem_month_container.setStyle({visibility:"visible"});
}

