function InventoryDataTable(conf){
	this.conf = conf;
	
	// Initialization
	this.conf["dealer_id"] = (this.conf["dealer_id"] !== undefined) ? this.conf["dealer_id"] : 0;
	this.conf["homepage"] = (this.conf["homepage"] !== undefined) ? this.conf["homepage"] : 0;
	
	// Sorting
	this.conf["sort_by"] = (this.conf["sort_by"] !== undefined) ? this.conf["sort_by"] : 'year';
	this.conf["sort_direction"] = (this.conf["sort_direction"] !== undefined) ? this.conf["sort_direction"] : 'desc';
	
	// Paging
	this.conf["limit"] = (this.conf["limit"] !== undefined) ? this.conf["limit"] : 10;
	this.conf["start"] = (this.conf["start"] !== undefined) ? this.conf["start"] : 1;
	this.conf["page"] = (this.conf["page"] !== undefined) ? this.conf["page"] : 1;	
	
	// Filters
	this.conf["active"] = (this.conf["active"] !== undefined) ? this.conf["active"] : 1;
	this.conf["condition"] = (this.conf["condition"] !== undefined) ? this.conf["condition"] : '';
	this.conf["make"] = (this.conf["make"] !== undefined) ? this.conf["make"] : '';
	this.conf["model"] = (this.conf["model"] !== undefined) ? this.conf["model"] : '';
	this.conf["year"] = (this.conf["year"] !== undefined) ? this.conf["year"] : '';
	this.conf["payment"] = (this.conf["payment"] !== undefined) ? this.conf["payment"] : '';
	this.conf["price"] = (this.conf["price"] !== undefined) ? this.conf["price"] : '';
	this.conf["exterior_color"] = (this.conf["exterior_color"] !== undefined) ? this.conf["exterior_color"] : '';
	this.conf["interior_color"] = (this.conf["interior_color"] !== undefined) ? this.conf["interior_color"] : '';
	this.conf["mileage"] = (this.conf["mileage"] !== undefined) ? this.conf["mileage"] : '';
	this.conf["transmission"] = (this.conf["transmission"] !== undefined) ? this.conf["transmission"] : '';
}
InventoryDataTable.prototype.sortColumn = function(obj){	
	var columns = $('.sortable');
	var oldDirection = '';
	var newDirection = 'desc';
	for(i=0;i<columns.length;i++){
		if($(obj).attr('field') == $(columns[i]).attr('field')){
			oldDirection = '';
			newDirection = 'desc';
			if($(columns[i]).hasClass('desc')){
				oldDirection = 'desc';
				newDirection = 'asc';
			}
			else if($(columns[i]).hasClass('asc')){
				oldDirection = 'asc';
				newDirection = 'desc';
			}
			$(columns[i]).removeClass(oldDirection);
			$(columns[i]).addClass(newDirection);
		}
		else{
			$(columns[i]).removeClass('desc');
			$(columns[i]).removeClass('asc');
		}
	}
	
	this.conf["sort_by"] = $(obj).attr('field')
	this.conf["sort_direction"] = newDirection;
	this.conf["page"] = 1;
	this.conf["start"] = 0;
	
	this.applyFilters();	
	return false;
}

InventoryDataTable.prototype.setLimit = function(limit){
	this.conf["limit"] = limit;
	this.conf["page"] = 1;
	this.conf["start"] = 0;
	this.applyFilters();	
	return false;
}

InventoryDataTable.prototype.setPage = function(page){
	this.conf["page"] = page;
	this.conf["start"] = ((page-1) * this.conf["limit"]);
	this.applyFilters();	
	return false;
}

InventoryDataTable.prototype.addFilter = function(type,val){
	this.conf[type] = val;
	this.conf["page"] = 1;
	this.conf["start"] = 0;
	if(this.conf['homepage']){
		if(type == 'condition'){
			window.location.href = '/inventory/'+val+'/';
		}
		else{
			window.location.href = '/inventory/?'+type+'='+val;
		}		
	}
	else{
		this.applyFilters();	
	}
	return false;
}

InventoryDataTable.prototype.removeFilter = function(type){
	this.conf[type] = '';
	this.conf["page"] = 1;
	this.conf["start"] = 0;
	this.applyFilters();	
	return false;
}

InventoryDataTable.prototype.clearFilters = function(){
	this.conf["condition"] = '';
	this.conf["make"] = '';
	this.conf["model"] = '';
	this.conf["year"] = '';
	this.conf["payment"] = '';
	this.conf["price"] = '';
	this.conf["exterior_color"] = '';
	this.conf["interior_color"] = '';
	this.conf["mileage"] = '';
	this.conf["transmission"] = '';
	this.conf["page"] = 1;
	this.conf["start"] = 0;
	this.applyFilters();	
	return false;
}

InventoryDataTable.prototype.expandOption = function(val){
	if($('#filter_option_'+val).is(':visible')){
		$('#filter_option_'+val).slideUp(200);
	}
	else{
		$('#filter_option_'+val).slideDown(200);
	}	
}

InventoryDataTable.prototype.applyFilters = function(){
	var updUsedFilters = $('#used_filters');
	var updUnusedFilters = $('#unused_filters');
	var updBody = $('.'+this.conf["table"]+' tbody');
	var updFoot = $('#paging_footer');
	var updHeading = $('.heading');
	$.ajax({
		url: '/include/api/inventory/buildDataTable.php',
		cache: false,
		data: {
			return_type: 'JSON',
			dealer_id: this.conf["dealer_id"],
			active: this.conf["active"],
			sort_by: this.conf["sort_by"],
			sort_direction: this.conf["sort_direction"],
			limit: this.conf["limit"],
			start: this.conf["start"],
			page: this.conf["page"],
			condition: this.conf["condition"],
			make: this.conf["make"],
			model: this.conf["model"],
			year: this.conf["year"],
			payment: this.conf["payment"],
			price: this.conf["price"],
			exterior_color: this.conf["exterior_color"],
			interior_color: this.conf["interior_color"],
			mileage: this.conf["mileage"],
			transmission: this.conf["transmission"]
		},
		success: function(r){
			var resp = JSON.parse(r);
			updUsedFilters.empty();
			updUnusedFilters.empty();
			updBody.empty();
			updFoot.empty();
			updUsedFilters.html(resp.used_filters);
			updUnusedFilters.html(resp.unused_filters);
			updBody.html(resp.tbody);
			updFoot.html(resp.tfoot);
			updHeading.html(resp.page_headline);
			$( 'html, body' ).animate( { scrollTop: 0 }, 'slow' );
		}
	})
}
