﻿

var SEARCH = {

	data: {
		relatedSpecialties: 'RelatedSpecID',
		specialties: 'SpecID',
		selectedRelated: 'selectedRelSpec',
		refineSearch: 'RefineSearch',
		keyword: 'keyword',
		AdvancedSearch: 'AdvancedSearch',
		hideOptions: 'hideOptions',
		showOptions: 'showOptions',
		relSpecLabel: 'relSpecLabel'
	},

	init: function() {

		this.data.specialties = document.getElementById(this.data.specialties);
		this.data.relatedSpecialties = document.getElementById(this.data.relatedSpecialties);
		this.data.refineSearch = document.getElementById(this.data.refineSearch);
		this.data.keyword = document.getElementById(this.data.keyword);
		this.data.relSpecLabel = document.getElementById(this.data.relSpecLabel);
		if (this.data.relSpecLabel) {
			this.data.relSpecLabel.style.display = 'none';
		}

		this.data.AdvancedSearch = document.getElementById(this.data.AdvancedSearch);
		this.data.showOptions = document.getElementById(this.data.showOptions);
		this.data.hideOptions = document.getElementById(this.data.hideOptions);
		if (this.data.refineSearch) {
			this.data.selectedRelated = document.getElementById(this.data.selectedRelated).value;
			this.doChange_relatedSpecialties();
		}

		Event.addListener(this.data.showOptions, 'click', this.setAdvancedSearch, this, true);
		Event.addListener(this.data.hideOptions, 'click', this.hideAdvancedSearch, this, true);
		Event.addListener(this.data.specialties, 'change', this.doChange_relatedSpecialties, this, true);

		this.data.AdvancedSearch.style.display = 'none';
		this.data.hideOptions.style.display = 'none';
	},


	doChange_relatedSpecialties: function() {

		var specID = this.data.specialties,
		kw = this.data.keyword.value,
		url = '/getSpecialtiesByService.asp';

		if (specID.options) {
			specID = specID.options[specID.selectedIndex].value;
		} else {
			specID = specID.value;
		}
		specID = parseInt(specID, 10);

		if (kw.length > 0) {
			//Just move along.
		} else {
			if (isNaN(specID)) {
				this.data.relatedSpecialties.selectedIndex = 0;
				this.data.relatedSpecialties.options.length = 1;
				this.data.selectedRelated = null;
				return;
			}
		}

		url += '?specID=' + encodeURIComponent(specID) + '&keyword=' + encodeURIComponent(kw);

		if (this.data.refineSearch) {
			YAHOO.util.Connect.asyncRequest('GET', url, {
				scope: this,
				success: this.setRefineRelatedSpecialties
			});
		} else {
			YAHOO.util.Connect.asyncRequest('GET', url, {
				scope: this,
				success: this.setRelatedSpecialties
			});
		}

	},

	setRefineRelatedSpecialties: function(o) {
		var specs = o.responseText,
		div = this.data.relatedSpecialties,
		arr = this.data.selectedRelated,
		arr = arr.split(','),
		x, c, l, ul, li, i;
		
		specs = Lang.JSON.parse(specs);
		if (specs.length > 0) {
			this.data.relSpecLabel.style.display = 'block';
			ul = document.createElement('ul');
			for (x = 0; x < specs.length; x++) {
				li = document.createElement('li');
				c = document.createElement('input');
				c.type = 'checkbox';
				c.id = 'relatedSpecID-' + specs[x].id;
				c.value = specs[x].id;
				c.name = 'relatedSpecID';
				for (i = 0; i < arr.length; i++) {
					if (c.value === arr[i].replace(' ', '')) {
						c.checked = true;
					}
				}
				l = document.createElement('label');
				l.htmlFor = c.id;
				l.innerHTML = specs[x].text;
				li.appendChild(c);
				li.appendChild(l);
				ul.appendChild(li);
			}
			div.appendChild(ul);

		}
	},

	setRelatedSpecialties: function(o) {

		var specs = o.responseText,
		select = this.data.relatedSpecialties,
		current, x, found = false;

		specs = Lang.JSON.parse(specs);

		current = this.data.selectedRelated;
		if (current) {
			current = parseInt(current.value, 10);
		}
		select.selectedIndex = 0;
		select.options.length = 1;

		if (specs.length > 0) {
			for (x = 0; x < specs.length; x++) {
				select[select.options.length] = new Option(specs[x].text, specs[x].id);

				if (specs[x].id === current) {
					select.selectedIndex = select.options.length - 1;
					found = true;
				}
			}

			if (!found) {
				this.data.selectedRelated = null;
			}
		} else {
			select.selectedIndex = select.options.length - 1;
			select[select.options.length] = new Option('No related specialties available.', '');
			select.selectedIndex = 1;
		}

	},

	setAdvancedSearch: function() {
		this.data.AdvancedSearch.style.display = 'block';
		this.data.showOptions.style.display = 'none';
		this.data.hideOptions.style.display = 'block';
	},
	hideAdvancedSearch: function() {
		this.data.AdvancedSearch.style.display = 'none';
		this.data.showOptions.style.display = 'block';
		this.data.hideOptions.style.display = 'none';
	},

	clearForm: function() {
		var t = document.getElementsByTagName('input'), s = document.getElementsByTagName('select'), i;
		for (i = 0; i < t.length; i++) {
			if (t[i].type === 'text') {
				t[i].value = '';
			}
			if (t[i].type === 'checkbox') {
				t[i].checked = false;
			}
		}
		for (i = 0; i < s.length; i++) {
			s[i].selectedIndex = 0;
		}

	},

	newwindow: function(href) {
		var top, left;
		top = screen.availWidth / 3;
		left = 0;
		window.open(href, 'resource', 'width=800,height=800,resizable=1,scrollbars=1, top=' + top + ', left=' + left + '');
	},

	closewindow: function(href) {
		href.close();
	},

	redirectParent: function(pageID) {
		window.opener.parent.location = 'Default.asp?PageID=' + pageID + '&P=Y';
	},

	displayBio: function(e, id, photoid) {
		var x = Event.getPageX(e), y = Event.getPageY(e), d, i, s;

		d = document.getElementById('bio-' + id);
		d.style.display = 'block';
		d.style.zIndex = '100';
		d.style.width = '345px';
		d.style.zIndex = '100';
		d.style.position = 'absolute';
		d.style.overflow = 'hidden';
		d.style.backgroundColor = '#DFDED8';
	},


	hideBio: function(id) {
		var d;
		d = document.getElementById('bio-' + id);
		d.style.display = 'none';
	}

};
Event.onDOMReady(SEARCH.init, SEARCH, true);








/*YAHOO.widget.Tooltip.prototype.doShow = function(e, context) {
	
	var yOffset = 25,
        me = this;

	if (YAHOO.env.ua.opera && context.tagName &&
        context.tagName.toUpperCase() == "A") {
		yOffset += 12;
	}

	return setTimeout(function() {

		var txt = me.cfg.getProperty("text");

		// title does not over-ride text
		if (me._tempTitle && (txt === "" || YAHOO.lang.isUndefined(txt) || YAHOO.lang.isNull(txt))) {
			me.setBody(me._tempTitle);
		} else {
			me.cfg.refireEvent("text");
		}

		me.moveTo(me.pageX + 50, me.pageY + yOffset);

		if (me.cfg.getProperty("preventoverlap")) {
			me.preventOverlap(me.pageX + 50, me.pageY);
		}

		YAHOO.util.Event.removeListener(context, "mousemove", me.onContextMouseMove);

		me.contextTriggerEvent.fire(context);

		me.show();

		me.hideProcId = me.doHide();

	}, this.cfg.getProperty("showdelay"));
};

function initToolTip() {
	myTooltip = new YAHOO.widget.Tooltip("bioTooltip", {
	context: "PhysName",
		text: "<p align='left'><strong>Why are we asking you this?</strong><br />Bloomington Hospital wishes to respect your religious/spiritual needs, so we need to be aware of any affiliations you may have.</p>",
		width: "300px",
		showDelay: 200
	});
}

YAHOO.util.Event.onDOMReady(initToolTip); */


