/*===========================================================================
	Presentational (Global)
	
	This JavaScript executes dynamic behaviors such as:
	* Preloading images
	* Flash Replacements (i.e. sIFR, UFO, SWFObject, etc.)
===========================================================================*/

/* Fix background-image Flickering for a:hover in Internet Explorer
===========================================================================*/

/*
	No More IE6 Background Flicker - Cristi Balan
	http://evil.che.lu/2006/9/25/no-more-ie6-background-flicker
*/

try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(e) {}

/* Decorate lists for styling purposes
===========================================================================*/

YAHOO.util.Event.onContentReady("primary-content", function() {
	var entries = [];
	
	var entry_content = YAHOO.util.Dom.getElementsByClassName("entry-content", "div", this);
	var entry_summary = YAHOO.util.Dom.getElementsByClassName("entry-summary", "div", this);
	
	for (var i = 0, i_length = entry_content.length; i < i_length; i++) {
		entries.push(entry_content[i]);
	}
	
	for (var i = 0, i_length = entry_summary.length; i < i_length; i++) {
		entries.push(entry_summary[i]);
	}
	
	// Create template element
	var list_item_content = document.createElement("div");
	list_item_content.className = "list-item-content";
	
	// Loop over entries
	for (var i = 0, iLength = entries.length; i < iLength; i++) {
		var list_items = entries[i].getElementsByTagName("li");
		
		for (var j = 0, j_length = list_items.length; j < j_length; j++) {
			add_inner_container(list_items[j], list_item_content.cloneNode(false));
		}
		
		YAHOO.util.Dom.addClass(entries[i].getElementsByTagName("ul"), "bulleted");
		YAHOO.util.Dom.addClass(entries[i].getElementsByTagName("ol"), "numbered");
	}
});

YAHOO.util.Event.onContentReady("utility-content", function() {
	var widget_contents = YAHOO.util.Dom.getElementsByClassName("widget-content", "div", this);
	
	// Create template element
	var list_item_content = document.createElement("div");
	list_item_content.className = "list-item-content";
	
	// Loop over entries
	for (var i = 0, iLength = widget_contents.length; i < iLength; i++) {
		var list_items = widget_contents[i].getElementsByTagName("li");
		
		for (var j = 0, j_length = list_items.length; j < j_length; j++) {
			add_inner_container(list_items[j], list_item_content.cloneNode(false));
		}
		
		YAHOO.util.Dom.addClass(widget_contents[i].getElementsByTagName("ul"), "bulleted");
		YAHOO.util.Dom.addClass(widget_contents[i].getElementsByTagName("ol"), "numbered");
	}
});

/* Global Image Preloads
===========================================================================*/

preload_images([
	URL_ROOT + "_media/_global/primary-nav-hover-background.gif",
	
	URL_ROOT + "_media/bullets/chevron-left-white-on-dark-gray.gif",
	URL_ROOT + "_media/bullets/chevron-right-white-on-dark-gray.gif",
	URL_ROOT + "_media/bullets/chevron-left-orange-yellow-on-dark-blue.gif",
	URL_ROOT + "_media/bullets/chevron-right-orange-yellow-on-dark-blue.gif",
	URL_ROOT + "_media/bullets/chevron-left-yellow-on-dark-blue.gif",
	URL_ROOT + "_media/bullets/chevron-right-yellow-on-dark-blue.gif",
	URL_ROOT + "_media/bullets/disc-orange-yellow-on-dark-blue.gif",
	
	URL_ROOT + "_media/utility-content/background-top-hover.gif",
	URL_ROOT + "_media/utility-content/background-hover.gif",
	URL_ROOT + "_media/utility-content/default-icon-hover.gif",
	URL_ROOT + "_media/utility-content/map-hover.jpg"//,
]);

/* Vote Buttons
===========================================================================*/

YAHOO.util.Event.onContentReady("primary-content", function() {
	var vote_buttons = YAHOO.util.Dom.getElementsByClassName("dem-vote-button", "input", this);
	var vote_buttons_length = vote_buttons.length; // optimization
	
	if (vote_buttons_length) {
		preload_images([
			URL_ROOT + "_media/buttons/vote-primary-content.gif",
			URL_ROOT + "_media/buttons/vote-hover-primary-content.gif",
			URL_ROOT + "_media/buttons/vote-active-primary-content.gif"//,
		]);
	}
	
	for (var i = 0, iLength = vote_buttons_length; i < iLength; i++) {
		vote_buttons[i].mouse_is_down = false;
		
		YAHOO.util.Event.addListener(document.documentElement, "mouseup", function(e) {
			this.mouse_is_down = false;
		}, vote_buttons[i], true);
	}
	
	YAHOO.util.Event.addListener(vote_buttons, "mouseover", function(e) {
		if (this.mouse_is_down) {
			this.src = URL_ROOT + "_media/buttons/vote-active-primary-content.gif";
		} else {
			this.src = URL_ROOT + "_media/buttons/vote-hover-primary-content.gif";
		}
	});
	
	YAHOO.util.Event.addListener(vote_buttons, "mouseout", function(e) {
		this.src = URL_ROOT + "_media/buttons/vote-primary-content.gif";
	});
	
	YAHOO.util.Event.addListener(vote_buttons, "mousedown", function(e) {
		this.mouse_is_down = true;
		this.src = URL_ROOT + "_media/buttons/vote-active-primary-content.gif";
	});
	
	YAHOO.util.Event.addListener(vote_buttons, "mouseup", function(e) {
		this.mouse_is_down = false;
		this.src = URL_ROOT + "_media/buttons/vote-hover-primary-content.gif";
	});
});

YAHOO.util.Event.onContentReady("utility-content", function() {	
	var vote_buttons = YAHOO.util.Dom.getElementsByClassName("dem-vote-button", "input", this);
	var vote_buttons_length = vote_buttons.length; // optimization
	
	if (vote_buttons_length) {
		preload_images([
			URL_ROOT + "_media/buttons/vote-utility-content.gif",
			URL_ROOT + "_media/buttons/vote-hover-utility-content.gif",
			URL_ROOT + "_media/buttons/vote-active-utility-content.gif"//,
		]);
	}
	
	for (var i = 0, iLength = vote_buttons_length; i < iLength; i++) {
		vote_buttons[i].mouse_is_down = false;
		
		var widget = null;
		var parent = vote_buttons[i].parentNode;
		
		while (parent && parent != document.documentElement) {
			if (YAHOO.util.Dom.hasClass(parent, "widget")) {
				widget = parent;
				break;
			}
			
			parent = parent.parentNode;
		}
		
		if (widget) {
			YAHOO.util.Event.addListener(widget, "mouseover", function(e) {
				this.src = URL_ROOT + "_media/buttons/vote-hover-utility-content.gif";
			}, vote_buttons[i], true);
			
			YAHOO.util.Event.addListener(widget, "mouseout", function(e) {
				this.src = URL_ROOT + "_media/buttons/vote-utility-content.gif";
			}, vote_buttons[i], true);
		}
		
		YAHOO.util.Event.addListener(document.documentElement, "mouseup", function(e) {
			this.mouse_is_down = false;
		}, vote_buttons[i], true);
	}
	
	YAHOO.util.Event.addListener(vote_buttons, "mouseover", function(e) {
		if (this.mouse_is_down) {
			this.src = URL_ROOT + "_media/buttons/vote-active-utility-content.gif";
			YAHOO.util.Event.stopPropagation(e);
		}
	});
	
	YAHOO.util.Event.addListener(vote_buttons, "mouseout", function(e) {
		this.src = URL_ROOT + "_media/buttons/vote-hover-utility-content.gif";
	});
	
	YAHOO.util.Event.addListener(vote_buttons, "mousedown", function(e) {
		this.mouse_is_down = true;
		this.src = URL_ROOT + "_media/buttons/vote-active-utility-content.gif";
	});
	
	YAHOO.util.Event.addListener(vote_buttons, "mouseup", function(e) {
		this.mouse_is_down = false;
		this.src = URL_ROOT + "_media/buttons/vote-hover-utility-content.gif";
	});
});

/* Send Buttons
===========================================================================*/

YAHOO.util.Event.onAvailable("send-comment-form", function() {
	preload_images(URL_ROOT + "_media/buttons/send-hover.gif");
	
	YAHOO.util.Event.addListener(this, "mouseover", function(e) {
		this.src = URL_ROOT + "_media/buttons/send-hover.gif"
	});
	
	YAHOO.util.Event.addListener(this, "mouseout", function(e) {
		this.src = URL_ROOT + "_media/buttons/send.gif"
	});
});

YAHOO.util.Event.onAvailable("send-contact-form", function() {
	preload_images(URL_ROOT + "_media/buttons/send-hover.gif");
	
	YAHOO.util.Event.addListener(this, "mouseover", function(e) {
		this.src = URL_ROOT + "_media/buttons/send-hover.gif"
	});
	
	YAHOO.util.Event.addListener(this, "mouseout", function(e) {
		this.src = URL_ROOT + "_media/buttons/send.gif"
	});
});

YAHOO.util.Event.onAvailable("send-search-holiday-form", function() {
	preload_images(URL_ROOT + "_media/buttons/search-hover.gif");
	
	YAHOO.util.Event.addListener(this, "mouseover", function(e) {
		this.src = URL_ROOT + "_media/buttons/search-hover.gif"
	});
	
	YAHOO.util.Event.addListener(this, "mouseout", function(e) {
		this.src = URL_ROOT + "_media/buttons/search.gif"
	});
});

YAHOO.util.Event.onAvailable("wp-email", function() {
	preload_images(URL_ROOT + "_media/buttons/send-hover.gif");
	
	YAHOO.util.Event.addListener(this, "mouseover", function(e) {
		this.src = URL_ROOT + "_media/buttons/send-hover.gif"
	});
	
	YAHOO.util.Event.addListener(this, "mouseout", function(e) {
		this.src = URL_ROOT + "_media/buttons/send.gif"
	});
});

/* Submit Buttons
===========================================================================*/

YAHOO.util.Event.onAvailable("filter-global500-filter-form", function() {
	preload_images(URL_ROOT + "_media/buttons/submit-hover.gif");
	
	YAHOO.util.Event.addListener(this, "mouseover", function(e) {
		this.src = URL_ROOT + "_media/buttons/submit-hover.gif"
	});
	
	YAHOO.util.Event.addListener(this, "mouseout", function(e) {
		this.src = URL_ROOT + "_media/buttons/submit.gif"
	});
});

/* Search Buttons
===========================================================================*/

YAHOO.util.Event.onAvailable("search-global500-search-form", function() {
	preload_images(URL_ROOT + "_media/buttons/search-hover.gif");
	
	YAHOO.util.Event.addListener(this, "mouseover", function(e) {
		this.src = URL_ROOT + "_media/buttons/search-hover.gif"
	});
	
	YAHOO.util.Event.addListener(this, "mouseout", function(e) {
		this.src = URL_ROOT + "_media/buttons/search.gif"
	});
});

/* Subscribe Button
===========================================================================*/

YAHOO.util.Event.onAvailable("subscribe-newsletter-subscribe-form", function() {
	preload_images([
		URL_ROOT + "_media/buttons/subscribe-hover.gif",
		URL_ROOT + "_media/buttons/subscribe-active.gif"//,
	]);
	
	this.mouse_is_down = false;
	
	var widget = null;
	var parent = this.parentNode;
	
	while (parent && parent != document.documentElement) {
		if (YAHOO.util.Dom.hasClass(parent, "widget")) {
			widget = parent;
			break;
		}
		
		parent = parent.parentNode;
	}
	
	if (widget) {
		YAHOO.util.Event.addListener(widget, "mouseover", function(e) {
			this.src = URL_ROOT + "_media/buttons/subscribe-hover.gif";
		}, this, true);
		
		YAHOO.util.Event.addListener(widget, "mouseout", function(e) {
			this.src = URL_ROOT + "_media/buttons/subscribe.gif";
		}, this, true);
	}
	
	YAHOO.util.Event.addListener(document.documentElement, "mouseup", function(e) {
		this.mouse_is_down = false;
	}, this, true);
	
	YAHOO.util.Event.addListener(this, "mouseover", function(e) {
		if (this.mouse_is_down) {
			this.src = URL_ROOT + "_media/buttons/subscribe-active.gif";
			YAHOO.util.Event.stopPropagation(e);
		}
	});
	
	YAHOO.util.Event.addListener(this, "mouseout", function(e) {
		this.src = URL_ROOT + "_media/buttons/subscribe-hover.gif";
	});
	
	YAHOO.util.Event.addListener(this, "mousedown", function(e) {
		this.mouse_is_down = true;
		this.src = URL_ROOT + "_media/buttons/subscribe-active.gif";
	});
	
	YAHOO.util.Event.addListener(this, "mouseup", function(e) {
		this.mouse_is_down = false;
		this.src = URL_ROOT + "_media/buttons/subscribe-hover.gif";
	});
});