$(function() {
	$.ajax({
        type: "GET",
        url: "/ticker-headlines.aspx?device=xml",
		dataType: "xml",
		success: function(xml) {
			var ticker = $("#ticker");
			var tickerWidth = ticker.width();
			var tickerHeight = ticker.height();
			var totalItems = $(xml).find('tickerItem').length;
			var tickerIndex = 0;
			var buttonLeft = $("<a>").attr("class","previous").html("Previous");
			var tickerCount = $("<div>").attr("class","tickerCount").html("1 of "+totalItems);
			var buttonRight = $("<a>").attr("class","next").html("Next");
			var mobile = $("#ticker.mobile").length;
			ticker.append(buttonLeft,tickerCount,buttonRight);
			buttonLeft.css({ top: (tickerHeight-buttonLeft.height())*.5 }).click();
			tickerCount.css({ top: (tickerHeight-tickerCount.height())*.5 });
			buttonRight.css({ top: (tickerHeight-buttonRight.height())*.5 });
			var xPos = buttonLeft.outerWidth(true)+tickerCount.outerWidth(true)+buttonRight.outerWidth(true);
			var yPos = 0;
			var tickerItemHeight = 0;
			
			if (mobile != 0){
				xPos = 0;
				yPos = tickerHeight;
				}
			
			$(xml).find('tickerItem').each(function(i) { 				 				
 				var iconPath = $(this).find('iconPath').text();
 				var title = $(this).find('title').text();
 				var link = $(this).find('link').text();
				
 				var tickerItem = $("<div>").attr("class","tickerItem").css({ width: tickerWidth-xPos });
 				ticker.append(tickerItem);
 				
 				if (iconPath != "") {
 					var tickerItemImage = $("<img>").attr("src",iconPath);
 					tickerItem.append(tickerItemImage);
 					}
 				
 				var tickerItemLink = $("<a>").html(title);
 				
 				if (link != "") {
 					tickerItemLink.attr("href",link);
 					}
 				else {
 					tickerItemLink.attr("class","inactive");
 					}
 				
 				tickerItem.append(tickerItemLink);
 				
 				if (mobile == 0){ 
 					yPos = (tickerHeight-tickerItem.height())*.5;
 					}
 				
				tickerItem.css({ top: yPos, left: tickerWidth });
				
				if (tickerItem.outerHeight() > tickerItemHeight) {
					tickerItemHeight = tickerItem.outerHeight();
					}
				});
			
			if (mobile != 0){
				ticker.height(tickerHeight+tickerItemHeight);
				}
			
			setTickerNav();
			advanceTicker();
			
			function advanceTicker() {
				tickerCount.html(tickerIndex+1+" of "+totalItems);
				
				$(".tickerItem").each(function(i) {
					$(this).animate({ opacity: 0 }, 250, function() {
						$(this).css({ left: tickerWidth });
						
						if (i == tickerIndex) {
							$(this).css({ opacity: 1 }).animate({ left: xPos }, 350, "easeOutSine", function() {
								if(jQuery.browser.msie) this.style.removeAttribute('filter');
								});
						}
					});
				});
			}
			
			function setTickerNav() {
				if (totalItems > 1) {
					buttonLeft.click(function() {
						tickerIndex--;
						
						if (tickerIndex < 0) {
							tickerIndex = totalItems-1;
							}
						
						advanceTicker();
						});
						
					buttonRight.click(function() {
						tickerIndex++;
						
						if (tickerIndex >= totalItems) {
							tickerIndex = 0;
							}
						
						advanceTicker();
						});
					}
				else {
					buttonLeft.addClass("inactive");
					buttonRight.addClass("inactive");
					}
				}
			}
		});
	});	
	
