arrowExpand=function(variable1){
  var $tr = $(this).parents('th:first');    // = $(this).parents('th:vinumber');//this is a parent TR tag
  var $vin = variable1; // this is VIN number
		// $(this).next("tr").toggle();             
		$.get('/uploads/includes/e.php', {'term': $vin}, function(data) {
        //$('#report tr.odd').after(data);
         if($('#more'+$vin).is('.collapsed')){
            $('#more'+$vin).removeClass('collapsed');
            $('#more'+$vin+' td:moreinfo').remove();
        }else{
            $('#more'+$vin).addClass('collapsed').append(data);

}
});
$("#vin").click(function(){ $(this).attr({ value: '' }); });
	$(':reset').bind('click', function() {
    var date = $("#auc_date").val();
    $("#lane").val('Ln-Run');
		 $("#ayear").val('Year');
         $("#byear").val('Year');
		 $("#make").val('Make');
		 $("#model").val('Model');
		 $("#type").val('Type');
		 $("#lowvalue").val('0');
		 $("#upvalue").val('999999');
		 $("#vin").val('VIN 1st 10');
         $.get('/uploads/srch_new.php', {'date': date}, function(data) {
			$('#report tr.cars').next().andSelf().remove();
            $('#report').append(data); //or .html(data);
			});
		/*date = document.getElementById('auc_date').value;
		$.get('../uploads/srch.php', {'date': date}, function(data) {
			$('#report tr.cars').remove();
			$('#report').append(data); //or .html(data);
			});*/
		
	});
     	$(':button').bind('click', function() {
        var date = $("#auc_date").val();
        var lane_val = $("#lane").val();
            if(lane_val == "Ln-Run"){lane_val = "";}
        var min_year = $("#ayear").val();
            if(min_year == "Year"){min_year = "";}
        var max_year = $("#byear").val();
            if(max_year == "Year"){max_year = "2020";}
        var make_val = $("#make").val();
            if(make_val == "Make"){make_val = "";}
        var model_val = $("#model").val();
            if(model_val == "Model"){model_val = "";}
        var type_val = $("#type").val();
            if(type_val == "Type"){type_val = "";}
        var min_val = $("#lowvalue").val();
        var max_val = $("#upvalue").val();
        var vin = $("#vin").val();	
            if(vin == "VIN 1st 10"){vin = "";}
		$.get('/uploads/srch_new.php', {'date': date, 'lane': lane_val, 'min_year': min_year, 'max_year': max_year, 'make': make_val, 'model': model_val, 'type': type_val,'min_mil': min_val, 'max_mil': max_val, 'vin': vin}, function(data) {
			$('#report tr.cars').next().andSelf().remove();
            $('#report').append(data); //or .html(data);
			});
		});
	
}
function my_sort_asc(a,b)
{
	if (a.sortKey < b.sortKey) return -1;
	if (a.sortKey > b.sortKey) return 1;
	return 0;
}

function my_sort_dsc(a,b)
{
	if (a.sortKey > b.sortKey) return -1;
	if (a.sortKey < b.sortKey) return 1;
	return 0;
}
/*performs comparison based on 3 critereas. Slow*/
function sort_3way_asc(a,b)
{
    if (a.keyA < b.keyA)
        return -1;
	if (a.keyA > b.keyA) 
        return 1;
	  		if(a.keyA == b.keyA && (a.keyB || b.keyB)){//concider second sorting criterea
				if(a.keyB < b.keyB) return -1;
				if(a.keyB > b.keyB) return 1;
						if(a.keyB == b.keyB && (a.keyC || b.keyC)){
									if(a.keyC < b.keyC ) return -1;
									if(a.keyC > b.keyC ) return 1;
						}				
				} 
	return 0;
} 
/*Sort desc. way 5,4,2,1 ...*/
function sort_3way_dsc(a,b)
{
    if (a.keyA > b.keyA)
        return -1;
	if (a.keyA < b.keyA)
        return 1;
        if(a.SpecialKey == 1){//this a lane sort,special case for dsc sort, 2d par-r need not to be kept asc
                    if(a.keyA == b.keyA && (a.keyB || b.keyB)){//concider second sorting criterea
                        if(a.keyB > b.keyB) return -1;
                        if(a.keyB < b.keyB) return 1;
                        }
        }
                else if(a.keyA == b.keyA && (a.keyB || b.keyB)){//concider second sorting criterea
                        if(a.keyB < b.keyB) return -1;
                        if(a.keyB > b.keyB) return 1;
                            if(a.keyB == b.keyB && (a.keyC || b.keyC)){
									if(a.keyC < b.keyC ) return -1;
									if(a.keyC > b.keyC ) return 1;
						}				
				} 
 
	return 0;
} 

/*Sorts the run-list*/
sortCarlist=function(var1, var2){

var	$srt_criteria = var1;
var $sort_order = var2;
var $child_num = 1;//1 is a first Ln-Run number
var tmp1;
var tmp2 = [];
/*This part will compute the proper criterials(index of colomn to sort) for sorting
i.e Year sort logic is as follows 1.sort by year, 2.sort by make 3.sort by model*/
var $criter1;
var $criter2;
var $criter3;
    if($srt_criteria == "year_sort"){
         $criter1 = 2;
         $criter2 = 3;
         $criter3 = 4;
        
    }
    if($srt_criteria == "make_sort"){
         $criter1 = 3;//make
         $criter2 = 2;//year
         $criter3 = 4;//model
    }
    if($srt_criteria == "model_sort"){
         $criter1 = 4;//model
         $criter2 = 2;//year
    }
    if($srt_criteria == "type_sort"){
         $criter1 = 5;//type /trim
         $criter2 = 2;//year
    }
    if($srt_criteria == "vin_sort"){
         $criter1 = 9;//vin
    }
	var $tmps = "0";
			//var $remove = $(this);
			var rows = $('#report').find('tbody > tr.cars').get();//get an array of DOM elements
			$('#report tbody tr.moreinfo').remove();
			//Need to figure what sorting is done and what are the proper variables
					if($srt_criteria == "lane_sort"){//special case, we need to separate Ln and Run#
						$.each(rows, function(index, row) {	
							//pre-compute keys. For better performance
								tmp1 = $(row).children('th').eq(1).text();
                                tmp2=tmp1.split("-");//split ln and run# into array
                                    row.keyA = tmp2[0];
                                    row.keyB = parseInt(tmp2[1]);//sort by number 1
                                    row.SpecialKey = 1;
                            });
						}
                            else if($srt_criteria == "miles_sort"){//special case, parseInt to compare values
                                $.each(rows, function(index, row) {
                                 if($(row).children('th').eq(8).text() == "n/a") row.keyA = 0;//to handle N/A abbreviation
                                    else { 
                                    var ReplacedNumber = parseInt($(row).children('th').eq(8).text().replace(/\,/g,''));//has to get rid of all "," in order to properly compare numbers
                                    row.keyA = ReplacedNumber;//parseFloat($(row).children('th').eq(8).text());
                                    }
                                });
                            }
						//pre-compute keys for even faster sort
                            else { 
                                $.each(rows, function(index, row) {
                                    row.keyA = $(row).children('th').eq($criter1).text().toUpperCase();
                                    if($criter2){row.keyB = $(row).children('th').eq($criter2).text().toUpperCase();}
                                    if($criter3){row.keyC = $(row).children('th').eq($criter3).text().toUpperCase();}
                                });
                            }                       
						if($sort_order=="asc"){
							rows.sort(sort_3way_asc);//sorting is done in the function above
								}else if ($sort_order == "dsc"){
												rows.sort(sort_3way_dsc);
											}
			$.each(rows, function(index, row) { //go to every row and reinsert to the proper place, append will take care not to have duplicates
			$('#report').append(row);	
			row.keyA = null; //clear pre-computed keys	
            row.keyB = null;
            row.keyC = null;
		});//now, re-inser moreinfo tags
			$('#report tbody tr').each(function() {
				if($(this).is('.cars')){
					var $vin_tmp = $(this).children('th').eq(9).text();//get the VIN number from parent
					var ht_ml = "<tr id='more"+$vin_tmp+"' class='moreinfo'></tr>";//and insert right after the parental TR
						$(this).after(ht_ml);
				}
			});
}

//$(document).ready(arrowExpand);
$(document).ready(function() {
	arrowExpand();
	var asc_icon = "/uploads/images/arrow_inactive_up.gif";
	var dsc_icon = "/uploads/images/arrow_inactive_down.gif";
	$('tr.headings th.sortable').find('img').click(function(){
		var $sort_criteria1=$(this).parent().attr('id'); 
		if($(this).parent().is('.asc')){//if sorted asceding order
			$(this).attr("src", dsc_icon);
			//do sorting here
			sortCarlist($sort_criteria1, "asc");
			$(this).parent().removeClass('asc').addClass('dsc');
		}else if($(this).parent().is('.dsc')){//is sorted decr. order
			$(this).attr("src", asc_icon);
			//do dec sorting here
			sortCarlist($sort_criteria1, "dsc");
			$(this).parent().removeClass('dsc').addClass('asc')
		}
		
	});
});
