// 
// values of 
var filterCfIndexArray = new Array();
var filterMonths = new Array();

// ----
function clearCfIndexAry() {
	if ( filterCfIndexArray ) { delete filterCfIndexArray; }
	filterCfIndexArray = new Array();
	if ( filterMonths ) { delete filterMonths; }
	filterMonths = new Array();
}

// ---
function addCfIndex(indexVal) {
	filterCfIndexArray.push(indexVal);
}

// ---
function addCfIndexes(indexArray) {
	var newAry=filterCfIndexArray.concat(indexArray);
	delete indexArray;

	// make them unique
	var uniqueObj=new Object();
	for (var t=0; t<newAry.length; ++t) { uniqueObj[ newAry[t] ]=''; }
	delete newAry;

	clearCfIndexAry();
	for (var u in uniqueObj) { addCfIndex(u); }
}

// ---
function removeCfIndexes(indexArray) {
	for (var a=0; a<indexArray.length; ++a) {
		for (var b=0; b< filterCfIndexArray.length; ++b) {
			if (indexArray[a] == filterCfIndexArray[b]) {
				filterCfIndexArray[b]=-1; // make value invalid
				break;
			}
		}
	}
	delete indexArray;

	// make them unique
	var uniqueObj=new Object();
	for (var t=0; t<filterCfIndexArray.length; ++t) { 
		var c=filterCfIndexArray[t];
		if ( c >= 0 ) { uniqueObj[ c ]=''; }
	}

	clearCfIndexAry();
	for (var u in uniqueObj) { addCfIndex(u); }
}

// ----
function IsValInArray(array, val) {
	var result=false;

	if (array.length < 1) { return result; }

	for (var x=0; x<array.length; ++x) {
		if ( val == array[x] ) {
			result = true;
			break;
		}
	}

	return result;
}

// =========================================================================

// ----
// argument format is like '2008-02'
function filterMainListByMonth(evt){
	// console.log('year & month: ['+this.id+']');
	filterMonths.push(this.id);

	var idAry=findCfIDsByMonth( filterMonths );
	addCfIndexes( idAry );

	toggleVisibilityOfMainLists();
	updateMarkerFlagByFilterIndex();
	redrawGeoMarkers();
}

// ----
function filterMainListByCountry(evt){
	// console.log('countryName: ['+this.innerHTML+']');

	var idAry=findCfIDsByCountry( this.innerHTML );
	addCfIndexes( idAry );

	toggleVisibilityOfMainLists();
	updateMarkerFlagByFilterIndex();
	redrawGeoMarkers();
}

// ----
function filterMainListByCity(evt){
	var tg=evt.getTarget();
	var el=Ext.fly(tg);
	var ctyBox=el.findParent('div.cityBox', 2);

	filterMainListByCityByToggle(ctyBox);

	evt.stopEvent();
}

function filterMainListByCityByToggle(cityBoxNode) {
	// console.log('location: ['+cityBoxNode.cityValue+', '+cityBoxNode.countryValue+']');
	var idAry=findCfIDsByCity( cityBoxNode.cityValue, cityBoxNode.countryValue );

	var tg=Ext.fly(cityBoxNode);
	if (tg.hasClass('highLighted')) {
		removeCfIndexes( idAry );
		tg.removeClass('highLighted');
	} else {
		addCfIndexes( idAry );
		tg.addClass('highLighted');
	}

	toggleVisibilityOfMainLists();
	updateMarkerFlagByFilterIndex();
	redrawGeoMarkers();
}

// 
function selectAndClearAllCitiesofCountry(evt) {

	var tg=Ext.fly(evt.getTarget());
	var countryValue = tg.prev().dom.innerHTML;
	var rtnd=tg.parent().next();
	var cds=rtnd.select('div.cityBox');

	if ('Select all' == this.value) {
		this.value='Clear all';
		cds.each(function(elem, thisObj, index) {
			if ( elem.isDisplayed() ) {
				if (false==elem.hasClass('highLighted')) {
					// console.log(elem.dom.cityValue);
					var idAry=findCfIDsByCity( elem.dom.cityValue, countryValue );
					addCfIndexes( idAry );
					elem.addClass('highLighted');
				}
			}
		});
	} else {
		cds.each(function(elem, thisObj, index){
			if (elem.hasClass('highLighted')) {
				// console.log(elem.dom.cityValue);
				var idAry=findCfIDsByCity( elem.dom.cityValue, countryValue );
				removeCfIndexes( idAry );
				elem.removeClass('highLighted');
			}
		});
		this.value='Select all';
	}

	toggleVisibilityOfMainLists();
	updateMarkerFlagByFilterIndex();
	redrawGeoMarkers();

	evt.stopEvent();
}

// ----
function filterMainListByShortname() {
	// console.log('shortname: ['+this.innerHTML+']');
	var idAry=findCfIDsByShortname( this.innerHTML );
	// console.log('results: '+idAry.length);

	var tg=Ext.fly(this);
	if (tg.hasClass('highLighted')) {
		// console.info(this.className);
		removeCfIndexes( idAry );
		tg.removeClass('highLighted');
		// console.info(this.className);
	} else {
		addCfIndexes( idAry );
		tg.addClass('highLighted');
	}

	toggleVisibilityOfMainLists();
	updateMarkerFlagByFilterIndex();
	redrawGeoMarkers();
}

// ----
function filterMainListByKeyword(){
	// console.log('kw ID: '+this.kwID);
	// console.log('# of cfIDs: '+axObj.kwIdxCf[ this.kwID ].cfsIndexes.length);

	var aryRef=axObj.kwIdxCf[ this.kwID ].cfsIndexes;
	var tmpAry=new Array();
	for (var e=0; e<aryRef.length; ++e) {
		tmpAry.push( axObj.cfs[ aryRef[e] ].id );
	}

	var tg=Ext.fly(this);
	if (tg.hasClass('highLighted')) {
		removeCfIndexes( tmpAry );
		tg.removeClass('highLighted');
	} else {
		addCfIndexes( tmpAry );
		tg.addClass('highLighted');
	}
	delete tmpAry;

	toggleVisibilityOfMainLists();
	updateMarkerFlagByFilterIndex();
	redrawGeoMarkers();
}

// ----
function toggleVisibilityOfMainLists() {
	var x, y;

	// filter Conference Information
	var tg=Ext.getDom('confCt');

	for (x=0; x<tg.childNodes.length; ++x) {
		if ('monthLabelGr' == tg.childNodes[x].className ) {
			if ( filterCfIndexArray.length < 1 ) {
				tg.childNodes[x].style.display='block';
			} else {
				tg.childNodes[x].style.display='none';
			}
		} else if ('monthBox' == tg.childNodes[x].className ) {
			for (y=0; y<tg.childNodes[x].childNodes.length; ++y) {
				var cfBxRef=tg.childNodes[x].childNodes[y];

				if ( filterCfIndexArray.length < 1 ) {
					cfBxRef.style.display='block';
				} else {
					if ( ! IsValInArray(filterCfIndexArray, cfBxRef.cfID) ) {
						cfBxRef.style.display='none';
					} else {
						cfBxRef.style.display='block';
						cfBxRef.parentNode.previousSibling.style.display='block';
					}
				}
			}
		}
	}
	updateNumOfConfs();

	// ---------------------------------------------
	// filter Submission deadline
	tg=Ext.getDom('sbdlCt');

	for (var x=0; x<tg.childNodes.length; ++x) {
		if ('dlDayLabel' == tg.childNodes[x].className ) {
			if ( filterCfIndexArray.length < 1 ) {
				tg.childNodes[x].style.display='block';
			} else {
				tg.childNodes[x].style.display='none';
			}
		} else if ('dlDayBox' == tg.childNodes[x].className ) {
			for (var y=0; y<tg.childNodes[x].childNodes.length; ++y) {
				var sdBxRef=tg.childNodes[x].childNodes[y];

				if ( filterCfIndexArray.length < 1 ) {
					sdBxRef.style.display='block';
				} else {
					if ( ! IsValInArray(filterCfIndexArray, sdBxRef.cfID) ) {
						sdBxRef.style.display='none';
					} else {
						sdBxRef.style.display='block';
						sdBxRef.parentNode.previousSibling.style.display='block';
					}
				}
			}
		}
	}
}

// ----
function removeAllHighlightInFilterPane(idStr) {
	var tg=Ext.get(idStr);
	if (!tg) {
		alert('target block ('+idStr+') does not exist!');
		return; 
	}

	var elms=tg.select('.highLighted');
	// console.info(idStr+' => '+elms.getCount());
	elms.each(function(elem, thisObj, index){
		elem.removeClass('highLighted');
	}, true)
}

// ----
// clear all filter condition and backs to default
function resetAllFilters (evt) {
	evt.preventDefault();

	removeAllHighlightInFilterPane('kwCt');
	removeAllHighlightInFilterPane('ccCt');
	removeAllHighlightInFilterPane('snFt');

	clearCfIndexAry();

	toggleVisibilityOfMainLists();

	showAllMarkers();
	redrawGeoMarkers();

	evt.stopPropagation();
}

// ---
// the process is same with above resetAllFilters function.
function resetAllFilters2 (btnObj, evt) {
	resetAllFilters(evt);
}

// ----
// key event assign to window
window.addEventListener('keyup', function(evt){
	if ( 27 == evt.keyCode ) { // ESC key
		resetAllFilters(evt);
		clearKeywordSearchInAttrBrowser();
	} else if (38 == evt.keyCode) {
		toggleBothSidePanes();		
	} else {
		// confirm('type key: '+evt.keyCode);
	}
}, true);

//
// ends here
//
