// initialize some variables
	var pos;
	var pane;
	var snps; 	
	var numera;
	var pointer;
	var indy;
	
// execute when document is completely loaded	
jQuery(function( $ ){
	pos = -1;
	pane = $("div.pane");
	snps = $("a[name]"); 	
	numera = snps.length; // console.log(numera + " SNPs total");
	pointer = $("#SNPpointer");		
	indy = $("#indicator");
	
	//by default, the scroll is only done vertically ('y'), change it to x.
	$.scrollTo.defaults.axis = 'x'; 			

	//this one is important, many browsers don't reset scroll on refreshes
	pane.scrollTo( 0 );	//reset scrollable pane to (0,0)
	$.scrollTo( 0 );	//reset the screen to (0,0)
		
	pointer.hide(); 
	
	pane.scroll(function(){
				reportPos();
						 });
	
	
	$('#chooserLink').click(function(){ 		// show or hide the strain selector
		$('#chooser').toggle();
		return false;
	});
	
	// write SNP counter
	$('#SNPlink').html('0 of ' + numera);

	$('a.jumper').click(function(){ 			// function for "previous" and "next" links
		
		// determine direction of scroll

		var btn = $(this).attr('id');
		
		if (btn == "prev" && pos == -1){ 		// check if anything has happened yet
		
			var psl = pane.scrollLeft(); 		
			if(psl == 0){ return; 				// do nothing if the page hasn't been scrolled 
			
				 } else { pos++; moove(pos); } // move to the first SNP
				 
		} else {
			if (btn == "prev" && pos > 0){pos--;} // move backwards
			else if (btn == "next" && pos < numera-1){pos++;}	// move forward
			
			moove(pos);								
			
		}
	});	

	$("div.sMark[id]").click(function(){ // only execute for divs of this class that also have an id
								 var mk = $(this).attr('id').substr(2); // 
								 if(mk){
									 var toClik = parseFloat(mk)-1; //console.log("move to SNP " + toClik);
									 moove(toClik); 
									 return false;
								 }
	 });	
	
	$("div.sMark[id]").bind("mouseenter",function(){
								   $("div.iBall").remove();
								   $("a.polyInfo").remove();
								   var goTo = $(this).parent().attr("href").substr(1);
								   $("#neighborHood").append('<a href="'+goTo+'" class="polyInfo" title="Go to Polymorphism Detail Page" target="_blank"><div class="iBall">&nbsp;</div></a>');
									var xSNP = $(this).offset();
									$(".iBall").css("left", xSNP.left-535 +"px");
									$(".iBall").show();
								   }); 
	//console.log("in second ready function");
	
});
	

function moove(there){		
			here = parseFloat(there);// 
			pane.scrollTo(snps[here], 750, { 
						   	offset:-350 ,
							onAfter:function() { 
								 pos=here; 
								 updatePos();
							}
				});	
			
	}

function updatePos() {
		var psl = pane.scrollLeft();	
		
	// determine position of SNP
		var x = $("a[name]:eq("+pos+")");
		var SNPpos = x.offset();	
		
	// move pointer	to SNP	
		if(SNPpos) {
			pointer.show().animate({left: SNPpos.left+"px", opacity: 1}, { duration: 100, queue: false });	
		}
		
	//move indicator to snpMarker	
		if(indy.attr('id')){ // continue if the div with that id exists
			var Nr = parseFloat(pos)+1; 
			var snpMark = $("#sM" + Nr); // find the corresponding snpMarker
			var MPos;
			if(snpMark.offset() != 'undefined') {
				MPos = snpMark.offset();
				if(MPos && MPos.left) {
					indy.show().animate({left: (MPos.left-538)+"px"}, { duration: 100, queue: false });	
				}
			}
			
		}
					
	//update SNP counter
		var whichSNP = parseFloat(pos);
		//whichSNP++;
		$('#SNPlink').html(''+(whichSNP+1)+' of '+numera+'');		
	}
	
function reportPos() {
		if(pos == -1){ pos = 0; }
		var x = $("a[name]:eq("+pos+")");
		var SNPpos = x.offset();
		var ptPos = pointer.offset();
		
		if(SNPpos && ptPos && SNPpos.left != ptPos.left){
			pointer.fadeTo(1,0.2); 										
		}
	}
	

	
