if (!window['VTLS']) window['VTLS'] = {};
VTLS.API_KEY = 'SAWIUerLlffRAzbTeOgGOzzkutND8Q6T';
VTLS.API_DOMAIN = 'queenvirgin.blogspot.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/vlite/1.0';
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.VLiteConfig = {
	'APP_ID' : 3,
	'WIDTH' : 220,
	'MIN_WIDTH' : 150,
	'SPECIALIST_ID' : '',
	'DEFAULT_TAB' : 'C',
	'CHECK_LABEL' : 'check up on your doc',
	'LOCATION' : '',
	'CHECK_NAME_HINT' : 'Type in doctor name (last or full)',
	'CHECK_LOC_HINT' : 'Type in city, state OR zip',
	'FIND_LABEL' : 'find a doc',
	'FIND_SPEC_HINT' : 'Select a specialist type',
	'FIND_LOC_HINT' : 'Type in city, state OR zip',
	'SUBMIT_CHECK_TEXT' : 'go',
	'SUBMIT_FIND_TEXT' : 'find',
	'ADV_SEARCH_TEXT' : 'advanced search',
	'ADV_SEARCH_CHECK_URL' : VTLS.VWS_VITALS_BASE_URL + '/ctl/search_check_up.ctl.php',
	'ADV_SEARCH_FIND_URL' : VTLS.VWS_VITALS_BASE_URL + '/v/?v=find_doc',
	'SEARCH_CHECK_URL' : VTLS.VWS_VITALS_BASE_URL + '/doctor/checkup',
	'SEARCH_FIND_URL' : VTLS.VWS_VITALS_BASE_URL + '/doctor/find',
	'VITALS_LOGO' : true
};

VTLS.VLiteRes = {
	'specialist' : {
		"101":"Allergist & Immunologist",
		"128":"Anesthesiologist (pain control)",
		"131":"Cardiothoracic Surgeon",
		"135":"Cardiologist (heart)",
		"104":"Colorectal Surgeon (colon & rectum)",
		"105":"Dermatologist (skin)",
		"136":"Endocrinologist (diabetes, hormones)",
		"107":"Family Practitioner",
		"137":"Gastroenterologist (digestive)",
		"108":"General Practitioner",
		"134":"Geriatricist (elderly care)",
		"138":"Infectious Disease (virus, bacteria, parasites)",
		"110":"Internist",
		"112":"Medical Geneticist",
		"139":"Nephrologist (kidney)",
		"140":"Neurologist (brain, nervous system)",
		"113":"Neurological Surgeon",
		"117":"Obstetrician & Gynecologist",
		"141":"Oncologist (cancer)",
		"118":"Ophthalmologist (eyes)",
		"119":"Oral & Maxillofacial Surgeon",
		"120":"Orthopaedic Surgeon",
		"121":"Otolaryngologist (ear, nose, throat)",
		"142":"Pediatric / Adolescent Psychiatrist",
		"143":"Pediatric Surgeon",
		"123":"Pediatrician",
		"124":"Physiatrist (physical, rehabilitation)",
		"125":"Plastic Surgeon",
		"144":"Psychiatrist",
		"145":"Pulmonologist (lungs)",
		"116":"Sports Medicine",
		"130":"Surgeon",
		"133":"Urologist"
	}
};

VTLS.VLiteControl = function () {
	this.tabs = [];
	this.form = VTLS.VLiteConfig.DEFAULT_TAB;
	this.value = {
		'check' : {
			'name' : '',
			'loc' : ''
		},
		'find' : {
			'sid' : '',
			'loc' : ''
		}
	}; 
};

VTLS.VLiteControl.prototype.selectByValue = function(el_select, val, trigger_on_change) {
	for (var i = 0; i < el_select.options.length; i++) {
		if (el_select.options[i].value == val) {		
			el_select.options[i].selected = true;
			if (trigger_on_change && el_select.onchange) el_select.onchange();
			return el_select.options[i];
		}
	}
}

VTLS.VLiteControl.prototype.setLoc = function(l) {
	if(l == undefined || l == '')
		return;
		
	VTLS.VLiteConfig.LOCATION = l;
}

VTLS.VLiteControl.prototype.setSpecialistID = function(id) {
	if(id == undefined || id == '')
		return;
		
	VTLS.VLiteConfig.SPECIALIST_ID = id;
}

VTLS.VLiteControl.prototype.setWidth = function(w) {
	if(w <= 200) // small ver
	{
		/*	
		'CHECK_LABEL' : 'check up on your doc',
		'CHECK_NAME_HINT' : 'Type in doctor name (last or full)',
		'CHECK_LOC_HINT' : 'Type in city, state OR zip',
		'FIND_LABEL' : 'find a doc',
		'FIND_SPEC_HINT' : 'Select a specialist type',
		'FIND_LOC_HINT' : 'Type in city, state OR zip',
		'SUBMIT_CHECK_TEXT' : 'go',
		'SUBMIT_FIND_TEXT' : 'find',
		'ADV_SEARCH_TEXT' : 'advanced search',
		*/
		VTLS.VLiteConfig.CHECK_LABEL = 'check up';
		VTLS.VLiteConfig.CHECK_NAME_HINT = 'doctor\'s name';
		VTLS.VLiteConfig.CHECK_LOC_HINT = 'city, state OR zip';
		VTLS.VLiteConfig.FIND_LABEL = 'find a doc';
		VTLS.VLiteConfig.FIND_SPEC_HINT = 'select a specialty';
		VTLS.VLiteConfig.FIND_LOC_HINT = 'city, state OR zip';
		VTLS.VLiteConfig.ADV_SEARCH_TEXT = 'advanced';
		VTLS.VLiteConfig.WIDTH = Math.max(VTLS.VLiteConfig.MIN_WIDTH, +w);
	}
	else
	{
		VTLS.VLiteConfig.WIDTH = Math.max(VTLS.VLiteConfig.MIN_WIDTH, +w);
	}
}

VTLS.VLiteControl.prototype.setCheckUpLabel = function(l) {
	if (typeof l != 'string') return;
	VTLS.VLiteConfig.CHECK_LABEL = l;
}

VTLS.VLiteControl.prototype.setFindLabel = function(l) {
	if (typeof l != 'string') return;
	VTLS.VLiteConfig.FIND_LABEL = l;
}

VTLS.VLiteControl.prototype.setVitalsLogo = function(b) {
	VTLS.VLiteConfig.VITALS_LOGO = (b ? true : false);
}

VTLS.VLiteControl.prototype.switchTo = function(me, f, tab) {
	for (var i = 0; i < this.tabs.length; i++) {
		this.tabs[i].div.style.display = 'none';
		VTLS.VUtil.removeClass(this.tabs[i].a, 'vl-selected');
		VTLS.VUtil.replaceClass(this.tabs[i].a, 'vl-font-tab-on', 'vl-font-tab-off');
		VTLS.VUtil.replaceClass(this.tabs[i].a, 'vl-color-tab-bg-on', 'vl-color-tab-bg-off');
	}
	tab.style.display = 'block';
	VTLS.VUtil.addClass(me, 'vl-selected');
	VTLS.VUtil.replaceClass(me, 'vl-font-tab-off', 'vl-font-tab-on');
	VTLS.VUtil.replaceClass(me, 'vl-color-tab-bg-off', 'vl-color-tab-bg-on');
	this.form = f;
	this.btn_submit.value = this.form == 'C' ? VTLS.VLiteConfig.SUBMIT_CHECK_TEXT : VTLS.VLiteConfig.SUBMIT_FIND_TEXT;
	this.a_adv.href = this.form == 'C' ? VTLS.VLiteConfig.SEARCH_CHECK_URL : VTLS.VLiteConfig.ADV_SEARCH_FIND_URL;
	this.a_adv.style.visibility = this.form == 'C' ? 'visible' : 'hidden';
};


VTLS.VLiteControl.prototype.setDefaultTab = function(val) {
	this.form = val;
}

VTLS.VLiteControl.prototype._firstFocus = function(self) {
	return function() {
		if (self.block) return;
		self.block = true;
		self.value = '';
		self.style.color = '';
	}
}

VTLS.VLiteControl.prototype._inputKey = function (e) {
	if (e == null) e = event;
	var nKeyCode = (e.keyCode ? e.keyCode : e.which);
	this.onchange(e);
	if (nKeyCode == 13) {
		//this.control.doSubmit();
		return false;
	}
}

VTLS.VLiteControl.prototype._inputChange = function (e) {
	if (e == null) e = event;
	var nKeyCode = (e.keyCode ? e.keyCode : e.which);
	if (nKeyCode == 13) {
		//this.control.doSubmit();
		return false;
	}
	else {
		this.updateval();
	}
}

VTLS.VLiteControl.prototype.renderAt = function(el) {
	if (typeof el == 'string') el = VTLS.VUtil.$(el);
	if (!el) return false;
	
	/* main div */
	var div_main = document.createElement('div');
	div_main.className = 'vl-main vl-font';
	div_main.style.width = VTLS.VLiteConfig.WIDTH + 'px';
	
	/* tab bar */
	var ul_nav = document.createElement('ul');
	ul_nav.className = 'vl-tab';
	div_main.appendChild(ul_nav);

	/* check tab */
	var li_check = document.createElement('li');
	var a_check = document.createElement('a');
	li_check.appendChild(a_check);
	a_check.href = '#';
	a_check.control = this;
	a_check.onclick = function () { this.control.switchTo(this, 'C', this.tab); return false; }
	a_check.className = 'vl-color-border';
	a_check.appendChild(document.createTextNode(VTLS.VLiteConfig.CHECK_LABEL));
	ul_nav.appendChild(li_check);

	/* find tab */
	var li_find = document.createElement('li');
	var a_find = document.createElement('a');
	li_find.appendChild(a_find);
	a_find.href = '#';
	a_find.control = this;
	a_find.onclick = function () { this.control.switchTo(this, 'F', this.tab); return false; }
	a_find.className = 'vl-color-border';
	a_find.appendChild(document.createTextNode(VTLS.VLiteConfig.FIND_LABEL));
	ul_nav.appendChild(li_find);

	/* form wrapper */
	var div_form = document.createElement('div');
	div_form.className = 'vl-form vl-color-border vl-color-tab-bg-on';
	div_main.appendChild(div_form);

	/* form */
	var form_form = document.createElement('form');
	form_form.control = this;
	form_form.style.margin = '0px';
	form_form.style.padding = '0px';
	//form_form.onsubmit = function() { try { this.control.doSubmit(); } catch (e) { } return false; }
	form_form.onsubmit = function() { return false; }
	div_form.appendChild(form_form);
	
	/* check div */
	var div_check = document.createElement('div');
	this.tabs.push({'div':div_check,'a':a_check});
	a_check.tab = div_check;
	div_check.className = 'vl-tab-div';
	div_check.style.display = VTLS.VLiteConfig.DEFAULT_TAB == 'C' ? 'block' : 'none';
	form_form.appendChild(div_check);
	
	/* check field: name */
	var i_check_name = document.createElement('input');
	i_check_name.type = 'text';
	i_check_name.className = 'vl-font';
	i_check_name.value = VTLS.VLiteConfig.CHECK_NAME_HINT;
	i_check_name.style.color = '#888';
	i_check_name.onfocus = this._firstFocus(i_check_name);
	i_check_name.style.width = (VTLS.VLiteConfig.WIDTH - 20) + 'px';
	i_check_name.control = this;
	i_check_name.updateval = function () { this.control.value.check.name = this.value; }
	i_check_name.onchange = this._inputChange;
	i_check_name.onkeypress = this._inputKey;
	var div_i_check_name = document.createElement('div');
	div_i_check_name.className = 'vl-wrap-input';
	div_i_check_name.appendChild(i_check_name);
	div_check.appendChild(div_i_check_name);

	/* check field: location */
	var i_check_loc = document.createElement('input');
	i_check_loc.type = 'text';
	i_check_loc.className = 'vl-font';
	i_check_loc.value = VTLS.VLiteConfig.CHECK_LOC_HINT;
	i_check_loc.style.color = '#888';
	i_check_loc.onfocus = this._firstFocus(i_check_loc);
	if(VTLS.VLiteConfig.LOCATION != '')
	{
		i_check_loc.onfocus();
		i_check_loc.value = VTLS.VLiteConfig.LOCATION;
		this.value.check.loc = VTLS.VLiteConfig.LOCATION;
	}
	i_check_loc.style.width = (VTLS.VLiteConfig.WIDTH - 20) + 'px';
	i_check_loc.control = this;
	i_check_loc.updateval = function () { this.control.value.check.loc = this.value; }
	i_check_loc.onchange = this._inputChange;
	i_check_loc.onkeypress = this._inputKey;
	var div_i_check_loc = document.createElement('div');
	div_i_check_loc.className = 'vl-wrap-input';
	div_i_check_loc.appendChild(i_check_loc);
	div_check.appendChild(div_i_check_loc);

	/* find div */
	var div_find = document.createElement('div');
	this.tabs.push({'div':div_find,'a':a_find});
	a_find.tab = div_find;
	div_find.className = 'vl-tab-div';
	div_find.style.display = VTLS.VLiteConfig.DEFAULT_TAB != 'C' ? 'block' : 'none';
	form_form.appendChild(div_find);
	
	/* find field: specialty */
	var i_find_name = document.createElement('select');
	var opt = document.createElement('option');
	opt.value = '';
	opt.text = VTLS.VLiteConfig.FIND_SPEC_HINT;
	document.all ? i_find_name.add(opt) : i_find_name.add(opt, null);
	for (sid in VTLS.VLiteRes.specialist) if (VTLS.VLiteRes.specialist.hasOwnProperty(sid)) {
		opt = document.createElement('option');
		opt.value = sid;
		opt.text = VTLS.VLiteRes.specialist[sid];
		document.all ? i_find_name.add(opt) : i_find_name.add(opt, null);
	}
	i_find_name.className = 'vl-font';
	i_find_name.style.width = (VTLS.VLiteConfig.WIDTH - 20) + 'px';
	i_find_name.control = this;
	i_find_name.control = this;
	i_find_name.updateval = function () { this.control.value.find.sid = this.value; }
	i_find_name.onchange = this._inputChange;
	if(VTLS.VLiteConfig.SPECIALIST_ID != '')
		this.value.find.sid = VTLS.VLiteConfig.SPECIALIST_ID;
	var div_i_find_name = document.createElement('div');
	div_i_find_name.className = 'vl-wrap-input';
	div_i_find_name.appendChild(i_find_name);
	div_find.appendChild(div_i_find_name);
	this.selectByValue(i_find_name, VTLS.VLiteConfig.SPECIALIST_ID);

	/* find field: location */
	var i_find_loc = document.createElement('input');
	i_find_loc.type = 'text';
	i_find_loc.className = 'vl-font';
	i_find_loc.value = VTLS.VLiteConfig.FIND_LOC_HINT;
	i_find_loc.style.color = '#888';
	i_find_loc.onfocus = this._firstFocus(i_find_loc);	
	if(VTLS.VLiteConfig.LOCATION != '')
	{
		i_find_loc.onfocus();
		i_find_loc.value = VTLS.VLiteConfig.LOCATION;
		this.value.find.loc = VTLS.VLiteConfig.LOCATION;
	}
	i_find_loc.style.width = (VTLS.VLiteConfig.WIDTH - 20) + 'px';
	i_find_loc.control = this;
	i_find_loc.updateval = function () { this.control.value.find.loc = this.value; }
	i_find_loc.onchange = this._inputChange;
	i_find_loc.onkeypress = this._inputKey;
	var div_i_find_loc = document.createElement('div');
	div_i_find_loc.className = 'vl-wrap-input';
	div_i_find_loc.appendChild(i_find_loc);
	div_find.appendChild(div_i_find_loc);
	
	/* brand div */
	var brand_div = document.createElement('div');
	brand_div.className = 'vl-brand';
	form_form.appendChild(brand_div);
	
	/* logo div */
	if (VTLS.VLiteConfig.VITALS_LOGO) {
		var logo_div = document.createElement('div');
		logo_div.className = 'vl-float-l';
		var img_logo = document.createElement('img');
		img_logo.style.width = '78px';
		img_logo.style.height = '38px';
		img_logo.src = VTLS.VWS_BASE_URL + '/client/res.php?a=vlite&v=1.0&t=img&f=logo.png&key=' + VTLS.API_KEY;
		a_logo = document.createElement('a');
		a_logo.href = VTLS.VWS_VITALS_BASE_URL;
		a_logo.title = 'powered by Vitals';
		a_logo.appendChild(img_logo);
		logo_div.appendChild(a_logo);
		brand_div.appendChild(logo_div);
	}
	
	/* submit div */
	var submit_div = document.createElement('div');
	submit_div.className = 'vl-float-r';
	submit_div.style.textAlign = 'right';
	brand_div.appendChild(submit_div);
	
	/* submit button */
	var btn_submit = document.createElement('input');
	btn_submit.type = 'button';
	btn_submit.value = '';
	btn_submit.className = 'vl-font vl-submit';
	btn_submit.control = this;
	btn_submit.onclick = function () { this.control.doSubmit(); };
	this.btn_submit = btn_submit;
	submit_div.appendChild(btn_submit);
	submit_div.appendChild(document.createElement('br'));
	
	/* advanced search */
	var a_adv = document.createElement('a');
	a_adv.href = '#';
	a_adv.appendChild(document.createTextNode(VTLS.VLiteConfig.ADV_SEARCH_TEXT));
	a_adv.style.lineHeight = '28px';
	a_adv.control = this;
	a_adv.target = '_blank';
	this.a_adv = a_adv;
	//submit_div.appendChild(a_adv);
	
	/* clear both */
	var clear_div = document.createElement('div');
	clear_div.style.clear = 'both';
	brand_div.appendChild(clear_div);
	
	el.appendChild(div_main);
	
	this.form == 'C' ? a_check.onclick() : a_find.onclick();
	
};

VTLS.VLiteControl.prototype.doSubmit = function() {
	var xhref = '';
	if (this.form == 'C') {
		if (this.value.check.name != '')
			xhref = VTLS.VLiteConfig.SEARCH_CHECK_URL + '/?name=' + encodeURIComponent(this.value.check.name) + '&location=' + encodeURIComponent(this.value.check.loc);
	}
	else {
		if (this.value.find.sid != '' && this.value.find.loc != '')
			xhref = VTLS.VLiteConfig.SEARCH_FIND_URL + '/?sid=' + encodeURIComponent(this.value.find.sid) + '&loc=' + encodeURIComponent(this.value.find.loc);
	}
	if (xhref == '') return;
	xhref += '&key=' + encodeURIComponent(VTLS.API_KEY);
	window.open(xhref);
};
