/**
 *  JavaScript for Bench & Bedside Magazine site
 */
  

YUI().use("YUI","node","event", function(Y) {

var BB = {}; // bench and bedside javascript object

BB.utils = {

	makeBrandClickable: function() {
		/**
		 *  Function to make brand div as clickable as the link within it.
		 */
		var myNode = Y.get("#brand");
		var getHref = function(e) {
			// "this" is DIV#brand
			e.preventDefault();
			//alert('event: ' + e.type + ' target: ' + e.target.get('tagName'));
			var theLink = this.query("a");
			if (theLink) {
				var theLinkHref = theLink.get("href");
				document.location = theLinkHref;
			}
		};
		if (myNode) {
			myNode.on("click",getHref);
		}
	},
	
	makeArticleClippingsClickable: function() {
		/**
		 *  Function to make all articleClipping divs as clickable as the article 
		 *  links within them, without having to wrap the entire div in an A tag
		 *  or add an onclick to every div.
		 */
		var myNode = false;
		var nodes = false;
		myNode = Y.get("#container");
		if (myNode) {
			nodes = myNode.queryAll(".articleClipping");
		}
		var getHref = function(e) {
			// "this" is DIV.articleClipping
			e.preventDefault();
			//alert('event: ' + e.type + ' target: ' + e.target.get('tagName'));
			var articleLink = this.query("a");
			var articleLinkHref = articleLink.get("href");
			document.location = articleLinkHref;
		};
		if (nodes) {
			nodes.on("click",getHref);
		}
	},
		
	addFirstChildToArticleClippings: function() {
		/**
		 *  Marks all first articles in a section as class "firstChild" for IE6 stylesheet
		 *  goodness, since IE6 doesn't support the first-child selector.
		 *  Thus, in the CSS, we can refer to .firstChild instead of :first-child
		 */
		var myNode = Y.get("#newsEvents");
		if (myNode) { 
			var firstNode = myNode.query(".articleClipping");
			firstNode.addClass("firstChild");
		}
		myNode = Y.get("#features");
		if (myNode) { 
			firstNode = myNode.query(".articleClipping");
			firstNode.addClass("firstChild");
		}
		myNode = Y.get("#departments");
		if (myNode) { 
			firstNode = myNode.query(".articleClipping");
			firstNode.addClass("firstChild");
		}
	},
	
	searchBox: {
		"initialValue": "SEARCH",
		clearValue: function() {
			var searchBoxValue = this.get("value");
			if (searchBoxValue == BB.utils.searchBox.initialValue) { 
				this.set("value","");
			}
		},
		restoreValue: function() {
			var searchBoxValue = this.get("value");
			if (searchBoxValue === "") { 
				this.set("value",BB.utils.searchBox.initialValue);
			}
		},
		init: function() {
			var searchBox = Y.get("#q");
			searchBox.set("value",BB.utils.searchBox.initialValue);
			if (!document.all) searchBox.set("type","search");
			searchBox.on("focus",BB.utils.searchBox.clearValue);
			searchBox.on("blur",BB.utils.searchBox.restoreValue);
		}
	},

	init: function() {
		Y.on("available", BB.utils.makeBrandClickable, ".article");
		Y.on("available", BB.utils.makeArticleClippingsClickable, ".home");
		Y.on("available", BB.utils.addFirstChildToArticleClippings, ".home");
		Y.on("available", BB.utils.searchBox.init, "#q");
	}

};


Y.on("domready", BB.utils.init);


});