﻿/* -- Global JavaScript (include jQuery first) -- */
(function () {
	var doSearch = function (query) {
		window.location.href = '/search.aspx?searchtext=' + query;
	};

	var slideSponsorLogos = function (images) {
		var scroll_amt = $(images[0]).outerWidth(true);

		images.each(function (n) {
			$(this).animate({
				left: -scroll_amt + 'px'
			}, 2000, 'linear', function () {
				if (n == images.length - 1) {
					// Reset positions, move left-most image to the end, repeat process
					images.css('left', 0);
					$(images[0]).parent().append(images[0]);

					var dom_arr = images.get();
					var first_img = dom_arr.shift();

					dom_arr.push(first_img);
					images = $(dom_arr);

					slideSponsorLogos(images);
				}
			});
		});
	};

	$(window).load(function () {
		var sponsor_callout = $('.er-sponsors-callout');

		if (sponsor_callout.length > 0) {
			var wrapper = sponsor_callout.find('div')[0];
			var images = sponsor_callout.find('img');

			// Get width of images to see if they need to scroll
			var images_width = 0;

			images.each(function () {
				images_width += $(this).outerWidth(true);
			});

			if (images_width > $(wrapper).width()) slideSponsorLogos(images);
		}
	});

	$(document).ready(function () {
		// Implement search feature, which relies on JavaScript for some reason
		$('#q').keydown(function (event) {
			if (event.keyCode == '13') {
				doSearch($(this).val());
				return false;
			}
		});

		$('#goSearch').click(function (event) {
			doSearch($('#q').val());
			return false;
		});

		// Add support for @placeholder in IE and other old browsers
		var i = document.createElement('input');

		if (!('placeholder' in i)) {
			// Browser does not support @placeholder. Time to make it work.
			$('input[type=text]').each(function () {
				if ($(this).val() == '') $(this).val($(this).attr('placeholder'));

				$(this).focus(function () {
					if ($(this).val() == $(this).attr('placeholder')) $(this).val('');
				});

				$(this).blur(function () {
					if ($(this).val() == '') $(this).val($(this).attr('placeholder'));
				});
			});
		}
	});
})();

/* -- Older functions that should probably be rewritten to stay out of the global namespace -- */

function popUp(URL, name, w, h, d, l, m, r, sc, st, t) {
	var featureStr = "";
	featureStr = "width=" + w + ",height=" + h + ",directories=" + d + ",location=" + l + ",menubar=" + m + ",resizable=" + r + ",scrollbars=" + sc + ",status=" + st + ",toolbar=" + t;
	window.open(URL, name, featureStr);
}

function emailThisPage() {
	var pagetitle = self.document.forms[0].pageTitle.value;
	var pageURL = "/EmailThisPage.aspx?pageUrl=" + self.location + "&pageTitle=" + pagetitle;
	popUp(pageURL, 'emailthispage', 590, 530, 0, 0, 0, 0, 0, 0, 0);
}
