if( Browser.Engine.trident && Browser.Engine.version <= 4 ){

	window.addEvent('domready', function () {
		$('banner1').setStyle('display','none');
		$('sparkleTopBannerOff').setStyle('display','none');
		$('sparkleBottom').setStyle('display','');
		$('sparkleTop').setStyle('display','');
		$('banner2').setStyle('display','');
	});

} else {

	window.addEvent('domready', function () {
		$('sparkleTop').fade('hide');
		$('sparkleTopBannerOff').fade('hide');
		$('sparkleBottom').fade('hide');
		$('banner2').fade('hide');
	});


	window.addEvent('load', function () {

		$('banner1').set('tween', {duration: 1200});
		$('banner2').set('tween', {duration: 1200});
		$('sparkleBottom').set('tween', {duration: 1200});
		$('sparkleTop').set('tween', {duration: 1600});
		$('sparkleTopBannerOff').set('tween', {duration: 1600});

		var startDelay = 1400;
	
		(function(){ $('banner1').fade('out'); }).delay(startDelay);
		(function(){ $('sparkleBottom').fade('in'); }).delay(startDelay+900);
		(function(){ $('sparkleTop').fade('in'); }).delay(startDelay+2400);
		(function(){ $('banner2').fade('in'); }).delay(startDelay+3900);
		(function(){ $('sparkleBottom').fade('toggle'); }).periodical(startDelay+3900);
	
		(function(){ 
			$('sparkleTopBannerOff').setStyle('z-index','110');
			(function(){ $('sparkleTopBannerOff').fade('toggle'); }).periodical(startDelay+2400);
		}).delay(startDelay+4900);
	});
}


var focusedElement;
window.addEvent('domready',function(){
	// Using domready doesnt work well with calendars

	$('donate_button').addEvent('click', function(e){
		e.preventDefault();
		showDonate('1');
	});

	// HELP ICONS
	var helpIcon = new Element('img', {
		'src': 'images/icon-help.png',
		'alt': '',
		'height': '15',
		'class': 'js_helpIcon',
		'width': '15'
	});

	$$('span.help').each(function(el, i) {

		// Place help box
		var inputField;
		if( el.getPrevious('label') ){
			inputField = el.getPrevious('label').getFirst();

		} else if( el.getPrevious('span') && el.getPrevious('span').get('class') == 'label' ){
			inputField = el.getPrevious('span').getFirst();

		// Check for calendar button
		} else if( el.getPrevious('button') && el.getPrevious('button').getPrevious('input') ){
			inputField = el.getPrevious('button').getPrevious('input');

		} else if( el.getPrevious('input') || el.getPrevious('textarea') || el.getPrevious('select') ){
			inputField = el.getPrevious();

		} else {
			return;
		}

		var elemName = inputField.get('name')+(inputField.get('type') == 'radio' ? "_"+inputField.get('value') : '');

		// Create help box
		var helpBox = new Element('div', {
			'class': 'helpBox',
			'id': 'helpBox_'+i
		});
		helpBox.fade('hide');
		var helpBoxP = new Element('p');
		helpBoxP.set('html', el.get('html'));
		helpBoxP.inject(helpBox);
		helpBox.inject($(document.body));

		inputField.addEvent('focus', function(e){
			helpBox.fade('in');
			focusedElement = elemName;
		});

		inputField.addEvent('blur', function(e){
			helpBox.fade('out');
			focusedElement = '';
		});


		// Add help icons
		el.set('html', '');
		el.setStyle('display', 'inline');
		var helpIconLocal = helpIcon.clone();
		helpIconLocal.set('id','helpIcon_'+i);
		helpIconLocal.addEvent('mouseenter', function(e){
			helpBox.fade('in');
		});
		helpIconLocal.addEvent('mouseleave', function(e){
			if( focusedElement != elemName ) helpBox.fade('out');
		});
		helpIconLocal.inject(el);

		// Set help box position
		// Browser.Engine.webkit = Safari
		helpBox.setStyles({
			'top': helpIconLocal.getCoordinates().top-8,
			'left': helpIconLocal.getCoordinates().right+15
		});
	});

	// CONFIRM EVENT
	$$("*[class*=confirm]").each(function(el) {
		var msg = String( el.getProperty("class").match(/confirm\['(.+)'\]/)[1] );

		if( msg ){
			el.addEvent('click', function(e){
				if( !confirm(msg) ) e.preventDefault();
			});
		}
	});

});

// REPOSITION HELP BOXES FOR DYNAMIC PAGE CONTENT
function repositionHelpBoxes()
{
	$$('.js_helpIcon').each(function(el, i) {
		$('helpBox_'+el.get('id').split('_')[1]).setStyles({
			'top': el.getCoordinates().top-8,
			'left': el.getCoordinates().right+(Browser.Engine.webkit ? 20 : 10)
		});
	});
}

function showDonate(fromButton)
{
	// CREATE BOX
	var box = new Element('div', {
		'class': 'overlayBox'
	});

	var heading = new Element('h1', {
		'class': '',
		'html': "Help us stay online by donating"
	});

	heading.inject(box, 'top');

	new Element('a', {
		'class': 'overlayBoxClose closebutton',
		'href': '#',
		'html': 'Close'
	}).inject(box, 'top');


	// CONTENT - either from source of direct content
	var content = new Element('div', {
		'html': "<table style='width: 100%'><tr><td style='width: 200px; padding-right: 10px; padding-top: 10px'><p style='font-size: 13px; line-height: 140%'>Listerners like you make our programs possible.  Please consider making a small contribution.</p></td><td><img src='images/paypal-secure.gif' alt='PayPal'/></td><td style='padding-left: 10px'><ul style='padding-top: 5px; padding-left: 20px'><li style='margin-bottom: 8px; font-size: 14px'><a href='https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&amp;business=info@energytalkradio.com&amp;item_name=Energy+Talk+Radio+Donation&amp;currency=USD&amp;amount=5.00' target='_blank' class='closebutton'>Donate $5.00</a></li><li style='margin-bottom: 8px; font-size: 14px'><a href='https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&amp;business=info@energytalkradio.com&amp;item_name=Energy+Talk+Radio+Donation&amp;currency=USD&amp;amount=10.00' target='_blank' class='closebutton'>Donate $10.00</a></li><li style='margin-bottom: 8px; font-size: 14px'><a href='https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&amp;business=info@energytalkradio.com&amp;item_name=Energy+Talk+Radio+Donation&amp;currency=USD&amp;amount=25.00' target='_blank' class='closebutton'>Donate $25.00</a></li><li style='margin-bottom: 8px; font-size: 14px'><a href='https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&amp;business=info@energytalkradio.com&amp;item_name=Energy+Talk+Radio+Donation&amp;currency=USD' target='_blank' class='closebutton'>Donate Selected Amount</a></li><li><a href='#' class='closebutton'>Not right now</a></li></ul></td></tr></table>"
	}).inject( box );

	box.inject($(document.body));

	var overlay = new Element('div', { 
		'id': 'backOverlay',
		'styles': { 'opacity':'0','visibility':'visible' }
	}).inject($(document.body));

	overlay.setStyles({ 'top': -$(window).getScroll().y,'height':$(window).getScrollSize().y+$(window).getScroll().y });
	overlay.tween('opacity', 0.6);//onComplete: center.tween opacity

	// Wait till overlay is completed
	overlay.get('tween').addEvent('onComplete',function(){
		box.setStyle('display','block'); // Display first to get size
		box.setStyles({ 'top': (($(window).getSize().y-box.getSize().y)/2)+$(window).getScroll().y, 'left': ($(window).getSize().x-box.getSize().x)/2, 'visibility': 'visible' });
	});


	$$('.closebutton').each(function(el){
		el.addEvent('click',function(e){
			box.dispose();
			overlay.dispose();
			if( !fromButton ){
				Cookie.write('donate', 'no', {duration: 20});
			}
		});
	});
}

