$.fn.jqScroll = function(options){
  return this.each(function(i){
		var trackHeight, buttonHeight, scrollHeight, contentHeight,
				increment = 50,
				scrolling = false,
				scrollTimer = null,
				pagecontent = $(this),
				outer = $('<div class="jqScrollWrap"/>'),
				inner = $('<div class="innerscroll"/>'),
				scroller = $('<div class="scroller" />'),
				scrollbar = $('<div class="scrollbar" />').appendTo(scroller),
				scrollbot = $('<div class="bottom" />').appendTo(scrollbar),
				track = $('<div class="track" />').appendTo(scrollbot),
				button = $('<div class="button" />').appendTo(track),
				up = $('<div class="up" />').appendTo(scrollbot),
				down = $('<div class="down" />').appendTo(scrollbot);
		
		pagecontent.wrap(outer);
		pagecontent.wrapInner(inner);
		pagecontent.after(scroller);

		initvars();
		
		pagecontent.mousewheel(function(objEvent, intDelta){
			if(intDelta > 0) scrollPage('up')
			else if (intDelta < 0) scrollPage('down');
		});
		
		$.each([up,down],function(){
			$(this).mouseover(function(){
				var button = $(this);
				scrolling = true;
				scrollTimer = setInterval(function(){
					button.hasClass('up')? scrollPage('up', 10) : scrollPage('down', 10);
				}, 50);
			}).mouseout(clearScroll).click(clearScroll);
		});
		
		button.draggable({
			axis:'y',
			containment:'parent',
			drag: function(event, ui){ buttonScrollContent(ui.position.top); }
		});
		
		function clearScroll(){
			clearInterval(scrollTimer);
			scrolling = false;
			testheight();
		}
			
		function buttonScrollContent(buttonTop){
			var p = Math.round((buttonTop/(trackHeight-buttonHeight))*100);
			var diff = Math.round(((contentHeight - scrollHeight)*p)/100);
			pagecontent.scrollTop(diff);
		}
		
		function scrollPage(dir, inc){
			var i = inc || increment;
			var t = pagecontent.scrollTop();
			var p = dir=='up'? t-i : t+i;
			pagecontent.scrollTop(p);
			if(p <=0 || p >= contentHeight - scrollHeight) clearScroll();
			updateButton();
		}
		
		function updateButton(){
			var d = contentHeight - scrollHeight;
			var t = pagecontent.scrollTop();
			var p = Math.round((t/d)*100);
			var b = Math.round((trackHeight-buttonHeight)*(p/100));
			button.css({top:b});
		}
		
		function initvars(){
			scrollbar.show();
			var h = pagecontent.outerHeight(true);
			scrollbar.css({height: h});
			trackHeight = track.height();
			buttonHeight = button.height();
			scrollHeight = scrollbar.height();
			contentHeight = pagecontent[0].scrollHeight;
			pagecontent.scrollTop(0);
			if(contentHeight <= scrollHeight) scrollbar.hide();
		}
		
		function testheight(){
			if(contentHeight != pagecontent[0].scrollHeight) initvars();
		}
	});
}

$.fn.reorder = function(){
  function randOrd() { return(Math.round(Math.random())-0.5); }
  return $(this).each(function(){
    var $this = $(this);
    var $children = $this.children();
    var childCount = $children.length;
    if(childCount > 1){
      $children.remove();
      var indices = new Array();
      for (i=0;i<childCount;i++) { indices[indices.length] = i; }
      indices = indices.sort(randOrd);
      $.each(indices,function(j,k) { $this.append($children.eq(k)); });
    }
  });
}

$(function(){
	// set auto height of members left column to adjust to right content height
	var h = Math.round( $('#rightcontent').outerHeight()/76)*76;
	var th = $('#leftcontent .members').outerHeight(true);
	var h = th > h? h : th;
	// init left scroller
	$('#leftcontent ul.members').css('height',h).reorder().jqScroll();
	// add target blank footer links
	$('#footerwrap p a, #header p.info a, a[rel="external"]').attr('target','_blank');
});
