// JavaScript Document

/* kapp search object */
Search = function(formElem, inputElem, submitElem){
	if (!inputElem)
		return;
	
	this.formElem = formElem;
	this.inputElem = inputElem;
	this.submitElem = submitElem;
	this.defaultValue = this.inputElem.getAttribute('k_defaultValue') ? this.inputElem.getAttribute('k_defaultValue') : this.inputElem.value;
	
	// if the submit button was "disabled" by default, then we should always clear its value when the input its activated
	this.defaultDisabled = this.submitElem.getAttribute('k_disabled') == 'disabled';
	if (this.defaultDisabled) {
		this.inputElem.toggleClassName('tdisabled');
		this.submitElem.toggleClassName('sdisabled');
	}
	
	this.activateCallback = this.activateInput.bindAsEventListener(this);
	this.confirmCallback = this.confirmInput.bindAsEventListener(this);
	this.returnCheckCallback = this.returnCheck.bindAsEventListener(this);
	
	this.setEvents();
};
Search.prototype.setEvents = function(){
	Event.observe(this.inputElem, 'click', this.activateCallback);
	Event.observe(this.inputElem, 'keydown', this.activateCallback);
	Event.observe(this.inputElem, 'keypress', this.returnCheckCallback);
	Event.observe(this.submitElem, 'click', this.confirmCallback);
};
// check to see if the return character was pressed
Search.prototype.returnCheck = function(event){
	if (event.keyCode == 13 && !this.inputElem.value.blank() && this.submitElem.getAttribute('disabled') != 'disabled') {
		this.submit();
	}
};
// when a search input is activated, then clear the default value and enable the input button
Search.prototype.activateInput = function(event){
	if (this.defaultDisabled) {
		this.inputElem.value = '';
		this.inputElem.removeClassName('tdisabled');
		this.submitElem.removeAttribute('k_disabled');
		this.submitElem.removeClassName('sdisabled');
	}
	Event.stopObserving(this.inputElem, 'click', this.activateCallback);
	Event.stopObserving(this.inputElem, 'keydown', this.activateCallback);
	Event.observe(this.inputElem, 'blur', this.confirmCallback);
};
// if nothing was entered in an input and focus is off the input, then we have to go back to the default value
Search.prototype.confirmInput = function(event){
	if (this.inputElem.value.blank()) {
		this.inputElem.value = this.defaultValue;
		
		// because the blur event occurs before the onclick for the submit button, 
		// we have to tell the browser to ignore the next click since the query was originally empty. 
		this.lastEmpty = true;

		this.defaultDisabled = true;
		this.inputElem.toggleClassName('tdisabled');
		this.submitElem.setAttribute('k_disabled', 'disabled');
		this.submitElem.toggleClassName('sdisabled');

		this.setEvents();
	} else if (event.target == this.submitElem && event.type == 'click' && this.lastEmpty){
		this.ignoreClick = true;
	} else {
		this.lastEmpty = false;
	}
};
Search.prototype.submit = function(){
	if (this.formElem.getAttribute('id') == 'searchRHForm') {
		var query = $('searchRHText').value;
		this.formElem.setAttribute('action', 'http://www.righthealth.com/topic/' + query);
	}
	this.formElem.submit();
};
if (typeof kapp == "undefined" || !kapp) {
    kapp = {};
}
kapp.namespace = function() {
    var a = arguments, o = null, i, j, d;
    for (i = 0; i < a.length; i = i+1) {
        d = a[i].split(".");
        o = kapp;

        // kapp is implied, so it is ignored if it is included
        for (j = (d[0] == "kapp") ? 1 : 0; j < d.length; j = j+1) {
            if (!o[d[j]]) {
				o[d[j]] = {};
			}
            o = o[d[j]];
        }
    }

    return o;
};
kapp.JSONrequest = function (url) {
	//alert(url);
	if ($('tmpscript')) {$('tmpscript').remove();}
    json = document.createElement('script');
    json.setAttribute('type', 'text/javascript');
    json.setAttribute('id', 'tmpscript');
    json.setAttribute('charset', "utf-8");
    json.setAttribute('src', url + '&noCacheIE=' + (new Date()).getTime());
    $$('head')[0].appendChild(json);
};

/* daily dose functions */
kapp.enableDailyDose = function(){
	if ($('dailydose_mail'))
		Event.observe('dailydose_mail', 'click', kapp.dailydose.enableSend);
	if ($('dailydose_feedup'))
		Event.observe($('dailydose_feedup'),'submit',kapp.dailydose.send);
};
kapp.namespace("kapp.dailydose");
kapp.dailydose.enableSend = function() {
	$('dailydose_mail').value = '';
	$('dailydose_mail').style.color = '#000';
	$('dailydose_submit').removeAttribute('disabled');
	$('dailydose_submit').removeClassName('disabled');
};
kapp.dailydose.send = function() {
	$('dailydose_message').show();
	$('dailydose_feedup').style.background = 'transparent url(/images/loading.gif) top right no-repeat';
	var url = $('dailydose_feedup').action + '?' + ($('dailydose_code') ? 'code=' + $('dailydose_code').value : 'mail=' + $('dailydose_mail').value);
	kapp.JSONrequest(url);
	kapp.dailydose.success = 0;
	window.setTimeout(kapp.dailydose.error,5000);
};
kapp.dailydose.returnHandler = function(response) {
	$('dailydose_feedup').style.background = 'none';
	kapp.dailydose.success = response["status"];
	$('dailydose_message').innerHTML = response['status']==1 ? ($('dailydose_feedup').action.indexOf('unsubscribe') != -1 ? "You have been unsubscribed from Daily Dose." : "Congratulations! You will begin receiving your Daily Dose shortly.<br/>") : "There was some kind of problem. We'll work on it!<br/>";
	if (response['status'] == 1)
		$('dailydose_feedup').hide();
};
kapp.dailydose.error = function() {
	$('dailydose_feedup').style.background = 'none';
	if (kapp.dailydose.success == 0) {
		$('dailydose_message').innerHTML = "There was some kind of problem. We'll work on it!<br/>";
	}
};
init = function() {
	document.search = new Search($('searchForm'), $('searchText'), $('searchSubmit'));
	document.searchRH = new Search($('searchRHForm'), $('searchRHText'), $('searchRHSubmit'));
	kapp.enableDailyDose();
};
Event.observe(window,'load',init);