


Filter = function() {
	
};

Filter.prototype = {
	
	adminAjaxURL: '/layouts/ajax/ajaxbikeslist.cfm' 
	,init: function(id, returnContainerID){
		this.formID = id;
		this.checkboxClass = id+'_checkbox';
		this.showMoreLink = id+'_showMore';
		this.showLessLink = id+'_showLess';
		this.formFieldClass = id+'_formField';
		this.listNavClass = 'listNav';
		this.listCurrentPageFieldID = id+'_listCurrentPage';
		this.returnContainerID = returnContainerID;
		this.formEl = $('#'+this.formID);
		this.returnContainer = $('#'+this.returnContainerID);
		this.listCurrentPageField = $('#'+this.listCurrentPageFieldID); 
		this.formSubmitedID = id+'_formSubmited';
		
		variable = this;
		
		this.initEvents();
		
	}
	,initSlider:function(id,maxPrice){
		var sliderID = id+'-range';
		var sliderPopupID = id+'-popup';
		var sliderPopupContentID = id+'-popup-content';
		var maxSliderPrice = 8500;
		
		if(maxPrice != undefined && !isNaN(Number(maxPrice))){
			maxSliderPrice = maxPrice;
		}
		
		$( "#" + sliderID ).slider({
			range: true,
			animate: true,
			min: 0,
			max: maxSliderPrice,
			step: 100,
			values: [ 0, maxSliderPrice ],
			slide: function( event, ui ) {
				$( "#" + variable.formID + "_priceSliderValue" ).val(ui.values[0] + ',' + ui.values[1]);
				$( "#" + sliderPopupContentID ).html(ui.value + '.00');
				$( "#" + sliderPopupID ).css("left", ui.handle.offsetLeft-($( "#" + sliderPopupContentID ).width()/3)+4+"px");
			},
			start: function( event, ui ) { 
				$( "#" + sliderPopupID ).show(); 
			},
			stop: function(event, ui) { 
				$( "#" + sliderPopupID ).hide(); 
				variable.submitForm();
			}
		});
		
	
	}
	,initEvents:function(){
		
		$("."+this.checkboxClass).change(function (index) {
			
			if($(this).val() == 'alle' ){
				var checkBoxName = $(this).attr('name').split('_')[0];
				var checked = $(this).attr('checked');
				var checkBoxsSel = $( "#" + variable.formID + " input:checkbox[name='" + checkBoxName + "']" );

				checkBoxsSel.attr('checked', checked);
			}
		
			variable.submitForm();
		});
		
		$("."+this.showMoreLink).click(function (index) {
			variable.showHideFilterOptions(this, 'show');
			
		});
		
		$("."+this.showLessLink).click(function (index) {
			variable.showHideFilterOptions(this, 'hide');
		});
		
		this.initFormFieldsEvents();
		
		
		/*  this.adminSubMenuToggleLink.bind('click', {variable:this}, function(e) {
				variable.showHideSubMenu();
			});
		*/
		
	}
	,initFormFieldsEvents:function(){
		$("."+this.formFieldClass).bind('change', {variable:this}, function(e) {
			//console.log($(this).attr('name'));
			//console.log($('#'+variable.formID+' select[name="'+$(this).attr('name')+'"]') );
			var changedFormFields = $('#'+variable.formID+' select[name="'+$(this).attr('name')+'"]');
			changedFormFields.val($(this).val());
			variable.submitForm();
		});

	}
	,initListNavEvents:function(){

		$('.'+this.listNavClass+' a').bind('click', {variable:this}, function(e) {
			var pageNum = $(this).attr('href').split('=')[1];
			variable.listCurrentPageField.val(pageNum);
			variable.submitForm();
			
			return false;
		});
	}
	,removeFormFieldsEvents:function(){
		$("."+this.formFieldClass).unbind('change');
	}
	,removeListNavEvents:function(){
		$('.'+this.listNavClass+' a').unbind('click');
	}
	,setReturnContainer:function(containerID){
		this.returnContainerID = containerID;
	}
	,onloadSubmit:function(forceLoad){
		//alert($('#' + this.formSubmitedID).attr('checked'));
		if(forceLoad == undefined){
			forceLoad = false;
		}
		
		if($('#' + this.formSubmitedID).attr('checked') == 1 || forceLoad == true){
			this.submitForm();
		}
	}
	,submitForm:function(){
	
		this.removeFormFieldsEvents();
		this.removeListNavEvents();
		
		this.returnContainer.fadeOut('slow');
		
		//$.ajaxSetup({async:false});
		
		$.post(this.adminAjaxURL, this.formEl.serialize(),
			function(msg){
				//var returnData = jQuery.parseJSON(msg);  
				//variable.populateReturnData(returnData);
				variable.populateReturnData(msg);
			}
		);
		$('#' + this.formSubmitedID).attr('checked', true);;
		//console.log($('#' + this.formSubmitedID).val());
	}
	,populateReturnData:function(returnData){
		//this.returnContainer.html(returnData.data);
		this.returnContainer.html(returnData);
		this.returnContainer.fadeIn('slow');
		
		this.initFormFieldsEvents();
		this.initListNavEvents();

	}
	,showHideFilterOptions:function(thisEl, action){
		featureID = thisEl.id.split('_')[1];
		
		if(action == 'show'){
			$('#showMore_'+featureID).hide();
			$('#fil'+featureID+' .over').show();
		}
		else if(action == 'hide'){
			$('#showMore_'+featureID).show();
			$('#fil'+featureID+' .over').hide();
		}
		
	}
};


