$(function(){
	//start which?
	$("#cat").removeAttr("name");
	$("#retailer-comparison #compareBtn").click(function() {
		var page = $("#retailer-comparison select[name='catUrl'] option:selected").val();
		var allCategories = $("select#cat option:selected").val();
		
		if(allCategories.toLowerCase() == 'all') {
			window.location.href = 'http://www.whichcompare.co.uk';
		} else if(page.length > 0 && page.toLowerCase() != 'all') {
			window.location.href = 'http://www.whichcompare.co.uk' + page;
		}
		return false;
		
	});
	//end which?

	
	$("select#cat").change(function(){
					
		//hide all children where label is not equal to selected
		$('div.subcategory select').filter(function() {
				
              return $(this).attr("id") != $("select#cat").val();

            }).css({ display:"none"}).removeAttr("name");
			
			//start which?
				// the .removeAttr("name") above
			//end which?
		
		//show child where label is equal to selected
		$('div.subcategory select').filter(function() {

              return $(this).attr("id") == $("select#cat").val();

            }).css({ display:"inline"}).attr({name: "catUrl"});
			
			//start which?
				// the .attr("name:...") above
			//end which?
	
		//reset ALL to be selected
		$('div.subcategory select').filter(function() {

              return $(this).attr("id") == $("select#cat").val();

            }).children().filter(function() {

                  return $(this).val() == "All";

                }).attr('selected', 'selected')

	
	
		//Set disabled class if all is not selected 
		$('#retailer-comparison fieldset').filter(function(){
				
			return $("select#cat").val() != "All";
			
		}).removeAttr('class');
		
		
		//Set disabled class if all is selected
		$('#retailer-comparison fieldset').filter(function(){
				
			return $("select#cat").val() == "All";
			
		}).attr('class', 'disabled');
		
				
	})
	
	//behaviour in case of page refresh
	
	
})