var pagedata = {};

function cachePage(pageID, parentID, pageTitle, pageBody, imageSource) {
	if (pagedata[pageID] == undefined) {
		var ele = {
			"id":pageID,
			"parentID":parentID,
			"title":pageTitle,
			"body":pageBody,
			"image":imageSource
		};
		pagedata[pageID] = ele;
	}
}



function displayPage(pageID) {
	var html = "<div id='previewBody' class='body'>";
	if (pagedata[pageID] == undefined) {
		html += "<h2 class='previewTitle'>Error 404 - Page Not Found</h2><p>I'm sorry Dave, I can't do that.<br />Actually, we couldn't find the page you're looking for. But we did find you a link.</p>";
	} else {
		var bc = "";
		bc += "<a href='/DocController?DocumentId=" + pageID + "'>" + pagedata[pageID].title + "</a>";
		var traversal = pagedata[pageID];
		while (traversal.parentID > 0) {
			traversal = pagedata[traversal.parentID];
			if (traversal != undefined) {
				bc = "<a href='/DocController?DocumentId=" + traversal.id + "'>" + traversal.title + "</a>" + " | " + bc;
			}
		}
		bc = "<a href='http://www.mitel.com'>Home</a>" + " | " + bc;
		html += "<p class='breadcrumbs'>" + bc + "</p>";
		html += "<h2 class='previewTitle'>" + pagedata[pageID].title+ "</h2>";
		if (pagedata[pageID].image.length > 0) {
			html += "<img class='previewImage' src='/images/" + pagedata[pageID].image + "' />";
		}
		html += pagedata[pageID].body;
	}
	html += "</div>";
	html += "<div class='continueLink'><a href='/DocController?documentId=" + pageID + "'>More...</a></div>";
	$("#pmPreview").text("");
	$("#pmPreview").append(html);
}

$(document).ready(function() {
	if (!$.isHandHeld()) {
		$("#pmMain").floatmenu("pmList", "pmHighlight");
		$("#pmSpacer").css("height", $("#pmMain").height());
		
		$("li[id^='item_dynamic_']").hover(function() {
			var id = $(this).attr("id");
			var docID = id.substring(id.lastIndexOf("_") + 1);
			var data = $.ajax({
				url:'/DocXMLController?documentId=' + docID,
				type:"GET",
				dataType:"text",
				success:function(data) {
					var xml = $.xmlparse(data);
					$("#list_dynamic_" + docID).text("");
					$("PAGE[level = '3']", xml).sort(function(a, b) {
						var aSubject = $("SUBJECT:first", a).text().toLowerCase();
						var bSubject = $("SUBJECT:first", b).text().toLowerCase();
						return aSubject.localeCompare(bSubject);
					}).each(function() {
						if ($("PAGETYPE:first", this).text().toUpperCase() == 'PRODUCT') {
							$("#list_dynamic_" + docID).append('<li id="product_' + $("DOCUMENT_ID:first", this).text() + '"><div class="pmItemBox" id="preview_' + $("DOCUMENT_ID:first", this).text() + '"><a href="/DocController?documentId=' + $("DOCUMENT_ID:first", this).text() + '">' + $("SUBJECT:first", this).text() + "</a></div></li>\r\n");
							cachePage($("DOCUMENT_ID:first", this).text(), docID, $("SUBJECT:first", this).text(), $("SYNOPSYS:first", this).text(), $("IMAGE2:first", this).text());
						}
					});
					$("#list_dynamic_" + docID).floatmenu("pmList", "pmHighlight");
					$("#pmSpacer").css("height", $("#pmMain").height());
					$(".pmItemBox").hover(function() {
						var id = $(this).attr("id");
						var docID = id.substring(id.lastIndexOf("_") + 1);
						displayPage(docID);
						return true;
					}, function() {return true;});
				}
			});
		}, function() {
			
		});
		$(".pmItemBox").hover(function() {
			var id = $(this).attr("id");
			var docID = id.substring(id.lastIndexOf("_") + 1);
			displayPage(docID);
			return true;
		}, function() {
			return true;
		});
		$("#pmMain").removeClass("pmPlainList");
		$(".pmJSElement").css("display", "block");
	}
});
