﻿window.innerBehavior = new function() {
	eval(estrada.namespace);
	tilt.attachEvent(document, "layout", function() {
		document.body.className = "l-inner";
		
		var layout = window.commonLayout.layout;

		/////////////////////////////////////////////////////////
		// change cascading property classes to styling classes
		/////////////////////////////////////////////////////////
		$(".secondary-feed-c")
			.removeClass("secondary-feed-c")
			.addClass("secondary-feed");
		$(".center-feed-c")
			.removeClass("center-feed-c")
			.addClass("center-feed");
		$(".left-feed-c")
			.removeClass("left-feed-c")
			.addClass("left-feed");
		$(".ad-nav-c")
			.removeClass("ad-nav-c")
			.addClass("ad-nav");
		
		////////////////////////////////////////////
		// put the content into the layout	
		////////////////////////////////////////////
		layout.push(grab(document.getElementById("context"), {
			"nameplate": "top"
		}));
		layout.push({
			"top": "search"
		});
		layout.push(grab(document.getElementById("context"), {
			"home": "top"
		}));		
		layout.push(grab(document.getElementById("context"), {
			"top-nav": "top"
		}));
		layout.push(grab(document.getElementById("context"), {
			"top-image": "heading"
		}));
		layout.push(grab(document.getElementById("context"), {
			"header": "heading"
		}));	
		layout.push(grab(document.getElementById("navigation"), {
			"standard-nav": "left"
		}));
		layout.push(grab(document.getElementById("feeds"), {
			"left-nav-feed": "left",
			"left-nav-feed-sd": "left",
			"left-nav-feed-d": "left"
		}));
		layout.push(grab(document.getElementById("context"), {
			"ad-nav": "left"
		}));
		layout.push(grab(document.getElementById("context"), {
			"ad-nav-c": "context"
		}));
		layout.push(grab(document.getElementById("feeds"), {
			"left-feed": "left"
		}));
		layout.push(grab(document.getElementById("context"), {
			"left-feed": "left"
		}));
		layout.push(grab(document.getElementById("legacy-feeds"), {
			"left-feed": "left"
		}));
		layout.push(grab(document.getElementById("feeds"), {
			"quote": "left"
		}));
		layout.push(grab(document.getElementById("feeds"), {
			"profile": "right",
			"profile-c": "right"
		}));
		layout.push(grab(document.getElementById("legacy-feeds"), {
			"feed": "right"
		}));
		layout.push(grab(document.getElementById("content"), {
			"center-content1": "center"
		}));
		layout.push(grab(document.getElementById("content"), {
			"extended-page": "center"
		}));
		layout.push(grab(document.getElementById("content"), {
			"center-content2": "center"
		}));
		layout.push({
			"center": "extensions"
		});
		layout.push(grab(document.getElementById("feeds"), {
			"content": "center"
		}));
		layout.push(grab(document.getElementById("feeds"), {
			"rotating-stories": "center"
		}));
		layout.push(grab(document.getElementById("feeds"), {
			"center-feed": "center"
		}));
		layout.push(grab(document.getElementById("feeds"), {
			"cfeed": "center"
		}));
		layout.push(grab(document.getElementById("content"), {
			"center-content3": "center",
			"center-content4": "center"
		}));
		layout.push(grab(document.getElementById("content"), {
			"secondary-content1": "right",
			"secondary-content2": "right"
		}));
		layout.push(grab(document.getElementById("feeds"), {
			"secondary-feed": "right",
			"right-feed": "right",
			"feeds": "right"
		}));
		layout.push(grab(document.getElementById("content"), {
			"secondary-content3": "right",
			"secondary-content4": "right"
		}));
		layout.push(grab(document.getElementById("context"), {
			logo: "bottom"
		}));
		layout.push(grab(document.getElementById("context"), { 
			address: "bottom", 
			copyright: "bottom"
		}));
		layout.push(grab(document.getElementById("feeds"), {
			"menu": "center"
		}));
		layout.push(grab(document.getElementById("context"), {
			"menu": "center"
		}));
		
		// modify markup for presentation
		$("#l-left .ad-nav ul li a").wrap("<div class=\"l-link-wrapper\"></div>");
		$("#l-left .related-nav ul li a").wrap("<div class=\"l-link-wrapper\"></div>");
		$("#l-left .feed ul li a").wrap("<div class=\"l-link-wrapper\"></div>");
		$("#l-left .left-feed a").wrap("<div class=\"l-link-wrapper\"></div>");
		$("#l-left .left-nav-feed a").wrap("<div class=\"l-link-wrapper\"></div>");
		$("#l-left .left-nav-feed-sd a").wrap("<div class=\"l-link-wrapper\"></div>");
		$("#l-left .left-nav-feed-d a").wrap("<div class=\"l-link-wrapper\"></div>");
		
		$("#l-center .content-query>ul>li>div").addClass("l-query-holder");
		
		// bind behaviors
		layout.bind("gnav").to("left");
		layout.bind("date-selector").to("center");
		layout.bind("category-tree").to("center");
		layout.bind("grid-visibility").to("center");
		layout.bind("flash-replacement").to(document);
		layout.bind("pretty-search").to(document);
		layout.bind("profile-behavior").to(document);
		layout.bind("content-copy-placement").to(document);
		layout.bind("format-feed-queries").to("right");
		layout.bind("pretty-dates").to("right");
		layout.bind("format-feed-queries").to("left");
		layout.bind("pretty-dates").to("left");
		layout.bind("format-feed-queries").to("center");
		layout.bind("pretty-center-dates").to("center");	
		layout.bind("mark-content-title").to("center");
		layout.bind("first-p").to("center");
		layout.bind("last-li").to("top");
		layout.bind("last-li").to("right");
		layout.bind("last-li").to("left");
		layout.bind("first-li").to("left");
		layout.bind("first-li").to("center");
		layout.bind("first-h3").to("center");
		layout.bind("menu").to("center");
		layout.bind("quote").to(document);
		layout.bind("tweak-feeds").to("left");
		layout.bind("tweak-feeds").to("right");
		layout.bind("place-menu").to("center");
		layout.bind("cancel-button").to(document);
		layout.bind("descendant-headers").to(document);

		// add wrapping divs to feeds for presentation
		$("#l-left .left-feed .content-query")
			.wrap("<div class=\"l-query-wrapper\"></div>");
		$("#l-left .left-feed .l-query-wrapper")
			.prepend("<div class=\"l-feed-wrap-top\">")
			.append("<div class=\"l-feed-wrap-bottom\">");
		$("#l-left .left-nav-feed .content-query")
			.wrap("<div class=\"l-query-wrapper\"></div>");
		$("#l-left .left-nav-feed .l-query-wrapper")
			.prepend("<div class=\"l-feed-wrap-top\">")
			.append("<div class=\"l-feed-wrap-bottom\">");
		$("#l-left .left-nav-feed-sd .content-query")
			.wrap("<div class=\"l-query-wrapper\"></div>");
		$("#l-left .left-nav-feed-sd .l-query-wrapper")
			.prepend("<div class=\"l-feed-wrap-top\">")
			.append("<div class=\"l-feed-wrap-bottom\">");
		$("#l-left .left-nav-feed-d .content-query")
			.wrap("<div class=\"l-query-wrapper\"></div>");
		$("#l-left .left-nav-feed-d .l-query-wrapper")
			.prepend("<div class=\"l-feed-wrap-top\">")
			.append("<div class=\"l-feed-wrap-bottom\">");
		$("#l-right .secondary-feed .content-query")
			.wrap("<div class=\"l-query-wrapper\"></div>");
		$("#l-right .secondary-feed .l-query-wrapper")
			.prepend("<div class=\"l-feed-wrap-top\">")
			.append("<div class=\"l-feed-wrap-bottom\">");
		
		// add corners
		$(".center-content1, \
			.center-content2, \
			.center-content3, \
			.center-content4, \
			#extensions, \
			.cfeed, \
			.center-feed").corner("round");
		$(".secondary-content1, \
			.secondary-content2, \
			.secondary-content3, \
			.secondary-content4").corner("round");
		$(".extended-page").each(function() {
			if ($(this).hasClass("event")) {
				$(this).corner("round");
			}
		});
		
		$.contentQuery();
	});
}
////////////////////////////////////////////////////////////////////
// Adds a class to the first text brick title in the “center”
// content area.
////////////////////////////////////////////////////////////////////
estrada.behavior("mark-content-title").is({
	bind: function(node) {
		var oPageTitle = $("h1.page");
		var oCenterContent1 = $("div.center-content1");
		if (oCenterContent1.length > 0) {
			var oFirstTitle = $("h3:first", oCenterContent1);
			if ((oFirstTitle.length > 0) && (oFirstTitle.hasClass("title"))) {
				oFirstTitle.addClass("l-content-title");
			} else { 
				if ((oFirstTitle.length > 0) && (oFirstTitle.hasClass("no-title"))) {
					oFirstTitle.remove();
				} else {
					if (oPageTitle.length > 0) {
						var oTitle = document.createElement("h3");
						oTitle.className = "title l-content-title";
						oTitle.innerHTML = oPageTitle.html();
						oCenterContent1.prepend(oTitle);
					}
				}
			}
			oPageTitle.remove();
		} else {
			if (oPageTitle.length > 0) {
				var oTitle = document.createElement("h3");
				oTitle.className = "title l-content-title";
				oTitle.innerHTML = oPageTitle.html();
				var oExtensionContent = $("div#extensions");
				if (oExtensionContent.length > 0) {
					oExtensionContent.prepend(oTitle);		
				} else {
					var oExtendedPage = $("div.extended-page");
					if (oExtendedPage.length > 0) {
						oExtendedPage.prepend(oTitle);		
					} else {
						$("#l-center").prepend(oTitle);
					}
				}
			}
			oPageTitle.remove();
		}
	}
});
////////////////////////////////////////////////////////////////////
// Add class to first p element in center-content1
////////////////////////////////////////////////////////////////////
estrada.behavior('first-p').is({
	bind: function(node) {
		firstElement(node, "p", "div", "center-content1")
	}
});
////////////////////////////////////////////////////////////////////
// Add class to first h3 in heach center-content area
////////////////////////////////////////////////////////////////////
estrada.behavior('first-h3').is({
	bind: function(node) {
		firstElement(node, "h3", "div", "center-content1");
		firstElement(node, "h3", "div", "center-content2");
		firstElement(node, "h3", "div", "center-content3");
		firstElement(node, "h3", "div", "center-content4");
		firstElement(node, "h3", "div", "center-feed");
		firstElement(node, "h3", "div", "cfeed");
	}
});
////////////////////////////////////////////////////////////////////
// Add class to first li in each ul
////////////////////////////////////////////////////////////////////
estrada.behavior('first-li').is({
	bind: function(node) {
		$("ul", node).each(function() {
			$("li:first", this).addClass("l-first");
		});
	}
});
////////////////////////////////////////////////////////////////////
// Modify markup of quote feed
////////////////////////////////////////////////////////////////////
estrada.behavior('quote').is({
	bind: function(node) {
		$(".quote .contributor").each(function() {
			$(this).after("<div class=\"contributor\">" +
				$(this).html() +
				"</div>");
			$(this).remove();
		});
	}
});
////////////////////////////////////////////////////////////////////
// mark the last list in an unordered list
////////////////////////////////////////////////////////////////////
estrada.behavior('last-li').is({
	bind: function(node) {
		foreach(map(node.getElementsByTagName("ul")), function(oUl) {
			if (oUl.hasChildNodes()) {
				var arrChildren = oUl.childNodes;
				var intLength = arrChildren.length;
				if (intLength > 0) {
					for (var i = (intLength-1); i >= 0; i--) {
						if (arrChildren[i].nodeName.toLowerCase() == 'li') {
							arrChildren[i].className += ' l-last';
							i = -1;
						}
					};
				}
			};		
		});
	}
});
////////////////////////////////////////////////////////////////////
// tweak feeds and add class to parent
////////////////////////////////////////////////////////////////////
estrada.behavior('tweak-feeds').is({
	bind: function(node) {
		$(".feed .publication").parent().addClass("pubfeed");
		$(".content-query .event").parent().addClass("calfeed");
		$(".feed .documents").parent().addClass("docfeed");
	}
});
////////////////////////////////////////////////////////////////////
// custom navigation system for Graceland University
////////////////////////////////////////////////////////////////////
function logTime(stime,msg) {
return;
	var oDate = new Date();
	console.log(msg + " " + (oDate.getTime() - stime.getTime()));
	return oDate;
}
estrada.behavior("gnav").is({
	bind: function(node) {
		// get top level page and descendant navigation
		var strSectionNavUrl = $(".section-nav a:first").attr('href');
		if (strSectionNavUrl && strSectionNavUrl.length > 0) {
			$.ajah(strSectionNavUrl, null,
				function() {
					var oNow = new Date();
					var oPageNav = $('.standard-nav:first');
					if (oPageNav) {
						var oTopSectionNav = $('.standard-nav', $(this));
						if (oTopSectionNav) {
							$('.current', oTopSectionNav).remove();
							var oCurrent = $('li.current', oPageNav);
							if (oCurrent.length > 0) {
								var oPageAncestorLis = $('> a', oCurrent).parents("li");
								if (oPageAncestorLis.length <= 0) {
									oPageAncestorLis = $('> span', oCurrent).parents("li");
								}
								var oTopPageLi = oPageAncestorLis.eq(oPageAncestorLis.length-1);
								var strCurrentId = $('> a', oCurrent).attr('className');
								if (!strCurrentId) {
									strCurrentId = $('> span', oCurrent).attr('className');
								}
								var oSectionLink = $('a.' + strCurrentId, oTopSectionNav);
								if (oSectionLink.length > 0) {
									var oAncestorLis = oSectionLink.parents("li");
									if (oAncestorLis.length > 0) {
										var oTopSectionLi = oAncestorLis.eq(oAncestorLis.length-1);
										for(idx=0;idx < oPageAncestorLis.length;++idx) {
											if (!(oAncestorLis.eq(idx).hasClass("current")
												|| oAncestorLis.eq(idx).hasClass("ancestor"))) {
												oAncestorLis.get(idx).className =
													oPageAncestorLis.get(idx).className;
											}
										}
									}
								} else {
									var oGrandparent = oCurrent.parent().parent();
									if (oGrandparent.length > 0) {
										if (oGrandparent.hasClass("standard-nav")) {
											var oTopUl = $("> ul", oTopSectionNav);
											oTopUl.append(oCurrent);
										} else {
											var strGrandLinkId = $('> a', oGrandparent).attr('className');
											if (strGrandLinkId) {
												var oSectionLink = $('a.' + strGrandLinkId, oTopSectionNav);
												if (oSectionLink.length > 0) {
													oPageAncestorLis = $('> a', oGrandparent).parents("li");		
													var oAncestorLis = oSectionLink.parents("li");
													if (oAncestorLis.length > 0) {
														var oTopSectionLi = oAncestorLis.eq(oAncestorLis.length-1);
														for(idx=0;idx < oPageAncestorLis.length;++idx) {
															if (!(oAncestorLis.eq(idx).hasClass("current")
																|| oAncestorLis.eq(idx).hasClass("ancestor"))) {
																oAncestorLis.get(idx).className =
																	oPageAncestorLis.get(idx).className;
															}
														}
													}												
													oSectionLink.parent().replaceWith(oGrandparent);
												} else {
													$("> ul", oTopSectionNav).append(oCurrent.parent().parent());
												}
											} else {
												$("> ul", oTopSectionNav).append(oCurrent.parent().parent());
											}
										}
									} else {
										$("> ul", oTopSectionNav).append(oCurrent.parent().parent());
									}
								}
							}
							$(oPageNav).replaceWith(oTopSectionNav);
						}
					}
					// modify navigation markup for presentation
					$(".standard-nav > ul > li > a, .standard-nav > ul > li > span")
						.wrap("<div class=\"l-link-wrapper l-closed\"></div>")
						.before("<div class=\"l-nav-ctrl\"></div>");
					$(".standard-nav > ul > li.ancestor li a, .standard-nav > ul > li.ancestor li span")
						.wrap("<div class=\"l-link-wrapper l-closed\"></div>")
						.before("<div class=\"l-nav-ctrl\"></div>");
					// hide all sub-nav levels
					$(".standard-nav > ul > li ul").hide();
					// now open and show path to current
					var oStandardNav = $(".standard-nav");
					var oCurrent = $(".standard-nav .current");
					if (oCurrent.length <= 0) {
						$(".standard-nav > ul").show();
					} else {
						oCurrent.parents().each(function() {
							$("> div.l-link-wrapper",this)
								.addClass("l-opened")
								.removeClass("l-closed");
							$(this).show();
						});
						var oChildUl = $("> ul", oCurrent);
						if (oChildUl.length > 0) {
							openNavSection($(".l-nav-ctrl:first", oCurrent));
						}
					}
					// mark li elements that have no sub navigation
					$(".standard-nav > ul li").each(function() {
						if ($(this).children("ul").length <= 0) {
							$(this).addClass("l-empty");
							$(".l-closed",this).removeClass("l-closed");
						}
					});
					var oParent;
					$(".standard-nav li.ancestor > div.l-link-wrapper").each(function() {
						oParent = $(this).parent();
						if (!$(oParent).hasClass("l-empty")) {
							$(this).addClass("l-opened").removeClass("l-closed");
						}
					});
					//$(".standard-nav > ul > li > div.l-link-wrapper").corner("round tl bl");
					// add click handler
					$(".standard-nav .l-nav-ctrl").click(clickHandler);
					function openNavSection(oNavCtrl) {
						var oGrandparent = oNavCtrl.parent().parent();
						if (oGrandparent) {
							if (!($(oGrandparent).hasClass("l-prepared"))) {
								$("ul > li > a, ul > li > span", oGrandparent)
									.wrap("<div class=\"l-link-wrapper l-closed\"></div>")
									.before("<div class=\"l-nav-ctrl\"></div>");
								$(oGrandparent).addClass("l-prepared");
								
								$(".l-nav-ctrl", oGrandparent).click(clickHandler);
							}
							if ($(oGrandparent).children("ul").length > 0) {
								if (oNavCtrl.parent().hasClass("l-opened")) {
									oNavCtrl.parent().removeClass("l-opened");
									oNavCtrl.parent().addClass("l-closed");
								} else {
									oNavCtrl.parent().removeClass("l-closed");
									oNavCtrl.parent().addClass("l-opened");
								}
								$(oGrandparent).children("ul:first").toggle();
							}
						}
					}
					function clickHandler(eventObject) {
						openNavSection($(this));
					}
				},
				function() {
					//console.info('error');
				}
			);
		}
	}
});
////////////////////////////////////////////////////////////////////
// turns a list of links into a drop down menu
////////////////////////////////////////////////////////////////////
estrada.behavior("menu").is({
	bind: function(node) {
		var oMenu = first(getElements(node,"div", "menu"));
		if (oMenu) {
			var oLinks = first(getElements(oMenu,"div","links"));
			var strTitle = "";
			if (oLinks) {
				var oH3 = first(oMenu.getElementsByTagName("h3"));
				if (oH3) {
					var oA = first(oH3.getElementsByTagName("a"));
					if (oA) {
						strTitle = oA.innerHTML;
					}
				}
			} else {
				oLinks = oMenu;
			}
			var html = ""
			var currentUrl;
			estrada.psite = {};
			estrada.psite.go = function(url) {
				if (url) {
					window.location = url;
				}
			}
			foreach(oLinks.getElementsByTagName("a"), function(a, i) {
				var isCurrent = a.className == "current";
				if (isCurrent) {
					currentUrl = a.href;
				}
				html += "<option value=\"" + a.href + "\"" + (isCurrent ? " selected" : "") + ">" + a.innerHTML + "</option>";
			})
			oMenu.className = oMenu.className.replace("feed","");
			oMenu.innerHTML = "<h3>" + strTitle + "</h3><select onchange=\"estrada.psite.go(this.value)\">" + (currentUrl ? "" : "<option value=\"\">SELECT ONE</option>") + html + "</select";
			if (oMenu.parentNode.firstChild) {
				oMenu.parentNode.insertBefore(oMenu,oMenu.parentNode.firstChild);
			}
		}
	}
});
////////////////////////////////////////////////////////////////////
// Move the page menu under .l-content-title in #content
////////////////////////////////////////////////////////////////////
estrada.behavior("place-menu").is({
	bind: function(node) {
		var oMenu = first(getElements(node, "div", "menu"));
		if (oMenu) {
			var oTitle = first(getElements(node, "h3", "l-content-title"));
			if (oTitle) {
				oTitle.parentNode.insertBefore(oMenu, oTitle);
				oMenu.parentNode.insertBefore(oTitle, oMenu);
			}
		}
	}
});
///////////////////////////////////////////////////////////////////////
// replace images with flash by convention
///////////////////////////////////////////////////////////////////////
estrada.behavior("flash-replacement").is({
	bind: function(node) {
		profileFlashManager.flashReplacement();
	}
});
window.profileFlashManager = new function() {

	this.flashReplacement = function() {
		foreach(map(document.getElementsByTagName("img")), function(img) {
			var oSrc = img.src;
			if (oSrc && oSrc.indexOf(".swf.") > 0) {
				replaceWithFlash(img);
			}
		});	
	};

	function replaceWithFlash(img) {
		var width = img.currentStyle ? img.currentStyle.width.replace(/px/, "") : img.width;			
		var height = img.currentStyle ? img.currentStyle.height.replace(/px/, "") : img.height;
		var src = img.src;
		if (width && height && src) {
			var movie = src.replace(/\.swf\..+/, ".swf");
			var version = 4;
			var useNetscapePlugins = navigator.plugins && navigator.mimeTypes.length;
			if (detectFlash())
			{
				var child = img;
				var oParent = img.parentNode;
				if (oParent.nodeName.toUpperCase() == "A") {
					child = oParent;
					oParent = oParent.parentNode;
				}
				var classname = child.className;
				var flashDiv = document.createElement("div");
				flashDiv.className = classname;
				flashDiv.innerHTML = createMovieHtml();
				oParent.replaceChild(flashDiv, child);
			}
			
		}

		function createMovieHtml()
		{
			var sizeAttributes = "width=\"" + width + "\" height=\"" + height + "\"";
			return useNetscapePlugins ?
				"<embed wmode=\"transparent\" type=\"application/x-shockwave-flash\" src=\"" + movie + "\" " + sizeAttributes + "></embed>"
				:
				"<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" " + sizeAttributes + "><param name=\"wmode\" value=\"transparent\"><param name=\"movie\" value=\"" + movie + "\" /></object>";
		};
		
		function detectFlash()
		{
			return getFlashVersion() >= version;

			function getFlashVersion()
			{
				if (useNetscapePlugins)
				{
					var plugin = navigator.plugins["Shockwave Flash"];
					if(plugin && plugin.description)
					{
						var description = plugin.description;
						var idx = description.indexOf('.')-2;
   						return description.substring(idx, idx+2);
					}
				} 
				else
				{
					var version = 0;
					for(var i = 4; i >= 3; i--)
					{
						var testObject;
						try
						{
							testObject = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." +  i);
						}
						catch(e)
						{
							continue;
						}
						return i;
					}
				}
				return 0;
			};
		};
	};
};
///////////////////////////////////////////////////////////////////////
// add visibility code if grid is present
///////////////////////////////////////////////////////////////////////
estrada.behavior('grid-visibility').is({
	bind: function(node) {
		var oGridVisibility = $(".grid .visibility");
		if (oGridVisibility.length > 0) {
			oGridVisibility.attr("id","grid-visibility");
			tilt.addScript('grid.js');
		}
	}
});
///////////////////////////////////////////////////////////////////////
// replace the cancel link with a button element and click event handler
///////////////////////////////////////////////////////////////////////
estrada.behavior('cancel-button').is({
	bind: function(node) {
		$("ul.commands li a").each(function () {
			var strLocation = $(this).attr("href");
			$(this).before("<button class=\"btn-cancel\">Cancel</button>");
			$(".btn-cancel").click(function(event) {
				window.location = strLocation;
				return false;
			});
			$(this).remove();
		});
	}
});
////////////////////////////////////////////////////////////////////
// format the dates for a calendar feed to be “Month Day, Year”
// an example would be "August 30, 2007"
////////////////////////////////////////////////////////////////////
estrada.behavior("pretty-dates").is({
	bind: function(node) {
		formatDates(node, "dtstart");
	}
});
////////////////////////////////////////////////////////////////////
// format the dates for a calendar feed to be “Day/Month/Year”
// an example would be "08/30/2007"
////////////////////////////////////////////////////////////////////
estrada.behavior("pretty-center-dates").is({
	bind: function(node) {
		formatDates(node, "date");
	}
});
////////////////////////////////////////////////////////////////////
// place reused content into the center-content1 property
////////////////////////////////////////////////////////////////////
estrada.behavior("content-copy-placement").is({
	bind: function(node) {
		var strPid, oPlaceholder, strPlaceholderPid;
		var arrPlaceholders = $(".feed-placeholder");
		if (arrPlaceholders.length > 0) {
			$(".content-copy .content-copy-item > div").each(function() {
				oContent = $(this);
				strPid = oContent.attr("class");
				if (strPid && strPid.length > 0) {
					arrPlaceholders = $(".feed-placeholder");
					$(arrPlaceholders).each(function() {
						strPlaceholderPid = trim($(this).html());
						if (oContent.hasClass(strPlaceholderPid)) {
							$(this).before(oContent.html());
							$(this).remove();
						}
					});
				}
			});
		}
	}
});
////////////////////////////////////////////////////////////////////
// format feed queries
////////////////////////////////////////////////////////////////////
estrada.behavior("format-feed-queries").is({
	bind : function(node) {
		$("#l-right .content-query,#l-left .content-query,#l-center .content-query").each(function(intIdx, oQuery) {
			var oEvents = $(".event", this);
			if (oEvents.length > 0) {
				var oNewEventList, oAbbr, oA, oNewVevent;
				var oDtend, oStart, oEnd, oNow, strDate;
				oNewEventList = document.createElement("ul");
				oNewEventList.className = "events";
				this.appendChild(oNewEventList);
				oEvents.each(function() {
					oAbbr = first(getElements(this, "abbr", "dtstart"));
					if (oAbbr) {
						oDtend = first(getElements(this, "abbr", "dtend"));
						if (oDtend == null) {
							oDtend = oAbbr;
						}
						oStart = $.dates.iso8601date($.dates.parse(oAbbr.title));
						oEnd = $.dates.iso8601date($.dates.parse(oDtend.title));
						oNow = $.dates.iso8601date($.dates.parse("now"));
						if (oStart >= oNow) {
							strDate = $.dates.format($.dates.parse(oAbbr.title), "MMM d, yyyy");;
						} else if (oEnd > oStart) {
							strDate = $.dates.format($.dates.parse("now"), "MMM d, yyyy");;
						} else {
							strDate = $.dates.format($.dates.parse(oAbbr.title), "MMM d, yyyy");;
						}
						oA = first(getElements(this, "a", "title"));
						oNewVevent = oNewEventList.appendChild(document.createElement("li"));
						oNewVevent.className = "vevent";
						oNewVevent.innerHTML =
							"<span class=\"dtstart\">"
							+ strDate
							+ "</span>&nbsp;<a class=\"url\" href=\""
							+ oA.href + "\">"
							+ oA.innerHTML;
						oNewVevent.appendChild(oA);
						oAbbr.parentNode.removeChild(oAbbr);
						oA.parentNode.removeChild(oA);
					}
				});
				$("ul:first",this).remove();
			}
		});
	}
});
///////////////////////////////////////////////////////////////////////
// apply presentation and behavior to profiles
///////////////////////////////////////////////////////////////////////
estrada.behavior("profile-behavior").is({
	bind: function(node) {
		var arrProfiles, intNumProfiles, intIdx, arrLink, arrUserView;
		arrProfiles = $(".inner-profile").hide();
		if (intNumProfiles = arrProfiles.length) {
			$(arrProfiles).each(function() {
				arrLink = $("a.title", this);
				arrUserView = $(".user-view:first", this);
				if (arrUserView && arrUserView.length > 0) {
					arrLink.attr("href", arrUserView.text());
					arrUserView.remove();
				}
				$("a.title",this).wrap("<div class=\"l-profile-link\"></div>");
				$("a.title",this).wrap("<div class=\"l-profile-link-wrapper\"></div>");
				$(".l-profile-link-wrapper",this)
					.prepend("<div class=\"l-prev-profile\"></div>")
					.append("<div class=\"l-next-profile\"></div>");
				$(".l-profile-link-wrapper a.title", this)
						.append("<div class=\"l-more\">READ MORE</span>");
			});
			profileInfo.intCurrentProfileNumber = showProfile();
			$(".l-next-profile").each(function() {
				tilt.attachEvent(this, "click", profileInfo.nextProfile);
			});
			$(".l-prev-profile").each(function() {
				tilt.attachEvent(this, "click", profileInfo.prevProfile);
			});
			profileInfo.arrProfiles = arrProfiles;
			profileInfo.intNumberOfProfiles = intNumProfiles;
		} else {
			arrProfiles = $(".small-profile").hide();
			if (intNumProfiles = arrProfiles.length) {
				showProfile();
			}
		}
		
		function showProfile() {
			var intIdx = Math.floor(intNumProfiles * Math.random());
			$(arrProfiles[intIdx]).show();
			return intIdx;
		}
	}
});
///////////////////////////////////////////////////////////////////////
// hide .descendant property if section image is present
///////////////////////////////////////////////////////////////////////
estrada.behavior("descendant-headers").is({
	bind: function(node) {
		var arrTopImages = $(".top-image");
		if (arrTopImages.length > 1) {
			$(arrTopImages).each (function() {
				if ($(this).hasClass("descendants")) {
					$(this).hide();
				};
			});
		}
		var arrHeaders = $(".header");
		if (arrHeaders.length > 1) {
			$(arrHeaders).each (function() {
				if ($(this).hasClass("descendants")) {
					$(this).hide();
				};
			});
		}
	}
});
function formatDates(node, strClass) {
	if (node && strClass && strClass.length > 0) {
		foreach(map(getElements(node,"abbr",strClass)), function(oAbbr) {
			if (oAbbr.className && oAbbr.className.indexOf(strClass) >= 0) {
				var oDateObj = $.dates.parse(oAbbr.title);
				if (oDateObj) {
					var strPrettyDate = $.dates.format(oDateObj, "M/dd/yyyy");
					if (oAbbr.innerHTML) {
						oAbbr.innerHTML = strPrettyDate;
					} else {
						var oParent = oAbbr.parentNode;
						if (oParent.childNodes && oParent.childNodes.length > 1 && oParent.childNodes[1].nodeType == 3) {
							oParent.childNodes[1].nodeValue = "";
						}
		                var div = oParent.insertBefore(document.createElement("div"), oAbbr);
		                oParent.removeChild(oAbbr);
		                div.className = oAbbr.className;
		                div.innerHTML = strPrettyDate;
					}
				}
			}
		});
	}
}
function trim(s) {
	return s.replace(/^\s+|\s+$/g, '');
};
function firstElement(oNode, oTagName, oParentElement, oClass) {
	var oC1 = first(getElements(oNode,oParentElement,oClass));
	if (oC1) {
		var oP = first(oC1.getElementsByTagName(oTagName));
		if (oP) {
			oP.className += ' l-first';
		}
	}
}
///////////////////////////////////////////////////////////////////////////////
// The following are custom renderers for specific content query keys
///////////////////////////////////////////////////////////////////////////////
var global_renderer_scene = {
	DATA_KEY: 'renderer_scene_data',
	
	renderer: {	
		noFormRenderer: function(config) {
			$('#scene div.label').each(function() {
				$(this).html($(this).html()+":");
			});
			$('#scene .static-image').each(function() {
				var oSrc = $(this).html();
				$(this).html("<img src=\""+
					oSrc+
					"\" alt=\"static scene image\"></img>");
			});
		}
	}
};
$.contentQuery('scene', global_renderer_scene.renderer);

var global_renderer_profile = {
	DATA_KEY: 'renderer_profile_data',
	
	renderer: {
		noFormRenderer: function(config) {
			$('#profile div.label').each(function() {
				$(this).html($(this).html()+":");
			});
			$('#profile img').each(function() {
				$(this).attr("style","padding-bottom: 10px");
			});
		}
	}
};
$.contentQuery('profile', global_renderer_profile.renderer);