/* 
 * SAAM is the scope of all custom objects created for the Smithsonian American Art Museum
 * by NavigationArts. This is so any outside JS code won't conflict with the functions and variables.
 */
if (!SAAM) {
	var SAAM = {
		/*
		 * search form default value (based on the label's content)
		 */
		searchText: "",
		searchAlert: "Please enter a search term.",
		searchFormValidate: function() {
		    var obj = $("#navSearch div.text input");
		    if ($(obj).val() == SAAM.searchText || $(obj).val() === "") {
			        alert(SAAM.searchAlert);
			        $(obj).focus();
			        return false;
		        } 
			else {
				return true;
			}
		},
		
		/*
		 * stylesheet switching variables/functions. Clicking on the Larger/Smaller buttons will activate this.
		 */
		stylesheetCookieName: "saamStyle",
		stylesheetCookieValue: "",
		stylesheetSwitcher: function(stylesheetName) {
			$("link[@rel*=style][title]").each(function(i) {
				this.disabled = true;
				if ($(this).attr("title") == stylesheetName) {
					this.disabled = false;
				}
			});
			$.cookie(SAAM.stylesheetCookieName, stylesheetName, { expires: 7, path: '/' });
		}
	};
}

	
/*
 * jQuery DOMdocument.ready
 */
$(document).ready(function($){
	/*
	SAAM.qs = window.location.search.substring(1, window.location.search.length);
	
	if(SAAM.qs == "grid") {
		$("body").prepend("<div id='grid'></div>");
		$("#grid").css("height", $("#container").height() + "px");
	}
	*/
	
	/* Safari CSS fix, since Safari doesn't have doesn't have any CSS unique identifier. */
		if($.browser.safari) {
			$("body").addClass("isSafari");
		}
	
	/* Opera CSS fix, since Opera doesn't have doesn't have any CSS unique identifier. */
		if($.browser.opera) {
			$("body").addClass("isOpera");
		}
	
	/* left nav XML parsing could leave empty UL tags */
		$("#contentNav ul:empty").remove();
	
	/* if there is a left nav, add a class to change the visual appearance of the container. */
		if($("#contentNav ul").size() > 0) {
			$("#contentNav").addClass("activated");
		}
	
	/* collections/search form */
		if($("#resultsPerPage").size() > 0) {
			$("#resultsPerPage").change(function() {
				var tmp = $(this).attr("value");
				$("#artworkResultsPerPage").val(tmp);
			});
		}
	
	/* artwork search results page */
		if($("#contentSearchFilter").size() > 0) {
			$("#contentSearchFilter input").click(function() {
				$("#contentSearchFilter form").submit();
			});
		}
	/* pages with a callout DIV, need to make the height of the callout at least as tall as the right rail */
		if($("div.callout").size() > 0) {
			SAAM.tmpObj = $("div.callout");
			tmp = $("#contentMain").height() - $("div.callout").height() - parseInt($("#contentRelated div.widget").css("margin-bottom"), 10);
			//$(SAAM.tmpObj).css("min-height", ($("#contentRelated").height() - tmp) + "px");
		}
	
	/* artist object page */
		if($("#contentArtistDetails").size() > 0) {
			if($("#contentArtistThumb").size() === 0) {
				$("div.columns").addClass("noPortrait");
			}
			
			if($("div.column1").size() === 0) {
				$("div.callout").addClass("noBiography");
			}
			
			if($("#contentArtistDescriptionAdditional").size() > 0) {
				SAAM.tmpObj = $("#contentArtistDescriptionAdditional").prev("h4");
				$(SAAM.tmpObj).click(function(){
					$("#contentArtistDescriptionAdditional").show();
					$(SAAM.tmpObj).addClass("on");
				});
			}
		}
	
	/* site search form interaction */
		SAAM.searchText = $("#navSearch div.textField label").html();
		$("#navSearch div.textField input")
			.val(SAAM.searchText)
			.focus(function(){
					if($(this).val() == SAAM.searchText) {
						$(this).val("");
					}
				})
			.blur(function(){
					if($(this).val() === "") {
						$(this).val(SAAM.searchText);
					}
				});
		
		/* remove the value of the input so the BG image shows through. */
		$("#navSearch div.submitField input").attr("value", "");
		
		/* validate search form onSubmit */
		$("#navSearch form").submit(function() {
			return SAAM.searchFormValidate();
		});
		
	/* Fontsizing widget click functionality */
        $("#toolsFontsize div").click(function() {
			SAAM.stylesheetSwitcher($(this).attr("title"));
			return false;
        });
		
	/* Fontsizing widget onload activation */
        SAAM.stylesheetCookieValue = $.cookie(SAAM.stylesheetCookieName);
        if (SAAM.stylesheetCookieValue) {
			SAAM.stylesheetSwitcher(SAAM.stylesheetCookieValue);
		}
	
	/* search form submits */
		$("div.submitField label").click(function() {
			$(this).find("input").click();
		});
	
	/* IE6 poor CSS support workarounds */
		$("ul li:last-child").addClass("lastChild");
		$("ul li:first-child").addClass("firstChild");
	
	/* homepage only */
		if ($("div.homeWidget").size() > 0) {
			/* add events to the homepage widget */
			$("div.homeWidget li")
				.mouseover(function(){
						$("div.homeWidget li.on").removeClass("on");
						$(this).addClass("on");
					})
				.click(function(){
						location.href = $("a", this).attr("href");
						return false;
					});
		}

		/*
		 * IE fix for submitting a form by hitting enter in IE.
		 * cf. http://www.thefutureoftheweb.com/blog/submit-a-form-in-ie-with-enter
		if ($("input").size() > 0) {
			$(function(){
				$("input").keydown(function(e){
					if (e.keyCode == 13) {
						$(":submit, input[type=image]", $(this).parents("form")).click();
						return false;
					}
				});
			});
		}
		 */
});
