
window.addEvent('domready', function () {
		
	$('blackout').setStyle('opacity', 0);
	$('blackoutLevel2').setStyle('opacity', 0);
	
    addElementHelp();

    setupDates();

    addConfirmationToLinks();
    
    makeCaptchas();
	
});



var addBlackout = function (level) {

	var size = window.getScrollSize();

	var depth = level == 1 ? 500 : 1500;

	var style = 'opacity: 0; background: #000000; position: absolute; top: 0px; left: 0px; overflow: hidden; z-index: ' + depth + '; display: block;';
	style += ' height: ' + size.y + 'px; width: ' + size.x + 'px;';

	var blackout = level == 1 ? $('blackout') : $('blackoutLevel2');

	if (blackout.getStyle('opacity') == 0) {
		blackout.set('style', style);
		blackout.fade(0.7);
	}
	else {
		// Just ensure it's the right size.
		blackout.setStyle('width', size.x);
		blackout.setStyle('height', size.y);
	}
}

var removeBlackout = function (level) {
	var blackout = level == 1 ? $('blackout') : $('blackoutLevel2');
	blackout.fade(0);
}

function setupDates() {
    $$('.date input').each(function (el) {
        if (typeof DatePicker == "function") {
			new DatePicker(el);
		}
		else if (typeof CalendarEightysix == "function") {
			new CalendarEightysix(el);
		}
		else if (typeof vlaDatePicker == "function") {
			new vlaDatePicker(el);
		}
		else {
			new calendarInput(el)
		}
    });
}

// Add the popup help to elements.
function addElementHelp() {
    $$('.openHelp').each(function (el) {
        el.addEvent('click', function () {
            var field = el.get('id').replace('helpButtonFor', '');
            var helpContents = $('helpFor' + field).get('html');
            $('helpPopup').fade(1);
            $('helpPopup').setStyle('display', 'block');
            var yPos = el.getPosition().y - 20;
            $('helpPopup').setPosition({ x: 100, y: yPos });
            $('helpContents').set('html', helpContents);
            addBlackout(2);
        });
    });

    $$('.closePopup').each(function (el) {
        if (el.get('id') != 'CloseAjaxForm') {
            el.addEvent('click', function () {
                var popup = el.getParent().getParent();
                popup.fade(0);
                removeBlackout(2);
            })
        }
    });
}

// Any link with the 'confirmLink' class should
// open a confirmation dialogue before calling the
// link.
function addConfirmationToLinks() {
    $$('.confirmLink').each(function (el) {

        // If it's an Ajax link, then we need to temporarily remove the event.
        // We'll store it in tempEl for safe keeping.
        if (el.hasClass('AjaxNonFormLink')) {
            var tempEl = new Element('a');
            tempEl.cloneEvents(el, 'click');
            el.removeEvents();
        }
        el.addEvent('click', function () {
            var rtn = confirm("Are you sure?");

            // Put the event back.
            if (rtn && el.hasClass('AjaxNonFormLink')) {
                el.removeEvents();
                el.cloneEvents(tempEl, 'click');
                el.fireEvent('click');
                return false;
            }

            return rtn;
        });
    });
}

// Give labels to captchas.
function makeCaptchas(el) {
    $$('.innocentLookingSpan').each(function (el) {
        var code = 3559 * Math.floor(Math.random() * 9999)
        code += 25832;
        if (el.get('html').search('[[thecode]]') == -1) {
            el.set('html', 'To help us reduce spam, please enter [[thecode]] into the box below.');
        }
        el.set('html', el.get('html').replace('[[thecode]]', "<span style='font-size:1.3em;'>" + code + '</span>'));
    });
}

function fadeAndHide(el) {
    el.fade(0);
    setTimeout(function () { el.setStyle('display', 'none'); }, 700);
}

function fadeAndShow(el) {
    el.setStyle('display', 'block');
    el.fade(1);
}
