// Chart is passed in dasherized form w/o 'Chart' suffix, e.g. instant-mood
function updateChart(sourceEl, unit, number, patient, view_type) {
	var chart_url = '/mood/chart/';
	// Unit of days is only applicable to instant mood charts
	chart_url += (unit == 'd') ? 'instant_mood' : 'all';
	new Ajax.Request(chart_url, {
		method: 'get', 
		parameters: {
			patient_id: patient, 
			u: unit, 
			n: number, 
			view: view_type
		}, 
		onCreate: function() {
			showWait(sourceEl);
		}, 
		onSuccess: function(response) {
			var json = response.responseJSON;
			if (!(json instanceof Array)) {
				// Wrap single chart object literal in array to handle same as for multiple charts
				json = [json];
			}
			
			json.each(function(chart_json) {
				try {
				    if ($(chart_json.id)) {
    					// Swap in new image and image map
    					$(chart_json.id).src = chart_json.url;
    					// console.log('Chart id: ' + chart_json.id + ' chart url: ' + chart_json.url);
    					$(chart_json.id + '_map').update(chart_json.image_map);
	
    					// Swap in new time scale image and select tab
    					$(chart_json.id + '_TimescaleChart').src = chart_json.timescale_url;
					}
				}
				catch (e) {
					// Some charts are grouped together under a single timescale, so they won't have their own.
					// We ignore these and continue setting timescales for those that do have a matching name.
					// console.log('Chart: ' + chart_json.id + ' had error: ' + e);
				}
			});

			selectTimeScaleTab(unit, number);
			setupVertDistressTooltips();
		}, 
		onComplete: function(transport) {
			hideWait();
		}
	});
}

function selectTimeScaleTab(unit, number) {
	var timescale = (unit == 'a') ? 'a' : number + unit;
	var selectedTab = $$('.tab-' + timescale).first();
	$$('.timeframe').each(function(switcher) {
		cycleTabs(switcher, selectedTab);
	});
}

function showAnchorMenu(object) {
	var mnu = 'mnu-' + object;
	findPos(mnu,'lnk-' + object,0,25); 
	showLayer(mnu);
	//hideDropShadow(mnu);
	showDropShadow(mnu);
}

function setTimeScaleTabs(timescale_id) {
	var tab_selector = ('.tab-' + timescale_id);
	$$(tab_selector).each(function(tab) {
		tab.addClassName('yah');
	});
}

// this is retrieving a locally built hash called distress_tool_tips
function setupSlantDistressTooltips(imageMapId) {
	for (var i in distress_tool_tips) {
		if($(i)) {
			$(i).onmouseover = function() {
				showToolTip('toolTip',
							imageMapId,
							distress_tool_tips[this.id].slant_offset-30,
							distress_tool_tips[this.id].vert_offset,
							'<b>' + (distress_tool_tips[this.id].name + '</b><br />' + distress_tool_tips[this.id].short_definition));
			}
		}
		if($(i)) {
			$(i).onmouseout = function() { delayCloseToolTip(2000); };
		}
	}
}

 function setupVertDistressTooltips() {
	var tips = $$('#MoodProfileChart_map .area-tip');
	tips.each(function(t) {
		t.onmouseover = function() { showAreaTip('/patients/mood_distress_tooltip/' + this.id,this.id,'MoodProfileChart'); };
		t.onmouseout = function() { delayCloseToolTip(); };
	});
}


