;;(function($){	
	$.fn.JAccordeon=function(options){
		var def = {
				active:1,
				header: 'h3'
			};
		var opts = $.extend(def, options);

		return this.each(function(){
			$(this).addClass("ui-accordion");
			$(this).find(opts.header).addClass("ui-accordion-header");
			$(this).find('.ui-accordion-header').next().addClass("ui-accordion-content").hide();
			
			$(this).find('.ui-accordion-header').click(function() {
				var elemList = '#gb_slider,a.gb_select_reset,.gb_go_button,#gb_slide_from,#gb_slide_to,.gb_amount';
				//hide special elements for smooth animation
				$(this).next('.ui-accordion-content').find(elemList).hide();
				//hide special elements  end
				$(this).next('.ui-accordion-content').toggle('fast',function(){
					$(this).find(elemList).show();
				});
				
				

				$(this).toggleClass("ui-state-active");
				return false;
			}).next().hide();
							
			if(typeof(opts.active)==typeof(1)){
				$(this).find('.ui-accordion-header:eq('+(opts.active-1)+')').toggleClass("ui-state-active").next().toggle();
			} else if(typeof(opts.active)==typeof([1,1])){
				for(var i=0; i<opts.active.length;i++){
					$(this).find('.ui-accordion-header:eq('+(opts.active[i]-1)+')').toggleClass("ui-state-active").next().toggle();
				}
			}
		});
	};
	
	$.fn.selectRange = function(start, end) {
	    return this.each(function() {
	        if(this.setSelectionRange) {
	            this.focus();
	            this.setSelectionRange(start, end);
	        } else if(this.createTextRange) {
	            var range = this.createTextRange();
	            range.collapse(true);
	            range.moveEnd('character', end);
	            range.moveStart('character', start);
	            range.select();
	        }
	    });
	};
	
	$.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());
})(jQuery);

var GrabBag=function(opt){
	this.from=0;
	this.to=0;
	var _self=this;
	
	this.def={
		accordion:{
			active:[1,2]
		},
		slider:{
				range: true,
				min: $('#grabbag #gb_min').val()*1,
				max: $('#grabbag #gb_max').val()*1,
				step: 1,
				values: [$('#grabbag #gb_from').val()*1, $('#grabbag #gb_to').val()*1],
				slide: function(event, ui) {
					_self.from=ui.values[0];
					_self.to=ui.values[1];
					$('#grabbag #gb_from').val(ui.values[0]);
					$('#grabbag #gb_to').val(ui.values[1]);
					if(!$.browser.chrome){
						$("#grabbag .gb_pricearea .gb_go_button a").fadeTo("slow", 1);
					}
					}
				},
		actionlink:"#"
	};

	this.init=function(opt){
		var opts = $.extend(this.def, opt);
		/* Loading effects */
		$('#grabbag .gb_searchfilters,#grabbag .gb_pricearea').show();
		$('#grabbag .gb_content').css({height:'auto'});
		
		$('#grabbag .gb_accordion').show();
		
		try{
			if(opts.slider.min < opts.slider.max){
				$('#grabbag #gb_slider').slider(opts.slider);
				$('#grabbag #gb_slide_from').text(opts.slider.min);
				$('#grabbag #gb_slide_to').text(opts.slider.max);
			} else {
				$('#grabbag .gb_pricearea').parent().hide();
			}
		}catch(e){
			$('#grabbag .gb_pricearea').parent().hide();
		}
		_self.from=opts.slider.values[0];
		_self.to=opts.slider.values[1];
		
		if(!$.browser.chrome){
			$("#grabbag .gb_pricearea .gb_go_button a").fadeTo("fast", 0.3);
		}
		
		$('#grabbag #gb_from').change(function(){
			var val=parseInt($(this).val());
			if(typeof(val) == typeof(1) && val >= opts.slider.min && val <= _self.to){
				$(this).val(val);
				$('#grabbag #gb_slider').slider('values',0,val);
				_self.from=val;
			} else if(typeof(val) == typeof(1) && val >= opts.slider.min){
				$(this).val(_self.to);
				$('#grabbag #gb_slider').slider('values',0,_self.to);
				_self.from=_self.to;
			} else {
				$(this).val(opts.slider.min);
				$('#grabbag #gb_slider').slider('values',0,opts.slider.min);
				_self.from=opts.slider.min;
			}
			//$("#grabbag .gb_pricearea .gb_go_button a").fadeTo("slow", 1);
		}).focus(function(){
			$(this).selectRange(0,$(this).val().length);
		}).keypress(function (e) {
			if(e.which==13){
				// submit data on enter
				$(this).change();
				$('#grabbag #gb_to').focus();
			}
		});
		
		$('#grabbag #gb_to').change(function(){
			var val=parseInt($(this).val());
			if(typeof(val) == typeof(1) && val <= opts.slider.max && val >= _self.from){
				$(this).val(val);
				$('#grabbag #gb_slider').slider('values',1,val);
				_self.to=val;
			} else if(typeof(val) == typeof(1) && val <= opts.slider.max){
				$(this).val(_self.from);
				$('#grabbag #gb_slider').slider('values',1,_self.from);
				_self.to=_self.from;
			}  else {
				$(this).val(opts.slider.max);
				$('#grabbag #gb_slider').slider('values',1,opts.slider.max);
				_self.to=opts.slider.max;
			}
		}).focus(function(){
			$(this).selectRange(0,$(this).val().length);
		}).keypress(function (e) {
			if(e.which==13){
				// submit data on enter
				$(this).change();
				$('#grabbag .gb_pricearea .gb_go_button a').click();
			}
		});
		
		$('#grabbag .gb_searchfilters .gb_sel ul').each(function(){
			var $sel = $('<select></select>');

			$(this).find("li:gt(0) a").each(function(){
				$sel.append(
					$('<option></option>')
					.val($(this).attr("href"))
					.text($(this).text())
					.attr("selected",(($(this).parent().hasClass("selected"))?true:false))
				);
			});
			
			$sel.change(function(){
				document.location.href=$(this).val();
			});
			$(this).before($sel);
			
			if (!$.browser.msie || ($.browser.msie && parseInt($.browser.version) > 6)){
				$sel.sSelect({ddMaxHeight:200});
			} else {
				$(this).find("li:eq(0) a").each(function(){
					$sel.prepend(
						$('<option></option>')
						.val($(this).attr("href"))
						.text($(this).text())
						.attr("selected",(($(this).parent().hasClass("selected"))?true:false))
					);
				});
				$sel.css({
					display:'inline'
				});
			}
			
			if($(this).find(".selected a").attr("href") && $(this).find(".selected a").attr("href") != $(this).find("a:eq(0)").attr("href")){
				$(this).parent().find('.newListSelected')
				.append(
					$("<a></a>")
					.attr("href",$(this).find("a:eq(0)").attr("href"))
					.addClass("gb_select_reset")
					.text("x")
					.css({
						"z-index":000,
						"margin-top":-14,
						"margin-left":136
					})
				).find('.selectedTxt').css({
					color:"#CC0000"
				});	
			} else {
				$(this).parent().find('.newListSelected .selectedTxt').text($(this).find("li:eq(0) a").text());
			}
		});
		
		$('#grabbag .gb_searchfilters').css({
			height:$('#grabbag .gb_searchfilters .gb_sel').length*23
		});
		
		$('#grabbag .gb_accordion').JAccordeon(opts.accordion); // active: array aus offenen accordion reitern 1-n
		
		$('#grabbag .gb_pricearea .gb_go_button a').click(function(){
			document.location.href=opts.actionlink.split("$from").join(_self.from).split("$to").join(_self.to);
		});
		
		/* Build nav */
		buildNav('#grabbag .gb_categories h1','gb_title');
		
		
		if($('#grabbag .gb_categories ul.levels3').length > 0){
			buildNav('#grabbag .gb_categories ul li ul li ul li > span ~ a','gb_level_d');
			buildNav('#grabbag .gb_categories ul li ul li > span ~ a','gb_level_c');
			buildNav('#grabbag .gb_categories ul li > span ~ a','gb_level_a');
		} else if($('#grabbag .gb_categories ul.levels4').length > 0){
			buildNav('#grabbag .gb_categories ul li ul li ul li ul li > span ~ a','gb_level_d');
			buildNav('#grabbag .gb_categories ul li ul li ul li > span ~ a','gb_level_c');
			buildNav('#grabbag .gb_categories ul li ul li > span ~ a','gb_level_b');
			buildNav('#grabbag .gb_categories ul li > span ~ a','gb_level_a');
		} else {
			buildNav('#grabbag .gb_categories ul li ul li ul li > span ~ a','gb_level_d');
			buildNav('#grabbag .gb_categories ul li ul li > span ~ a','gb_level_c');
			buildNav('#grabbag .gb_categories ul li > span ~ a','gb_level_a');
		}
		
		$("#grabbag .gb_categories  ul li .gb_amount").click(function(){
			$(this).parent().find("a:eq(0)").click();
		});
		
		$('#grabbag .gb_content').prepend(
			$('<div></div>')
			.attr('id','gb_loading_layer')
			.css({
				position:'absolute',
				display:'none',
				width:206,
				'margin-left':($.browser.msie)?0:19,
				height:'1px',
				'background-image':'url(/images/gb_loader.gif)',
				'background-position':'50% 50%',
				'background-repeat':'no-repeat',
				'background-color':'#000000',
				'z-index':999
			})
		);
				
	};
	
	this.showLoading=function(){
		$('#grabbag #gb_loading_layer')
		.css({
			height:	$('#grabbag .gb_content').height()
		})
		.fadeTo("fast", 0.7)
		.show();
	};
	
	this.hideLoading=function(){
		$('#grabbag #gb_loading_layer').fadeTo("fast", 0, function(){
			$(this).hide();		
		});
	};
	
	this.setCartValue=function(value){
		$('#grabbag .gb_content .gb_chart a.value').text(value);
	};
	
	this.setWishlistValue=function(value){
		$('#grabbag .gb_content .gb_bookmark a.value').text(value);
	};
	
	this.init(opt);
};

function buildNav(selector,className){
	var $obj=$(selector);
	$obj.wrap(
		$("<div></div>")
		.addClass("gb_nav_top")
	);
	$obj.parent().wrap(
		$("<div></div>")
		.addClass("gb_nav_bottom")
	);
	$obj.parent().parent().wrap(
		$("<div></div>")
		.addClass("gb_nav_middle")
	);
	$obj.parent().parent().parent().wrap(
		$("<div></div>")
		.addClass(className)
		.addClass("gb_nav_wrap")
	);
};