Rckp.SignUp = {

	/** The animation for the email signup layer @type Df.Animate */
	animator: null,
	/** Elements used by this NameSpace */
	$elements: {
		form: "emailForm",
		wrapper: 'cEmailSignUpBlk',
		closeBtn: 'cEmailFormCloseBtn',
		btn: 'sEmailUpdates'
    },
	visible: false,
    /** Perform when dom is loaded and ready */
	load: function() {
		this.animator = Rckp.animate( this.$elements.wrapper, 'height:1px','height:360px', 500, 25 );
		this.$elements.btn.observe('click', this.toggle.bind(this) );
		this.$elements.closeBtn.observe('click', this.hide.bind(this) );
    },
	formErrors: [],
	validateForm: function() {
		this.formErrors = [];
		var email1 = $("emailAddress");
		var email2 = $("cEmailSignUpEmailConfirmInp");

		email1.removeClassName("hasError");
		email2.removeClassName("hasError");

		if( !validEmail(email1.getValue()) ) {
			this.formErrors.push("Please specify a valid email address for the field 'Email Address'.");
			email1.activate();
			email1.addClassName("hasError");
		}
		if( !validEmail( email2.getValue() ) ) {
			this.formErrors.push("Please specify a valid email address for the field 'Confirm Email Address'.");
			if (validEmail(email1.getValue())) { email2.activate()};
			email2.addClassName("hasError");
		}
		if (validEmail(email1.getValue()) && validEmail(email2.getValue()) ){
			if( email1.getValue() !== email2.getValue() ) {
				this.formErrors.push("Your 'Email' and 'Confirm Email' must be identical.");
				email1.activate();
				email1.addClassName("hasError");
				email2.addClassName("hasError");
			}
		}
		return this.formErrors.length == 0;
	},
	submitForm: function() {
		var form = this.$elements.form;
		if( !this.validateForm() ) {
			alert( this.formErrors.join("\r\n") );
			return false;
		}
		var req = form.request( { onComplete: this.showThanks.bind(this) } );

      return false;
    },
	showThanks: function() {
		//$("emailAddress").value = $("cEmailSignUpEmailConfirmInp").value = "";

		this.hide();
		setTimeout( function() { Rckp.SignUpThanks.show(); }, 300 );
    },
	toggle: function() {
		return this.visible ? this.hide() : this.show();
    },
    show: function() {
		// TODO: if emailthanks open, close it
		$("emailForm").style.visibility="hidden";
		this.visible = true;
		this.animator.play();
		setTimeout( function() { 
			Rckp.SignUp.$elements.wrapper.style.overflow = 'auto'; 
			$("emailForm").style.visibility="visible";      	
		}, 700 );
		if (!isIE6) {
			$("sEmailUpdates").style.backgroundPosition = "bottom left";
		}
	},
	hide: function() {
		this.visible = false;
		$("emailForm").style.visibility="hidden";
		this.animator.reverse();
		this.$elements.wrapper.setStyle({overflow:"visible"});
		if (!isIE6) {
			$("sEmailUpdates").style.backgroundPosition = "top left";
		}
	}
};

Rckp.SignUpThanks = {

	/** The animation for the email signup layer @type Df.Animate */
	animator: null,
    /** Elements used by this NameSpace */
	$elements: {
		wrapper: 'cEmailSignUpThanksBlk',
		closeBtn: 'cEmailFormThanksCloseBtn'
    },
    visible: false,
    /** Perform when dom is loaded and ready */
    load: function() {
		this.animator = Rckp.animate( this.$elements.wrapper, 'height:1px','height:360px', 500, 25 );
		this.$elements.closeBtn.observe('click', this.hide.bind(this) );
    },
    toggle: function() {
		return this.visible ? this.hide() : this.show();
    },
    show: function() {
		this.visible = true;
		this.animator.play();
		
		var emailAdd = $("emailAddress").value;
		var contest = $("contest").value;
		var month = $("month").options[$("month").selectedIndex].value;
		var gender = Form.getInputs("emailForm","radio","cEmailSignUpGenderRad").find(function(radio) { return radio.checked; }).value;
		var zip = $("zip").value;
		var collections = $("cEmailSignUpCollectionSel").options[$("cEmailSignUpCollectionSel").selectedIndex].value;
		
		$("eDialog").update("<img src=\"https://secure.ed4.net/GSI/dualpost/?c=11220&e=" + emailAdd + "&src=" + contest + "&b=" + gender + "&d=" + month + "&f=" + zip + "&g=" + collections + "&laY\" width=\"1\" height=\"1\" />");
		setTimeout( function() { Rckp.SignUpThanks.hide(); }, 7000 );
      
		if (!isIE6) {
			$("sEmailUpdates").style.backgroundPosition = "bottom left";
		}
		// TODO: hide after 10 seconds
	},
	hide: function() {
		this.visible = false;
		this.animator.reverse();
		if (!isIE6) {  
			$("sEmailUpdates").style.backgroundPosition = "top left";
		}
	}
};
