/**
 * Class AjaxFilter
 * 
 * Replaces all catalog filter combos on the page by Ajax handlers to seemlessly lookup values.
 * @copyright  Christian Schiffler 2011
 * @author     Christian Schiffler <http://www.cyberspectrum.de>
 */

var AjaxFilter = new Class({
	Implements: [Options],
	options:[],
	dummyFormElement:new Element('div'),
	RequestClass: new Class({
		Extends: Request,
		options: {
			evalScripts: false,
			evalResponse: false
		},
		success: function(text, xml)
		{
			var js;
			text = text.stripScripts(function(script){js = script;});
			js = js.replace(/<!--|\/\/-->|<!\[CDATA\[\/\/>|<!\]\]>/g, '');
			this.onSuccess(text, xml, js);
		}
	}),
	initialize:function(options)
	{
		this.setOptions(options);
		this.grabOptions();
	},
	getUrl: function()
	{
		var url=window.location.href;
		var params=url.replace('.html', '').split('/');
		var options=$$($(this.options.id).getElements('select'));
		// overwrite parameters.
		for(var i=params.length;i>=0;i--)
		{
			for(var j=0;j<options.length;j++)
			{
				var e=options[j];
				if(params[i]==e.name)
				{
					var value = e.getElement(':selected').value;
					if(value.length==0)
						params.splice(i,2);
					else
						params[i+1] = value;
					options.splice(j, 1);
				}
			}
		}
		// add remaining parameters.
		for(var j=0;j<options.length;j++)
		{
			var e=options[j];
			var value = e.getElement(':selected').value;
			if(value.length!=0)
			{
				params.push(e.name);
				params.push(value);
			}
		}		
		return params.join('/')+'.html';
	},
	grabOptions: function()
	{
		var container=$(this.options.id);
		var me=this;
		// now track all filter options.
		$$(container.getElements('select')).each(function(e){
			e.addEvent('change', function(e)
			{
				var spinner = new Spinner(this.options.id, {
					img:{ 'style': 'background: url("plugins/mediabox/images/MinimalLoading.gif") no-repeat fixed center center transparent; width: 100px; height:100px;'},
					content: {'style':'background-color: #000;'}
				}).show();
				// now load via ajax.
				
				var objRequest = new me.RequestClass({
					'link': 'chain',
					'method': 'get',
					url: me.getUrl(),
					onSuccess: (function(text, xml, js){spinner.destroy(); this.pageLoaded(text, xml, js);}).bind(me)
				}).send();
			});
		});
	},
	pageLoaded: function(text, xml, js)
	{
		var el=Elements.from(text);
		var id=this.options.id;
		var scanNode=function(e)
		{
			// skip text nodes
			if(!e)return null;
			if(e.id==id)return e;
			var childNode, i = -1, childNodes = e.childNodes;
			while((childNodes.length>++i) && (childNode = childNodes.item(i)))
			{
				var tmp2=scanNode(childNode);
				if(tmp2)
					return tmp2;
			}
		}
		var e=null;
		for(var i=0;i<el.length;i++)
		{
			e = scanNode(el[i]);
			if(e)
				break;
		}
		if(e)
		{
			$(e).replaces(this.options.id);
			this.grabOptions();
		}
	}
});

// now start up our AjaxPage handler. Remember to do this only once!
window.addEvent('domready', function(){
	new AjaxFilter({id: 'modul_suche'});
	$$('mtb_hotel_search').each(function(){new AjaxFilter({id: 'mtb_hotel_search'});});
});
