if (!window['VTLS']) window['VTLS'] = {};
VTLS.API_KEY = 'sX3wnGsYTYs7bzrbrYSyWOZ5XOYNfpw6';
VTLS.API_DOMAIN = 'righthealth.com';
VTLS.VWS_BASE_URL = 'http://api.vitals.com';
VTLS.VWS_VITALS_BASE_URL = 'http://www.vitals.com';
VTLS.WIDGET_BASE_URL = 'http://api.vitals.com/client/vdeck/0.9';
VTLS.VUtil = {
	'$' : function (id) {
		return document.getElementById(id);
	},
	'hasClass' : function (el, c) {
		return el.className.match(new RegExp('(\\s|^)' + c + '(\\s|$)')) !== null ? true : false;
	},
	'addClass' : function (el, c) {
		if (!this.hasClass(el, c)) el.className += ' ' + c;
	},
	'removeClass' : function (el, c) {
		if (!this.hasClass(el, c)) return;
		el.className = el.className.replace(new RegExp('(\\s|^)' + c + '(\\s|$)'), ' ');
	},
	'replaceClass' : function (el, c1, c2) {
		this.removeClass(el, c1);
		this.addClass(el, c2);
	},
	'isElement' : function (el) {
		return !!(el && el.nodeType && el.nodeType == 1);
	}
}
VTLS.VDeckRes = {
	'specialist' : {
		'164':'Addiction Medicine Specialist',
		'165':'Adolescent Specialist',
		'101':'Allergy & Immunology Specialist',
		'128':'Anesthesiologist (pain control)',
		'135':'Cardiologist (heart)',
		'131':'Cardiothoracic Surgeon',
		'166':'Clinical Pharmacologist',
		'104':'Colorectal Surgeon (colon & rectum)',
		'163':'Critical Care Specialist',
		'105':'Dermatologist (skin)',
		'167':'Emergency Physician',
		'136':'Endocrinologist (diabetes, hormones)',
		'107':'Family Practitioner',
		'137':'Gastroenterologist (digestive)',
		'108':'General Practitioner',
		'112':'Geneticist',
		'134':'Geriatric Specialist (elderly care)',
		'168':'Hematology Specialist',
		'169':'Hepatologist',
		'138':'Infectious Disease Specialist (virus, bacteria, parasites)',
		'110':'Internist',
		'139':'Nephrologist (kidney)',
		'113':'Neurological Surgeon',
		'140':'Neurologist (brain, nervous system)',
		'172':'Nuclear Medicine Specialist',
		'117':'Obstetrician & Gynecologist',
		'141':'Oncology Specialist (cancer)',
		'118':'Ophthalmologist (eyes)',
		'119':'Oral & Maxillofacial Surgeon',
		'120':'Orthopaedic Surgeon',
		'173':'Osteopath',
		'121':'Otolaryngologist (ear, nose, throat)',
		'147':'Pain Management Specialist',
		'174':'Pathologist',
		'142':'Pediatric / Adolescent Psychiatrist',
		'187':'Pediatric Specialist',
		'143':'Pediatric Surgeon',
		'123':'Pediatrician',
		'146':'Phlebologist',
		'124':'Physiatrist (physical, rehabilitation)',
		'125':'Plastic Surgery Specialist',
		'161':'Preventive Medicine / Wellness',
		'144':'Psychiatrist',
		'145':'Pulmonologist (lungs)',
		'189':'Radiologist',
		'190':'Rheumatologist',
		'191':'Sleep Medicine Specialist',
		'116':'Sports Medicine Specialist',
		'130':'Surgical Specialist',
		'133':'Urologist'
	}
}

VTLS.VDeckControl = function (ploc, psid) {
	if (!ploc) ploc = '';
	if (!psid) psid = 0;
	this.values = {'location': ploc, 'specialist_id': psid};
	this.el = {};
	this.searching = false;
	this.rs = null;
	this.page = 0;
	this.perpage = 5;
	this.rccordion = [];
	if (!VTLS.VDeckControl.hasOwnProperty('collection')) VTLS.VDeckControl.collection = [];
	VTLS.VDeckControl.collection.push(this);
	this.instance = VTLS.VDeckControl.collection.length - 1;
}

VTLS.VDeckControl.onResults = function(rs) {

	if (!rs.response || !rs.response.results) return;
	if (!rs.hasOwnProperty('jsonp_i')) return;
	var instance = parseInt(rs['jsonp_i']);
	rs = rs.response.results;
	
	if (instance >= VTLS.VDeckControl.collection.length || instance < 0) return;
	var searchctl = VTLS.VDeckControl.collection[instance];
	if (searchctl.el.status.tmr) clearInterval(searchctl.el.status.tmr);

	for (var r in rs) if (rs.hasOwnProperty(r)) {
		r = rs[r];
		r.review = r.review ? r.review.split(String.fromCharCode(30)) : null;
		if (r.review) r.review = (function(arrv, arrk) {
			var ob = {};
			for (var i = 0, l = arrv.length; i < l; i++) ob[arrk[i]] = arrv[i];
			return ob;
		})(r.review, ['author','rating','title','comment','date']);
		r.highlight = r.highlight ? r.highlight.split(',') : [];
		//r.display_title = VTLS.VDeckRes.specialist[searchctl.values.specialist_id];
		if (r.display_title.length < 1 && r.abms_titles.length > 0) {
			a = r.abms_titles;
			a = a.split(';');
			speca = []; specn = [];
			for (j = 0, l = a.length; j < l; j++) {
				c = a[j].charAt(0);
				s = a[j].substring(1);
				if (c == '@') speca.push(s);
				else specn.push(s);
			}
			r.display_title = speca.length ? speca.join(', ') : (specn.length ? specn.join(', ') : '');
		}
		if (r.display_title.indexOf(VTLS.VDeckRes.specialist[searchctl.values.specialist_id]) == -1)
			r.display_title = VTLS.VDeckRes.specialist[searchctl.values.specialist_id] + (r.display_title ? ', ' + r.display_title : '');
		delete r.abms_titles;
	}

	//searchctl.el.status.innerHTML = '';
	searchctl.page = 0;
	if (searchctl.onResults && typeof searchctl.onResults == 'function') searchctl.onResults(rs);
	searchctl.el.submit.disabled = false;
	searchctl.el.submit.style.visibility = 'hidden';
	searchctl.el.submit.value = 'Search';
	searchctl.searching = false;
}

VTLS.VDeckControl.prototype.search = function () {
	if (this.searching) return;
	this.searching = true;
	//this.el.submit.style.visibility = 'hidden';
	this.el.submit.disabled = true;
	this.el.result.innerHTML = '';
	(function(stat){
		stat.tmr = setInterval(function() {
			if (!stat.i || stat.i > 4) stat.i = 1;
			var s = 'Searching';
			for (var i = 1; i < 5; i++) s += (i < stat.i ? '.' : ' ');
			stat.i++;
			stat.value = s;
		}, 100);
	})(this.el.status);
	var q  = '?key=' + VTLS.API_KEY + '&app_id=5&method=find&distance=5&review=1&jsonp_cb=VTLS.VDeckControl.onResults&jsonp_i=' + this.instance + '&location=' + encodeURIComponent(this.values.location) + '&specialist_id=' + encodeURIComponent(this.values.specialist_id);
	var scr = document.createElement('script');
	scr.setAttribute('type', 'text/javascript');
	scr.src = VTLS.VWS_BASE_URL + '/' + q + '&' + (new Date().getTime().toString());
	if (this.el.script != null)
		document.getElementsByTagName('head').item(0).replaceChild(scr, this.el.script)
	else
		document.getElementsByTagName('head').item(0).appendChild(scr);
	this.el.script = scr;
}

VTLS.VDeckControl.prototype.onResults = function (rs) {
	if (!rs) return;
	this.rs = rs;

	var r = null;
	var s = '';
	var t = 0;
	var l = rs.length;
	this.rccordion = [];
	var rp = this.renderPagination(this.page, l, this.perpage);
	if (rp === false) return;
	this.el.result.innerHTML = '';
	this.el.result.appendChild(rp);
	var si = this.page * this.perpage;
	for (var i = si; i < si + this.perpage; i++) {
		if (i >= l) break;
		r = this.renderResult(rs[i]);
		this.el.result.appendChild(r);
		t = 1 - t;
	}
	if (l > 0) {
		var rp2 = this.renderPagination(this.page, l, this.perpage);
		this.el.result.appendChild(rp2);
	}
	if (this.rccordion.length > 0) {
		var r1 = this.rccordion[0];
		VTLS.VUtil.addClass(r1.bar, 'vdeck-result-current');
		r1.bar.style.backgroundColor = '#0e998c';
		r1.det.style.display = 'block';
	}


}

VTLS.VDeckControl.prototype.renderPagination = function (curpage, count, perpage) {
	var self = this;
	var numpages = Math.ceil(count / perpage);
	var div = document.createElement('div');
	div.className = 'vdeck-page';
	div.style.marginBottom = '6px';

	if (numpages < 1) 	{
		div.style.textAlign = 'center';
		div.innerHTML =  'No results';
		return div;
	}
	if (curpage < 0 || curpage >= numpages) return false;
	
	var a;
	var divprev = document.createElement('div');
	divprev.className = 'vdeck-page-prev';
	divprev.style.textAlign = 'left';
	if (numpages > 1 && curpage > 0) {
		a = document.createElement('a');
		a.innerHTML = '&lt;&lt; Prev';
		a.href = '#';
		a.onclick = function() {
			self.page--;
			self.onResults(self.rs);
			return false;
		}
	}
	else {
		a = document.createElement('span');
		a.innerHTML = '&nbsp;';
	}
	divprev.appendChild(a);

	var divnext = document.createElement('div');
	divnext.className = 'vdeck-page-next';
	divnext.style.textAlign = 'right';
	if (numpages > 1 && curpage < numpages - 1) {
		a = document.createElement('a');
		a.innerHTML = 'Next &gt;&gt;';
		a.href = '#';
		a.onclick = function() {
			self.page++;
			self.onResults(self.rs);
			return false;
		}
	}
	else {
		a = document.createElement('span');
		a.innerHTML = '&nbsp;';
	}
	divnext.appendChild(a);
	
	var divpage = document.createElement('div');
	divpage.className = 'vdeck-page-num';
	divpage.style.textAlign = 'center';
	for (var i = 1; i <= numpages; i++) {
		if (curpage + 1 != i) {
			a = document.createElement('a');
			a.innerHTML = i + ' ';
			a.href = '#';
			a.i = i - 1;
			a.onclick = function() {
				self.page = this.i;
				self.onResults(self.rs);
				return false;
			}
		}
		else {
			a = document.createElement('span');
			a.className = 'vdeck-page-current';
			a.innerHTML = i + ' ';
			a.style.fontWeight = 'bold';
		}
		divpage.appendChild(a);
		//divpage.appendChild(document.createTextNode(' '));
	}
	
	divprev.style.width = divnext.style.width = '20%';
	divpage.style.width = '60%';

	divpage.style.styleFloat = divprev.style.styleFloat = divnext.style.styleFloat = 'left';
	divpage.style.cssFloat = divprev.style.cssFloat = divnext.style.cssFloat = 'left';
	
	div.appendChild(divprev);
	div.appendChild(divpage);
	div.appendChild(divnext);
	
	var divclear = document.createElement('div');
	divclear.style.clear = 'both';
	div.appendChild(divclear);
	return div;
}

VTLS.VDeckControl.prototype.renderResult = function (r) {
	var div = document.createElement('div');
	div.className = 'vdeck-result';
	if (!r) return div;

	div.style.marginBottom = '6px';
	
	var divbar = document.createElement('div');
	divbar.className = 'vdeck-result-caption'
	
	var divbara = document.createElement('a');
	div.bar = divbara;
	divbara.className = 'vdeck-result-caption-a';
	divbara.style.padding = '6px';
	divbara.style.display = 'block';
	divbara.style.overflow = 'hidden';
	divbara.style.whiteSpace = 'nowrap';
	divbara.setAttribute('href', '#');
	divbara.style.backgroundColor = '#a4a4a4';
	divbara.style.color = 'white';
	divbara.style.fontSize = 'larger';
	divbara.style.fontWeight = 'bold';
	divbara.style.textDecoration = 'none';
	divbara.innerHTML = r.display_name + (r.display_title ? ' &ndash; ' + r.display_title : '');
	divbara.mom = div;
	divbara.instance = this;
	divbara.onclick = function() {
		for (var i = 0, l = this.instance.rccordion.length; i < l; i++) {
			var d = this.instance.rccordion[i];
			d.bar.style.backgroundColor = '#a4a4a4';
			d.det.style.display = 'none';
		}
		this.mom.bar.style.backgroundColor = '#0e998c';
		this.mom.det.style.display = 'block';
		return false;
	}
	divbar.appendChild(divbara);
	div.appendChild(divbar);
	
	var divdet = document.createElement('div');
	divdet.className = 'vdeck-result-container';
	div.det = divdet;
	divdet.style.display = 'none';
	divbar.appendChild(divdet);
	
	var divdets = document.createElement('div');
	divdets.className = 'vdeck-result-details';
	divdets.style.padding = '6px';
	divdet.appendChild(divdets);


	var xh = '';
	var divh = document.createElement('div');
	divh.className = 'vdeck-result-block';
	divh.style.styleFloat = divh.style.cssFloat = 'left';
	divh.style.width = '38%';
	if (r.highlight.length > 0) xh += '<div class="vdeck-result-block-header">HIGHLIGHTS</div>&bull; ' + r.highlight.join('<br/>&bull; ') + '';
	divh.innerHTML = xh; xh = '';
	
	var diva = document.createElement('div');
	diva.className = 'vdeck-result-block';
	diva.style.styleFloat = diva.style.cssFloat = 'left';
	diva.style.width = '30%';
	xh += '<div class="vdeck-result-block-header">PRACTICE LOCATION</div>';
	if (r.line1) xh += r.line1 + '<br/>';
	if (r.line2) xh += r.line2 + '<br/>';
	if (r.city && r.state) xh += r.city + ', ' + r.state + '<br/>';
	diva.innerHTML = xh; xh = '';

	var divq = null, dd;
	if (r.review) {
		divq = document.createElement('div');
		divq.className = 'vdeck-result-block';
		divq.style.styleFloat = divq.style.cssFloat = 'left';
		divq.style.width = '28%';
		xh += '<div class="vdeck-result-block-header">PATIENT REVIEW</div>';
		if (r.review.title) xh += '<b>' + r.review.title + '</b> &mdash; ';
		xh += '<b></b>&quot;<i>' + r.review.comment + '</i>&quot;<br/>';
		if (r.review.author || r.review.date) {
			xh += '<div class="vdeck-result-block-note">';
			if (r.review.author) xh += r.review.author + (r.review.date ? ', ' : '');
			if (r.review.date) xh += (new Date(+r.review.date * 1000)).toDateString();
			xh += '</div>';
		}
		divq.innerHTML = xh; xh = '';
	}
	
	var divc = document.createElement('div');
	divc.style.clear = 'both';
	
	divdets.appendChild(diva);
	divdets.appendChild(divh);
	if (divq) divdets.appendChild(divq);
	divdets.appendChild(divc);
	
	
	var divdeta = document.createElement('a');
	divdeta.style.padding = '6px';
	divdeta.style.display = 'block';
	divdeta.style.fontWeight = 'bold';
	divdeta.style.textDecoration = 'none';
	divdeta.style.color = '#0e998c';
	divdeta.style.backgroundColor = '#efefef';
	divdeta.className = 'vdeck-result-bottom-a';
	divdeta.innerHTML = 'VIEW FULL PROFILE AND RATINGS AT VITALS &raquo;';
	divdeta.setAttribute('href', VTLS.VWS_VITALS_BASE_URL + '/doctors/Dr_' + r.master_name + '.html?utm_source=' + VTLS.API_DOMAIN + '&utm_medium=widget&utm_campaign=widget_partner');
	divdeta.setAttribute('target', '_blank');
	divdet.appendChild(divdeta);
	
	this.rccordion.push(div);

	return div;
	
}

VTLS.VDeckControl.prototype.renderField = function (lbl, dval, w, elin, elpar) {

	var elf = document.createElement('div');
	elf.className = 'vdeck-form-field';
	elf.style.styleFloat = elf.style.cssFloat = 'left';
	elf.style.paddingRight = '35px';
	elf.style.whiteSpace = 'nowrap';
	elf.style.height = '40px';
	
	var ellbl = document.createElement('div');
	ellbl.className = 'vdeck-field-label';
	ellbl.style.styleFloat = ellbl.style.cssFloat = 'left';
	ellbl.innerHTML = lbl + ':&nbsp;';
	elf.appendChild(ellbl);
	
	var elvalch = document.createElement('div');
	elvalch.className = 'vdeck-field-edit';
	elvalch.style.styleFloat = elvalch.style.cssFloat = 'left';
	elvalch.style.width = w + 'px';
	elf.appendChild(elvalch);
	
	var elval = document.createElement('div');
	elval.className = 'vdeck-field-value';
	elval.style.styleFloat = elval.style.cssFloat = 'left';
	elval.style.fontWeight = 'bold';
	elval.innerHTML = dval + '&nbsp;';
	elvalch.style.overflow = 'hidden';
	elvalch.appendChild(elval);

	var elok = document.createElement('input');
	elok.className = 'vdeck-field-ok';
	elok.type = 'button';
	elok.value = 'OK';
	elok.style.height = elin.style.height;
	elok.style.fontSize = '11px';
	elok.style.width = '25px';
	elok.elin = elin;
	elok.onclick = function() { if (this.elin.value != '') this.elin.ondone(); }
	
	var elcancel = document.createElement('a');
	elcancel.className = 'vdeck-field-cancel';
	elcancel.innerHTML = 'Cancel';
	elcancel.href = '#';
	elcancel.elin = elin;
	elcancel.onclick = function() {
		this.elin.value = this.elin.instance.values[this.elin.valkey];
		this.elin.ondone();
		return false;
	}
	
	var elokcancel = document.createElement('div');
	elokcancel.className = 'vdeck-field-okcancel';
	elokcancel.style.display = 'none';
	elokcancel.appendChild(elok);
	elokcancel.appendChild(document.createElement('br'));
	elokcancel.appendChild(elcancel);
	elin.okcancel = elokcancel;
	elin.cancel = elcancel;
	
	var elch = document.createElement('a');
	elch.className = 'vdeck-field-change';
	elch.style.styleFloat = elch.style.cssFloat = 'left';
	elch.innerHTML = 'Change';
	elch.setAttribute('href', '#');
	elch.style.fontSize = 'smaller';
	elch.instance = this;
	elch.field = elin;
	elch.dval = elval;
	elch.okcancel = elokcancel;
	elch.cancel = elcancel;
	elch.onclick = function() {
		if (this.instance.searching) return;
		this.style.display = 'none';
		this.dval.style.display = 'none';
		this.okcancel.style.display = 'inline';
		this.cancel.style.display = 'inline';
		this.field.style.display = 'inline';
		this.field.focus();
		this.instance.el.submit.style.visibility = 'visible';
		return false;
	}

	elin.elch = elch;
	elin.instance = this;
	elin.style.display = 'none';
	elin.style.width = (w - 28) + 'px';
	elin.ondone = function() {
		this.elch.field.style.display = 'none';
		this.elch.okcancel.style.display = 'none';
		this.elch.style.display = 'inline';
		this.elch.dval.style.display = 'inline';
		this.instance.values[this.valkey] = this.value;
		this.elch.dval.innerHTML = (this.valkey == 'specialist_id' ? VTLS.VDeckRes.specialist[this.value] : this.value) + '&nbsp;';
	}
	elvalch.appendChild(elin);

	//elvalch.appendChild(document.createElement('br'));
	elvalch.appendChild(elch);
	
	elvalch.appendChild(elokcancel);

	elpar.appendChild(elf);
	
}

VTLS.VDeckControl.prototype.renderAt = function (el) {

	if (typeof el == 'string') el = document.getElementById(el);
	if (!el) return false;
	var parentw = this.parentw = el.offsetWidth;
	
	var eltop = this.el.top = document.createElement('div');
	eltop.className = 'vdeck-form';
	el.appendChild(eltop);

	//eltop.style.paddingBottom = '10px';
	//eltop.style.height = '35px';
	eltop.style.styleFloat = eltop.style.cssFloat = 'left';
	eltop.style.clear = 'both';
	
	var elfields = this.el.fields = document.createElement('div');
	elfields.className = 'vdeck-form-fields';
	eltop.appendChild(elfields);
	elfields.style.styleFloat = elfields.style.cssFloat = 'left';
	
	var elinloc = document.createElement('input');
	this.el.inloc = elinloc;
	elinloc.valkey = 'location';
	elinloc.setAttribute('type', 'text');
	elinloc.setAttribute('value', this.values.location);
	this.el.floc = this.renderField('LOCATION', this.values.location, Math.ceil(parentw * 0.22), elinloc, elfields);
	//elinloc.onblur = function() { if (this.value != '') this.ondone(); }
	elinloc.onkeypress = function(e) {
		if (this.value == '') return;
		if (!e) e = event;
		var k = (e.keyCode ? e.keyCode : e.which)
		if (k == 13) this.ondone();
	}

	var elinsid = document.createElement('select');
	this.el.insid = elinsid;
	elinsid.style.height = elinloc.style.height;
	elinsid.valkey = 'specialist_id';
	for (var sid in VTLS.VDeckRes.specialist) if (VTLS.VDeckRes.specialist.hasOwnProperty(sid)) {
		var opt = document.createElement('option');
		opt.style.fontSize = 'smaller';
		opt.text = VTLS.VDeckRes.specialist[sid];
		opt.value = sid;
		document.all ? elinsid.add(opt) : elinsid.add(opt, null);
	}
	this.el.fsid = this.renderField('SPECIALTY', VTLS.VDeckRes.specialist[this.values.specialist_id], Math.ceil(parentw * 0.37), elinsid, elfields);
	//elinsid.onblur = function() { this.ondone(); }
	elinsid.onchange = function() { this.ondone(); }
	
	var eltr = document.createElement('div');
	eltr.className = 'vdeck-form-submit';
	eltr.style.styleFloat = eltr.style.cssFloat = 'right';
	eltop.appendChild(eltr);
	
	var elclear = document.createElement('div');
	elclear.style.clear = 'both';
	eltop.appendChild(elclear);

	var elsubmit = this.el.submit = document.createElement('input');
	elsubmit.className = 'vdeck-form-submit-button';
	elsubmit.setAttribute('type', 'button');
	elsubmit.style.fontSize = '11px';
	elsubmit.style.width = '70px';
	elsubmit.value = 'Search';
	elsubmit.instance = this;
	elsubmit.elinsid = elinsid;
	elsubmit.elinloc = elinloc;
	elsubmit.onclick = function () {
		if (elinsid.okcancel.style.display != 'none') elinsid.cancel.onclick();
		if (elinloc.okcancel.style.display != 'none') elinloc.cancel.onclick();
		this.instance.search();
		
	}
	eltr.appendChild(elsubmit);
	
	//var elstatus = this.el.status = document.createElement('span');
	//elstatus.innerHTML = '&nbsp;'
	//eltr.appendChild(elstatus);
	this.el.status = elsubmit;
	
	var elresult = this.el.result = document.createElement('div');
	elresult.className = 'vdeck-results';
	elresult.style.width = parentw + 'px';
	elresult.style.overflow = 'hidden';
	elresult.style.styleFloat = elresult.style.cssFloat = 'left';
	elresult.style.clear = 'both';
	el.appendChild(elresult);

	if (this.values.location || this.values.specialist_id) {
		elsubmit.disabled = true;
		this.search();
	}
	else {
		if (!this.values.location) elinloc.elch.onclick();
		if (!this.values.specialist_id) elinsid.elch.onclick();
	}
	
	return true;
}

VTLS.VDeckControl.prototype.setLocation = function(loc) {
	if (!loc) loc = '';
	this.values.location = loc;
	if (this.el && this.el.inloc) {
		this.el.inloc.value = loc;
		this.el.inloc.ondone();
	}
}

VTLS.VDeckControl.prototype.setSpecialistID = function(sid) {
	if (!sid) sid = 0;
	sid = +sid;
	this.values.specialist_id = sid;
	if (this.el && this.el.insid) {
		this.el.insid.value = sid;
		this.el.insid.ondone();
	}
}

window['VDeckControl'] = VTLS.VDeckControl;
