/**
 * @fileoverview Global JavaScript functions used on most pages.
 * Most functions use the DOMAssistant utility library for efficiency.
 * More information: http://www.domassistant.com/
 */

/**
 * Replaces the submit button of the global search form with a link to enable CSS styling
 */
var seSearch = function () {
	var config = {
		containerId: 'quicksearch', // ID of the element containing the submit button
		hiddenClass: 'hidden'
	};
	function init(props) {
		// Check for DOM support
		if (!document.getElementById || !document.createTextNode) {
			return;
		}
		// If options were supplied, apply them to the config object.
		for (var key in props) {
			if (config.hasOwnProperty(key)) {
				config[key] = props[key];
			}
		}
		var searchButton = document.getElementById(config.containerId).getElementsByTagName('input')[0]; // Get the first input element
		// Check that the input element exists and is of type submit
		if (searchButton && searchButton.getAttribute('type') === 'submit') {
			// Create a link with the button's value as text
			var link = document.createElement('a');
			link.href = '#';
			link.appendChild(document.createTextNode(searchButton.value));
			link.onclick = function () {
				__doPostBack(searchButton.name, '');
			};
			// Insert the link and hide the button.
			// Removing or replacing the button prevents the form from
			// being submitted by keyboard in Internet Explorer.
			searchButton.parentNode.appendChild(link);
			searchButton.className = config.hiddenClass;
		}
	}
	return {
		init: init
	};
}();

/**
 * @requires DOMAssistant
 * Inserts an iframe with an external ticker script or Flash file
 */
var seTicker = function () {
	var config = {
		activeClassName: 'ticker' // className added to the container element for styling with JS on
	};
	function init(targetEl, URL) {
		// Check for DOM support
		if (!document.getElementById || !document.createTextNode) {
			return;
		}
		var tickerContainer = document.getElementById(targetEl);
		// Return if the element does not exist
		if (!tickerContainer) {
			return;
		}
		// Inject the iframe
		tickerContainer.innerHTML = '<iframe id="stockScroller" src="' + URL + '" frameborder="0" scrolling="no"></iframe>';
		$(tickerContainer).addClass(config.activeClassName);
	}
	return {
		init: init
	};
}();




/**
 * @requires DOMAssistant
 * Inserts a link that calls window.print()
 */
var addPrintLink = function () {
	var config = {
		targetEl: 'nav-supp', // id of the element the link is inserted into
		printClassName: 'print', // className that must exist for the link to be inserted
		linkText: 'Print the page', // The link text
		linkId: 'print-link', // Id of the inserted link
		isFirstChild: true // Flag that defines if the link is inserted as the first or last child of sTargetEl.
	};
	function init(props) {
		// Check for DOM support
		if (!document.getElementById || !document.createTextNode) {
			return;
		}
		// If options were supplied, apply them to the config object.
		for (var key in props) {
			if (config.hasOwnProperty(key)) {
				config[key] = props[key];
			}
		}
		var target = document.getElementById(config.targetEl);
		// Return if the element does not exist
		if (!target) {
			return;
		}
		// Return if the browser does not support window.print
		if (!window.print) {
			return;
		}
		if ($(target).hasClass(config.printClassName)) { // Check that the link should be inserted on this page
			var link = document.createElement('a');
			link.id = config.linkId; // Give the link an id to allow styling
			link.href = '#'; // Make the link focusable for keyboard users
			link.appendChild(document.createTextNode(config.linkText));
			link.onclick = function () {
				window.print();
				return false;
			}
			if (config.isFirstChild && target.firstChild) { // Insert the link as the first child
				target.insertBefore(link, target.firstChild);
			}
			else { // Insert the link as the last child
				target.appendChild(link);
			}
		}
	}
	return {
		init: init
	};
}();

/**
 * @requires DOMAssistant
 * Highlights table rows onmouseover
 */
var hlTables = function () {
	var config = {
		tableClass: 'highlight', // className for tables that the highlight behaviour is applied to
		hlClass: 'highlight' // className added to table rows onmouseover for CSS styling
	};
	function init(props)	{
		// Check for DOM support
		if (!document.getElementById || !document.createTextNode) {
			return;
		}
		// If options were supplied, apply them to the config object.
		for (var key in props) {
			if (config.hasOwnProperty(key)) {
				config[key] = props[key];
			}
		}
		// Find all rows in tbody elements of the tables and assign event handlers
		$('table.' + config.tableClass + ' tbody tr').each(function () {
			$(this).addEvent('mouseover', toggle);
			$(this).addEvent('mouseout', toggle);
		});
	}
	function toggle(e) {
		if (e.type === 'mouseover') {
			$(this).addClass(config.hlClass);
		} else {
			$(this).removeClass(config.hlClass);
		}
    }
	return {
		init: init
	};
}();

/**
 * @requires DOMAssistant
 * @class Photo contacts list
 * @constructor
 * Toggles more info in photo contacts view
 */
function photoContacts(options) {
	// Check for DOM support
	if (!document.getElementById || !document.createTextNode) {
		return;
	}
	var defaults = {
		sContactListClass: 'photo-contacts-list',
		sExpandableClass: 'expandable',
		sContactGroupClass: 'contact-group',
		sContactClass: 'contact',
		sContactTitleClass: 'contact-title',
		sShowText: 'More info',
		sHideText: 'Close',
		sMoreInfoClass: 'more-info',
		sCloseClass: 'close',
		sExpandedClass: 'expanded',
		sHiddenClass: 'hidden',
		sViewAllClass: 'see-all',
		sViewAllText: 'See all information',
		sViewAllURL: null,
		bMakeExpandable: true
	};
	for (var key in defaults) {
		if (!options.hasOwnProperty(key)) {
			options[key] = defaults[key];
		}
	}
	this.options = options;
	var oExpanded = null;
	var oContactList = $(document).elmsByClass(this.options.sContactListClass)[0];
	if (!oContactList) {
		return;
	}
	if (this.options.sViewAllURL) {
		var oViewAll = document.createElement('div');
		oViewAll.className = this.options.sViewAllClass;
		var oViewAllLink = document.createElement('a');
		oViewAllLink.href = this.options.sViewAllURL;
		oViewAllLink.appendChild(document.createTextNode(this.options.sViewAllText));
		oViewAll.appendChild(oViewAllLink);
		oContactList.parentNode.insertBefore(oViewAll, oContactList);
	}
	if (this.options.bMakeExpandable) {
		$(oContactList).addClass(this.options.sExpandableClass);
		var arrContacts = $(oContactList).elmsByClass(this.options.sContactClass);
		var oContact, oContactGroup, oMoreInfo, oContactTitle, oShowLink, oHideLink, oHideP;
		for (var i = 0, len = arrContacts.length; i < len; i++) {
			oContact = arrContacts[i];
			oMoreInfo = $(oContact).elmsByClass(this.options.sMoreInfoClass)[0];
			oContactTitle = $(oContact).elmsByClass(this.options.sContactTitleClass)[0];
			if (!oMoreInfo || !oContactTitle) {
				continue;
			}
			$(oMoreInfo).addClass(this.options.sHiddenClass);
			oContact.moreInfo = oMoreInfo;
			oContact.parentNode.appendChild(oMoreInfo);
			oShowLink = document.createElement('a');
			oShowLink.href = '#';
			oShowLink.appendChild(document.createTextNode(this.options.sShowText));
			oContact.showLink = oShowLink;
			oHideP = document.createElement('p');
			oHideP.className = this.options.sCloseClass;
			oHideLink = document.createElement('a');
			oHideLink.href = '#';
			oHideLink.appendChild(document.createTextNode(this.options.sHideText));
			var self = this;
			(function () {
				var oCont = oContact;
				oShowLink.onclick = function () {
					self.show(oCont);
					return false;
				};
				oHideLink.onclick = function () {
					self.hide(oCont);
					return false;
				};
			})();
			oContactTitle.appendChild(oShowLink);
			oHideP.appendChild(oHideLink);
			oMoreInfo.appendChild(oHideP);
		}
	}
}
photoContacts.prototype = {
	/**
	 * @requires DOMAssistant
	 * Hides or displays expanded info
	 */
	show: function(oContact) {
		if (this.oExpanded) {
			$(this.oExpanded).removeClass(this.options.sExpandedClass);
			$(this.oExpanded.parentNode).removeClass(this.options.sExpandedClass);
			$(this.oExpanded.moreInfo).addClass(this.options.sHiddenClass);
			$(this.oExpanded.showLink).removeClass(this.options.sHiddenClass);
		}
		if (!$(oContact).hasClass(this.options.sExpandedClass)) {
			$(oContact).addClass(this.options.sExpandedClass);
			$(oContact.parentNode).addClass(this.options.sExpandedClass);
			$(oContact.moreInfo).removeClass(this.options.sHiddenClass);
			$(oContact.showLink).addClass(this.options.sHiddenClass);
			this.oExpanded = oContact;
		}
	},
	/**
	 * @requires DOMAssistant
	 * Hides expanded info
	 */
	hide: function(oContact) {
		if ($(oContact).hasClass(this.options.sExpandedClass)) {
			$(oContact).removeClass(this.options.sExpandedClass);
			$(oContact.parentNode).removeClass(this.options.sExpandedClass);
			$(oContact.moreInfo).addClass(this.options.sHiddenClass);
			$(oContact.showLink).removeClass(this.options.sHiddenClass);
			this.oExpanded = null;
		}
	}
};

/**
 * @requires DOMAssistant
 * Calls each function's init() method when the DOM is ready
 */
DOMAssistant.DOMReady(
	'seSearch.init()',
	'addPrintLinkRethink.init()',	
	'addPrintLink.init()',
	'hlTables.init()'
);
