var SORTER = {}; 
SORTER.sort = function(which, dir) {
	SORTER.dir = (dir == "desc") ? -1 : 1; 
	$(which).each(function() {
	// Find the list items and sort them 
	var sorted = $(this).find("> li").sort(function(a, b) {
		return $(a).text().toLowerCase() > $(b).text().toLowerCase() ? SORTER.dir : -SORTER.dir;
	});
	$(this).append(sorted); 
  });
}

$(document).ready(function() {
	
	$("table#vehicle_search_results")    
		.tablesorter()
		.tablesorterPager({
		 	container: $(".pager"),
			size: 25
		});
	
	// $("#truck-search, #plant-search").hide();
	
	$("select#vehicle_select").change(function() {
		
	  $("div#instructions").hide();
	  $("div#ass_logos").hide();
		
	  var selected = $(this).find('option:selected').val();
	
	  if (selected == "trucks") {
		$("#truck-search").show();
		$("#plant-search").hide();
	  }else if (selected == "plant") {
		$("#plant-search").show();
		$("#truck-search").hide();
	  }else if (selected == "") {
	    $("#truck-search").hide();
		$("#plant-search").hide();	
	  }
		
	});
	
	$('div#gallery_slider ul').bxSlider({
      displaySlideQty: 3,
      moveSlideQty: 1             
    });

    $("a.gallery").attr('rel', 'gallery').fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	300, 
		'speedOut'		:	200, 
		'overlayShow'	:	true,
		'cyclic'		: 	true
	});
	
	//$('.paging_container2').pajinate({
	//	items_per_page : 25
	//});
			
	$("select#view").val("listview");
	
	// $("div#listview").show();
	// $("div#gridview").hide();
	// $("div#gridview_all").hide();
	
	$("select#view").change(function() {
		  var view = $(this).find('option:selected').val();

		  if(view == "listview") {
			$("div#listview").show();
			$("div#gridview").hide();
			$("div#gridview_all").hide();
			
		  }else if(view == "gridview") {
			$("div#listview").hide();
			$("div#gridview").show();
			$('.paging_container2').pajinate({
				items_per_page : 25
			});
			
			$("a#showall").click(function() {
				$("div#gridview").hide();
				$("div#gridview_all").show();
			});
			$("a#hide_showall").click(function() {
				$("div#gridview_all").hide();
				$("div#gridview").show();
				
			});
		  }

		});
			
	$('.ascending').click(function() { 
	  SORTER.sort('.sortable');
	}); 
	$('.descending').click(function() {
	  SORTER.sort('.sortable', 'desc');
	});
	
	// $('span#other').hide();select_make
	
	$('#where_you_found_us').change(function(){
		if($(this).find(":selected").text() == "Other – please specify") {
			$('span#other').show();
		} else {
			$('span#other').hide();
		}
	});
	
	$('#select_make').change(function(){
		if($(this).find(":selected").text() == "Other – please specify") {
			$('span#other_two').show();
		} else {
			$('span#other_two').hide();
		}
	});
	
	$("a#tell_friend").click(function() {
		$("#tell-a-friend-form").toggle();
		$("#vehicle-enquiry-form").hide();
		$("#email-alerts").hide();
	});
	
	$("a#vehicle_enquiry_two").click(function() {
		$("#vehicle-enquiry-form").toggle();
		$("#tell-a-friend-form").hide();
		$("#email-alerts").hide();
	});
	
	$("a#print_details").click(function() {
		$("#vehicle-enquiry-form").hide();
		$("#tell-a-friend-form").hide();
		$("#email-alerts").hide();
	});
	
	$("a#download_pdf").click(function() {
		$("#vehicle-enquiry-form").hide();
		$("#tell-a-friend-form").hide();
		$("#email-alerts").hide();
	});
	
	$("a#email_alerts").click(function() {
		$("#email-alerts").toggle();
		$("#vehicle-enquiry-form").hide();
		$("#tell-a-friend-form").hide();
	});
	
	// Set cookies for truck search
	
	var selected_truck = $.cookie("truck_make"); 
  $("select#truck_make").val(selected_truck);
	
	$("select#truck_make").change(function() {
         
  	var truck_make = $(this).find('option:selected').attr('value');
  	$.cookie("truck_make", null);
		$.cookie("truck_make", truck_make, { path: '/' });
  	  
  });
  
  var selected_truck_type = $.cookie("truck_type"); 
  $("select#truck_type").val(selected_truck_type);
	
	$("select#truck_type").change(function() {
         
  	var truck_type = $(this).find('option:selected').attr('value');
  	$.cookie("truck_type", null);
		$.cookie("truck_type", truck_type, { path: '/' });
  	  
  });
  
  var selected_drive = $.cookie("drive"); 
  $("select#drive").val(selected_drive);
	
	$("select#drive").change(function() {
         
  	var drive = $(this).find('option:selected').attr('value');
  	$.cookie("drive", null);
		$.cookie("drive", drive, { path: '/' });
  	  
  });
  
  var selected_axle_config = $.cookie("axle_config"); 
  $("select#axle_config").val(selected_axle_config);
	
	$("select#axle_config").change(function() {
         
  	var axle_config = $(this).find('option:selected').attr('value');
  	$.cookie("axle_config", null);
		$.cookie("axle_config", axle_config, { path: '/' });
  	  
  });
  
  var selected_truck_year = $.cookie("truck_year"); 
  $("select#truck_year").val(selected_truck_year);
	
	$("select#truck_year").change(function() {
         
  	var truck_year = $(this).find('option:selected').attr('value');
  	$.cookie("truck_year", null);
		$.cookie("truck_year", truck_year, { path: '/' });
  	  
  });

  var selected_low_emission_zone = $.cookie("low_emission_zone"); 
  $("select#low_emission_zone").val(selected_low_emission_zone);

	$("select#low_emission_zone").change(function() {

  	var low_emission_zone = $(this).find('option:selected').attr('value');
  	$.cookie("low_emission_zone", null);
		$.cookie("low_emission_zone", low_emission_zone, { path: '/' });

  });

  var selected_euro = $.cookie("euro"); 
  $("select#euro").val(selected_euro);

	$("select#euro").change(function() {

  	var euro = $(this).find('option:selected').attr('value');
  	$.cookie("euro", null);
		$.cookie("euro", euro, { path: '/' });

  });
	
	
	// Set cookies for plant search
	
	var selected_plant = $.cookie("plant_make"); 
  $("select#plant_make").val(selected_plant);
	
	$("select#plant_make").change(function() {
         
  	var plant_make = $(this).find('option:selected').attr('value');
  	$.cookie("plant_make", null);
		$.cookie("plant_make", plant_make, { path: '/' });
  	  
  });
  
  var selected_plant_type = $.cookie("plant_type"); 
  $("select#plant_type").val(selected_plant_type);
	
	$("select#plant_type").change(function() {
         
  	var plant_type = $(this).find('option:selected').attr('value');
  	$.cookie("plant_type", null);
		$.cookie("plant_type", plant_type, { path: '/' });
  	  
  });
  
  var selected_plant_year = $.cookie("plant_year"); 
  $("select#plant_year").val(selected_plant_year);

	$("select#plant_year").change(function() {

  	var plant_year = $(this).find('option:selected').attr('value');
  	$.cookie("plant_year", null);
		$.cookie("plant_year", plant_year, { path: '/' });

  });
  
  // YouTube Video
  $("#show_video").click(function() {
	$.fancybox({
		'padding'		: 0,
		'autoScale'		: false,
		'transitionIn'	: 'none',
		'transitionOut'	: 'none',
		'width'			: 680,
		'height'		: 495,
		'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
		'type'			: 'swf',
		'swf'			: {
			'wmode'				: 'transparent',
			'allowfullscreen'	: 'true'
		}
	});
	return false;
  });
		
});

(function($){

})(window.jQuery);



window.log = function(){
  log.history = log.history || []; 
  log.history.push(arguments);
  if(this.console){
    console.log( Array.prototype.slice.call(arguments) );
  }
};

(function(doc){
  var write = doc.write;
  doc.write = function(q){ 
    log('document.write(): ',arguments); 
    if (/docwriteregexwhitelist/.test(q)) write.apply(doc,arguments);  
  };
})(document);



