var Search = Class.create();

Search.prototype =
{	
	AJAXSEARCH_URL: '/?-=view_searchajax',
	
    initialize : function()
    {    	
    	this.ajaxSearchForm=$('ajaxsearchform');
    	this.filterProductTypeId=$('filter_producttypeid');
    	this.filterOfferDate=$('filter_offer_date');
    	this.filterCountry=$('filter_country');
    	this.filterRegion=$('filter_region');
    	this.filterInterval=$('filter_interval');
    	this.filterLastminuteType=$('filter_lastminutetype');
    	this.exactDateContainer=$('exactdate_container');
    	this.ajaxLoadingImageContainer=$('filter_ajaxloadingimage_container'); //div kontenere, melyben a loading img van
    	this.useAjaxStartStopEvent=true; //ajax futas kozben hivja e meg a disabled + loading fv-t
    	this.createEvents();        	
    	fireEvent(this.filterCountry,'change');
    },
    
    createEvents : function()
    {    	
    	Event.observe(this.filterCountry,"change", this.startSearch.bindAsEventListener(this,this.filterCountry));
    	Event.observe(this.filterRegion,"change", this.startSearch.bindAsEventListener(this,this.filterRegion));
    	
    	if (this.filterInterval)
    		Event.observe(this.filterInterval,"change", this.filterIntervalChange.bindAsEventListener(this,this.filterInterval));

    	if (this.filterInterval)
    		Event.observe(this.filterInterval,"change", this.startSearch.bindAsEventListener(this,this.filterInterval));
    	
    	if (this.filterLastminuteType)
    		Event.observe(this.filterLastminuteType,"change", this.startSearch.bindAsEventListener(this,this.filterLastminuteType));
    	
    	for (var i=0;i<$$(('input.filterdisplaycategory')).length;i++)
    		Event.observe($$(('input.filterdisplaycategory'))[i],"click", this.startSearch.bindAsEventListener(this,$$(('input.filterdisplaycategory'))[i]));
    	
		if ($('filter_exactdateinput'))
		{
			var picker = new Control.DatePicker('filter_exactdateinput',{locale:'hu_HU'});
			Event.observe($('filter_exactdateinput'),"change", this.startSearch.bindAsEventListener(this,this.filterInterval));
		}    	
    },
    
    filterIntervalChange : function(e,el)
    {
    	if (el && el.value=='EXACT')
    	{
    		if ($('filter_exactdateinput'))
    		{
    			var picker = new Control.DatePicker('filter_exactdateinput',{locale:'hu_HU'});
    		}
    		else
    		{
	    		exactDateInput=document.createElement('input');
	    		exactDateInput.setAttribute('type','text');    		
	    		exactDateInput.setAttribute('name','filter_exactdateinput');    		
	    		exactDateInput.setAttribute('id','filter_exactdateinput');    		
	    		this.exactDateContainer.appendChild(exactDateInput);
    		}
    		$('filter_exactdateinput').show();
			var picker = new Control.DatePicker('filter_exactdateinput',{locale:'hu_HU'});
    	}
    	else if ($('filter_exactdateinput'))
    		$('filter_exactdateinput').parentNode.removeChild($('filter_exactdateinput'));
    },
    
    startSearch : function(e,el,fn)
    {
    	var thisref=this;

    	new Ajax.Request(this.AJAXSEARCH_URL+'&el='+el.name,
	    	{
	    		method:'post',
	    		parameters:this.ajaxSearchForm.serialize(),
	    		asynchronous:false,
	    		onSuccess: function(transport)
	    		{   
    				if (thisref.useAjaxStartStopEvent)
    					thisref.changeElementsAjaxStartStopEvent(false);
    				
	    			var objs=transport.responseText.evalJSON(); 
	    			
	    			if(objs.length>0)
	    			{
		    			for (var i=0;i<objs.length;i++)
		    			{
		    				switch(objs[i].type)
		    				{
		    					case 'combo':thisref.updateOptionList($(objs[i].id),objs[i].data,objs[i].options);
		    				}
		    				$('searchboxrecordcount').update(objs[i].recordcount);
		    			}
	    			}	    			
	    			
	    			if (typeof(fn)=='function')
	    				fn(e,el);
	    			
	    			
	    		},
	    		onCreate: function()
	    		{
	    			if (thisref.useAjaxStartStopEvent)
	    				thisref.changeElementsAjaxStartStopEvent(true);
	    		},
				onException: function(req,exception) {
					alert("The request had a fatal exception thrown.\n\n" + exception);
					return true;
				}
	    	}    	
    	);
    },
    
    changeElementsAjaxStartStopEvent : function(bool)
    {
    	if (this.filterProductTypeId)
			this.filterProductTypeId.disabled=bool;
		if (this.filterOfferDate)
			this.filterOfferDate.disabled=bool;
		if (this.filterCountry)
			this.filterCountry.disabled=bool;
		if (this.filterRegion)
			this.filterRegion.disabled=bool;
		if (this.filterInterval)
			this.filterInterval.disabled=bool;		
		if (this.ajaxLoadingImageContainer)
			this.ajaxLoadingImageContainer.style.display=bool?'':'none';
    },
    
    updateOptionList : function(el,items,defaultValue)
    {
    	oldValue=el.value;
    	el.options.length=1;
    	for (var i=0;i<items.length;i++)
    		el.options[el.options.length]=new Option(items[i].text,items[i].value);    		
    	el.value=oldValue;
    	if (defaultValue)
    		el.value=defaultValue;
    },
    
    initByLink : function(countryId,regionId,cityId)
    {
    	//$('ajaxsearchformsession').hide();$('ajaxsearchform').show();
    	
    	this.filterCountry.value=countryId;
    	this.updateOptionList(this.filterRegion,[{text:'-',value:regionId}],regionId);
    	this.filterIntervalChange(this,this.filterInterval);
    	el=new Element('input', {'name':'filter_city', 'type': 'hidden'})
    	el.value=cityId;
    	this.filterCountry.parentNode.appendChild(el);
    	this.startSearch(null,this.filterRegion,function() {$('ajaxsearchform').submit();} );	    	
    }
};


