// This could probably be optimized a ton; not my concern here.
// Go easy on me--this is my first attempt at using a JS object

function showFilters(id) {
	$('#filters_'+id).slideDown();
	pageTracker._trackEvent("Filters", "Toggled the homepage camera filters", id);
}
						

function miniFilters(script) {
	this.catid	= 0;
	this.stype	= 0;
	this.mini	= 0;
	this.maxprice = 0;
	this.script = script; // top-rated
	
	this.ClearFilters = function() {
		for(i=1;i<$('input[name="man'+this.catid+'[]"]').length;i++){
			$('#man'+this.catid+'_'+i).attr('checked', false);
		}
		$('#man'+this.catid+'_0').attr('checked', true);

		for(i=1;i<$('input[name="type'+this.catid+'[]"]').length;i++){
			$('#type'+this.catid+'_'+i).attr('checked', false);
		}
		$('#type'+this.catid+'_0').attr('checked', true);

		this.BuildData();
	}
	
	this.SelectAll_Man = function() {
		if($('#man'+this.catid+'_0').attr('checked')==true){
			pageTracker._trackEvent("Filters", "Reset Manufacturer Filters");
			for(i=1;i<$('input[name="man'+this.catid+'[]"]').length;i++){
				$('#man'+this.catid+'_'+i).attr('checked', false);
			}
		}
		
		$('#man'+this.catid+'_0').attr('checked', true);
		
		this.BuildData();
	}

	this.SelectAll_Type = function() {
		if($('#type'+this.catid+'_0').attr('checked')==true){
			pageTracker._trackEvent("Filters", "Reset Type Filters");
			for(i=1;i<$('input[name="type'+this.catid+'[]"]').length;i++){
				$('#type'+this.catid+'_'+i).attr('checked', false);
			}
		}
		
		$('#type'+this.catid+'_0').attr('checked', true);

		this.BuildData();
	}

	this.SelectAll_Filter = function() {
		if($('#filter'+this.catid+'_0').attr('checked')==true){
			pageTracker._trackEvent("Filters", "Reset Special Filters");
			for(i=1;i<$('input[name="filter'+this.catid+'[]"]').length;i++){
				$('#filter'+this.catid+'_'+i).attr('checked', false);
			}
		}

		$('#filter'+this.catid+'_0').attr('checked', true);

		this.BuildData();
	}
	
	this.SelectMan = function(id) {
		var is_all_blank=1;
		
		for(i=0;i<$('input[name="man'+this.catid+'[]"]').length;i++){
			if($('#man'+this.catid+'_'+i).attr('checked')==true)
				is_all_blank=0;
		}
		
		if(is_all_blank==0) {
			pageTracker._trackEvent("Filters", "Added Manufacturer Filter");
			$('#man'+this.catid+'_0').attr('checked', false);
		}
		else
			$('#man'+this.catid+'_0').attr('checked', true);
		
		this.BuildData();
	}
	
	this.SelectType = function(id) {
		var is_all_blank=1;
		
		for(i=0;i<$('input[name="type'+this.catid+'[]"]').length;i++){
			if($('#type'+this.catid+'_'+i).attr('checked')==true)
				is_all_blank=0;
		}
		
		if(is_all_blank==0) {
			pageTracker._trackEvent("Filters", "Added Type Filter");
			$('#type'+this.catid+'_0').attr('checked', false);
		}
		else
			$('#type'+this.catid+'_0').attr('checked', true);

		this.BuildData();	
	}

	this.SelectFilter = function(id) {
		var is_all_blank=1;
		
		for(i=0;i<$('input[name="filter'+this.catid+'[]"]').length;i++){
			if($('#filter'+this.catid+'_'+i).attr('checked')==true)
				is_all_blank=0;
		}
		
		if(is_all_blank==0) {
			pageTracker._trackEvent("Filters", "Added Special Filter");
			$('#filter'+this.catid+'_0').attr('checked', false);
		}
		else
			$('#filter'+this.catid+'_0').attr('checked', true);
		
		this.BuildData();
	}
	
	this.BuildData = function() {
		var query_string;
		
		var man_array = new Array();
		var man_flags = new Array();
		
		var type_array = new Array();
		var type_flags = new Array();
		
		var filter_array = new Array();
		var filter_flags = new Array();
		
		for(i=0;i<$('input[name="man'+this.catid+'[]"]').length;i++){
			man_array[i] = $('#man'+this.catid+'_'+i).attr('value');
			man_flags[i] = $('#man'+this.catid+'_'+i).attr('checked');
		}

		for(i=0;i<$('input[name="type'+this.catid+'[]"]').length;i++){	
			type_array[i] = $('#type'+this.catid+'_'+i).attr('value');
			type_flags[i] = $('#type'+this.catid+'_'+i).attr('checked');
		}
		
		for(i=0;i<$('input[name="filter'+this.catid+'[]"]').length;i++){
			filter_array[i] = $('#filter'+this.catid+'_'+i).attr('value');
			filter_flags[i] = $('#filter'+this.catid+'_'+i).attr('checked');
		}
		
		
		//These are not pretty, but they do work.
		if(man_array != '')
			query_string = "&man_array=" + man_array + "&man_flags=" + man_flags;
		
		if(type_array != '')
			query_string += "&type_array=" + type_array + "&type_flags=" + type_flags;
			
		if(filter_array != '')
			query_string += '&filter_array=' + filter_array + '&filter_flags=' + filter_flags;

		query_string += "&catid=" + this.catid;
		query_string += "&mini=" + this.mini;
		query_string += "&maxprice=" + this.maxprice;
		query_string += "&stype=" + this.stype;
		
		$('#'+this.script+'-'+this.catid).html('<div class="ajax_load">Loading</div>').load('/assets/'+this.script+'.php?'+query_string);
	}
}