﻿document.observe("pop:PostPage", function(e) {
	var emailButton = $("btn_notifyMe");
	if (emailButton) {
		$("page").observe("click", function(e) {
			if (e.element().id == "btn_notifyMe") {
				Page.doAsyncPostBack(e);
			}
		});
	}
	
	bindEmailAddressLabel();
});

function bindEmailAddressLabel() {
	new InPlaceInputLabel("ctl00_txt_email");
}

sIFR.activate(H1, bucketH2);
sIFR.replace(H1, { selector: 'h1' });
sIFR.replace(bucketH2, { selector: 'div.bucket h2' });

var InPlaceInputLabel = Class.create({
	initialize: function(sInputID) {
		this.input = $(sInputID);
		this.lbl = $$('label[for='+ sInputID +']')[0];

		if (this.input && this.lbl) {
			this.form = this.input.up('form');
			this.input.setAttribute('label',this.lbl.innerHTML.strip());
			if (this.input.getAttribute('type') == 'password') {
				this.ispassword = true;
				this.input.setAttribute('ispassword','true');
				this.input.setAttribute('type','text');
			}
			
			this.input.observe('focus',this.__Focus.bindAsEventListener(this));
			this.input.observe('blur',this.__Blur.bindAsEventListener(this));
			this.form.observe('submit',this.__Submit.bindAsEventListener(this));

	    	this.updateLabel();
		}
	},
	/**
	 * Hides the 'label' text for the field, if the text has not changed
	 */
	hideLabel: function() {
		if (this.input.value.strip() == this.input.getAttribute('label')) {
			this.input.value = ''; // removes label text
			this.input.removeClassName('label');
			if (this.ispassword) {
				this.input.setAttribute('type','password');
			}
		}
	},
	/**
	 * Sets the value of the label to the LABEL text, if no othe value is in place.
	 */
	updateLabel: function() {
	    if (this.input.value.strip().length == 0) {
			this.input.value = this.input.getAttribute('label');
			this.input.addClassName('label');
			if (this.ispassword) {
				this.input.setAttribute('type','text');
			}
	    }
	},
	__Focus: function(e) {
		this.hideLabel();
	},
	__Blur: function(e) {
		this.updateLabel();
	},
	__Submit: function(e) {
		this.hideLabel();
	}
});
