window.commonBehavior = new function() {
	eval(estrada.namespace);
	tilt.attachEvent(document, "layout", function() {
		var layout = window.commonLayout.layout;

		// bind behaviors
		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("content-copy-placement").to(document);
		layout.bind("first-p").to("middle");
		layout.bind("last-li").to("left");
		layout.bind("last-li").to("middle");
		layout.bind("first-li").to("left");
		layout.bind("first-li").to("middle");
		layout.bind("cancel-button").to(document);
		layout.bind("scripture").to("center");

		// add corners
		$("#l-header").corner("round top");
		$("#l-left .ad #sharethis_0").corner("round");
		$("#l-footer-holder").corner("round bottom");

		
		$.contentQuery();
	});
}
////////////////////////////////////////////////////////////////////
// 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 li in each ul
////////////////////////////////////////////////////////////////////
estrada.behavior('first-li').is({
	bind: function(node) {
		$("ul", node).each(function() {
			$("li:first", this).addClass("l-first");
		});
	}
});
////////////////////////////////////////////////////////////////////
// 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;
						}
					};
				}
			};		
		});
	}
});
///////////////////////////////////////////////////////////////////////
// replace images with flash by convention
///////////////////////////////////////////////////////////////////////
estrada.behavior("flash-replacement").is({
	bind: function(node) {
		flashManager.flashReplacement();
	}
});
///////////////////////////////////////////////////////////////////////
// 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();
		});
	}
});
////////////////////////////////////////////////////////////////////
// display a div containg scripture text
////////////////////////////////////////////////////////////////////
estrada.behavior('scripture').is({
	bind: function(node) {
		$(".scripture", node).each(function() {
			$(this).wrap(
				"<a href=\"http://www.biblegateway.com/passage/?search="
				+ $(this).html()
				+ "&amp;version=9;\" target=\"blank\">")
		});
	}
});
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';
		}
	}
}
