if (typeof(cp) == "undefined") {var cp = {};}
if (typeof(cp.cmpn) == "undefined") {cp.cmpn = {};}

cp.cmpn.Content =
{
	ERR_MSG: "We couldn't find that page.",
	
	load: function(sectionName, pageId) {
		INDEX_PAGE = false;
		SECTION_NAME = sectionName;
		PAGE_ID = pageId;
		//alert("SECTION_NAME [" + SECTION_NAME + "], PAGE_ID [" + PAGE_ID + "]");
		cp.service.Ajax.bringLight(pageId, this.ERR_MSG, this.handleLight, this.handleDarkness);
	},

	loadPath: function(sectionName, path) {
		INDEX_PAGE = false;
		SECTION_NAME = sectionName;
		PAGE_ID = null;
		cp.service.Ajax.bringLightFromPath(path, this.ERR_MSG, this.handleLight, this.handleDarkness);
	},
	
	handleLight: function(content) {
		// reset scrollbar to the top
		$("#content").scrollTop(0);

		// load content from server into div
		$("#content").html(content);
		
		// fix for IE9's flash player bug
		if (cp.util.Util.IS_IE) {
			var coll = document.getElementsByTagName("object");
			for (var i = 0; i < coll.length; ++i) {
				coll[i].outerHTML = coll[i].outerHTML;
			}
		
			/*
			<meta http-equiv="X-UA-Compatible" content="IE=8"/>
			var firstIframe = $("iframe:first");
			// if we have an embedded flash player on the page
			if (firstIframe) {
				firstIframe.appendTo("form:eq(0)");
			}
			*/
		}
		
		var title		= $(".asset-meta-data .title").text();
		var pageTitle	= "Clickable Poems: " + (title ? title : "Literature in the Digital Age");
		
		//  move meta data from file to above the scrolling div
		$("#content-info").html($(".asset-meta-data").html());
		
		// now that content has loaded and meta data moved,
		// enable links in the breadcrumbs as well as any that might be in the content
		cp.util.Util.enableLinks("content-info");
		cp.util.Util.enableLinks("content");
		
		// build send-to-friend and bookmark links if we have the info
		//alert("#handleLight, PAGE_ID [" + PAGE_ID + "], SECTION_NAME [" + SECTION_NAME + "]");
		if (PAGE_ID && SECTION_NAME != "home") {
			var url				= "http://www.clickablepoems.com/?p=" + PAGE_ID;
			var bookmarkTitle	= "Clickable Poems ~ " + (title ? title : PAGE_ID);
			
			$("#content-links").html('<a href="mailto:?subject=' + bookmarkTitle + '&body=Check out this page from ClickablePoems.com: ' + url + '">Send to a Friend</a> | <a id="bookmark" href="#">Bookmark</a>');
	
			cp.util.Util.enableBookmark($("#bookmark"), url, bookmarkTitle);
		}
		
		else {
			$("#content-links").html("");
		}
		
		document.title = pageTitle;
		
		//alert("SECTION_NAME = [" + SECTION_NAME + "]");
		if (SECTION_NAME != null) {
			cp.util.Util.removeHighlight();
			$("#nav-" + (PAGE_ID == 1 ? "submissions" : SECTION_NAME)).addClass("highlight");
			
			// IE6 users don't get section-specific bg images
			if (!cp.util.Util.IS_IE6) {
				var imgName = SECTION_NAME;
				// home only one bg image
				if (SECTION_NAME != "home" && INDEX_PAGE) {
					imgName += "-index";
				}
				
				//alert("SECTION_NAME [" + SECTION_NAME + "], INDEX_PAGE [" + INDEX_PAGE + "], bg img [" + imgName + "]");
				
				$("#content-outer-wrapper").css("background-image", "url('/img/bg-" + imgName + ".png')");
			}
		}
	},
	
	handleDarkness: function(xhr, ajaxOptions, thrownError) {
		//alert(xhr.status + "\n\n" + thrownError);
		$("#content").html(ERR_MSG);
	}
}
