function advanceSlideShow() {
	if(jQuery("#startstop").hasClass("started")) {
		var nextli = jQuery(".selected").parent().next("li");
		if(nextli.length == 1) {
			nextli.find("a").click();
		} else {
			jQuery(".selected").parent().parent().find("li:first a").click();
		}
	}
}


function insertAtCursor(myField, myValue) {
	//IE support
	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
	}

	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos)
		+ myValue
		+ myField.value.substring(endPos, myField.value.length);
	} else {
		myField.value += myValue;
	}
}



function setSelectionRange(input, selectionStart, selectionEnd) {
  if (input.setSelectionRange) {
    input.focus();
    input.setSelectionRange(selectionStart, selectionEnd);
  }
  else if (input.createTextRange) {
    var range = input.createTextRange();
    range.collapse(true);
    range.moveEnd('character', selectionEnd);
    range.moveStart('character', selectionStart);
    range.select();
  }
}


function setCaretToPos (input, pos) {
  setSelectionRange(input, pos, pos);
}


jQuery(document).ready(function($) {
	if ($.browser.msie && $.browser.version < 9) $('select.wide')
    .bind('focus mouseover', function() { $(this).addClass('expand').removeClass('clicked'); })
    .bind('click', function() { $(this).toggleClass('clicked'); })
    .bind('mouseout', function() { if (!$(this).hasClass('clicked')) { $(this).removeClass('expand'); }})
    .bind('blur', function() { $(this).removeClass('expand clicked'); });

	$("#slideshow").jqGalScroll();

	$(".jqGSPagination").prepend($("#startstop")).show();

	setInterval('advanceSlideShow()', 4964);

	$("#startstop").click(function() {
		$(this).toggleClass("started").toggleClass("stopped");
	});

	$(".new-user-form-show").bind("click", function() {
		$this = $(this);
		if($(".new-user-form:visible").length > 0) {
			$(".new-user-form").slideUp("fast");
			$this.html("Add New User");
		} else {
			$(".new-user-form").slideDown("fast");
			$this.html("Hide Form");
		}
	});

	$(".ETA").each(function() {

			$(this).datepicker();

	});

	$(".datepicker").live("focus", function() {

		$(this).datepicker();

	});

	

	$(".save-order-status").live("click", function() {

		var $this = $(this);

		var order_id = $this.attr("data-order-id");

		var order_status = $("#order-status-" + order_id).val();

		var order_eta = $("#order-eta-" + order_id).val();



		var notify = confirm("Do you want to notify the customer if the status of this order has changed?  Click cancel if not.");
		
		if(notify) {
			notify = "1";
		} else {
			notify = "0";
		}
		
		$this.html("Saving...");

		

		$.get("/ajax/order/save_status.php", {

			"order_id" : order_id,

			"order_status" : order_status,

			"order_eta" : order_eta,

			"notify" : notify

		}, function(response, data) {

			if(response != "FALSE") {

				$this.html("Save");						

			} else {

				$this.html("FAIL");

			}

		});

	});

	

	$(".order-status-select").bind("change", function() {

		var $this = $(this);

		var order_id = $this.attr("data-order-id");

		$this.parent("td").parent("tr").find(".save-order-status").trigger("click");

	});

	

	$("#add_order_customer").bind("change", function() {

		var $this = $(this);

		var customer_id = $this.val();



		$.get("/ajax/products/product_options.php", {

			"customer_id" : customer_id

		}, function(response, data) {

			if(response != "") {

				$("#add_order_product").html(response);						

			} else {

				$("#add_order_product").html("<option>No products. Try Again.</option");

			}

		});

	});

	

	$(".user_customer_select").bind("change", function() {

		var $this = $(this);

		var customer_id = $this.val();

		var user_id = $this.attr("data-user-id");

		

		$.get("/ajax/save_user_customer.php", {

			"customer_id" : customer_id,

			"user_id" : user_id

		}, function(response, data) {

		});

	});



	$(".calculator-quantity").live("keyup", function() {

		var $this = $(this);

		var daily_value_id = $this.attr("data-ingredient-id");

		var divisor = $("#ingredient-" + daily_value_id + " .value").html() / $this.val();

		var result = 100 / divisor;

		result = Math.round((result*10) / 10);

		

		$("#ingredient-" + daily_value_id + " .result").html(result + "%");

	});

	

	

	$("#tabs").tabs();

	

	/****************************************************************/

	//Autocomplete -------------------------------------------------

	/****************************************************************/

	

	$("#calculator-autocomplete").bind("focus", function() {

		$.get("/ajax/calculator/ingredient_names.php", {}, function(result, data) {

			var data = result.split(";");

			$("#calculator-autocomplete").autocomplete(data);

		});

		

	});

	

	$("#calculator-autocomplete").bind("keyup", function(e) {

		var $this = $(this);

		var code = (e.keyCode ? e.keyCode : e.which);

		if(code == 13) { //If the enter key was pressed

			var name = $(this).val();

			$.get("/ajax/calculator/ingredient_data.php", { "name" : name }, function(result, data) {

				var ingredient = JSON.parse(result);

				if(ingredient['name'] != "") {

					$("#calculator-table").append('<tr id="ingredient-'+ingredient['daily_value_item_id']+'"><td>'+ingredient['name']+'</td><td class="value">'+ingredient['value']+'</td><td>'+ingredient['unit']+'</td><td><input type="textbox" class="calculator-quantity dark-border" maxlength="8" data-ingredient-id="'+ingredient['daily_value_item_id']+'" size="3" rel=""></td><td class="result">0%</td></tr>');

					$(".placeholder-box").hide();

					$("#calculator-table").show();

				}

				$this.val("");

			});

		}

		

	});

	

	$("#add_order_product_name").bind("focus", function() {

		var $this = $(this);

		var customer_id = "";

		if($this.attr("data-is-client") == 0) {

			customer_id = $("#add_order_customer").val();

		}

		$.get("/ajax/order/order_products.php", { "customer_id" : customer_id }, function(result, data) {

			var data = result.split(";");

			$("#add_order_product_name").autocomplete(data);

		});

		

	});

	$("#add_order_quantity").bind("focus", function() {

		var id = $("#add_order_product_id").val();

		$.get("/ajax/products/product_data.php", { "id" : id }, function(result, data) {

			var product = JSON.parse(result);

				$("#quantity-explanation").html("Quantity in '" + product['unit'] + "'");

		});

	});



	/****************************************************************/

	//Simple Ajax

	/****************************************************************/

	

	$("#add_product_customer").bind("change", function() {

		var customer_id = $(this).val();

		$("#add_product").attr("action", "/products/?customer_id=" + customer_id);

	});

	

	$("#products-select-customer").bind("change", function() {

		var $this = $(this);

		var customer_id = $this.val();

		if(customer_id > 0) {

			$(".placeholder-box h1").html("Loading...");

			

			$.get("/ajax/products/product_customers.php", { "customer_id" : customer_id }, function(result, data) {

				trimmed_result = $.trim(result);

				

				if(trimmed_result != "" && trimmed_result != " ") {

					$(".placeholder-box").hide();

					$(".products-container").html(result);

					$(".products-container").show();

				} else {

					$(".products-container table").hide();

					$(".placeholder-box").show();

					$(".placeholder-box h1").html("No products found.  Select another customer.");

				}

			});

		}

	});

	

	$("#add_order_customer").bind("change", function() {

		var customer_id = $(this).val();

		$("#add_order").attr("action", "/order/?customer_id=" + customer_id);

	});

	

	$("#orders-select-customer").bind("change", function() {

		var $this = $(this);

		var customer_id = $this.val();

		if(customer_id > 0) {

			$(".placeholder-box h1").html("Loading...");

		

			$.get("/ajax/order/order_customers.php", { "customer_id" : customer_id }, function(result, data) {

				trimmed_result = $.trim(result);

				

				if(trimmed_result != "" && trimmed_result != " ") {

					$(".placeholder-box").hide();

					$(".orders-container").html(result);

					$(".orders-container").show();

				} else {

					$(".orders-container").hide();

					$(".placeholder-box").show();

					$(".placeholder-box h1").html("No orders found. Select another customer.");

				}

			});

		}

	});

	

	$("#add_doc_customer").bind("change", function() {

		var customer_id = $(this).val();

		$("#add_doc").attr("action", "/laboratory/?customer_id=" + customer_id);

	});

	

	$("#labs-select-customer").bind("change", function() {

		var $this = $(this);

		var customer_id = $this.val();

		if(customer_id > 0) {

			$(".placeholder-box h1").html("Loading...");

		

			$.get("/ajax/laboratory/customer_labs.php", { "customer_id" : customer_id }, function(result, data) {

				trimmed_result = $.trim(result);

				

				if(trimmed_result != "" && trimmed_result != " ") {

					$(".placeholder-box").hide();

					$(".labs-container").html(result);

					$(".labs-container").show();

				} else {

					$(".labs-container table").hide();

					$(".placeholder-box").show();

					$(".placeholder-box h1").html("No lab reports found.  Select another customer.");

				}

			});

		}

	});

	

	$(".show-place-order-form").bind("click", function() {

		var $this = $(this);

		if($(".place-order-form:visible").length > 0) {

			$(".place-order-form").slideUp();

			$this.each(function() {

				$(this).html("Place New Order");

				$(this).removeClass("button_orange").addClass("button_blue");

			});

		} else {

			$(".place-order-form").slideDown();

			$this.each(function() {

				$(this).html("Hide New Order Form");

				$(this).removeClass("button_blue").addClass("button_orange");

			});

		}

	});

	

	$("#orders-select-status").bind("change", function() {



		var $this = $(this);

		var status = $this.val();



		if(status != "") {

			$(".placeholder-box.filter-status h1").html("Loading...");

		

			$.get("/ajax/order/order_customers.php", { "status" : status }, function(result, data) {

				trimmed_result = $.trim(result);

				

				if(trimmed_result != "" && trimmed_result != " ") {

					$this.parent("div").find(".placeholder-box").hide();

					$(".orders-status-container").html(result);

					$(".orders-status-container").show();

				} else {

					$(".orders-status-container").hide();

					$(".placeholder-box.filter-status").show();

					$(".placeholder-box.filter-status h1").html("No orders found. Select another status.");

				}

			});

		}

	});
	
	$("#add_order_customer").bind("change", function(){
		var customer_id = $(this).val();
		$.get("/ajax/order/order_customer_products.php", { "customer" : customer_id }, function(result, data) {

				$("#add_order_product_id").html(result);
				$("#add_order_product_id").removeAttr("disabled");
		});
	});

	/*$("select").bind("click", function() {
		$(this).width(400).css({"max-width" : "1000px"});
		$(this).parent("div").css({"overflow" : "hidden"});
	});*/
	

	/********************************************************/

	//Triggers

	$("#orders-select-status").trigger("change");

	$("#products-select-customer").trigger("change");

	$("#orders-select-customer").trigger("change");

	$("#labs-select-customer").trigger("change");

});


