var selections = new Object();
var oldsel = "all";

$(document).ready(
	function () {
		$('.cv_category').click(
			function() {
				$('.cv_category').parent().removeClass("current");
				$(this).parent().addClass("current");
				setSelectedValue();
				var key=$(this).attr("rel");
				
				$('#tag').load(prelink_current+'tagserver/'+key,selections,
					function (responseText, textStatus, XMLHttpRequest) {
						addTagsHook();
					});
				
				$('#results').change(
					function() {
						selections['results']=0;
						selections['pagenum']=0;
						refreshSearchResults();
					});
				
				$('#priceorder').click(
					function() {
						$('#brandorder').removeClass('active');
						$(this).addClass('active');
						$(this).toggleClass('descending');
						if($(this).hasClass('descending'))
							selections['order']='price desc';
						else
							selections['order']='price asc';
						refreshSearchResults();
					});
				$('#brandorder').click(
						function() {
							$('#priceorder').removeClass('active');
							$(this).addClass('active');
							$(this).toggleClass('descending');
							if($(this).hasClass('descending'))
								selections['order']='brand desc';
							else
								selections['order']='brand asc';
							refreshSearchResults();

						});
			});		
		
		$('.cv_category:first').click();
		
	});

function setSelectedValue() {
	var value = $('#tag input:radio:checked').val();
	var category = $('#tag input:radio:checked').attr('rel');
	if(!value) {
		value= $('#stag').val();
		category = $('#stag').attr('rel');
	}
		
	if(value=='all')
		$('#selected_'+category).html('');
	else
		$('#selected_'+category).html(value);
	selections['cat_'+category]=value;
	selections['pagenum']=0;
}

function addTagsHook() {
	$('#tag input:radio').each(
		function() {
			var cat=$(this).attr('rel');
			var value=$(this).val();
			if(selections['cat_'+cat]==value) {
				$(this).attr('checked','checked');
				$(this).change();
			}
		});
	$('#stag').each(
			function() {
				var cat=$(this).attr('rel');
				$(this).val(selections['cat_'+cat]);
			});
	$('#tag input:radio').change(
		function() {
			setSelectedValue();
			refreshSearchResults();
		}); 
	$('#stag').change(
		function() {
			setSelectedValue();
			refreshSearchResults();
		});
	
	
	jQuery.each(jQuery.browser, function(i, val) {
		/*if(i=="msie" && val) {			     
			$('#stag').bind('mouseenter',function () {
				$(this).css("width","auto");
			});
			
			$('#stag').bind('mouseout',function () {
				$(this).css("width","170px");
			});
		}*/
	});
	$(function () {
	    if ($.browser.msie) {
	        $('input:radio').click(function () {
	            this.blur();
	            this.focus();
	        });
	    }
	});

}

function refreshSearchResults() {
	var results = $('#results').val();
	selections['results']=results;
	$('#risultati').load(prelink_current+'cercavini/search',selections,
		function() {
			$("#prevpage").click(
				function() {
					selections['pagenum']=parseInt(selections['pagenum'])-1;
					refreshSearchResults();
				});
			$("#nextpage").click(
					function() {
						selections['pagenum']=parseInt(selections['pagenum'])+1;
						refreshSearchResults();
					});
			$('a.pagelink').click(
				function() {
					var pagenum=$(this).attr("rel");
					selections['pagenum']=pagenum;
					refreshSearchResults();
				});
	});
}
