﻿///<reference path="~/scripts/jquery.intellisense.js" />
///<reference path="~/scripts/true_core.js" />

//<![CDATA[

if (!window.Legacy) {
	Type.registerNamespace("Legacy");
}

/******************************/
//Legacy.FocusToggle
/******************************/
Legacy.FocusToggle = function(p_el) {
	Legacy.FocusToggle.constructBase(this, [p_el]);
	this.initialize();
}

Legacy.FocusToggle.prototype = {
	initialize: function() {
		var initArgs = eval('(' + this.jq.attr("initArgs") + ')');
		this._defaultVal = initArgs.defaultVal;
		if (this.jq.val() == '') {
			this.jq.val(this._defaultVal);
		}
	},
	dispose: function() {
		Legacy.FocusToggle.callBaseMethod(this, 'dispose');
	},
	handle_focus: function() {
		if (this.jq.val() == this._defaultVal) {
			this.jq.val('');
		}
	},
	handle_blur: function() {
		if (this.jq.val() == '') {
			this.jq.val(this._defaultVal);
		}
	}
}

Legacy.FocusToggle.register("Legacy.FocusToggle", True.Behavior);


/******************************/
//Legacy.ContactForm - fields, processing, confirmation
/******************************/
Legacy.ContactForm = function(p_el) {
	Legacy.ContactForm.constructBase(this, [p_el]);
	this.initialize();
}

Legacy.ContactForm.prototype = {
	initialize: function() {
		var initArgs = eval('(' + this.jq.attr("initArgs") + ')');
	},
	dispose: function() {
		Legacy.ContactForm.callBaseMethod(this, 'dispose');
	},
	handle_click: function(p_e) {
		if ($(p_e.target).hasClass('js-send')) {
			p_e.preventDefault();
			p_e.stopPropagation();
			var vals = { date: '', email: '', name: '', phone: '', template: '' };
			for (var p in vals) {
				var item = $("#" + p, this.element);
				var focusToggle = item.getTrueTypes({ typeName: 'Legacy.FocusToggle' });
				if (!focusToggle || focusToggle._defaultVal != item.val()) {
					var input = $("#" + p, this.element);
					if (input.length > 0) {
						vals[p] = $("#" + p, this.element).val();
					}
				}
			}

			if (vals.name == '' || (vals.email + vals.phone) == '') {
				alert('Please be sure to enter your name and an email address or phone number.');
			}
			else {
				this._completeCount = 0;
				this._processdata = undefined;

				var self = this;
				$.post("/EmailContact.ashx", vals, function(data) {
					self._processingComplete(data);
				}
				);

				$(".fields", this.element).hide();
				$(".processing", this.element).show();
				setTimeout(function() { self._processingComplete(); }, 1000);
			}
		}
	},
	_processingComplete: function(p_data) {
		this._completeCount++;
		if (p_data) {
			this._processdata = p_data;
		}
		if (this._completeCount == 2 && this._processdata) {
			$(".processing", this.element).hide();
			$(".confirmation", this.element).show();
		}
	}
}

Legacy.ContactForm.register("Legacy.ContactForm", True.Behavior);

/******************************/
//Legacy.Expander
/******************************/
Legacy.Expander = function(p_el) {
	Legacy.Expander.constructBase(this, [p_el]);
	this.initialize();
}

Legacy.Expander.prototype = {
	initialize: function() {
		this.jq.find(".js-action-trigger").click(Function.createDelegate(this._trigger, this)).end();
	},
	dispose: function() {
		Legacy.Expander.callBaseMethod(this, 'dispose');
	},
	_trigger: function() {
		var open = Boolean($(".js-expand:visible", this.element).length);
		var self = this;
		var action = 'slideDown';
		var showClass = '.js-expanded';
		var hideClass = '.js-closed';
		if(open){
			action = 'slideUp';
			showClass = '.js-closed';
			hideClass = '.js-expanded';
		}
		this.jq.find(".js-expand")[action](200, function(){
			$(showClass, self.element).show();
			$(hideClass, self.element).hide();
		}).end();
	}
}

Legacy.Expander.register("Legacy.Expander", True.Behavior);

//]]>