$(window).on("load", function() {
	// Cell atlas
	var svgCell = $("#svg_cell");
	if (svgCell.length) {
		var svg = $(svgCell[0].contentDocument).find('svg');
		// Highlight organelles gene page
		var loc_txt = '';
		var all_locs = '';
		$('td.main_location, td.other_location').each(function() {
			var name = $(this).attr('data-name');
			if (name) {
				var locs = name.split(',');
				if (locs.length > 0 && locs[0] !== "") {
					for (var i in locs) {
						if (locs.hasOwnProperty(i)) {
							var loc = locs[i].trim();
							var $g = svg.find('g.' + loc + ' path').css({'fill': '#7fd822', 'opacity': 0.5});
							all_locs += (all_locs ? ', ' : '') + loc.replace(/_/g, ' ').ucfirst();
						}
					}
				}
			}
		});
		svg.prependTo(svgCell.parent());
		svgCell.remove();
	}

	// Blood atlas
	var svgBlood = $("#svg_blood");
	if (svgBlood.length) {
		var svg = $(svgBlood[0].contentDocument).find('svg');
		// svg.attr('style','');
		// svg.attr('width',svgBlood.attr('width'));
		// svg.attr('height',svgBlood.attr('height'));
		$('.bloodrna').each(function() {
			var lineage = $(this).attr('bloodlinage');
			if (!lineage) return;
			var nx = $(this).attr('nx')*1;
			var $g = svg.find('.'+lineage+' path').css({'fill':'ffffff', 'opacity':'0.3'});
			if (nx>0 && Math.log(nx)/Math.log(6520)>0) {
				$g.css({'fill':'#d90000', 'opacity':Math.log(nx)/Math.log(6520)});
			}
		});
		svg.prependTo(svgBlood.parent());
		svgBlood.remove();
	}

	// Brain atlas
	$('#svg_coronal, #svg_sagittal').each(function() {
		var $svg = $(this.contentDocument).find('svg').attr('id', $(this).attr('id')+'_svg');
		$svg.find('g:has(title), path:has(title)').each(function() {
			var t = $(this).find('title:first');
			$(this).attr('title', t.text().charAt(0).toUpperCase() + t.text().slice(1));
			t.remove();
		});
		$svg.find('.region').css({'fill':'#ffffff', 'opacity':'1'});
		$('.brainrna').each(function() {
			var region = $(this).attr('brainregion');
			if (!region) return;
			var nx = $(this).attr('nx')*1;
			var $g = $svg.find('.'+region).css({'fill':'ffffff', 'opacity':'1'});
			if (nx>0 && Math.log(nx)/Math.log(1740)>0) {
				$g.css({'fill':'hsl(0, 100%, '+(100-50*(Math.log(nx)/Math.log(1740)))+'%)'});
			}
		});
		$svg.prependTo($(this).parent());
		$(this).remove();
	});

	$('.protein_function_toggle').click(function() {
		if ($(this).text() === "show all") {
			toggleText('protein_function', true);
		} else {
			toggleText('protein_function', false);
		}
	});
	$('.entrez_gene_summary_toggle').click(function() {
		if ($(this).text() === "show all") {
			toggleText('entrez_gene_summary', true);
		} else {
			toggleText('entrez_gene_summary', false);
		}
	});
	function toggleText(text_classname, show) {
		var textspan = $('.'+text_classname+'_text');
		var textspandots = $('.'+text_classname+'_textdots');
    if (show) {
			textspan.removeClass('hidden');
			textspandots.addClass('hidden');
			$('.'+text_classname+'_toggle').text('show less');
			Cookies.set(text_classname+'_toggle', 1);
    } else {
			textspan.addClass('hidden');
			textspandots.removeClass('hidden');
			$('.'+text_classname+'_toggle').text('show all');
			Cookies.set(text_classname+'_toggle', 0);
    }
	}


	/*
	$('.cell_anchor').hover(function() {
		//mousein
		$('.cell_corner').css({'height':'40px'});
	}, function() {
		//mouseout
		$('.cell_corner').css({'height':'20px'});
	});
	*/
	
		/*
		$('.woman')
		.on("click", function(e) {
			var $html = $('<div class="womanContainer" style="background-color:#fff; text-align:center; padding:10px;"></div>').append($('.womanContainer .slideToggle').clone(true), '<br>', $('.womanContainer .woman').clone(true).off('click').css({'width':'400px', 'display':'inline-block'}));
			var options = {
				opacity: 0.9,
				open: true,
				title: '<a href="https://data.humancellatlas.org/analyze/visualization/anatomogram" target="_blank">Anatomogram</a> of '+$('.gene_name').text()+' expression in human tissue. See the <a href="/learn/dictionary">histological dictionary</a> for more tissue information.',
				html: $html,
			};
			$.colorbox(options);
			e.preventDefault();
		});
		$('.tissue_link, .tissue_group').on("mouseover", function(e) {
			var uberon = $(this).attr('uberon');
			if ($(e.target).hasClass('tissue_group')) {
				uberon = $(this).find('.tissue_link').map(function() {
					return $(this).attr('uberon');
				}).get().join(',');
			}
			if (!uberon) return;
			uberon = uberon.split(',');
			var color = $(this).closest('.tissue_group').attr('color');
			for (var i=0; i<uberon.length; i++) {
				var $g = $('.woman_center, .man_center').find('.'+uberon[i]);
				if ($g.prop('tagName') == 'use') {
					$g = $('.woman_center, .man_center').find($g.attr('xlink:href'));
				}
				$g.css({'fill':color}).addClass('uberon_color').children().css('fill', '');
			}
		}).on("mouseout", function() {
			var $g = $('.woman_center, .man_center').find('.uberon_color').css({'fill':'none'});
		});
		*/
});