$(document).ready(function(){
	$(document).pngFix( );

	var VIEW = '';
	var overlay = '';
	var hash = window.location.hash.replace('#','');

	var not_active = false;
	$('div.main_selection ul li').hover(function(){
		if($(this).css('background-color') == 'transparent'){
			not_active = true;
			$(this).css('background-color','white').children().css('color','black');
		}
		},function(){
			if(not_active){
				$(this).css('background-color','transparent').children().css('color','white');
				not_active= false;
			}
		});



	if (hash) {
		overlay = hash.split('/');
		hash = overlay[0];
		if (overlay.length > 1) overlay = overlay[1];
		else overlay = '';
	}
	//just uses the first view in div
	if(!hash && strpos(window.location,'boneviewer')){
		var views = $('div.image_overlay a').eq(0);
		if(views.length > 0){
			window.location.hash = $(views).text();
			hash = $(views).text();
		}
	}
//	if(hash){	hash now set above when !hash.  csl 6/25/09
	if(strpos(window.location,'boneviewer') && hash){
		VIEW = hash;
		checkForOverlays();
		$('.element_text').show();
		$('#insert_big_jpg').attr('src',$('.'+hash).attr('src')).show();;
		$('#image_overlay_'+hash).css('color','white');
		if(overlay){
			if($('li.'+overlay+'_'+VIEW).size() > 0){
				$('#insert_big').attr('src',$('li.'+overlay+'_'+VIEW).children('a').attr('href')).show();
			}
		}
	}
	$('div.image_overlay img').each(function(i,n){
        if($(n).attr('src').search('www/images/default.gif') != -1){
            $(this).parent().remove();
        }
	});


	$('.image_overlay').click(function(){
		$('.list').hide();
		$('#insert_big').hide();
		$('div.image_overlay a').css('color','gray');
		$(this).children('a').css('color','white');
		VIEW = $(this).children('a').text();
		$('#insert_big_jpg').attr('src',$('.'+VIEW).attr('src')).show();
		checkForOverlays();
		window.location.hash = VIEW;
	});

			$('li.category a.overlay_gif').click(function(){
					if($(this).css('color') == 'white' || $(this).css('color') == '#808080'){
						var type = $(this).text().toLowerCase();
						if(type != 'morphology'){
							$('ul.morph_hover:visible').hide();
							var url = $('.'+type+'_'+VIEW).children('a').attr('href');
							$('#insert_big').attr("src",url);
							$('#insert_big').show();
							window.location.hash = VIEW + "/"+type;
						}
						else{
							var pos = $(this).position();
							var off  = $(this).offset();
							$('ul.morphology_'+VIEW).css('left',pos.left-20);
							$('ul.morphology_'+VIEW).css('top',off.top-10);
							var width = $(this).width();
							width = width + 4;
							if($('ul.morphology_'+VIEW).width() < width){
								$('ul.morphology_'+VIEW).width(width);
							}
							$('ul.morphology_'+VIEW).slideToggle();
						}
					}
				return false;
			});


			$('ul.morph_hover a').click(function(){
					$('#insert_big').attr("src",$(this).attr('href')).show();
					window.location.hash = VIEW + "/" + $(this).text();
					return false;
			});


			$('li.category a').hover(function(){
				if($(this).css('color') == 'gray' && !$(this).parent().parent().hasClass('morph_hover')){
					$(this).css('color','white');
				}
			},function(){
				if($(this).css('color') == 'white' && !$(this).parent().parent().hasClass('morph_hover')){
					$(this).css('color','gray');
				}
			});


	//makes a way to get rid of overlay
//	$('body').click(function(){
//		$('#insert_big').hide();
//		$('#insert_big').attr('src','');
//	});

	$('#prevenance').click(function(){
		var offset = $('#insert_big_jpg').offset();
		$('.boneviewer_description').css('top',offset.top+380);
		$('.boneviewer_description').toggle();
		return false;
	});
	$('#legend').click(function(){
		window.open($(this).attr('href'),"legendwindow","menubar=0,resizable=0,width=505,height=685");
		return false;
	});
	$('#movie').click(function(){
		if($(this).attr('href').search('.mov') != -1){
			window.open($(this).attr('href'),"moviewindow","menubar=0,resizable=0,width=320,height=256");
		}
		return false;
	});

	$('ul#definition_links li a').click(function(){
		return false;
	});

	//home page roll_over
	//$('.large_image').hide();
	$('.dark_image').hide();
	$('.feature_text').hide();
	$('.human_large').show();
	$('.human_dark').show();
	$('div.species_selector img').hover(
	function(){
		var name = $(this).attr('id');
		var name2 = $(this).attr('id');
		$('.large_image').hide();
		$('.dark_image').hide();
		$('.feature_text').hide();
		name = name.split('_');
		name2 = name2.split('_');
		for (i = 0; i < name2.length; i++) {
			name2[i] = name2[i].substring(0,1).toUpperCase() + name2[i].substring(1,name2[i].length);
		}
		if(name.length == 2){
			$('div.species_selection h2').text(name2[0]);
			$('div.species_selection h3').text('');
			$('div.species_selection h4').text('');
			$('.'+name[0]+'_large').show();
			$('.'+name[0]+'_dark').show();
		}
		if(name.length == 3){
			$('div.species_selection h2').text('');
			$('div.species_selection h3').text(name2[0]);
			$('div.species_selection h4').text(name2[1]);
			$('.'+name[0]+'_'+name[1]+'_large').show();
			$('.'+name[0]+'_'+name[1]+'_dark').show();
		}
		},
	function(){		
	});

	function strpos( haystack, needle, offset){
	    // http://kevin.vanzonneveld.net
	    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	    // +   improved by: Onno Marsman    
 	    var i = (haystack+'').indexOf( needle, offset ); 
	    return i===-1 ? false : i;
	}

	function checkForOverlays(){
		$('li.category').each(function(i,n){
			if(!$(n).hasClass('standard')){
				if($(n).children('ul').children('li').size() == 0){
					if($(n).children('a').attr('href').search('mov') != -1){
						$(n).children('a').css('color','gray');			
						$(n).children('a').css('cursor','pointer');			
					}
					else{
						$(n).children('a').css('color','#333333');			
						$(n).children('a').css('cursor','auto');			
					}
				}
				else{
					var found = false;
					if($(n).attr('class').search('morphology') != -1){
						$('ul.morph_hover').each(function(j,k){
							if($(k).attr('class')){
								if($(k).attr('class').search(VIEW) != -1){
									found = true;
								}
							}
						});	
					}
					else{
						$(n).children('ul').children('li').each(function(j,k){
							if($(k).attr('class')){
								if($(k).attr('class').search(VIEW) != -1){
									found = true;
								}
							}
						});	
					}
					if(!found){
						$(n).children('a').css('color','#333333');			
						$(n).children('a').css('cursor','auto');			
					}
					else{
						$(n).children('a').css('color','gray');			
						$(n).children('a').css('cursor','pointer');			
					}
				}
			}
		});
	}


});
