jQuery.fn.clearForm = function() {
  return this.each(function() {
    var type = this.type, tag = this.tagName.toLowerCase();
    if (tag == 'form')
      return jQuery(':input',this).clearForm();
    if (type == 'text' || type == 'password' || tag == 'textarea')
      this.value = '';
    else if (type == 'checkbox' || type == 'radio')
      this.checked = false;
    else if (tag == 'select')
      this.selectedIndex = -1;
  });
};

(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();
 		$optdiv = jQuery('.filteroptions');
 		
 		$parent.css('overflow','hidden');
 		$optdiv.css('overflow','hidden');
 		
 		// build element specific options
   		var o = jQuery.meta ? jQuery.extend({}, opts, $this.data()) : opts;
   		//var m = {};
   		var c = 0;
		jQuery('body').append('<div id="jq-option-test" style="display:none"><span></span></div>');
		
		// Selects / Options in ein Object transferieren
		
		
		// SELECT
   		/*
   		$this.find('select').each(function(){
   			var type = this.tagName.toLowerCase();
   			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;
   			m['element'+c].options = {};
   			m['element'+c].option_nav = [];
   			
   			var d = 0;
   			var last_letter = '';
   			
   			// Options
   			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++;
   				
   				// Navigation
   				var first_letter = jQuery(this).text().substring(0,1).toUpperCase();
   				if (m['element'+c].label == "Watt") {
	   				var first_letter = jQuery(this).text().substring(0,3).toUpperCase();
   				}
   				if (last_letter!=first_letter) { 
   					m['element'+c].option_nav.push(first_letter);
   				}
   				last_letter = first_letter;
   			});
   			m['element'+c].option_length = d;
   			c++;
   		});
   		*/   		
   		
   		var c = 0;
   		
		$m = jQuery(m);
		$.each(m, function(i, el){
   			$.each(el.options, function(j, option){
   				jQuery('#jq-option-test span').text(option.label);
   				option.label_height = jQuery('#jq-option-test').height()+4;
   			});
   			el.label = $this.find('#' + el.field_id).closest('div').children().first().html();
   		});
   		
   		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.append('<div class="jq_filter"><div class="selects"></div><div class="selects_bg">&nbsp;</div></div>');
		$parent.find('.jq_filter').find('.selects_bg').css('opacity','0.60');
		$optdiv.find('.options_bg').css('opacity','0.90');
		$optdiv.find('.options, .options_bg').hide();
		for (e in m) {
			var type = m[e].type.toLowerCase();
				// regular selects
				var cclass="";	
				
				var tmp = $this.find('#'+m[e].field_id+'').attr('value');
				if(tmp.length > 0 && tmp.substring(0,3) != 'All') {
					var cclass="highlight";				
				}
				$parent.find('.jq_filter .selects').append('<div class="select '+ cclass +'" id="jq_filter_select_'+e+'"><div class="label">'+m[e].label+'</div><div class="value_label">'+$this.find('#' + m[e].field_id + '-selected').attr('value')+'</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(){
	 		jQuery('.filteroptionsInit').remove();
	 		jQuery('.filteroptions').show();
			if (jQuery(this).hasClass('select_open')) {
				jQuery(this).removeClass('select_open');
				$optdiv.find('.options, .options_bg').fadeOut('normal');
				jQuery('.options_bg > .options_bg_body').html('');
			} else {
				jQuery(this).addClass('select_initialised');
				jQuery(this).parents('.jq_filter').find('.select').removeClass('select_open');
				jQuery(this).addClass('select_open');
				$optdiv.find('.options').empty();
				$optdiv.find('.options, .options_bg').fadeIn('normal');
				var n = jQuery(this).attr('id').replace(/jq_filter_select_/,'');
				var h = '';

				jQuery('.filteroptions .options').attr('id','jq_filter_options_'+n).find('.col, .option_nav').remove();
				var options_height = jQuery('.filteroptions .options').height();
				var show_letter_nav = false;
				if (m[n].option_length>50 && m[n].label != 'Watts' && m[n].label != 'Watt' && m[n].label != 'Potencia' && m[n].label != 'potenza') show_letter_nav = true;
				var c = 0;
				jQuery('.filteroptions .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('.filteroptions .letter_nav').append('<span class="option_nav_letter">'+m[n].option_nav[i]+'</span>');
					}
				}
				jQuery('.filteroptions .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);
				});
				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('.filteroptions .option_nav .col_nav span').bind('mouseup',option_scroll_clear);
				jQuery('.filteroptions .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 (m[n].label == 'Watt') {
	   					var first_letter = m[n].options[o].label.substring(0,3).toUpperCase();
					}
					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('.filteroptions .inner_options').html(h);
				var p = jQuery('.filteroptions .options .col:last').position();
				if (p.left>jQuery('.filteroptions .options').width()) {
					//jQuery('.jq_filter .inner_options').css('width','8000px');
				} else {
					jQuery('.filteroptions .col_nav').hide();
					//jQuery('.jq_filter .letter_nav').hide();
				}
				if (show_letter_nav==false) jQuery('.filteroptions .letter_nav').hide();
				jQuery('.filteroptions .options').css('overflow','hidden');				
				jQuery('.filteroptions .option').hover(function(){jQuery(this).addClass('option_hover');},function(){jQuery(this).removeClass('option_hover');});
				jQuery('.filteroptions #jq_filter_option_'+m[n].option_selected).addClass('option_selected');
				jQuery('.filteroptions .option').click(function(){
					jQuery(this).parents('.jq_filter').find('.select').removeClass('select_open');
					jQuery('.filteroptions .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('form#lichtbasis_product').find('select[name="'+m[s].name+'"] option').removeAttr('selected');
					// jQuery('form#lichtbasis_product').find('select[name="'+m[s].name+'"] option[value="'+m[s].options[o].value+'"]').attr('selected','selected');
					jQuery('input#'+m[s].field_id+'-selected').attr('value', m[s].options[o].value);
					jQuery('input#'+m[s].field_id).attr('value', m[s].options[o].value);
					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));
					$optdiv.find('.options, .options_bg').fadeOut('normal');
					m[n].option_selected = o;
					jQuery('form#lichtbasis_product').submit();
					// FGAD rein : bei Select > neu laden
				});
				
			}
			// 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('form#lichtbasis_product').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(){
//			//console.info(jQuery('form#lichtbasis_product'));
//			jQuery('form#lichtbasis_product').submit();
//		});
		
		// reset generieren
		
		var label_reset = jQuery(this).find('input[type="reset"]').attr('value');
		$parent.find('.jq_filter .selects').append('<div class="resetJQF"><div class="reset_label">'+label_reset+'</div><div class="reset_stub">&nbsp;</div></div>');
		$parent.find('.jq_filter .selects .resetJQF').hover(function(){jQuery(this).addClass('reset_hover');},function(){jQuery(this).removeClass('reset_hover');});
		$parent.find('.jq_filter .selects .resetJQF').click(function(){
			//console.info(jQuery('form#lichtbasis_product'));
			// jQuery('form#lichtbasis_product').clearForm().submit();
			var sitelang = jQuery('#sitelang').val();
			// var url = '/nc/'+sitelang+'/shop.html';
			//window.location = url
			var url = '/nc/'+sitelang+'/shop.html';
			jQuery('form#lichtbasis_product').find('#resetFlag').val("reset");
			jQuery('form#lichtbasis_product').find('#categoryID').val("");
			jQuery('form#lichtbasis_product').attr('action', url); 
 			jQuery('form#lichtbasis_product').submit();
		});
		
		// help generieren
		var label_help = jQuery(this).find('input.help').attr('value');
		var label_moreFilter = jQuery(this).find('input.moreFilter').attr('value');
		var label_lessFilter = jQuery(this).find('input.lessFilter').attr('value');
		var moreSwitch = jQuery(this).find('input#showmoreFlag').attr('value');
		
		if (moreSwitch == 'go') {
			$parent.find('.jq_filter .selects').append('<div class="moreJQF"><div class="more_label"><a id="showless" href="javascript:;">'+label_lessFilter+'</a></div><div class="more_stub">&nbsp;</div></div>');
			$parent.find('.jq_filter .selects .moreJQF').hover(function(){jQuery(this).addClass('more_hover');},function(){jQuery(this).removeClass('more_hover');});
		} else {
			$parent.find('.jq_filter .selects').append('<div class="moreJQF"><div class="more_label"><a id="showmore" href="javascript:;">'+label_moreFilter+'</a></div><div class="more_stub">&nbsp;</div></div>');
			$parent.find('.jq_filter .selects .moreJQF').hover(function(){jQuery(this).addClass('more_hover');},function(){jQuery(this).removeClass('more_hover');});
		}

		$parent.find('.jq_filter .selects').append('<div class="helpJQF"><div class="help_label"><a id="showhelp" href="#helpModal">'+label_help+'</a></div><div class="help_stub">&nbsp;</div></div>');
		$parent.find('.jq_filter .selects .helpJQF').hover(function(){jQuery(this).addClass('help_hover');},function(){jQuery(this).removeClass('help_hover');});

//		$parent.find('.jq_filter .selects .helpJQF').click(function(){
//			this.fancybox({
//				'id'			: 'helpModal',
//				'scrolling'		: 'no',
//				'titleShow'		: false,
//				'onClosed'		: function() {
//			    	$("#helpModal").hide();
//				}
//			});
//		});
//
		// 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() + 1000);
		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;
		$optdiv.find('.options_bg').height(h1-h2-add_subtract);
		$optdiv.find('.options_bg_body').height(h1-h2-10-add_subtract);
		$optdiv.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_value>16&&!target.hasClass('select_period')) {
//			var t = target.find('.value_label').text().substr(0,35-l_label);
			var t = target.find('.value_label').text().substr(0,16);
			target.find('.value_label').text(t+'…');
		}
  	}
  	
  	function adjust_inputs(target) {
  		var label_width = target.parents('.input_container').find('.label').width();
  		if (target.parents('form#lichtbasis_product').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);

function lookup(inputString) {
	var sitelang = jQuery('#sitelang').val();
	if(inputString.length == 0) {
		jQuery('.options, .options_bg').fadeOut(); // Hide the suggestions box
	} else {
		jQuery.post("/fileadmin/scripts/rpc.php", {queryString: ""+inputString+"|"+sitelang}, function(data) { // Do an AJAX call
			jQuery('.filteroptionsInit').remove();
			jQuery('.filteroptions').show();
			jQuery('.options, .options_bg').fadeIn(); // Show the suggestions box
			jQuery('.options').append().html(data); // Fill the suggestions box
			jQuery('.filteroptions .option_nav #jq-option-col-bk').click(function(){
				var pos = jQuery('.inner_options').position();
				jQuery('.inner_options').animate({left:+pos.left+175},300);
			});
			jQuery('.filteroptions .option_nav #jq-option-col-fw').click(function(){
				var pos = jQuery('.inner_options').position();
				jQuery('.inner_options').animate({left:+pos.left-175},300);
			});

		});
	}
}

jQuery.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return jQuery.getUrlVars()[name];
  }
});

jQuery(document).ready(function(){

	jQuery('#delname').change( function() {
		var sitelang = jQuery('#sitelang').val();
		jQuery(this).parent().parent('form').attr("action",'nc/'+sitelang+'/shop/warenkorb.html?tx_msitnotepad_pi1%5BshowUid%5D=113&tx_msitnotepad_pi1%5BtoDo%5D=p1');
		jQuery(this).parent().parent('form').submit();
	});
	
	jQuery("a.fanzzy").fancybox({
		'titleShow'		: false
	});
			
	jQuery('#lichtbasis_product').filter_gui();
	jQuery("a#showhelp").fancybox({
		'hideOnContentClick': true,
		'width': 400,
		'overlayOpacity': 0.7,
		'overlayColor': '#000'

	});
	jQuery("a#showmore").click(function () {
		jQuery('form#lichtbasis_product').find('#showmoreFlag').val("go");
		jQuery('form#lichtbasis_product').attr('action', document.URL); 
 		jQuery('form#lichtbasis_product').submit();
    });

	jQuery("a#showless").click(function () {
		jQuery('form#lichtbasis_product').find('#showmoreFlag').val(" "); 
		jQuery('form#lichtbasis_product').attr('action', document.URL); 
 		jQuery('form#lichtbasis_product').submit();
    });

});
