$(document).ready(function(){

	$('.alphalisting a').bind('click', function(e){
		$('#content').html("<p style=\"color:#666;padding:0 0 0 20px;\">loading members...</p>");
		$.get("http://www.a-p-a.net/members_listing_ajax/"+e.currentTarget.innerHTML+"/", function(data){
			if(data.length > 1){
				$('#content').html(data);
			} else {
				$('#content').html("<p style=\"color:#666;padding:0 0 0 20px;\">no members found...</p>");
			}
			
					// $('#content-holder .content-item').html(data);
					// $mb = $('#content-holder .content-item .mbblock');
					// var w = ($mb.length / 2) * 120;
					// $('#content-holder').css({'width':w});
					// triggerTooltip();
					// triggerSlider();
		});
	});
	
	var $paneTarget = $('#content-scroll');
	
	var $members = $('#content-holder .content-item .mbblock');
	for(i=0;i<$members.length;i++){
		if((i % 3) == 0){
			var s = $members[i];
			$(s).before('<div class="mwrapper" id="w_'+i+'"></div>');
			$(s).next().next().remove().prependTo($('#w_'+i));
			$(s).next().remove().prependTo($('#w_'+i));
			$(s).remove().prependTo($('#w_'+i));
		}
	}
	$('#content-holder .content-item').append('</div>');
	
	$('.alphascrolling a').bind('click', function(e){
		$paneTarget.stop().scrollTo( $(".content-item .letter_"+e.currentTarget.innerHTML+""), 800, {axis:'x'} );
	});

	function triggerTooltip(){
		$("a.trigger").tooltip({
		   /* tooltip configuration goes here */ 
		   position: ['top', 'center'],
		   offset: [80,0],
		   onBeforeShow: function() { 
		       $(".tooltip").hide();
		   }
		});
	}

	$.extend($.ui.slider.defaults, {
		range: "min",
		animate: true,
		orientation: "horizontal"
	});

	$.extend($.ui.slider.prototype, { 
	  up: function() { 
	    this.value(this.value() + this.options.step); 
	  }, 
	  down: function() { 
	    this.value(this.value() - this.options.step); 
	  } 
	});
	
	var r = Math.floor(Math.random()*101);
		
    function triggerSlider(){
		$("#content-slider").slider({
	        animate: true,
	        step: 10,
	        handle: ".content-slider-handle",
	        change: handleSliderChange,
	        slide: handleSliderSlide,
			value: r
	    });
	}

	$("a.prev").click(function() { 
	  $("#content-slider").slider("down");return false;
	}); 

	$("a.next").click(function() { 
	  $("#content-slider").slider("up");return false;
	});

    function handleSliderChange(e, ui)
    {
      var maxScroll = $("#content-holder").width();
      $("#content-scroll").animate({scrollLeft: ui.value * (maxScroll / 100) }, 500);
    }

    function handleSliderSlide(e, ui)
    {
      //var maxScroll = $("#content-holder").width();
      //$("#content-scroll").attr({scrollLeft: ui.value * (maxScroll / 100) });
    }

	triggerTooltip();
	triggerSlider();
	
	// Marshall Street Logo width hack
	// $("a[name^='Marshal'] img").css({ "width":"259px", "height":"22px" });
	// $("a[name^='Marshal']").parent().parent().css("width","259px");
	
	// radomise the initial slider/container point
	var maxScroll = $("#content-holder").width();
	$("#content-scroll").animate({scrollLeft: r * (maxScroll / 100) }, 500);
	
	function loadVideo(v){
		var s1 = new SWFObject("/swf/player.swf","ply","600","368","9","#000000");
		s1.addParam("allowfullscreen","true");
		s1.addParam("allowscriptaccess","always");
		s1.addParam("flashvars","file=/video/"+v+".flv");
		s1.write("flashcontent");
	}
});