﻿///<reference path="~/scripts/jquery.intellisense.js" />
///<reference path="~/scripts/true_core.js" />

//<![CDATA[

if (!window.CS) {
	Type.registerNamespace("CS");
}

if (!window.CS.FocusToggle) {

	CS.FocusToggle = function(p_el) {
		CS.FocusToggle.constructBase(this, [p_el]);
		this.initialize();
	}

	CS.FocusToggle.prototype = {
		initialize: function() {
			var initArgs = eval('(' + this.jq.attr("initArgs") + ')');
			this._class = initArgs.className;
			if (this.jq.val() != '') {
				this.jq.trigger('focus');
			}
		},
		dispose: function() {
			CS.FocusToggle.callBaseMethod(this, 'dispose');
		},
		handle_focus: function() {
			this.jq.removeClass(this._class);
		},
		handle_blur: function() {
			if (this.jq.val() == '') {
				this.jq.addClass(this._class);
			}
		}
	}

	CS.FocusToggle.register("CS.FocusToggle", True.Behavior);

	CS.HeaderDirections = function(p_el) {
		CS.HeaderDirections.constructBase(this, [p_el]);
		var self = this;
		this._href = $('.js-button', this.element).click(function(p_e) {
			self._onButtonClick(p_e);
		}).attr('href');
		this.element.href = "javascript:;";
		this._directionsJQ = $('.js-directions');
		if (this._directionsJQ.length == 0) {
			$(this.element).show();
		}
	}

	CS.HeaderDirections.prototype = {
		handle_keydown: function(p_e) {
			if (p_e.keyCode == 13) {
				this._onButtonClick(p_e);
				p_e.preventDefault();
				p_e.stopPropagation();
			}
		},
		_onButtonClick: function(p_e) {
			var from = $('.js-headerAddress').val();
			if (from != '') {
				var loqJQ = $(":trueType('CS.Location')");
				if (loqJQ.length == 0) {
					if (p_e.target.href) {
						p_e.target.href = this._href + '?from=' + encodeURIComponent(from);
					}
					else {
						location.href = this._href + '?from=' + encodeURIComponent(from);
					}
				}
				else {
					var control = loqJQ.getTrueTypes({ type: CS.Location });
					control.getDirections(from);
				}
			}
			p_e.stopPropagation();
		}
	}

	CS.HeaderDirections.register("CS.HeaderDirections", True.Behavior);
}

//]]>