// Begin jQuery 
jQuery(document).ready(function($){
	
	/* incase we need these values in JS */
	var base_url = 'http://www.softwoodbargains.com/';
	var site_url = 'http://www.softwoodbargains.com/';

	/**~~~~~~~~~~~~~~~~~~~ Confirm ~~~~~~~~~~~~~~~~~~**/
	
	// Confirm delete stock
	$('.delete-stock').click(function(e){
		e.preventDefault();
		var link = $(this).attr('href');
		if( confirm("Are you sure you wish to delete that stock listing? You cannot undo this action") )
		{
			window.location.href = link;
		};
	});
	
	/**~~~~ serahcing by lotref removes all other search options ~~~~~~**/
	
	$('#searchdropdown-lotref').change(function(){
		var lotref = $(this).val();
		if(lotref == 'all')
		{
			// user has decided to not search by lotref
			//alert('your going to search by other stuff');
			$('#searchdropdown-thickness').show().prev('label').show();
			$('#searchdropdown-width').show().prev('label').show();
			$('#searchdropdown-product').show().prev('label').show();
			$('#searchdropdown-species').show().prev('label').show();
			
			var thickness = 'all';
			var width = 'all';
			var product = 'all';
			var species = 'all';
			
			update_search_dropdown_thickness(thickness, width, product, species);
			update_search_dropdown_width(thickness, width, product, species);
			update_search_dropdown_product(thickness, width, product, species);
			update_search_dropdown_species(thickness, width, product, species);
		}
		else
		{
			// user wants to choose a single lot ref. all other criteria are useless
			//alert('your choosing a single id');
			$('#searchdropdown-thickness').val('all').hide().prev('label').hide();
			$('#searchdropdown-width').val('all').hide().prev('label').hide();
			$('#searchdropdown-product').val('all').hide().prev('label').hide();
			$('#searchdropdown-species').val('all').hide().prev('label').hide();
		}
	});
	
	$("#searchdropdown-thickness").change(function(){
		var thickness = $("#searchdropdown-thickness").val();
		var width = $("#searchdropdown-width").val();
		var product = $("#searchdropdown-product").val();
		var species = $("#searchdropdown-species").val();
		
		update_search_dropdown_width(thickness, width, product, species);
		update_search_dropdown_product(thickness, width, product, species);
		update_search_dropdown_species(thickness, width, product, species);
	});
	
	$("#searchdropdown-width").change(function(){
		var thickness = $("#searchdropdown-thickness").val();
		var width = $("#searchdropdown-width").val();
		var product = $("#searchdropdown-product").val();
		var species = $("#searchdropdown-species").val();
		
		update_search_dropdown_thickness(thickness, width, product, species);
		update_search_dropdown_product(thickness, width, product, species);
		update_search_dropdown_species(thickness, width, product, species);
	});
	
	$("#searchdropdown-product").change(function(){
		var thickness = $("#searchdropdown-thickness").val();
		var width = $("#searchdropdown-width").val();
		var product = $("#searchdropdown-product").val();
		var species = $("#searchdropdown-species").val();
		
		update_search_dropdown_thickness(thickness, width, product, species);
		update_search_dropdown_width(thickness, width, product, species);
		update_search_dropdown_species(thickness, width, product, species);
	});
	
	$("#searchdropdown-species").change(function(){
		var thickness = $("#searchdropdown-thickness").val();
		var width = $("#searchdropdown-width").val();
		var product = $("#searchdropdown-product").val();
		var species = $("#searchdropdown-species").val();
		
		update_search_dropdown_thickness(thickness, width, product, species);
		update_search_dropdown_width(thickness, width, product, species);
		update_search_dropdown_product(thickness, width, product, species);
	});
	
	function update_search_dropdown_thickness(thickness, width, product, species)
	{
		$.ajax({
			type: "POST",
			url: site_url+'public_ajax/search_dropdown_thickness',
			data: "thickness="+thickness+"&width="+width+"&species="+species+"&product="+product,
			success: function(data){
				$("#searchdropdown-thickness").html(data);
			}
		 });
	}
	
	function update_search_dropdown_width(thickness, width, product, species)
	{
		$.ajax({
			type: "POST",
			url: site_url+'public_ajax/search_dropdown_width',
			data: "thickness="+thickness+"&width="+width+"&species="+species+"&product="+product,
			success: function(data){
				$("#searchdropdown-width").html(data);
			}
		 });
	}
	
	function update_search_dropdown_product(thickness, width, product, species)
	{
		$.ajax({
			type: "POST",
			url: site_url+'public_ajax/search_dropdown_product',
			data: "thickness="+thickness+"&width="+width+"&species="+species+"&product="+product,
			success: function(data){
				$("#searchdropdown-product").html(data);
			}
		 });
	}
	
	function update_search_dropdown_species(thickness, width, product, species)
	{
		$.ajax({
			type: "POST",
			url: site_url+'public_ajax/search_dropdown_species',
			data: "thickness="+thickness+"&width="+width+"&species="+species+"&product="+product,
			success: function(data){
				$("#searchdropdown-species").html(data);
			}
		 });
	}
	
	/**~~~~~~~~~ Fancybox ~~~~~~~~~~~**/
	
	$('.fancybox-image').fancybox({
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'cyclic'		: true,
		'easingIn'		: 'easeOutBack',
		'easingOut'		: 'easeInBack'
	});
	
	/** Admin delete images **/
	$('.admin-delete-image').click(function(e){
		e.preventDefault();
		var link = $(this).attr('href');
		if( confirm("Are you sure you wish to delete that image? You cannot undo this action!") )
		{
			window.location.href = link;
		};
	});
	
	
});