
/*
Nicholas Konidaris II, Dec 2007
npk@ucolick.org

(c) 2007
*/

var isnan = function (x) { return x !== x; };
var NPK = function () {
	// Public

	return {
		index : 10,
		subsample : [],
		plot : null,
		cmd : null,
		first_cmd_pos : 0,
		prev : function () 
		{
			if(NPK.index === 0) { return; }
			NPK.index = NPK.index - 2;
			NPK.draw();
		},
		next : function () 
		{
			if(NPK.index >= NPK.data.length-2) { return; }
			if(NPK.index >= NPK.subsample.length-2 && NPK.subsample.length > 0) { return; }
			NPK.index = NPK.index + 2;
			NPK.draw();
		},
		get_cmd : function ()
		{
			var r0 = NPK.index-80;
			var r1 = NPK.index+80;
			var d = [];

			if(r0 < 0) r0 = 0;
			NPK.first_cmd_pos = r0;

			var len = (NPK.subsample.length !== 0 ? NPK.subsample.length : NPK.data.length);
			if(r1 > len) r1 = len - 1;
			if(NPK.subsample.length === 0) {
				for(var i = r0; i < r1; i++) {
					d.push([NPK.data[i].mb, NPK.data[i].ub]);
				}
			} else {
				for(var i = r0; i < r1; i++) {
					j = NPK.subsample[i];
					d.push([NPK.data[j].mb, NPK.data[j].ub]);
				}
			}
			NPK.cmd = d;
			return d;
		},
		draw : function ()
		{
			var imgs = $('img');
			var j = (NPK.subsample.length === 0 ? NPK.index : NPK.subsample[NPK.index]);
			var j2 = (NPK.subsample.length === 0 ? NPK.index+1 : NPK.subsample[NPK.index+1]);
			var d0 = NPK.data[j];
			var d1 = NPK.data[j2];

			$('.ft_s').html(' [' + NPK.index + '/' + (NPK.subsample.length === 0 ? NPK.data.length : NPK.subsample.length) + '] objnos: [' + d0.objno + ', ' + d1.objno + '] zs: [' + d0.z + ", " + d1.z + "]");
			// IMG
			imgs[0].src = 'deep2jpeg2/' + d0.objno + '.acs.jpeg';
			imgs[1].src = 'deep2jpeg2/' + d1.objno + '.acs.jpeg';

			// SED
			var options = {
				yaxis: {min: -1, max: 3.5},
				xaxis: {min: 13, max: 16.0},
				selection: {mode: "xy" }
			};
			var sed = NPK.zip(d0.nus, d0.sed);
			$.plot($('#sed1'), [{data: sed, points: {show: true}}], options);
			sed = NPK.zip(d1.nus, d1.sed);
			$.plot($('#sed2'), [{data: sed, points: {show: true}}], options);

			// CMD
			var cmd = NPK.get_cmd();
			options = {
				xaxis: {min: -22.5, max: -18},
				yaxis: {min: 0.2, max: 1.5},
				selection: {mode: "xy" }
			};
			NPK.plot = $.plot($('#cmd1'), [{data: cmd, points: {show: true, fillcolor: '#000000', radius: 0.6}}, 
				{data: [[d0.mb, d0.ub]], points: {show: true, fillcolor: '#ff0000'}}], options);
			$('#cmd1').bind("selected", NPK.selected);
			$.plot($('#cmd2'), [{data: cmd, points: {show: true, radius: 0.6}}, 
				{data: [[d1.mb, d1.ub]], points: {show: true, fillcolor: '#ffffff'}}], options);

		},
		selected : function (event, area)
		{
			var mb = (area.x1+area.x2)/2;
			var ub = (area.y1+area.y2)/2;

			var min = 100.0;
			var ix = -1;
			for(var i = 0; i < NPK.cmd.length; i=i+1) {
				var dist = Math.pow(mb - NPK.cmd[i][0],2) + Math.pow(ub - NPK.cmd[i][1],2);
				if(min > dist) { 
					min = dist;
					ix = i;
					if(min === 0.0) { break; }
				} 
			}
			NPK.index = NPK.first_cmd_pos+ix;
			NPK.draw();

		},
		zip : function (a,b)
		{
			var l10 = Math.log(10);
			var r = [];
			var i = 0;
			for(i = 0; i<a.length;i++) {
				if(a[i] !== a[i]) { continue; }
				if(b[i] !== b[i]) { continue; }
				r.push([Math.log(a[i])/l10,Math.log(b[i])/l10]);
			}
			return(r);
		},
		sample : function ()
		{
			$('#samptxt').toggle();
		},
		show_help : function ()
		{
			$('#helptxt').toggle();
		},
		goto_obj : function (on) 
		{
			$('#qresponse').html('');
			for(var i = 0; i < NPK.data.length; i++) {
				if(NPK.data[i].objno === on) {
					NPK.index = i;
					NPK.draw();
					$('#objno').val('');
					$('#samptxt').fadeOut();
					return;
				}
			}
			$('#qresponse').html('Could not find: ' + on);
		},
		search : function (predicate)
		{
			$('#qresponse').html('');
			NPK.subsample = [];
			for(var i = 0; i < NPK.data.length; i++) 
				if(eval(predicate)) 
					NPK.subsample.push(i);

			if(NPK.subsample.length !== 0) NPK.index = 0;
			else $('#qresponse').html('Your search returned 0 results');
		},
		bsample : function ()
		{
			// deal with object number first, it is a special case.
			var on = parseInt($('#objno').val());
			if(!isnan(on)) { 
				NPK.subsample = [];
				NPK.goto_obj(on);
				return;
			}

			var predicate = '';
			var txts = $(':input');

			// start at 1 to drop Object number.
			for(var i = 1; i < txts.length; i++) {
				var o = txts[i];
				if(o.value === '') continue;
				var n = parseFloat(o.value);
				if(isnan(n)) continue;
				var l = o.id[o.id.length-1] === 'l';
				if(predicate !== '') predicate += ' && ';
				if(o.id.substring(0,3) == "sed") 
					predicate += 'NPK.data[i].sed['+ o.id.substring(3,5) +']' + (l ? '>' : '<') + n;
				else
					predicate += 'NPK.data[i].'+o.id.substring(0,o.id.length-1) + (l ? '>' : '<') + n;
			}
			NPK.search(predicate);
			$('#desc').html('Exploring ' + (NPK.subsample.length === 0 ? '' + NPK.data.length : ' a subsample of ' + NPK.subsample.length) + ' AEGIS galaxies with z<sub>q</sub> > 2 and ACS images');
			NPK.draw();
		},
		startup : function () 
		{
			var zip = NPK.zip;
			$('div#hd').html('<h1>AEGIS Sed Explorer <button id="help" class="act">?</class></h1>');
			$('#desc').html('Exploring ' + (NPK.subsample.length === 0 ?  NPK.data.length : '' + 
				NPK.subsample.length) + ' AEGIS galaxies with z<sub>q</sub> > 2 and ACS images</span></p>');
			NPK.draw();

			// Initially hide the help
			$('#helptxt').toggle();
			// Capture mouse clicks
			$('#prev').click(NPK.prev);
			$('#next').click(NPK.next);
			$('#sample').click(NPK.sample);
			$('#bsamp').click(NPK.bsample);
			$('#help').click(NPK.show_help);
		}
	};

}();




