// When the DOM is ready, init scripts.
jQuery(function( $ ){
	var is_ajax_toolbar = 0;
	/* SPIELWIESE VON SANDRO */
	var fltOptions = {};
	var w24Loader = new w24_loading();

	$("#w24-cat, #w24-topic").each(function(){
		var name = this.id.match(/^w24-(.*)/)[1];
		fltOptions[name] = $(this).val();
	});

	/* left nav category/filter switch */
	$('.w24-left-nav-switch').click(function(){
		if ($(this).parent().is('.w24-cat-btn')) {
			$('.w24-flt-btn').find('a').removeClass('active');
			$('.w24-cat-btn').find('a').addClass('active');
			$('#narrow-by-list2').removeClass('display-none');
			$('.w24-filter-list').addClass('display-none');
		}
		else {
			$('.w24-cat-btn').find('a').removeClass('active');
			$('.w24-flt-btn').find('a').addClass('active');
			$('#narrow-by-list2').addClass('display-none');
			$('.w24-filter-list').removeClass('display-none');
			w24Ajax(fltOptions);
		}
		return false;
	});

	/* left nav select boxes*/
	$('.w24-left-select').bind('click', w24FltDropdown);

	function w24FltDropdown(ev){
		if ($(this).parent().parent().find('.w24-select-dropdown').is('.open')) {
			$(this).parent().parent().find('.w24-select-dropdown').slideUp('fast').removeClass('open');
			$("body").unbind("click", closeSelect);
		}
		else {
			$('.w24-filter-list').find('.open').css('display','none').removeClass('open');
			$(this).parent().parent().find('.w24-select-dropdown').slideDown('fast').addClass('open');
			$("body").bind("click", closeSelect);
		}
		ev.stopPropagation();
	}


	function closeSelect() {
		$('.w24-select-dropdown').removeClass('open').hide();
	}


	//$('.w24-flt-option').click(function(){
	$('.w24-flt-option, .w24-flt-pricesubmit, .w24-flt-checkbox').live('click', function(){
		/*
		$('.w24-cancel-filter').css('display','block').removeClass('display-none');
		var fltBlock = $(this).parentsUntil('.w24-filter-list');
		fltBlock.find('.w24-left-select').addClass('display-none');
		fltBlock.find('.w24-cancel-btn').css('display','block').removeClass('display-none').html($(this).html().substring(0,strpos($(this).html(),' (')));
		*/

		if ($(this).is('.w24-flt-pricesubmit')) {
			t1 = 'price';
			t2 = $('.w24-preis-von').val() + '/' + $('.w24-preis-bis').val();
		}
		else {
			var tmp = this.id.split('---');
			var t1 = tmp[0];
			tmp[0] = '';
			//var t2 =$('.w24-left-nav-switch').click(function(){ tmp.join('')});
			var t2 = tmp[1];
		}
		fltOptions[t1] = t2;
		w24Ajax(fltOptions);
	});


	$('.w24-cancel-btn').live('click', function(){
		//if ($(this).is('.w24-cancel-filter, .w24-cancel-cat')) {
		if ($(this).is('.w24-cancel-filter')) {
			$('.w24-filter-list').show();
			$('.w24-filter-list-ajax').hide();
			w24Ajax({"cat": $('#w24-topic').val()});
		}
		else {
			var tmp = this.id.split('---');
			var t1 = tmp[0];
			if ($(this).is('.w24-cancel-cat')) {
				delete fltOptions;
				fltOptions[t1] = $('#w24-topic').val();
			}else {
				if (t1 == 'Hersteller') {
					delete fltOptions['sup'];
				} else {
					delete fltOptions[t1];
				}
			}
			w24Ajax(fltOptions);
		}
		return false;
	});

	/* toggle header sloagan */
	/*
	$(document).everyTime(5000, function() {
		if ($('.header-slogan1').is('.display-none')) {
			$('.header-slogan2').fadeOut('400', function() {
				// Animation complete
				$('.header-slogan2').addClass('display-none');
				$('.header-slogan1').fadeIn('400', function() {
					// Animation complete
					$('.header-slogan1').removeClass('display-none');
				});
			});
		}
		else {
			$('.header-slogan1').fadeOut('400', function() {
				// Animation complete
				$('.header-slogan1').addClass('display-none');
				$('.header-slogan2').fadeIn('400', function() {
					// Animation complete
					$('.header-slogan2').removeClass('display-none');
				});
			});
		}
	}, 0);
	*/

	$(document).everyTime(5000, function() {
		var max = 8;
		var a = $('.header-slogan').find('.display-slogan');
		a.fadeOut('400', function() {
				// Animation complete
				a.addClass('display-none');
				a.removeClass('display-slogan');
				var next = parseInt(a.attr('id').substr(6,1)) + 1;
				if (next > max) {
					next = 1;
				}

				$('.header-slogan' + next).fadeIn('400', function() {
					// Animation complete
					$('.header-slogan' + next).removeClass('display-none');
					$('.header-slogan' + next).addClass('display-slogan');
				});

			});
	}, 0);


	/* Functions */
	function strpos (haystack, needle, offset) {
	  var i = (haystack+'').indexOf(needle, (offset || 0));
	  return i === -1 ? false : i;
	}

	function w24Ajax(fltOptions) {
		w24Loader.start();
		if (window.location.hostname == 'wicked.local') {
			var url = 'http://' + window.location.hostname + '/w24/ajax';
		}
		else {
			var url = 'http://' + window.location.hostname + '/ajax';
		}
		fltOptions['amode'] = 'flt';
		//if (opt['cat'] == '') {
		if(typeof(fltOptions['cat']) == "undefined" || fltOptions['cat'] === null){
			fltOptions['cat'] = $('#w24-cat').val();
		}
		fltOptions['topic'] = $('#w24-topic').val();
//		console.log(fltOptions);
		$.post( url,
			fltOptions,
			function(data) {
				if(data) {
					//alert('FOO!');
					//var html = $(data);
					var menu = $(data.menu);
					var list = $(data.list);
					//$('.w24-left-select',html).bind('click', w24FltDropdown);
					$('.w24-left-select',menu).bind('click', w24FltDropdown);
					//$('.w24-filter-list').html(html);
					//$('.w24-filter-list').hide();
					//$('.w24-filter-list-ajax').html(menu);
					//$('.w24-filter-list-ajax').show();
					$('.w24-filter-list').html(menu);
					$('.w24-cat-img').hide();
					if (data.list != 'false' && data.list != '' && data.list !== null) {
						//console.log("foo");
						//$('.w24-heizkoerper-top').hide();
						$('.category-products').html(list);
						$('.w24-produkt-view-main-container').html(list);
					}
				} else {
					alert('ERROR!');
				}
				ajax_toolbar();
				w24Loader.end();

			}, "json"
		);
	}

	function ajax_toolbar()
	{
		var toolbar = $(".toolbar, .pages");
		if(!toolbar.size())
		{
			return;
		}
		/* Statische onchange Handler deaktivieren */
		$(".limiter select", toolbar)[0].onchange = "";
		$(".sorter select", toolbar)[0].onchange = "";

		if(is_ajax_toolbar)
		{ /* Nur einmal ausführen */
			return;
		}
		is_ajax_toolbar = 1;


		$(".toolbar a, .pages a").live("click", function(){
			add_to_flt(this.href);
			on_toolbar();
			return false;
		});

		$(".toolbar select, .pages select").live("change", function(){
			add_to_flt($(this).val());
			on_toolbar();
			return false;
		});

	}

	function on_toolbar()
	{
		w24Ajax(fltOptions);
	}

	function add_to_flt(url)
	{
		var list = parse_url(url);
		$.each(list, function(key, val){
			fltOptions[key] = val;
		});
	}

	function parse_url(url) {
		var urlParams = {};
	    var e,
	        a = /\+/g,  // Regex for replacing addition symbol with a space
	        r = /([^&=]+)=?([^&]*)/g,
	        d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
	        q = url.match(/[^?]*\?(.*)/)[1];

	    while (e = r.exec(q))
	       urlParams[d(e[1])] = d(e[2]);
	    return urlParams;
	};

/*END*/
});

