(function(b) {
	b.fn.ticker = function(q) {
		var e = b.extend({}, b.fn.ticker.defaults, q), m = "#"
				+ b(this).attr("id"), n = b(this).attr("tagName");
		return this
				.each(function() {
					function h(c) {
						var d = 0, f;
						for (f in c)
							c.hasOwnProperty(f) && d++;
						return d
					}
					function i(c) {
						if (e.debugMode)
							window.console && window.console.log ? window.console
									.log(c)
									: alert(c)
					}
					function r() {
						if (a.contentLoaded == false)
							if (e.ajaxFeed)
								e.feedType == "xml" ? b
										.ajax({
											url : e.feedUrl,
											cache : false,
											dataType : e.feedType,
											async : true,
											success : function(c) {
												for ( var d = count = 0; d < c.childNodes.length; d++)
													if (c.childNodes[d].nodeName == "rss")
														xmlContent = c.childNodes[d];
												for (c = 0; c < xmlContent.childNodes.length; c++)
													if (xmlContent.childNodes[c].nodeName == "channel")
														xmlChannel = xmlContent.childNodes[c];
												for (c = 0; c < xmlChannel.childNodes.length; c++)
													if (xmlChannel.childNodes[c].nodeName == "item") {
														xmlItems = xmlChannel.childNodes[c];
														var f;
														d = false;
														for ( var g = 0; g < xmlItems.childNodes.length; g++) {
															if (xmlItems.childNodes[g].nodeName == "title")
																f = xmlItems.childNodes[g].lastChild.nodeValue;
															else if (xmlItems.childNodes[g].nodeName == "link")
																d = xmlItems.childNodes[g].lastChild.nodeValue;
															if (f !== false
																	&& f != ""
																	&& d !== false) {
																a.newsArr["item-"
																		+ count] = {
																	type : e.titleText,
																	content : '<a href="'
																			+ d
																			+ '">'
																			+ f
																			+ "</a>"
																};
																count++;
																d = f = false
															}
														}
													}
												if (h(a.newsArr < 1)) {
													i("Couldn't find any content from the XML feed for the ticker to use!");
													return false
												}
												j();
												a.contentLoaded = true
											}
										})
										: i("Code Me!");
							else if (e.htmlFeed)
								if (b(m + " LI").length > 0) {
									b(m + " LI").each(function(c) {
										a.newsArr["item-" + c] = {
											type : e.titleText,
											content : b(this).html()
										}
									});
									j()
								} else {
									i("Couldn't find HTML any content for the ticker to use!");
									return false
								}
							else {
								i("The ticker is set to not use any types of content! Check the settings for the ticker.");
								return false
							}
					}
					function j() {
						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 == h(a.newsArr) - 1)
							a.position = 0;
						else
							a.position++;
						distance = b(a.dom.contentID).width();
						time = distance / e.speed;
						b(a.dom.wrapperID).find(a.dom.titleID).fadeIn().end()
								.find(a.dom.titleElem).fadeIn("slow", s)
					}
					function s() {
						if (a.play) {
							var c = b(a.dom.titleElem).width() + 20;
							b(a.dom.revealID).css("left", c + "px");
							if (e.displayType == "fade")
								b(a.dom.revealID).hide(
										0,
										function() {
											b(a.dom.contentID).css("left",
													c + "px").delay(20).fadeIn(
													"slow", k)
										});
							else
								e.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",
																			k)
														})
						} else
							return false
					}
					function k() {
						if (a.play) {
							b(a.dom.contentID).delay(e.pauseOnItems).fadeOut(
									"slow");
							e.displayType == "fade" ? b(a.dom.contentID)
									.fadeOut(
											e.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");
												j()
											})
									: b(a.dom.revealID)
											.hide(
													0,
													function() {
														b(a.dom.tickerID)
																.delay(
																		e.pauseOnItems)
																.fadeOut(
																		e.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");
																			j()
																		})
													})
						} else
							b(a.dom.revealElem).hide()
					}
					function l() {
						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 o() {
						a.play = true;
						a.paused = false;
						k()
					}
					function p(c) {
						l();
						switch (c) {
						case "prev":
							if (a.position == 0)
								a.position = h(a.newsArr) - 2;
							else if (a.position == 1)
								a.position = h(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 == h(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 (n != "UL" && e.htmlFeed === true) {
						i("Cannot use <"
								+ n.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 (e.controls) {
							b(a.dom.controlsID)
									.live(
											"click mouseover mousedown mouseout mouseup",
											function(c) {
												var d = c.target.id;
												if (c.type == "click")
													switch (d) {
													case a.dom.prevID.replace(
															"#", ""):
														a.paused = true;
														b(a.dom.playPauseID)
																.addClass(
																		"paused");
														p(d);
														break;
													case a.dom.nextID.replace(
															"#", ""):
														a.paused = true;
														b(a.dom.playPauseID)
																.addClass(
																		"paused");
														p(d);
														break;
													case a.dom.playPauseID
															.replace("#", ""):
														if (a.play == true) {
															a.paused = true;
															b(a.dom.playPauseID)
																	.addClass(
																			"paused");
															l()
														} else {
															a.paused = false;
															b(a.dom.playPauseID)
																	.removeClass(
																			"paused");
															o()
														}
													}
												else if (c.type == "mouseover"
														&& b("#" + d).hasClass(
																"controls"))
													b("#" + d).addClass("over");
												else if (c.type == "mousedown"
														&& b("#" + d).hasClass(
																"controls"))
													b("#" + d).addClass("down");
												else if (c.type == "mouseup"
														&& b("#" + d).hasClass(
																"controls"))
													b("#" + d).removeClass(
															"down");
												else
													c.type == "mouseout"
															&& b("#" + d)
																	.hasClass(
																			"controls")
															&& b("#" + d)
																	.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 && l()
						}).mouseout(function() {
							a.paused == false && o()
						});
						r()
					})()
				})
	};
	b.fn.ticker.defaults = {
		speed : 0.1,
		ajaxFeed : false,
		feedUrl : "",
		feedType : "xml",
		displayType : "reveal",
		htmlFeed : true,
		debugMode : true,
		controls : true,
		titleText : "Latest",
		pauseOnItems : 3E3,
		fadeInSpeed : 300,
		fadeOutSpeed : 300
	}
})(jQuery);
