$(document).ready(function(){
	$.ajax({
		type: "GET",
		url: "/xml/ppv.xml",
		data: {
		 key: "value"
		},
		dataType: "xml",
		success: function(xml) {
			$(xml).find("event").each(function() {
			var name = $(this).attr("name");
			var id = $(this).attr("id");
			var dayweek = $(this).attr("dayweek");
			var month = $(this).attr("month");
			var day = $(this).attr("day");
			var time = $(this).attr("time");
			var price = $(this).attr("price");
			var hd = $(this).attr("hd");
			var live = $(this).attr("live");
			var replay = $(this).attr("replay");
			var type = $(this).attr("type");
			var order = $(this).attr("order");
			var description = $(this).find("description").text();
			
			if (document.location.href.indexOf("hdtv/ppv-in-hd.jsp") != -1 && hd === "0") return true;
			replays = new Array();
			(live === "1") ? liveInfo = " (live!) " : liveInfo = "";
			ppvInfo = name + " -- " + dayweek + ", " + month + " " + day + ", " + time + liveInfo + " -- " + price;

			if (order === "last") {		
				$("<div/>", {
					"id": id
				}).css({"padding":"7px 0 7px 0"}).appendTo("#ppvListing");
			} else {
				$("<div/>", {
					"id": id
				}).css({"border-bottom":"1px solid #ccc","padding":"7px 0 7px 0"}).appendTo("#ppvListing");
			}


			$("<div/>", {
				"id": id + "_box"
			}).css({"width":"556px","overflow":"hidden","position":"relative"}).appendTo("#" + id);

			if (id === "blank") {
				$("<div/>", {
				"class": "contentImage",
				"id": id + "_image"
				}).append("<img src = '/images/blank.gif' width='200' height='100' border='0' alt='" + name + "' title='" + name + "'/>").appendTo("#" + id + "_box");
			} else {			
				$("<div/>", {
					"class": "contentImage",
					"id": id + "_image"
				}).append("<img src = '/images/io/ppv/" + id + ".jpg' width='200' height='100' border='0' alt='" + name + "' title='" + name + "'/>").appendTo("#" + id + "_box");
			}

			$("<div/>", {
				"class": "contentBlurb",
				"id": id + "_blurb"
			}).css({"overflow":"hidden"}).appendTo("#" + id + "_box");

			$("<h2/>").append(ppvInfo).appendTo("#" + id + "_blurb");
			$("<p/>").append(description).appendTo("#" + id + "_blurb");
			$("<div/>", {
				"class": "ppvPhone"
			}).append("To order the event, use your remote 15 minutes before showtime. Learn more about <a href='http://optimum.custhelp.com/app/answers/detail/a_id/340' onclick='fullWin(this);return false'>ordering Pay Per View events</a>.").appendTo("#" + id + "_blurb");

			if (replay === "1") {
				$("<h3/>", {
					"class": "dark"
				}).append("Replay Schedule").appendTo("#" + id);

				$("<div/>", {
					"class": "clearfix",
					"id": id + "_replays"
				}).appendTo("#" + id);

				$("<div/>", {
					"id": id + "_replayCol1"
				}).css({'float': 'left', 'width': '175px','margin-right':'15px'}).appendTo("#" + id + "_replays");

				$("<div/>", {
					"id": id + "_replayCol2"
				}).css({'float': 'left', 'width': '175px','margin-right':'15px'}).appendTo("#" + id + "_replays");

				$("<div/>", {
					"id": id + "_replayCol3"
				}).css({'float': 'left', 'width': '175px'}).appendTo("#" + id + "_replays");
				
				$(this).find("replay").each(function() {
					replayInfo = $(this).attr("date") + " -- " + $(this).attr("time") + " -- Ch. " + $(this).attr("channel");
					replays.push(replayInfo);
				});
				
				var replayColLength = replays.length/3;
				for (r=0;r<=replays.length;r++) {
					if (r < replayColLength) {
						$("<div/>").append(replays[r]).appendTo("#" + id + "_replayCol1");
					} else if (r < (replayColLength * 2)) {
						$("<div/>").append(replays[r]).appendTo("#" + id + "_replayCol2");
					} else {
						$("<div/>").append(replays[r]).appendTo("#" + id + "_replayCol3");
					}
				}
				
			}

			});
		},
		failure: function() {alert("There was a problem retrieving the XML data.")}
	 });
});

