// JavaScript Document
jQuery(document).ready(function($) {
	//var root = $(".scrollable").scrollable({circular:true, speed: 1200}).autoscroll({ autoplay: true });
	//window.api = root.data("scrollable");
	
	/*MENU SUPERFISH*/
	$('ul#menu').superfish({delay: 1000, autoArrows:false, dropShadows:false});
	
	/* NEWS TICKER */
	$('#TickerUL').liScroll({travelocity: 0.03});
	
	/*Search Box (remove default text on focus, then put back in)*/
	$('#s').focusin(function(){
		var sv = $('#s').attr('value'); 
		if( sv == 'Search' ){ $(this).attr('value', '');}
	});
	$('#s').focusout(function(){
		var sv = $('#s').attr('value'); 
		if( !sv ){ $(this).attr('value', 'Search');}
	});
	
	//Submit Social Filter on Select
	$(".feedpage button").hide();
	$(".feedpage p").width(270);
	$(".feedpage select").change(function() { 
		$('form#SocialFilter').submit(); 
	});
	
	$('.Upcoming:last').addClass('furthest');
	
	$('#SocialMediaFeed').tinyscrollbar();
	
	$(".inline").colorbox({inline:true, width:"460px"});
	
if( $(window).width() < 480 ){ //IF WINDOW WIDTH IS IPHONE SIZE
	
	$.colorbox.remove(); //Disable ColorBox

	//Athlete Bio Overlay
	$('.popupcard').hide();
	$('.showbio').click(function(){
		var bioID = $(this).attr('href');
		var BioDiv = $(bioID).html();
		//$('body').addClass('biopopupbody');
		//alert(BioDiv);
		$('#Wrapper').after('<div class="popupcard curbio" style="display:none;"><a href="#close" class="closebio">x</a>'+BioDiv+'</div>');
		$('body>*').not('.curbio, .curbio *').hide(); $('html, body').animate({ scrollTop: 0 }, 0); 
			
		$('.curbio').fadeIn('slow');
		return false;
	});
	$('.closebio').live('click', function(e){
		if (e.target === this){
			//$('body').removeClass('biopopupbody');
			$('.curbio').remove();
			$('body>*').not('.popupcard, .desktop, .MainMenu').fadeIn('slow'); 
			return false;
		}
	});
	
}
	var FullMenu = $('.MainMenu').html();
	function MobileBehaviors(){
	  	//MOBILE BEHAVIORS
	  	var WinW = $(window).width();
	  	if( WinW <= 1024 ) {
	  		
	  		if( $('.newsletter.mobile').length == 0 && $('.newsletter').length > 0 ){
		  		//MOVE NEWSLETTER BELOW FIRST ARTICLE
		  		var news = '<div class="newsletter mobile">'+$('.newsletter').html()+'</div>';
		  		$('.newsletter').remove();
		  		$('article:eq(0)').append(news); 
			}
			
	  		//MOVE SIDEBAR BELOW FIRST ARTICLE
			if( $('.Sidebar.mobile').length == 0  && $('.Sidebar').length > 0 ){
		  		var sidebar = '<aside class="Sidebar mobile">'+$('.Sidebar').html()+'</aside>';
		  		 $('.Sidebar').remove();
		  		 $('article:eq(0)').after(sidebar); 
			}
			
			//MOVE SEARCHBOX ABOVE ARTICLE
			if( $('.SearchBox.mobile').length == 0  && $('.SearchBox').length > 0 ){
		  		var searchbox = '<div class="SearchBox mobile">'+$('.SearchBox').html()+'</div>';
		  		 $('.SearchBox').remove();
		  		 $('.MainMenu').before(searchbox); 
			}
	  		
			if( $('.MainMenu select').length == 0  && $('.MainMenu').length > 0 ){
		  		// Create the dropdown base
		  		$("<select />").appendTo(".MainMenu");
		  		
		  		// Create default option "Go to..."
		  		$("<option />", {
		  		   "selected": "selected",
		  		   "value"   : "",
		  		   "text"    : "MENU ▼"
		  		}).appendTo(".MainMenu select");
		  		
		  		// Populate dropdown with menu items
		  		$(".MainMenu a").each(function(i) {
		  		 var el = $(this);
		  		 var isSub = '';
		  		 var parentTag = $(this).parent().parent().parent().get(0).tagName;
		  		 if( parentTag == 'LI' ){ isSub = ' - '; }
		  		 
		  		// if( isSub == 1 ){ var text = el.text(); } else var text = el.text();
		  
		  		 $("<option />", {
		  		     "value"   : el.attr("href"),
		  		     "text"    : isSub+el.text()
		  		 }).appendTo(".MainMenu select");
		  		});
		  		
				$('.MainMenu ul').remove();
		  		$(".MainMenu select").change(function() { window.location = $(this).find("option:selected").val(); });
			}
	  
	  	}else{
			var docH = $(document).height();
			var conH = $('.Content').height()+167;
			if( docH > conH ) $('.Content').height(docH-167);
			$("a.youtubeimg").colorbox({iframe:true, innerWidth:'60%', innerHeight:'70%'});
			$("a.flickrimg").colorbox({ width: false, height: false, maxWidth: false, maxHeight: false });
			
			if( $('.newsletter.mobile').length > 0 ){
		  		//MOVE NEWSLETTER BACK TO TOP
		  		var news = '<div class="newsletter">'+$('.newsletter').html()+'</div>';
		  		$('.newsletter').remove();
		  		$('.Content h1').append(news); 
			}
			
			if( $('.Sidebar.mobile').length > 0 ){
		  		var sidebar = '<aside class="Sidebar">'+$('.Sidebar').html()+'</aside>';
		  		 $('.Sidebar').remove();
		  		 $('.Content div:eq(0)').after(sidebar); 
			}
			
			if( $('.MainMenu select').length > 0 ){
				$('.MainMenu select').remove();	
				$('.MainMenu').prepend(FullMenu);
			}
			if( $('.SearchBox.mobile').length > 0 ){
		  		var searchbox = '<div class="SearchBox">'+$('.SearchBox').html()+'</div>';
		  		 $('.SearchBox').remove();
		  		 $('.MainMenu').after(searchbox); 
			}
			
		}
	}
	MobileBehaviors();
	$(window).resize(function() {
	  MobileBehaviors();
	});
	
	//Set Background Image Height to Content Height
	var docH = $(document).height();
	$('.Backing').css({'height': docH+'px', 'margin-bottom': '-'+docH+'px'});
	

});	
