var sendScheduleRequest = function() {
	var schdName = $("#schdName").val();
	var schdPhone = $("#schdPhone").val();
	var schdLocation = $("#schdLocation").val();

	if (schdName.length == 0 || schdPhone.length == 0 || schdLocation.length == 0) {
		alert("Please enter at least your name, phone number and select preferred location.");
	} else {
		var params = "";
		var request = { type: "Post", url: "", data: "", success: null, attr: null };

		params = "{name: '" + schdName + "', dob: '" + $("#schdBirthDate").val() + "', ";
		params += "phone: '" + schdPhone + "', location: '" + schdLocation + "', ";
		params += "aptType: '" + $("#schdType").val() + "', provider: '" + $("#schdProvider").val() + "', ";
		params += "prefDate: '" + $("#schdDate").val() + "', prefTime: '" + $("#schdTime").val();
		params += "'}";

		request.url = "Schedule.asmx/SendSchedulingRequest";
		request.data = params;
		request.success = closeScheduleRequest;

		sendAjaxRequest(request);
	}
};

var closeScheduleRequest = function() {
	$("#dlgSchedule").dialog("close");
};

/*
* vertical news ticker
* Tadas Juozapaitis ( kasp3rito@gmail.com )
* http://www.jugbit.com/jquery-vticker-vertical-news-ticker/
*/
(function($) {
	$.fn.vTicker = function(options) {
		var defaults = {
			speed: 700,
			pause: 4000,
			showItems: 3,
			animation: "",
			mousePause: true,
			isPaused: false,
			direction: "up",
			height: 0
		};

		var options = $.extend(defaults, options);

		moveUp = function(obj2, height, options) {
			if (options.isPaused)
				return;

			var obj = obj2.children("ul");
			var clone = obj.children("li:first").clone(true);

			if (options.height > 0)
				height = obj.children("li:first").height();

			obj.animate({ top: "-=" + height + "px" }, options.speed, function() {
				$(this).children("li:first").remove();
				$(this).css("top", "0px");
			});

			if (options.animation == "fade") {
				obj.children("li:first").fadeOut(options.speed);
				if (options.height == 0) {
					obj.children("li:eq(" + options.showItems + ")").hide().fadeIn(options.speed).show();
				}
			}

			clone.appendTo(obj);
		};

		moveDown = function(obj2, height, options) {
			if (options.isPaused)
				return;

			var obj = obj2.children("ul");
			var clone = obj.children("li:last").clone(true);

			if (options.height > 0)
				height = obj.children("li:first").height();

			obj.css("top", "-" + height + "px")
			.prepend(clone);

			obj.animate({ top: 0 }, options.speed, function() {
				$(this).children("li:last").remove();
			});

			if (options.animation == "fade") {
				if (options.height == 0) {
					obj.children("li:eq(" + options.showItems + ")").fadeOut(options.speed);
				}
				obj.children("li:first").hide().fadeIn(options.speed).show();
			}
		};

		return this.each(function() {
			var obj = $(this);
			var maxHeight = 0;

			obj.css({ overflow: "hidden", position: "relative" })
			.children("ul").css({ position: "absolute", margin: 0, padding: 0 })
			.children("li").css({ margin: 0, padding: 0 });

			if (options.height == 0) {
				obj.children("ul").children("li").each(function() {
					if ($(this).height() > maxHeight) {
						maxHeight = $(this).height();
					}
				});

				obj.children("ul").children("li").each(function() {
					$(this).height(maxHeight);
				});

				obj.height(maxHeight * options.showItems);
			} else {
				obj.height(options.height);
			}

			var interval = setInterval(function() {
				if (options.direction == "up") {
					moveUp(obj, maxHeight, options);
				} else {
					moveDown(obj, maxHeight, options);
				}
			}, options.pause);

			if (options.mousePause) {
				obj.bind("mouseenter", function() {
					options.isPaused = true;
				}).bind("mouseleave", function() {
					options.isPaused = false;
				});
			}
		});
	};
})(jQuery);

(function($) {
	$.fn.tipHover = function(options) {
		var defaults = {
			id: false,
			type: false,
			ajax: false,
			ajaxUrl: "",
			highlight: true,
			width: 290,
			height: "auto",
			xOffset: 0,
			yOffset: 0,
			orientation: "right",
			appendTo: "body",
			sticky: false,
			//mouseOutClose: false,
			backgroundColor: "#F6F6F6",
			showTitle: true,
			title: "",
			showFooter: false
		};

		var options = $.extend(defaults, options);

		tipInit = function(el, event, options) {
			var linkId = el.id
			var $this = $(el);
			var dim = getDimensions(el, event);
			var tipTitle = (options.title) ? options.title : $this.html();

			if ($("#tipMain").length) tipDispose();

			$tip = $(["<div id=\"tipMain\">",
				"<div id=\"tipTitle\"></div>",
				"<div id=\"tipBody\"></div>",
				"<div id=\"tipFooter\"></div>",
			"</div>"].join(""))
			["appendTo"](options.appendTo).hide();
			$tipTitle = $("#tipTitle").html(tipTitle);
			$tipBody = $("#tipBody");

			tipShow($this, options, dim);
		};

		tipShow = function($this, options, dim) {
			var $tip = $("#tipMain");
			var $tipTitle = $("#tipTitle");
			var $tipBody = $("#tipBody");
			var yOffset = parseInt(options.yOffset, 10);
			var xOffset = parseInt(options.xOffset, 10);
			var tipHeight;
			var defHeight = isNaN(parseInt(options.height, 10)) ? "auto" :
				(/\D/g).test(options.height) ? options.height : options.height + "px";

			if (!options.showTitle) $tipTitle.hide();

			if (options.contentDiv) {
				var $content = $("#" + options.contentDiv);

				if (options.incChildDiv) {
					var $childDiv = $this.parent().children("#copy");
					$content.children("div.incChild").html($childDiv.html());
				}
				$tipBody.append($content.html());
			}

			if (options.ajax) {
				var request = { type: "Post", url: "", data: "", success: null, attr: null };

				request.url = options.ajaxUrl;
				request.data = "{id: '" + $this.attr("id") + "'}";
				request.success = fireCallback;

				sendAjaxRequest(request);
			}

			if (options.showFooter) {
				$("#tipFooter").html($this.html()).show();
			}

			//set horizontal
			var borderWidth = (parseInt($tip.css("borderLeftWidth"), 10) || 0) + (parseInt($tip.css("borderRightWidth"), 10) || 0);
			var tipWidth = (parseInt(options.width, 10) || 275) + borderWidth;
			var posX = ((dim.elWidth + dim.elX + tipWidth + xOffset) > dim.winWidth || options.orientation == "left")
				? dim.elX - tipWidth - xOffset : dim.elWidth + dim.elX + xOffset;
			var pY = posX < 0 ? dim.mouseY + yOffset : dim.mouseY;

			//set vertical
			var baseline = (dim.scrollTop + dim.winHeight);
			var posY = dim.elY;
			tipHeight = defHeight == "auto" ? Math.max($tip.outerHeight(), $tip.height()) : parseInt(defHeight, 10);
			if ((posY + tipHeight + yOffset) > baseline) {
				posY = (tipHeight >= dim.winHeight) ? sTop : (baseline - tipHeight - yOffset);
			}

			$tip.css({ position: "absolute", top: posY, left: posX, width: options.width, height: defHeight, backgroundColor: options.backgroundColor });
			$tip.show();
		};

		tipDispose = function() {
			var $tip = $("#tipMain");

			if ($tip.length) {
				$tip.remove();
				$tip.unbind();
			}
		};

		return this.live("mouseover", function(event) {
			var el = this;
			var timer = setTimeout(function() { tipInit(el, event, options); }, 500);

			$(el).bind("mouseout", function(event) {
				tipDispose();
				clearTimeout(timer);
			});
		});
	};
})(jQuery);

getDimensions = function(el, event) {
	var dimensions = {
		winHeight: $(window).height(),
		winWidth: $(window).width(),
		scrollTop: $(document).scrollTop(),
		mouseX: event.pageX,
		mouseY: event.pageY,
		elX: $(el).offset().left,
		elY: $(el).offset().top,
		elWidth: el.offsetWidth
	};

	return dimensions;
}

sendAjaxRequest = function(request) {
	$.ajax({
		type: request.type,
		url: request.url,
		data: request.data,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: function(data) {
			// Execute user defined method
			if (typeof request.success === "function")
				request.success(data);
		},
		error: function() {
			alert("There was an error processing the request.");
		}
	});
}

fireCallback = function(data) {
	var info = data.d;

	$("#tipBody").append(info);
};
