(function(b) {
	b.fn.ticker = function(p) {
		var d = b.extend( {}, b.fn.ticker.defaults, p), k = "#"
				+ b(this).attr("id"), l = b(this).attr("tagName");
		return this
				.each(function() {
					function f(c) {
						var e = 0, m;
						for (m in c)
							c.hasOwnProperty(m) && e++;
						return e
					}
					function g(c) {
						if (d.debugMode)
							window.console && window.console.log ? window.console
									.log(c)
									: alert(c)
					}
					function h() {
						if (a.contentLoaded == false) {
							if (d.ajaxFeed)
								g("Code Me!");
							else if (d.htmlFeed)
								if (b(k + " LI").length > 0)
									b(k + " LI").each(function(c) {
										a.newsArr["item-" + c] = {
											type : d.titleText,
											content : b(this).html()
										}
									});
								else {
									g("Couldn't find any content for the ticker to use!");
									return false
								}
							else {
								g("Couldn't find any content for the ticker to use!");
								return false
							}
							a.contentLoaded = true
						}
						b(a.dom.titleElem).html(
								a.newsArr["item-" + a.position].type);
						b(a.dom.contentID).html(
								a.newsArr["item-" + a.position].content);
						if (a.position == f(a.newsArr) - 1)
							a.position = 0;
						else
							a.position++;
						distance = b(a.dom.contentID).width();
						time = distance / d.speed;
						b(a.dom.wrapperID).find(a.dom.titleID).fadeIn().end()
								.find(a.dom.titleElem).fadeIn("slow", q)
					}
					function q() {
						if (a.play) {
							var c = b(a.dom.titleElem).width() + 0;
							b(a.dom.revealID).css("left", c + "px");
							if (d.displayType == "fade")
								b(a.dom.revealID).hide(
										0,
										function() {
											b(a.dom.contentID).css("left",
													c + "px").delay(20).fadeIn(
													"slow", i)
										});
							else
								d.displayType != "scroll"
										&& b(a.dom.revealElem)
												.show(
														0,
														function() {
															b(a.dom.contentID)
																	.css(
																			"left",
																			c
																					+ "px")
																	.show();
															b(a.dom.revealID)
																	.css(
																			"margin-left",
																			"0px")
																	.delay(20)
																	.animate(
																			{
																				marginLeft : distance
																						+ "px"
																			},
																			time,
																			"linear",
																			i)
														})
						} else
							return false
					}
					function i() {
						if (a.play) {
							b(a.dom.contentID).delay(d.pauseOnItems).fadeOut(
									"slow");
							d.displayType == "fade" ? b(a.dom.contentID)
									.fadeOut(
											d.fadeOutSpeed,
											function() {
												b(a.dom.wrapperID)
														.find(
																a.dom.revealElem
																		+ ","
																		+ a.dom.contentID)
														.hide()
														.end()
														.find(
																a.dom.tickerID
																		+ ","
																		+ a.dom.revealID
																		+ ","
																		+ a.dom.titleID)
														.show()
														.end()
														.find(
																a.dom.tickerID
																		+ ","
																		+ a.dom.revealID
																		+ ","
																		+ a.dom.titleID)
														.removeAttr("style");
												h()
											})
									: b(a.dom.revealID)
											.hide(
													0,
													function() {
														b(a.dom.tickerID)
																.delay(
																		d.pauseOnItems)
																.fadeOut(
																		d.fadeOutSpeed,
																		function() {
																			b(
																					a.dom.wrapperID)
																					.find(
																							a.dom.titleElem
																									+ ","
																									+ a.dom.revealElem
																									+ ","
																									+ a.dom.contentID)
																					.hide()
																					.end()
																					.find(
																							a.dom.tickerID
																									+ ","
																									+ a.dom.revealID
																									+ ","
																									+ a.dom.titleID)
																					.show()
																					.end()
																					.find(
																							a.dom.tickerID
																									+ ","
																									+ a.dom.revealID
																									+ ","
																									+ a.dom.titleID)
																					.removeAttr(
																							"style");
																			h()
																		})
													})
						} else
							b(a.dom.revealElem).hide()
					}
					function j() {
						a.play = false;
						b(
								a.dom.tickerID + "," + a.dom.revealID + ","
										+ a.dom.titleID + "," + a.dom.titleElem
										+ "," + a.dom.revealElem + ","
										+ a.dom.contentID).stop(true, true);
						b(a.dom.revealID + "," + a.dom.revealElem).hide();
						b(a.dom.wrapperID).find(
								a.dom.titleID + "," + a.dom.titleElem).show()
								.end().find(a.dom.contentID).show()
					}
					function n() {
						a.play = true;
						a.paused = false;
						i()
					}
					function o(c) {
						j();
						switch (c) {
						case "prev":
							if (a.position == 0)
								a.position = f(a.newsArr) - 2;
							else if (a.position == 1)
								a.position = f(a.newsArr) - 1;
							else
								a.position -= 2;
							b(a.dom.titleElem).html(
									a.newsArr["item-" + a.position].type);
							b(a.dom.contentID).html(
									a.newsArr["item-" + a.position].content);
							break;
						case "next":
							b(a.dom.titleElem).html(
									a.newsArr["item-" + a.position].type);
							b(a.dom.contentID).html(
									a.newsArr["item-" + a.position].content)
						}
						if (a.position == f(a.newsArr) - 1)
							a.position = 0;
						else
							a.position++
					}
					var a = {
						position : 0,
						time : 0,
						distance : 0,
						newsArr : {},
						play : true,
						paused : false,
						contentLoaded : false,
						dom : {
							contentID : "#ticker-content",
							titleID : "#ticker-title",
							titleElem : "#ticker-title SPAN",
							tickerID : "#ticker",
							wrapperID : "#ticker-wrapper",
							revealID : "#ticker-swipe",
							revealElem : "#ticker-swipe SPAN",
							controlsID : "#ticker-controls",
							prevID : "#prev",
							nextID : "#next",
							playPauseID : "#play-pause"
						}
					};
					if (l != "UL" && d.htmlFeed === true) {
						g("Cannot use <"
								+ l.toLowerCase()
								+ "> type of element for this plugin - must of type <ul>");
						return false
					}
					(function() {
						b(a.dom.wrapperID)
								.append(
										'<div id="'
												+ a.dom.tickerID.replace("#",
														"")
												+ '"><div id="'
												+ a.dom.titleID
														.replace("#", "")
												+ '"><span style="display: none;"><!-- --\></span></div><p id="'
												+ a.dom.contentID.replace("#",
														"")
												+ '"></p><div id="'
												+ a.dom.revealID.replace("#",
														"")
												+ '"><span style="display: none;"><!-- --\></span></div></div>');
						b(a.dom.wrapperID).removeClass("no-js").addClass(
								"has-js");
						b(
								a.dom.tickerElem + "," + a.dom.titleElem + ","
										+ a.dom.contentID).hide();
						if (d.controls) {
							b(a.dom.controlsID)
									.live(
											"click mouseover mousedown mouseout mouseup",
											function(c) {
												var e = c.target.id;
												if (c.type == "click")
													switch (e) {
													case a.dom.prevID.replace(
															"#", ""):
														a.paused = true;
														b(a.dom.playPauseID)
																.addClass(
																		"paused");
														o(e);
														break;
													case a.dom.nextID.replace(
															"#", ""):
														a.paused = true;
														b(a.dom.playPauseID)
																.addClass(
																		"paused");
														o(e);
														break;
													case a.dom.playPauseID
															.replace("#", ""):
														if (a.play == true) {
															a.paused = true;
															b(a.dom.playPauseID)
																	.addClass(
																			"paused");
															j()
														} else {
															a.paused = false;
															b(a.dom.playPauseID)
																	.removeClass(
																			"paused");
															n()
														}
													}
												else if (c.type == "mouseover"
														&& b("#" + e).hasClass(
																"controls"))
													b("#" + e).addClass("over");
												else if (c.type == "mousedown"
														&& b("#" + e).hasClass(
																"controls"))
													b("#" + e).addClass("down");
												else if (c.type == "mouseup"
														&& b("#" + e).hasClass(
																"controls"))
													b("#" + e).removeClass(
															"down");
												else
													c.type == "mouseout"
															&& b("#" + e)
																	.hasClass(
																			"controls")
															&& b("#" + e)
																	.removeClass(
																			"over")
											});
							b(a.dom.wrapperID)
									.append(
											'<ul id="'
													+ a.dom.controlsID.replace(
															"#", "")
													+ '"><li id="'
													+ a.dom.playPauseID
															.replace("#", "")
													+ '" class="controls"></li><li id="'
													+ a.dom.prevID.replace("#",
															"")
													+ '" class="controls"></li><li id="'
													+ a.dom.nextID.replace("#",
															"")
													+ '" class="controls"></li></ul>')
						}
						b(a.dom.contentID).mouseover(function() {
							a.paused == false && j()
						}).mouseout(function() {
							a.paused == false && n()
						});
						h()
					})()
				})
	};
	b.fn.ticker.defaults = {
		speed : 1,
		ajaxFeed : false,
		displayType : "reveal",
		htmlFeed : true,
		debugMode : true,
		controls : true,
		titleText : "",
		pauseOnItems : 3E3,
		fadeInSpeed : 300,
		fadeOutSpeed : 300
	}
})(jQuery);
