var MAX_PRICE=500, stype;
$.extend(jQuery, new AdvancedSearchBar());
function AdvancedSearchBar() {
	
	$.extend($.blockUI.defaults.overlayCSS, { backgroundColor: '#E4F2F9' });
	$(document).ready(function(){
		if($('#typeval').val() == 'other' || $('#typeval').val() == 'all'){
			stype = $('#typeval').val();
			$('div.slider, #price, #pcv').block();
			$('.blockUI').css('cursor', 'auto');
		}
		$('.tli').hover(function(){
				$(this).css({ backgroundColor:'#CCCCCC', cursor:'pointer' });
			},function(){
				$(this).css({ backgroundColor:'white', cursor:'default' });
			}
		);
	
		var slide_init = ($('#priceval').val()/MAX_PRICE) * 118;
		(slide_init == 0) ? slide_init = 0 : slide_init += 1;
		$('.slider').Slider({
			accept:'.indicator',
			onSlide:function(px, py, x, y){
				var cd = parseInt(MAX_PRICE * (px/100));
				if(cd == 0 && stype != 'other' && stype != 'all'){
					$('#pcv').html('Any');
				}else{
					if(stype == 'job'){
						$('#pcv').html('<span style="position:relative;top:-2px">&rsaquo;</span> $'+cd);
					}else if(stype != 'other' && stype != 'all'){
						$('#pcv').html('<span style="position:relative;top:-2px">&lsaquo;</span> $'+cd);
					}
				}
			},
			onChange:function(px, py, x, y){
				if(parseInt(MAX_PRICE * (px/100)) == 0 || stype == 'other' || stype == 'all'){
					$('#priceval').remove();
				}else{
					$('#priceval').remove();
				$('#SearchForm').append('<input id="priceval" type="hidden" name="p" value="'+parseInt(MAX_PRICE * (px/100))+'" />');
				}
			},
			values:[[slide_init, 0]]
		});
	
		$('#currloc').focus(function(){
			$(this).val('');
		});
		$('#currloc').blur(function(){
			var loc = $(this).get(0);
			(loc.value == '') ? $(this).val(loc.defaultValue) : $(this).val(loc.value);
		});
		$('#currloc').autocomplete('/search/AdvancedSearchBarComponent', { extraParams:{command:'ac'}, resultsClass:'sac_results', minChars:2, matchSubset:1, matchContains:1, cacheLength:10, onItemSelect:selectItem, formatItem:formatItem, selectOnly:1, width:208, autoFill: true });
	});
	
	this.cat_1 = function(cid, cparent){
		if($('#check_'+cid).attr('checked') == true){
			$('#SearchForm').append('<input id="catval_'+cid+'" type="hidden" name="c[]" value="'+cid+'" />');
			var ctype = $('#_st').text();
			ctype = ctype.toLowerCase(ctype).substring(0,1);
			if(ctype == 'f'){
				ctype = 'i';
			}
			setCategories(ctype, cparent);
		}else{
			$('input#catval_'+cid).remove();
			$('#cat_table_'+cparent).fadeOut('normal', function(){
				$('input[@id^=catval_'+cparent+'_]').remove();
				$(this).remove();
			})
		}
	}
	
	this.cat_2 = function(cid, cparent){
		if($('#check_'+cid).attr('checked') == true){
			$('#SearchForm').append('<input id="catval_'+cparent+'_'+cid+'" type="hidden" name="c[]" value="'+cid+'" />');
		}else{
			$('input#catval_'+cparent+'_'+cid).remove();
		}
	}
	
	this.updateCategories = function(){
		var box = $('#curr_type').get(0);
		var val = box.options[box.selectedIndex].value;
		if(val == 'job'){
			$('div.slider, #price, #pcv').unblock();
			$('#price').html('Pay:');
			setCategories('j');
		}else if(val == 'housing'){
			$('div.slider, #price, #pcv').unblock();
			$('#price').html('Cost:');
			setCategories('h');
		}else if(val == 'other'){
			$('div.slider, #price, #pcv').unblock();
			$('div.slider, #price, #pcv').block();
			$('.blockUI').css('cursor', 'auto');
			$('#priceval').remove();
			setCategories('o');
		}else if(val == 'item'){
			$('div.slider, #price, #pcv').unblock();
			$('#price').html('Price:');
			setCategories('i');
		}else{
			$('div.slider, #price, #pcv').unblock();
			$('div.slider, #price, #pcv').block();
			$('.blockUI').css('cursor', 'auto');
			$('#priceval').remove();
			setCategories('a');
		}
	}
}

function setCategories(ctype, cparent){
	var pic = new Image();
	pic.src = '/Styles/layout/images/icons/s_loading.gif';
	if($.browser.safari){
		$('#cat_filters > span').append('<img style="position:relative;top:4px;padding:0px;margin:-5px 0px 0px;" src="'+pic.src+'" />');
	}else{
		$('#cat_filters > span').append('<img style="position:relative;top:4px;padding:0px;margin:-4px 0px 0px;" src="'+pic.src+'" />');
	}
	if(ctype != 'a'){
		if(!cparent){
			var cparent = null;
		}
		$.ajax({
			type: 'Post',
			url: '/search/AdvancedSearchBarComponent',
			data: 'command=reset&t='+ctype+'&p='+cparent,
			success: catSuccess,
			error: catError
		})
	}else{
		$('#cat_table').fadeOut('normal', function(){
			$(this).empty();
			$(this).append('<tr><td><div style="position:relative;left:80px;top:50px;font-weight:bold;font-size:13px;">Select the type of listing to filter against</div></td></tr>').fadeIn();
			$('#cat_filters > span').empty();
		})
	}
}

function catSuccess(xml){
	if($('status', xml).text() == 'ok'){
		var cc = 1, out = '', level = $('level', xml).text(), cparent = $('parent', xml).text();
		
		if(level == 2){
			out += '<table id="cat_table_'+cparent+'">';
			$('category', xml).each(function(){
				out += (cc % 3 == 1) ? '<tr>' : '';
				out += '<td align="top" width="140px"><label><input class="catbox" type="checkbox" id="check_'+$(this).attr('id')+'" onclick="$.cat_2('+$(this).attr('id')+', '+cparent+')" />&nbsp;'+$(this).attr('val')+'</label></td>';
				out += (cc % 3 == 0) ? '</tr>' : '';
				++cc;
			})
			out += '</table>';
			$('#cat_filters').parent().append(out).fadeIn('normal', function(){
				$('#cat_filters > span').empty();
			});
		}else{
			$('category', xml).each(function(){
				out += (cc % 3 == 1) ? '<tr>' : '';
				out += '<td align="top" width="140px"><label><input class="catbox" type="checkbox" id="check_'+$(this).attr('id')+'" onclick="$.cat_1('+$(this).attr('id')+', '+$(this).attr('id')+')" />&nbsp;'+$(this).attr('val')+'</label></td>';
				out += (cc % 3 == 0) ? '</tr>' : '';
				++cc;
			})
			$('#cat_table').fadeOut('normal', function(){
				$(this).empty();
				$(this).append(out).fadeIn();
				$('#cat_filters > span').empty();
			})
		}
	}else{
		catError();
	}
}

function catError(){
	alert('There was an error processing your request. Please try again later.');
	$('#cat_filters > span').empty();
}

function selectItem(li) {
	if (li.extra) {
		$('#locval').val(li.extra[0]);
	}		
}

function formatItem(row) {
	var out = '<span class="name">'+row[0]+'</span>';
	return out;
}