// TODO: Select_getSelected es Select_clearMufaj atvetele a select.js-bol
// TODO: emberek kereso filter kezelese
var cBrowserFilter = Class.create({
	initialize: function(_container) {
		this['filters'] = new Object();
		this.container = gt( 'filterbox' );
		this.filtersToRender = 0;
		this.selectedTags = new Array();
		this.setSearchChkBox();
	},
	// add new filter, and render parts in filterbox (only)
	setFilterElement: function( filtername, filtertext, filtervalue, spanid ) {
		if ( !this['filters'][filtername] ) {
			this['filters'][filtername] = new Object;
		}
		this['filters'][filtername]['id'] = filtervalue;
		this['filters'][filtername]['text'] = filtertext;
		this['filters'][filtername]['spanid'] = spanid;
		this.renderFilterbox( 'add', filtername );
	},
	// removes filter, and rendered parts from filterbox
	removeFilterElement: function( filtername ) {
		if ( this['filters'][filtername] ) {
			this.renderFilterbox( 'remove', filtername );
			delete this['filters'][filtername];
		}
	},
	// returns given filter's parts
	getFilterElementValue: function( filtername ) {
		if ( this['filters'][filtername] && this['filters'][filtername]['id'] ) {
			return this['filters'][filtername]['id'];
		}
		return false;
	},
	// adds or removes filter parts in filterbox
	renderFilterbox: function( action, filtername ) {
		// remove
		if ( 'remove' == action ) {
			if ( gt( 'filterbox_' + filtername ) ) {
				var child = gt( 'filterbox_' + filtername );
	        	this.container.removeChild( child );
				this.filtersToRender = this.filtersToRender - 1;
			}
			if ( this.filtersToRender == 0 && this.selectedTags.length == 0 ) {
				if ( this.container.style.display == "block" ){
					this.container.style.display = "none"
				}
			}
		}
		// add
		else if ( 'add' == action ) {
			if ( this['filters'][filtername]['text'] && "" != this['filters'][filtername]['text'] ) {
				this.container.style.display = "block";
				if ( !gt( 'filterbox_' + filtername ) ) {
					var newDivTag = document.createElement("div");
					newDivTag.id = 'filterbox_' + filtername;
					newDivTag.innerHTML = "";
					this.container.appendChild( newDivTag );
				}
				var child = gt( 'filterbox_' + filtername );
				var onclick = 'browserFilter.changeFilterElementTrue( "' + filtername + '",  \
					"", "", "' + this['filters'][filtername]['spanid'] + '", "' + this['event'] + '" );';
				if ( "mufaj" == filtername.substring( 0, 5 ) ) {
					var sPrefix = "";
					if ( this['event'] == 'seriesfilterchange' ) {
						sPrefix = "s_";
					}
					onclick = 'Select_isClicked( "' + this['filters'][filtername]['id'] + '", "", "", \
						"' + this['event'] + '", "' + sPrefix + '" )';
				}
				else if ( filtername == "reszenkent" ) {
					onclick += ';gt( "reszenkent" ).checked = false;';
				}
				child.innerHTML = "<span style='font-weight: bold; cursor: pointer;' onclick='" + onclick + "' \
					>" + this['filters'][filtername]['text'] + "<font \
					style='font-color: red; color: red;'> X</font></span>";
				this.filtersToRender = this.filtersToRender + 1;
			}
		}
	},
	// adds, modifies or removes filters from object, render the filterbox and filterbar parts
	changeFilterElementTrue: function( filtername, filtertext, filtervalue, spanid, event ) {
		this.changeFilterElement( filtername, filtertext, filtervalue, spanid, true, event );
	},
	// adds, modifies or removes filters from object, render the filterbox and filterbar parts
	changeFilterElement: function( filtername, filtertext, filtervalue, spanid, makesearch, event ) {
		// set filter search event
		if ( event && "undefined" != event ) {
			this['event'] = event;
		}
		// add
		if ( !this['filters'][filtername] || !this['filters'][filtername]['id'] ) {
			this.setFilterElement( filtername, filtertext, filtervalue, spanid );
			this.addXToFilterBar( filtername );
			if ( makesearch ) {
				this.makeSearch();
			}
		}
		// remove / modify
		else {
			// clear filterbar heightens
			if ( this['filters'][filtername]['spanid']
				&& this['filters'][filtername]['spanid'] != ""
				&& gt( this['filters'][filtername]['spanid'] )
			) {
				this.removeXFromFilterBar( filtername );
			}
			// modify
			if ( this['filters'][filtername]['id'] != filtervalue
				|| this['filters'][filtername]['text'] != filtertext
				|| filtername == 'search_text'
			 ) {
				// 'mindegy', 'osszes', etc options
				if ( "" == filtervalue ) {
					this.removeFilterElement( filtername );
				}
				// new opcio, but not a remove one
				else {
					this.setFilterElement( filtername, filtertext, filtervalue, spanid );
					this.addXToFilterBar( filtername );
				}
				if ( makesearch ) {
					this.makeSearch();
				}
			}
			// remove
			else {
				this.removeFilterElement( filtername );
				if ( makesearch ) {
					this.makeSearch();
				}
			}
		}
		// specials, just like for children chkbox
		this.makeSpecialRender( filtername, filtertext, filtervalue, spanid );
	},
	makeExtendedSearch: function( filterEvent, filterobj ) {
		if ( filterobj && filterobj != "undefined" ) {
			var tmpObj = filterobj;
		}
		else {
			var tmpObj = new Object();
		}
		this['event'] = filterEvent + "filterchange";
		if ( filterobj && filterobj['veletlen'] ) {
			this.changeFilterElement( 'veletlen_valaszto', '', filterobj['veletlen'], '', false );
		}

		this.makeSearch( tmpObj );
	},
	// calls the valaszto's onParamsChange to make movie list refresh
	makeSearch: function( defaultProperties ) {
		dontdisplay = '2';
		if ( this['event'] && this['event'] != "" ) {
			var myEvent = this['event'];
		}
		else {
			var myEvent = "browserfilterchange";
		}
		var tabIds = myEvent.split('filterchange');
		var tmpObj = this.getFilters( tabIds[0] );

		if ( defaultProperties && defaultProperties != undefined ) {
			tmpObj = FilmX.apply( tmpObj, defaultProperties );
		}
		dispatcher.fireEvent( myEvent, tmpObj );
	},
	// clears filterbox content, and removes filterbar heighten (called when tab changed only)
	clearFilterBox: function() {
		for ( var i in this.filters ) {
			this.removeXFromFilterBar( i );
			this.removeFilterElement( i );
		}
		gt( 'in_cinema' ).checked = false;
		gt( 'reszenkent' ).checked = false;
		gt( 'veletlen_valaszto' ).checked = false;
		gt( 'tori' ).checked = false;
		gt( 'kisgyerek' ).checked = false;
		gt( 's_kisgyerek' ).checked = false;
		if ( $('tori_uj_korszak') && $('tori_uj_korszak').style.display == "inline" ){
			$('tori_uj_korszak').style.display = "none"
		}

		this.selectedTags = new Array();
		this.container.innerHTML = "";
		this.filtersToRender = 0;
		clickedElement = new Array();
		clickedElementTitle = new Array();
		if ( this.container.style.display == "block" ){
			this.container.style.display = "none"
		}
	},
	// adds (shows) div with X to given filterbar span
	addXToFilterBar: function( filtername ) {
		if ( this['filters'][filtername] && this['filters'][filtername]['spanid'] && this['filters'][filtername]['spanid'] != "" ) {
			if ( gt( this['filters'][filtername]['spanid'] ) ) {
				var spanToHeighten = gt( this['filters'][filtername]['spanid'] );
				spanToHeighten.style.fontWeight = 'bold';
			}
			var newDelSpanId = this['filters'][filtername]['spanid'] + '_delX';
			if ( gt( newDelSpanId ) ) {
				var newDelSpan = gt( newDelSpanId );
				newDelSpan.style.display = "inline";
			}
		}
	},
	// removes (hides) div with X from given filterbar span, and removes bold style
	removeXFromFilterBar: function( filtername ) {
		if ( this['filters'][filtername]['spanid'] ) {
			if ( gt( this['filters'][filtername]['spanid'] ) ) {
				var oldSpan = gt( this['filters'][filtername]['spanid'] );
				oldSpan.style.fontWeight = 'normal';
			}
			var delSpanId = this['filters'][filtername]['spanid'] + '_delX';
			if ( gt( delSpanId ) ) {
				var delSpan = gt( delSpanId );
				delSpan.style.display = "none"
			}
		}
	},
	// special render tasks, just like for children chkbox
	makeSpecialRender: function( filtername, filtertext, filtervalue, spanid ) {
		// for children checkbox
		if ( 'kivel' == filtername ) {
			if ( this['filters'][filtername] && this['filters'][filtername]['id'] == '100' ) {
				gt( 'kisgyerek' ).checked = true;
				gt( 's_kisgyerek' ).checked = true;
			}
			else {
				gt( 'kisgyerek' ).checked = false;
				gt( 's_kisgyerek' ).checked = false;
			}
		}
		// korszak
		else if ( 'korszak' == filtername ) {
			if ( this['filters'][filtername] && '' != this['filters'][filtername]['id'] ) {
				gt( 'tori' ).checked = true;
				if ( $('tori_uj_korszak') && $('tori_uj_korszak').style.display != "inline" ) {
					$('tori_uj_korszak').style.display = "inline"
				}
			}
			else {
				gt( 'tori' ).checked = false;
				if ( $('tori_uj_korszak') && $('tori_uj_korszak').style.display == "inline" ) {
					$('tori_uj_korszak').style.display = "none"
				}
			}
		}
		else if ( 'in_cinema' == filtername ) {
			if ( !this['filters'][filtername] || !this['filters'][filtername]['id'] || '' == this['filters'][filtername]['id'] || 0 == this['filters'][filtername]['id'] ) {
				gt( 'in_cinema' ).checked = false;
			}
		}
	},
	// adding selected tags to filterbox, and tag id to selected tags, called from tagger
	addTag: function( tagnumber, tagname, tagid ) {
		// adds tag to filterbox
		this.container.style.display = "block"
		if ( !gt( 'filterbox_tags_' + tagid ) ) {
			var newDivTag = document.createElement("div");
			newDivTag.id = 'filterbox_tags_' + tagid;
			newDivTag.innerHTML = "";
			this.container.appendChild( newDivTag );
		}
		var child = gt( 'filterbox_tags_' + tagid );
		var onclick = 'tagger.delTag(' + tagnumber + ')';
		child.innerHTML = "<span style='font-weight: bold; cursor: pointer;' onclick='" + onclick + "' \
			>" + tagname + "<font \
			style='font-color: red; color: red;'> X</font></span>";
		this.selectedTags.push( tagid );
	},
	// delete selected tags from filterbox, and tag id from selected tags, called from tagger
	delAllTags: function() {
		// removes all tags from filterbox
		for ( var i = 0; i < this.selectedTags.length; i++ ) {
			var tempId = this.selectedTags[i];
			if ( gt( 'filterbox_tags_' + tempId ) ) {
				var child = gt( 'filterbox_tags_' + tempId );
	        	this.container.removeChild( child );
			}
		}
		this.selectedTags = new Array();
		if ( this.filtersToRender == 0 ) {
			if ( this.container.style.display == "block" ){
				this.container.style.display = "none"
			}
		}
	},
	// handle in_cinema checkbox as a filter
	changeInCinemaFilter: function() {
		var myVal = ""; // former 0
		if ( gt( 'in_cinema' ) && gt( 'in_cinema' ).checked ) {
			myVal = 1;
		}
		this.changeFilterElementTrue( 'in_cinema', 'Moziban', myVal, '', 'browserfilterchange' );
	},
	// handle reszenkent checkbox as a filter
	changeReszenkentFilter: function() {
		var myVal = ""; // former 0
		if ( gt( 'reszenkent' ) && gt( 'reszenkent' ).checked ) {
			myVal = 1;
		}
		this.changeFilterElementTrue( 'reszenkent', 'Részenként érthető', myVal, '', 'seriesfilterchange' );
	},
	// handle tori checkbox as a filter
	changeMovieAge: function() {
		if ( gt( 'tori' ).checked ) {
			menuer.showTartalom('history');
			if ( $('tori_uj_korszak') && $('tori_uj_korszak').style.display != "inline" ) {
				$('tori_uj_korszak').style.display = "inline"
			}
		}
		else {
			if ( $('tori_uj_korszak') && $('tori_uj_korszak').style.display == "inline" ){
				$('tori_uj_korszak').style.display = "none"
			}
			this.changeFilterElementTrue( 'korszak', '', '', '', 'browserfilterchange' );
		}
	},
	pickMovieAge: function( id, name ) {
		if ( $('tori_uj_korszak') && $('tori_uj_korszak').style.display != "inline" ) {
			$('tori_uj_korszak').style.display = "inline"
		}
		this.changeFilterElementTrue( 'korszak', name, id, '', this['event'] );
	},
	setSearchChkBox: function( chkId ) {
		$('filmcim_kereso').style.fontWeight = 'normal';
		$('sorozatcim_kereso').style.fontWeight = 'normal';
		$('szinesz_kereso').style.fontWeight = 'normal';
		if ( chkId && $(chkId) && $(chkId + '_kereso')) {
			var myVal = "";
			$(chkId).checked = true;
			$(chkId + '_kereso').style.fontWeight = 'bold';
			switch ( chkId ) {
				case 'filmcim':
					myVal = "movie";
					break;
				case 'sorozatcim':
					myVal = "serie";
					break;
				case 'szinesz':
					myVal = "creative";
					break;
			}
			if ( !this['filters']['kereso_type'] || this['filters']['kereso_type']['id'] != myVal ) {
				this.changeFilterElement( 'kereso_type', '', myVal, '', false, this['event'] );
			}
		}
		else {
			if ( $('filmcim') && $('filmcim').checked ) {
				$('filmcim_kereso').style.fontWeight = 'bold';
				if ( !this['filters']['kereso_type'] || this['filters']['kereso_type']['id'] != 'movie' ) {
					this.changeFilterElement( 'kereso_type', '', 'movie', '', false, this['event'] );
				}
			}
			else if ( $('sorozatcim') && $('sorozatcim').checked ) {
				$('sorozatcim_kereso').style.fontWeight = 'bold';
				if ( !this['filters']['kereso_type'] || this['filters']['kereso_type']['id'] != 'serie' ) {
					this.changeFilterElement( 'kereso_type', '', 'serie', '', false, this['event'] );
				}
			}
			else if ( $('szinesz') && $('szinesz').checked ) {
				$('szinesz_kereso').style.fontWeight = 'bold';
				if ( !this['filters']['kereso_type'] || this['filters']['kereso_type']['id'] != 'creative' ) {
					this.changeFilterElement( 'kereso_type', '', 'creative', '', false, this['event'] );
				}
			}
		}
	},
	setConversationChkBox: function( chkId ) {
		$('filmcim_ajanlo_label').style.fontWeight = 'normal';
		$('sorozatcim_ajanlo_label').style.fontWeight = 'normal';
		$('minden_ajanlo_label').style.fontWeight = 'normal';
		if ( chkId && $(chkId) && $(chkId + '_label')) {
			var myVal = "";
			$(chkId).checked = true;
			$(chkId + '_label').style.fontWeight = 'bold';
			switch ( chkId ) {
				case 'filmcim_ajanlo':
					myVal = "movie";
					break;
				case 'sorozatcim_ajanlo':
					myVal = "serie";
					break;
				case 'minden_ajanlo':
					myVal = "all";
					break;
			}
			if ( !this['filters']['conversation_type'] || this['filters']['conversation_type']['id'] != myVal ) {
				this.changeFilterElement( 'conversation_type', '', myVal, '', false, this['event'] );
			}
		}
		else {
			if ( $('filmcim_ajanlo') && $('filmcim_ajanlo').checked ) {
				$('filmcim_ajanlo_label').style.fontWeight = 'bold';
				if ( !this['filters']['conversation_type'] || this['filters']['conversation_type']['id'] != 'movie' ) {
					this.changeFilterElement( 'conversation_type', '', 'movie', '', false, this['event'] );
				}
			}
			else if ( $('sorozatcim_ajanlo') && $('sorozatcim_ajanlo').checked ) {
				$('sorozatcim_ajanlo_label').style.fontWeight = 'bold';
				if ( !this['filters']['conversation_type'] || this['filters']['conversation_type']['id'] != 'serie' ) {
					this.changeFilterElement( 'conversation_type', '', 'serie', '', false, this['event'] );
				}
			}
			else {
				$('minden_ajanlo').checked = true;
				$('minden_ajanlo_label').style.fontWeight = 'bold';
				if ( !this['filters']['conversation_type'] || this['filters']['conversation_type']['id'] != 'all' ) {
					this.changeFilterElement( 'conversation_type', '', 'all', '', false, this['event'] );
				}
			}
		}
	},
	getConversationChkBoxValue: function() {
		var filterData = this.getFilterElementValue( 'conversation_type' );
		return filterData;
	},
	setSearchTabText: function() {
		var myVal = $('search_text').value;
		this.changeFilterElement( 'search_text', '', myVal, '', false, this['event'] );

	},
	getFilters: function( tabId ) {
		var ret = new Object();
		this['event'] = tabId + "filterchange";

		// kereso
		if ( tabId == "search" ) {
			this.setSearchTabText();
			this.setSearchChkBox();
			ret.searchQuery = this['filters']['search_text']['id'];
			ret.searchCategory = this['filters']['kereso_type']['id'];
		}
		// ajanlo
		if ( tabId == "convrecom" ) {
			this.setConversationChkBox();
			this.setSearchChkBox();
			ret.searchCategory = this['filters']['conversation_type']['id'];
		}
		else {
			// filmek, sorozatok, emberek

			// mufaj
			ret.cathegory = new Array();
			for ( var io = 1; io < 4; io++ ) {
				if ( this['filters']['mufaj' + io] && this['filters']['mufaj' + io]['id']
					&& "" != this['filters']['mufaj' + io]['id']
				) {
					ret.cathegory.push( this['filters']['mufaj' + io]['id'] );
				}
			}
			// tag
			if ( this.selectedTags && 0 < this.selectedTags.length ) {
				ret.tag = new Array();
				for ( var io = 0; io < this.selectedTags.length; io++ ) {
					ret.tag.push( this.selectedTags[io] );
				}
			}
			// ev (birth)
			if ( this['filters']['ev'] && this['filters']['ev']['id'] && "" != this['filters']['ev']['id'] ) {
				ret.birth = this['filters']['ev']['id'];
			}
			// orszag (nation)
			if ( this['filters']['orszag'] && this['filters']['orszag']['id']
				&& "" != this['filters']['orszag']['id']
			) {
				ret.nation = this['filters']['orszag']['id'];
			}

			// humor (humor)
			if ( this['filters']['humor'] && this['filters']['humor']['id']
				&& "" != this['filters']['humor']['id']
			) {
				ret.humor = this['filters']['humor']['id'];
			}
			// filmvege (happyend)
			if ( this['filters']['filmvege'] && this['filters']['filmvege']['id']
				&& "" != this['filters']['filmvege']['id']
			) {
				ret.happyend = this['filters']['filmvege']['id'];
			}
			// kivel (kivel)
			if ( this['filters']['kivel'] && this['filters']['kivel']['id']
				&& "" != this['filters']['kivel']['id']
			) {
				ret.kivel = this['filters']['kivel']['id'];
			}
			// milyet (milyet)
			if ( this['filters']['milyet'] && this['filters']['milyet']['id']
				&& "" != this['filters']['milyet']['id']
			) {
				ret.milyet = this['filters']['milyet']['id'];
			}
			// ajanlas (ajanlas)
			if ( this['filters']['ajanlas'] && this['filters']['ajanlas']['id']
				&& "" != this['filters']['ajanlas']['id']
			) {
				ret.ajanlas = this['filters']['ajanlas']['id'];
			}
			// korszak (korszak)
			if ( this['filters']['korszak'] && this['filters']['korszak']['id']
				&& "" != this['filters']['korszak']['id']
			) {
				ret.korszak = this['filters']['korszak']['id'];
			}
			// in_cinema (in_cinema)
			ret.in_cinema = "";
			if ( this['filters']['in_cinema'] && this['filters']['in_cinema']['id']
				&& "" != this['filters']['in_cinema']['id']
			) {
				ret.in_cinema = this['filters']['in_cinema']['id'];
			}
			// reszenkent (reszenkent?)
			ret.reszenkent = "";
			if ( this['filters']['reszenkent'] && this['filters']['reszenkent']['id']
				&& "" != this['filters']['reszenkent']['id']
			) {
				ret.reszenkent = this['filters']['reszenkent']['id'];
			}
			// veletlen_valaszto (veletlen)
			ret.veletlen = "";
			if ( this['filters']['veletlen_valaszto'] && this['filters']['veletlen_valaszto']['id']
				&& "" != this['filters']['veletlen_valaszto']['id']
			) {
				ret.veletlen = this['filters']['veletlen_valaszto']['id'];
			}
		}
		return ret;
	},
});
// removes elements from array, linear way
Array.prototype.remove = function ( subject ) {
	var r = new Array();
	for( var i = 0, n = this.length; i < n; i++) {
		if ( !(this[i] == subject ) ) {
			r[r.length] = this[i];
		}
	}
	return r;
};
// finds if given element is in array, linear way
Array.prototype.exists = function( search ) {
	for ( var i=0; i < this.length; i++ ) {
		if ( this[i] == search ) {
			return true;
		}
	}
	return false;
};
