
// Used in the Prescription form
window.addEvent('domready', readAddToggler);
function readAddToggler() {
	if ($('usefor')) {
		$('usefor').addEvent('change', function (sel) {
			readAddSet();
		});
		readAddSet();
	}
}
function readAddSet() {
//	alert($('usefor').value);
	if ($('usefor').value == 'close') {
		// Closeup:		Show read_add
		$('read_add_box').removeClass('hide');
	} else {
		// Distnace:	Hide read_add
		$('read_add_box').addClass('hide');
	}
}

// Used in the Buy Now form
window.addEvent('domready', selLensStyle);
function selLensStyle() {
  // onChange listener
  if ($('selLensStyle')) {
	$('selLensStyle').addEvent('change', function (sel) {
		toShow = $('selLensStyle').value;
		$$('.lens_opt').each(function (div) {
			div.addClass('hide');
		});
		$(toShow).toggleClass('hide');
	});
  }
  // hide all initially.  Set 'hide' on one to show at start.
  $$('.lens_opt').each(function (div) {
	div.toggleClass('hide');
  });
}

// For the newsletter email signup form
window.addEvent('domready', newsEmailSignup);
function newsEmailSignup() {
  if ($('emailaddress')) {
	var DefaultEmail = 'Email Address...';
	$('emailaddress').value = DefaultEmail;
	
	$('emailaddress').addEvent('focus', function (e) {
		if ($('emailaddress').value == DefaultEmail)
			$('emailaddress').value = '';
	});
	$('emailaddress').addEvent('blur', function (e) {
		if ($('emailaddress').value == '')
			$('emailaddress').value = DefaultEmail;
	});
  }
}


// For the search form input box
window.addEvent('domready', sideSearchInput);
function sideSearchInput() {
  if ($('side_search')) {
	var DefaultSearch = 'Enter Keywords...';
	$('side_search').value = DefaultSearch;
	
	$('side_search').addEvent('focus', function (e) {
		if ($('side_search').value == DefaultSearch)
			$('side_search').value = '';
	});
	$('side_search').addEvent('blur', function (e) {
		if ($('side_search').value == '')
			$('side_search').value = DefaultSearch;
	});
  }
}



// Will make all columns in tagged tables of equal width
window.addEvent('domready', equalTableColumns);
function equalTableColumns() {	// This one doesn't need TH present.  Goes through TD's in first row instead.
	$$('table.equalcolumns').each ( function (t) {
		var trs = t.getElementsByTagName('tr');
		tr = trs[0];
		var tds = tr.getElementsByTagName('td');
		var numCols = tds.length;
		var percWidth = 100/numCols;
		for (var i=0, len=numCols; i<numCols; i++) {
			td = tds[i];
			$(td).setStyle('width', percWidth+'%');
		}
	});
}


// for alert boxes - make them pulse
function UpdatedBoxes() {
	var boxes = $$('.alert');
	boxes.each (function (box) {
		var effect = box.effect('background-color', {duration: 1000} );
		var periodical;
		var fx = function() {	
			effect.start('#EFF7FE').chain(function() {
				effect.start('#F5E2EE');
			});
		}
		// Commence
		fx();
		periodical = fx.periodical(2100);
		// to stop: 	$clear(periodical);
	});
}
window.addEvent('domready',UpdatedBoxes);


// Tooltip functionality
function articleToolTips() {
  var myTips = new Tips($$('.toolTip'), {
	  maxTitleChars: 50   // We're hiding the 'title' through CSS, no need for that.  
	  // Have to be careful that these do not overlap SELECT boxes.  That isn't readable in IE.
  });
}
window.addEvent('domready',articleToolTips);


// Accordion Functionality (.accToggler and .accContent)
function flexiArticles() {
	// Get Togglers and Configure
	var accordionTogglers = $$('.accToggler');
	accordionTogglers.each(function(toggler){
		//remember the original color
		toggler.origColor = toggler.getStyle('color');
		//set the effect
		toggler.fx = new Fx.Style(toggler, 'color');
	});
	// Get Content Blocks
	var accordionContents = $$('.accContent');
	// Run it
	accordion = new Fx.Accordion(accordionTogglers, accordionContents,{
		onActive: function(toggler){
//			toggler.fx.start('#FF0000');						// Set the font colour of the active element
		},
		onBackground: function(toggler){
//			toggler.setStyle('color', toggler.origColor);
		},
		alwaysHide: true
	});
}
window.addEvent('domready', flexiArticles);