(function(jQuery) {
	jQuery.fn.filter_gui = function(options) {
 	//debug(this);
 	// build main options before element iteration
 	var opts = jQuery.extend({}, jQuery.fn.filter_gui.defaults, options);
 	// iterate and reformat each matched element
 	return this.each(function() {
 		$this = jQuery(this);
 		$parent = jQuery(this).parent();
 		
 		$parent.css('overflow','hidden');
 		
 		// build element specific options
   		var o = jQuery.meta ? jQuery.extend({}, opts, $this.data()) : opts;
   		var m = {};
   		var c = 0;
   		$this.find('select, input').each(function(){
   			var type = this.tagName.toLowerCase();
   			if (type=='input') type += '_'+jQuery(this).attr('type');
   			m['element'+c] = {};
   			m['element'+c].name = jQuery(this).attr('name');
   			m['element'+c].label = jQuery(this).parent().find('div:first').text();
   			m['element'+c].type = type;
   			if (type=='select') {
   				m['element'+c].options = {};
   				m['element'+c].option_nav = [];
   				var d = 0;
   				var last_letter = '';
   				jQuery('body').append('<div id="jq-option-test"><span></span></div>');
   				jQuery(this).find('option').each(function(){
   					m['element'+c].options['element'+d] = {};
   					m['element'+c].options['element'+d].value = jQuery(this).attr('value');
   					m['element'+c].options['element'+d].label = jQuery(this).text();
   					jQuery('#jq-option-test span').text(jQuery(this).text());
   					m['element'+c].options['element'+d].label_height = jQuery('#jq-option-test').height()+4;
   					m['element'+c].options['element'+d].selected = jQuery(this).attr('selected');
   					if (jQuery(this).attr('selected')==true) {
   						m['element'+c].option_selected = 'element'+d;
   					}
   					d++;
   					var first_letter = jQuery(this).text().substring(0,1).toUpperCase();
   					if (last_letter!=first_letter) { 
   						m['element'+c].option_nav.push(first_letter);
   					}
   					last_letter = first_letter;
   				});
   				m['element'+c].option_length = d;
   				jQuery('#jq-option-test').remove();
   			}
   			c++;
   		});
   		jQuery.fn.filter_gui.matrix = m;
		
		$this.hide();
		$parent.append('<div class="jq_filter"><div class="options"></div><div class="options_bg"><div class="options_bg_body"></div><div class="options_bg_top">&nbsp;</div></div><div class="selects"></div><div class="selects_bg">&nbsp;</div></div>');
		$parent.find('.jq_filter').find('.selects_bg').css('opacity','0.60');
		$parent.find('.jq_filter').find('.options_bg').css('opacity','0.90');
		$parent.find('.options, .options_bg').hide();
		for (e in m) {
			var type = m[e].type.toLowerCase();
			if (type=='select') { 
				if (m[e].name.indexOf('fromMonth')>0) {
					// month/year combinations
					var index = eval(e.replace(/element/,''));
					var plus2 = index+2;
					//alert(m['element'+(index-1)].label);
					$parent.find('.jq_filter .selects').append('<div class="select select_period" id="jq_filter_select_'+e+'"><div class="label">'+m[e].label+'</div><div class="value_label"><span class="value_label_fromDate"><span class="value_label_element'+index+'">'+$this.find('select[name="'+m[e].name+'"] option:selected').val()+'</span><span>&nbsp;|&nbsp;</span><span class="value_label_element'+(index+1)+'">'+$this.find('select[name="'+m[e].name.replace(/Month/,'Year')+'"] option:selected').text()+'</span></span><span class="sublabel">'+m['element'+plus2].label+'</span><span class="value_label_toDate"><span class="value_label_element'+(index+2)+'">'+$this.find('select[name="'+m[e].name.replace(/from/,'to')+'"] option:selected').val()+'</span><span>&nbsp;|&nbsp;</span><span class="value_label_element'+(index+3)+'">'+$this.find('select[name="'+m[e].name.replace(/fromMonth/,'toYear')+'"] option:selected').val()+'</span></span></div></div>');
				} else if (m[e].name.indexOf('fromYear')>0||m[e].name.indexOf('toYear')>0||m[e].name.indexOf('toMonth')>0) {
					// other date selects are ignored
				} else {
					// regular selects
					$parent.find('.jq_filter .selects').append('<div class="select" id="jq_filter_select_'+e+'"><div class="label">'+m[e].label+'</div><div class="value_label">'+$this.find('select[name="'+m[e].name+'"] option:selected').text()+'</div></div>');
				}
			} else if (type=='input_text') {
				$parent.find('.jq_filter .selects').append('<div class="input_container" id="jq_filter_input_'+e+'"><div class="label">'+m[e].label+'</div><div class="input_outer_container"><div class="input_inner_container"><input name="jq_filter_input_'+e+'" type="text" value="'+$this.find('input[name="'+m[e].name+'"]').val()+'" /></div><div class="input_stub">&nbsp;</div></div></div>');
			}
		}
		$parent.find('.jq_filter .select').hover(function(){jQuery(this).addClass('select_active');},function(){jQuery(this).removeClass('select_active');});
		$parent.find('.jq_filter .select').click(function(){
			if (jQuery(this).hasClass('select_open')) {
				jQuery(this).removeClass('select_open');
				jQuery(this).parents('.jq_filter').find('.options, .options_bg').fadeOut('normal');
			} else {
				jQuery(this).addClass('select_initialised');
				jQuery(this).parents('.jq_filter').find('.select').removeClass('select_open');
				jQuery(this).addClass('select_open');
				jQuery(this).parents('.jq_filter').find('.options').empty();
				jQuery(this).parents('.jq_filter').find('.options, .options_bg').fadeIn('normal');
				var n = jQuery(this).attr('id').replace(/jq_filter_select_/,'');
				var h = '';
				if (jQuery(this).hasClass('select_period')) {
					h += '<div class="option_label">'+jQuery(this).find('.label').text()+'</div><div class="option_col"><div class="option_row" id="jq_filter_options_fromMonth"></div><div class="option_row" id="jq_filter_options_fromYear"></div></div><div class="option_label">'+jQuery(this).find('.sublabel').text()+'</div><div class="option_col"><div class="option_row" id="jq_filter_options_toMonth"></div><div class="option_row" id="jq_filter_options_toYear"></div><div class="submit submit_options"><div class="submit_label">'+jQuery('input[name="jq-label-confirm"]').val()+'</div><div class="submit_stub">&nbsp;</div></div></div>';
					jQuery('.jq_filter .options').html(h);
					var src = ['fromMonth','fromYear','toMonth','toYear'];
					for (i=0;i<src.length;i++) {
						var e = 'element'+(eval(n.replace(/element/,''))+i);
						var h = '';
						var monthLabels_tmp = ['Monat','Januar','Februar','März','April','Mai','Juni','Juli','August','September','Oktober','November','Dezember'];
						for (o in m[e].options) {
							var lb = m[e].options[o].label;
							var v = m[e].options[o].value;
							//if ((i==0||i==2)&&lb!='Monat') lb = monthLabels_tmp[eval(lb)];
							if (v!=0) h += '<div class="option" id="jq_filter_option_'+e+'_'+o+'"><span>'+lb+'</span></div>';
						}
						jQuery('#jq_filter_options_'+src[i]).html(h);
						jQuery('.jq_filter #jq_filter_option_'+e+'_'+m[e].option_selected).addClass('option_selected');
					}
					jQuery('.jq_filter .option').hover(function(){jQuery(this).addClass('option_hover');},function(){jQuery(this).removeClass('option_hover');});
					jQuery('.jq_filter .option').click(function(){
						jQuery(this).parent().find('.option').removeClass('option_selected');
						jQuery(this).addClass('option_selected');
					});
					jQuery('.jq_filter .options .submit_options').hover(function(){jQuery(this).addClass('submit_options_hover');},function(){jQuery(this).removeClass('submit_options_hover');});
					jQuery('.jq_filter .submit_options').click(function(){
						jQuery(this).parents('.jq_filter').find('.select').removeClass('select_open');
						jQuery(this).parents('.options').find('.option_selected').each(function(){
							var tmp = jQuery(this).attr('id').replace(/jq_filter_option_/,'').split('_');
							var o = tmp[1];
							var s = tmp[0];
							jQuery(this).parents('form').find('select[name="'+m[s].name+'"] option').removeAttr('selected');
							jQuery(this).parents('form').find('select[name="'+m[s].name+'"] option[value="'+m[s].options[o].value+'"]').attr('selected','selected');
							m[s].option_selected = o;
							jQuery(this).parents('.jq_filter').find('span.value_label_'+s).text(m[s].options[o].value);
							
						});
						jQuery(this).parents('.jq_filter').find('.options, .options_bg').fadeOut('normal');
						/*
						jQuery(this).parents('.jq_filter').find('.select#jq_filter_select_'+s+' .value_label').text(m[s].options[o].label);

						*/
					});
				} else {
					jQuery('.jq_filter .options').attr('id','jq_filter_options_'+n).find('.col, .option_nav').remove();
					var options_height = jQuery('.jq_filter .options').height();
					var show_letter_nav = false;
					if (m[n].option_length>50) show_letter_nav = true;
					var c = 0;
					jQuery('.jq_filter .options').append('<div class="option_nav"><div class="options_label">'+m[n].label+'</div><div class="letter_nav"></div><div class="col_nav"><span id="jq-option-col-bk">&nbsp;</span><span id="jq-option-col-fw">&nbsp;</span></div></div><div class="inner_options"></div>');
					for (i=0;i<m[n].option_nav.length;i++) {
						if (i>0&&m[n].option_nav[i]!='') {
							jQuery('.jq_filter .letter_nav').append('<span class="option_nav_letter">'+m[n].option_nav[i]+'</span>');
						}
					}
					jQuery('.jq_filter .option_nav .option_nav_letter').click(function(){
						var l = jQuery(this).text();
						var pos = jQuery('#options_'+l).position();
						var c = jQuery('#options_'+l);
						window.jq_scroll_current = jQuery('.inner_options .col').index(c);
						jQuery('.inner_options').animate({left:-pos.left},1000);
					});
					//jQuery('.letter_nav').show();
					//jQuery('.letter_nav').append('<span class="option_nav_letter" id="test"></span>');
					window.jq_scroll_current = 0;
					var option_scroll = function() {
						jQuery('.inner_options').stop();
						noscroll = false;
						if (window.jq_scroll_dir=='fw') {
							if (window.jq_scroll_current<jQuery('.inner_options .col').length-1) { 
								window.jq_scroll_current++;
							} else {
								noscroll = true;
							}
						} else {
							if (window.jq_scroll_current>0) {
								window.jq_scroll_current--;
							} else {
								noscroll = true;
							}
						}
						if (noscroll===false) {
							var pos = jQuery('.inner_options .col:eq('+eval(window.jq_scroll_current)+')').position();
							jQuery('.inner_options').animate({left:-pos.left},300);
						}
												
					}
					var option_scroll_trigger = function() {
						var dir = jQuery(this).attr('id').replace(/jq-option-col-/,'');
						window.jq_scroll_dir = dir;
						option_scroll(dir);
						option_scroll_interval = setInterval(option_scroll,400);
					}
					var option_scroll_clear = function() {
						clearInterval(option_scroll_interval);
					}					
					jQuery('.jq_filter .option_nav .col_nav span').bind('mouseup',option_scroll_clear);
					jQuery('.jq_filter .option_nav .col_nav span').bind('mousedown',option_scroll_trigger);				
					var last_letter = '';
					var col_height = 0;	
					var h = '<div class="col">';
					for (o in m[n].options) {
						var first_letter = m[n].options[o].label.substring(0,1).toUpperCase();
						/*
						if ((col_height+m[n].options[o].label_height>options_height||(first_letter!=last_letter&&show_letter_nav===true))&&last_letter!='') { 
							var add = '';
							jQuery('.jq_filter .inner_options').append('<div class="col_border">&nbsp;</div><div class="col"'+add+'></div>');
							col_height = 0;
						}
						h = '<div class="option" id="jq_filter_option_'+o+'"><span>'+m[n].options[o].label+'</span></div>';
						if (m[n].options[o].label!='') {
							jQuery('.jq_filter .inner_options .col:last').append(h);
						}
						if (first_letter!=last_letter&&o!='element0') { 
							jQuery('.jq_filter .inner_options .col:last').attr('id', 'options_'+first_letter);
						}*/
						if ((col_height+m[n].options[o].label_height>options_height-1||(first_letter!=last_letter&&show_letter_nav===true))&&last_letter!='') { 
							var add = '';
							if (first_letter!=last_letter&&o!='element0') add = ' id="options_'+first_letter+'"';
							h += '</div><div class="col_border">&nbsp;</div><div class="col"'+add+'>';
							col_height = 0;
						}
						if (m[n].options[o].label!='') {
							h += '<div class="option" id="jq_filter_option_'+o+'"><span>'+m[n].options[o].label+'</span></div>';
						}
													
						
						
						last_letter = first_letter;
						col_height += m[n].options[o].label_height;
						//if (c%7==0&&c!=0) h+= '</div><div class="col">';
						//c++;
					}
					h += '</div>';
					jQuery('.jq_filter .inner_options').html(h);
					var p = jQuery('.jq_filter .options .col:last').position();
					if (p.left>jQuery('.jq_filter .options').width()) {
						//jQuery('.jq_filter .inner_options').css('width','8000px');
					} else {
						jQuery('.jq_filter .col_nav').hide();
						//jQuery('.jq_filter .letter_nav').hide();
					}
					if (show_letter_nav==false) jQuery('.jq_filter .letter_nav').hide();
					jQuery('.jq_filter .options').css('overflow','hidden');				
					jQuery('.jq_filter .option').hover(function(){jQuery(this).addClass('option_hover');},function(){jQuery(this).removeClass('option_hover');});
					jQuery('.jq_filter #jq_filter_option_'+m[n].option_selected).addClass('option_selected');
					jQuery('.jq_filter .option').click(function(){
						jQuery(this).parents('.jq_filter').find('.select').removeClass('select_open');
						jQuery('.jq_filter .option').removeClass('option_selected');
						jQuery(this).addClass('option_selected');
						var o = jQuery(this).attr('id').replace(/jq_filter_option_/,'');
						var s = jQuery(this).parents('.options').attr('id').replace(/jq_filter_options_/,'');
						jQuery(this).parents('form').find('select[name="'+m[s].name+'"] option').removeAttr('selected');
						jQuery(this).parents('form').find('select[name="'+m[s].name+'"] option[value="'+m[s].options[o].value+'"]').attr('selected','selected');
						jQuery(this).parents('.jq_filter').find('.select#jq_filter_select_'+s+' .value_label').text(m[s].options[o].label);
						adjust_labels(jQuery(this).parents('.jq_filter').find('.select#jq_filter_select_'+s));
						jQuery(this).parents('.jq_filter').find('.options, .options_bg').fadeOut('normal');
						m[n].option_selected = o;
					});
				}
			}
			// add custom scrolling
			//jQuery('.jq_filter .options').jScrollPane({showArrows:true}); 
		});
		$parent.find('.jq_filter input').focus(function(){
			jQuery(this).parents('.input_container').addClass('input_active');
			jQuery(this).parents('.input_container').addClass('input_initialised');
		});
		$parent.find('.jq_filter input').bind("change keyup", function() {
		   var n = jQuery(this).parents('.input_container').attr('id').replace(/jq_filter_input_/,'');
           jQuery(this).parents('form').find('input[name="'+m[n].name+'"]').val(jQuery(this).val());
      	}); 
		$parent.find('.jq_filter input').blur(function(){
			jQuery(this).parents('.input_container').removeClass('input_active');
		});
		
		// submit generieren
		
		var label_submit = jQuery(this).find('input[type="button"]').attr('value');
		$parent.find('.jq_filter .selects').append('<div class="submit"><div class="submit_label">'+label_submit+'</div><div class="submit_stub">&nbsp;</div></div>');
		$parent.find('.jq_filter .selects .submit').hover(function(){jQuery(this).addClass('submit_hover');},function(){jQuery(this).removeClass('submit_hover');});
		$parent.find('.jq_filter .selects .submit').click(function(){
			jQuery(this).parents('form').submit();
		});
		
		// dimensionen anpassen
		
		$parent.find('.jq_filter .select').each(function(){
			adjust_labels(jQuery(this));
		});
		
		$parent.find('.jq_filter input').each(function(){
			adjust_inputs(jQuery(this));
		});
		
		//jQuery(this).parent().find('.jq_filter').height(jQuery(this).parent().height()+21);
		if (c<3) {
			$parent.find('.jq_filter').height(181);
			if (jQuery.browser.msie===true) $parent.find('.jq_filter').height(181); 
		} else {
			$parent.height(190);
		}
					
		$parent.find('.jq_filter .selects_bg').height(jQuery(this).parent().find('.jq_filter .selects').height());
		var h1 = $parent.find('.jq_filter').height();
		var h2 = $parent.find('.jq_filter .selects_bg').height();
		var add_subtract = 30;
		if (jQuery.browser.msie===true && jQuery.browser.version<8) add_subtract = 36;
		$parent.find('.jq_filter').find('.options_bg').height(h1-h2-add_subtract);
		$parent.find('.jq_filter').find('.options_bg_body').height(h1-h2-10-add_subtract);
		$parent.find('.jq_filter').find('.options').height(h1-h2-25-add_subtract);		
			
		jQuery('.jq_filter, .jq_filter input').keydown(function(e) { 
			if (jQuery('.jq_filter').length>0) {
				if (e.keyCode == 13) {
					jQuery('.jq_filter .selects .submit').click();
					return false; 
				}
			}
		});
		
 	});
  	};
  	
  	function adjust_labels(target) {
  		var label_width = target.find('.label').width();
  		target.find('.value_label').width(150-label_width);
		var l_label = target.find('.label').text().length;
		var l_value = target.find('.value_label').text().length;
		if (l_label+l_value>20&&!target.hasClass('select_period')) {
			var t = target.find('.value_label').text().substr(0,20-l_label);
			target.find('.value_label').text(t+'…');
		}
  	}
  	
  	function adjust_inputs(target) {
  		var label_width = target.parents('.input_container').find('.label').width();
  		if (target.parents('form#tx_ddproductmatrix_pi1_form').length>0) {
  			target.width(270-label_width);
  		} else {
  			target.width(163-label_width);
  		}
  	}
  	
  	//
  	// private function for debugging
  	//
  	function debug($obj) {
 		if (window.console && window.console.log)
   			window.console.log('hilight selection count: ' + $obj.size());
  	};
  	//
  	// plugin defaults
  	//
  	jQuery.fn.filter_gui.defaults = {
 		foreground: 'red',
 		background: 'yellow'
  	};
	//
	// end of closure
	//
})(jQuery);

jQuery(document).ready(function(){
	if (jQuery('.tx_ddfiltergeneralmatrix_filter_wrap').length>0) {
		jQuery('.tx_ddfiltergeneralmatrix_filter_wrap').filter_gui();
	}
});