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

cp.util.Util =
{
	IS_IE: $.browser.msie,
	IS_IE6: $.browser.msie && $.browser.version < 7,
	
	removeHighlight: function() {
		$("#top-nav").find("a").each(function() {
			$(this).removeClass("highlight");
		});
	},
	
	/**
	 * Enables links in a container based on the anchor tag's ID.
	 */
	enableLinks: function(containerId) {
		$("#" + containerId).find("a").each(function() {
			var pageLink = $(this).attr("link");
			
			if (pageLink) {
				var pagePrefix = "page";
				
				if (pageLink.indexOf(pagePrefix) != -1) {
					//console.log("containerId [" + containerId + "], pageLink [" + pageLink + "], txt [" + $(this).html() + "]");
					
					$(this).click(function() {
						var sectionName = $(this).attr("section");
						cp.cmpn.Content.load(sectionName, pageLink.replace(pagePrefix, ""));
						
						if (containerId == "top-nav") {
							cp.util.Util.removeHighlight();
							$(this).addClass("highlight");
						}
					});
				}
			}
		});
	},
	
	enableBookmark: function(htmlObj, url, title) {
		//alert("#enableBookmark, htmlObj [" + htmlObj.parent().html() + "\n\nurl [" + url + "]\n\ntitle ["  + title + "]");
		
		// add a "rel" attrib if Opera 7+
		if (window.opera) {
			htmlObj.attr("href", url);
			htmlObj.attr("title", title);
			
			if (htmlObj.attr("rel") != ""){ // don't overwrite the rel attrib if already set
				htmlObj.attr("rel", "sidebar");
			}
		}
	
		$("#bookmark").click(function(event) {
			event.preventDefault(); // prevent the anchor tag from sending the user off to the link
			
			//alert("window.sidebar [" + window.sidebar + "], window.external [" + window.external + "]");
			
			// Firefox
			if (window.sidebar) {
				window.sidebar.addPanel(title, url, "");
			}
			
			else if (window.external) {
				// Chrome doesn't support dynamic book-marking
				if (navigator.userAgent.indexOf("Chrome") != -1) {
					cp.util.Util.alertUnsupportedBrowser(url, "Chrome");
				}
				
				// IE
				else {
					window.external.AddFavorite(url, title);
				}
			}
			
			// Opera 7+
			else if (window.opera) {
				return false; //do nothing - rel="sidebar" already enabled it
			}
			
			// other browsers (Safari, etc.) do not support bookmarking
			else {
				cp.util.Util.alertUnsupportedBrowser(url, null);
			}
		});
	},
	
	alertUnsupportedBrowser: function(url, browserName) {
		alert("Unfortunately, your " + (browserName == null ? "" : browserName + " ") + "browser does not support dynamic bookmarking.  Please add the link " + url + " manually.");
	}
}

