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

	// 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)
		});
	});
}
