window.addEvent('domready', function() {
	$$('input.defaultValue').each(function(el) {
		var defaultValue = el.value;
		if(defaultValue) {
			el.addEvent('focus', function() {
				if(el.value == defaultValue)
					el.value = '';
			});
			el.addEvent('blur', function() {
				if(el.value == '')
					el.value = defaultValue;
			});
		}
	});
});

var DefaultValueOverlay = new Class({
    /*
     * A simple encapsulation of the usual nicer method of having default 
     * values in input fields - to have a div overlaying the field that 
     * disappears when either it or the field is clicked.
     */
    initialize : function(overlayId, inputId) {
	this.overlayId = overlayId;
	this.inputId = inputId;
	// Check the elements exist and don't do anything if they don't
	if ($(overlayId) && $(inputId)) {
	    $(overlayId).addEvent('click', this.hideOverlay.bind(this));
	    $(inputId).addEvent('click', this.hideOverlay.bind(this));
	    $(inputId).addEvent('blur', this.showOverlay.bind(this));
	    // Check the visibility of the overlay is correct to start with
	    if ($(inputId).value != '') {
		this.hideOverlay();
	    }
	}
    },
    
    showOverlay : function() {
	// Only show it if the field is still empty
	if ($(this.inputId).value == '') {
	    $(this.overlayId).setStyle('display','');
	}
    },
    
    hideOverlay : function() {
	$(this.overlayId).setStyle('display','none');
	$(this.inputId).focus();
    }
});

/* Add a special lightbox popup event to the large photo */
window.addEvent('domready', function() {

	var closeButton = new ModalDialog.Button(new Element('a', {'href': '#', 'class': 'modal_button_close', 'text': 'Close'}));

    $$('a[rel=lightbox_photo]').addEvent('click', function(event) {
        event.stop();
        var link = $(event.target);
        if(link.tagName.toLowerCase() != 'a')
            link = link.getParent('a');
        if(!link)
            return;
        var url = link.href + '?initial=' + $('largeImage').src;
        new ModalDialog(ModalDialog.AjaxLoader(url, null, null, true), {
        	topButtons: [closeButton],
        	bottomButtons: [],
        	height: 'auto',
        	width: '750px'
        });
    });
    $$('a[rel=lightbox]').addEvent('click', function(event) {
        event.stop();
        var link = $(event.target);
        if(link.tagName.toLowerCase() != 'a')
            link = link.getParent('a');
        if(!link)
            return;
        var url = link.href;
        new ModalDialog(ModalDialog.AjaxLoader(url, null, null, true), {
        	topButtons: [closeButton],
        	bottomButtons: [],
        	maxHeight: 400,
        	width: '750px'
        });
    });
});

