var hd = {
// -------
	init: function() {
		this.playersCards();
		this.colorbox();
		this.playersScroller();
		
	    FB.Event.subscribe('edge.create', function(response) {
			var baseUrl = $('base').attr('href');
			window.location = baseUrl + '/?page=facebook&action=like&returnUrl=' + window.location.href;
	    });

	    FB.Event.subscribe('edge.remove', function(response) {
			var baseUrl = $('base').attr('href');
			window.location = baseUrl + '/?page=facebook&action=unlike&returnUrl=' + window.location.href;
	    });		
	},
// -------		
	playersScroller: function() {

		var scrollPane = $( ".scroll-pane" ), 
			scrollContent = $( ".scroll-content" );

		//build slider
		var scrollbar = $( ".scroll-bar" ).slider({
			slide: function( event, ui ) {
				var content =  $(this).parents('.scroll-pane').find('.scroll-content');
				if ( content.width() > scrollPane.width() ) {
					content.css( "margin-left", Math.round(
						ui.value / 100 * ( scrollPane.width() - content.width() )
					) + "px" );
				} else {
					content.css( "margin-left", 0 );
				}
			}
		});

		//append icon to handle
		var handleHelper = scrollbar.find( ".ui-slider-handle" )
							.append( "<span class='ui-icon ui-icon-grip-dotted-vertical'></span>" )
							.wrap( "<div class='ui-handle-helper-parent'></div>" ).parent();

		//change overflow to hidden now that slider handles the scrolling
		scrollPane.css( "overflow", "hidden" );

		//size scrollbar and handle proportionally to scroll distance
		function sizeScrollbar() {
			var remainder = scrollContent.width() - scrollPane.width();
			var proportion = remainder / scrollContent.width();
			var handleSize = 120;
			scrollbar.find( ".ui-slider-handle" ).css({
				width: handleSize,
				"margin-left": -handleSize / 2
			});
			handleHelper.width( scrollbar.width() - handleSize );
		}

		//reset slider value based on scroll content position
		function resetValue() {
			var remainder = scrollPane.width() - scrollContent.width();
			var leftVal = scrollContent.css( "margin-left" ) === "auto" ? 0 :
				parseInt( scrollContent.css( "margin-left" ) );
			var percentage = Math.round( leftVal / remainder * 100 );
			scrollbar.slider( "value", percentage );
		}

		//if the slider is 100% and window gets larger, reveal content
		function reflowContent() {
				var showing = scrollContent.width() + parseInt( scrollContent.css( "margin-left" ), 10 );
				var gap = scrollPane.width() - showing;
				if ( gap > 0 ) {
					scrollContent.css( "margin-left", parseInt( scrollContent.css( "margin-left" ), 10 ) + gap );
				}
		}

		//change handle position on window resize
		$( window ).resize(function() {
			resetValue();
			sizeScrollbar();
			reflowContent();
		});
		
		//init scrollbar size
		setTimeout( sizeScrollbar, 10 );//safari wants a timeout
	},
// -------			
	colorbox: function() {
		
		$(".colorbox a").colorbox({
			scrolling:false,
			opacity: '.6',			
			initialWidth: '140px',
			initialHeight: '160px',			
			fixed: true
		});
				
		$(".colorbox-inline").colorbox({
			inline:true,
			scrolling:false,
			opacity: '.6',
			initialWidth: '500px',
			initialHeight: '300px',
			fixed: true
		});
		
		$('#cboxCloseCustom').live('click', function(e) {
			$.colorbox.close();
			e.preventDefault();
		});
	},	
// -------	
	playersCards: function() {
		
		$("#players a, #pitch a").click(function(e) {
			e.preventDefault();			
			$("#players a")
				.removeClass('active')
				.children('img')
				.stop(true,true)	
				.css({
					'margin-left': '0px', 
					'margin-top': '0px', 
					'width':'132px', 
					'height':'187px'});
			$("#players a[href^="+ this.hash +"]")
				.addClass('active')
				.children('img')
				.animate({
					'margin-left': '-51px', 
					'margin-top': '-70px', 
					'width':'232px', 
					'height':'330px'}, 300);
			$('.player')
				.hide()
				.filter(this.hash)
				.show();
		});
		
		if ($('#players').length) {
			var hash = window.location.hash.slice(6);
			if (hash) {
				$("#players a[href^=#"+ hash +"]").trigger('click');
			} else {
				$("#players a:eq(0)").trigger('click');				
			}
		}
		
	}	
// -------
};

$(function() { 
	hd.init(); 
});
