
var ytplayer = '';
    


jQuery(function($){
       
    // Split this into two different searches b/c it won't work chaining it all together.
    $('#subNav li.active').not(':first-child').prepend('<span class="left"></span>');
    $('#subNav li.active').not(':last-child').append('<span class="right"></span>');
    
    // If the .tabsNav class is found on the page, then apply tabs plugin to it
    if( $('body#home .tabsNav').length > 0 )
    {
        $('body#home ul.tabsNav').tabs();
    } 
    
    // Make the column corners rounded if a column exists, and the curvycorners plugin is available
    /*
    if( $('.column.full').length > 0 || $('.column.fill').length > 0 && $.fn.corner ) 
    {
        $('.column.full, .column.fill').corner({
            tl: { radius: 8 },
            tr: { radius: 8 },
            bl: { radius: 8 },
            br: { radius: 8 },
            antiAlias: true 
    	}); 
    }
    */

    $('#stateInformation a[href^=http]').add('#stateInformation a[href^=https]').click(function() {
        var href = $(this).attr('href');
        window.open(href,'newWindow'); 
	    return false;
    });

    // Any link found in the #contentInner area that has the full URL needs to get the externalLink icon image
    $('#contentInner a[href^="http:"]').
    	add('#contentInner a[href^="https:"]').
    	not('a[href^="http://' + window.location.host + '"]').
    	not('a[href^="https://' + window.location.host + '"]').
    	after('<img class="externalLink" src="' + app['base_url'] + 'assets/images/icon_external_link.png" />');
    
    
    $('#map_panel_wrapper a[href^="http"] + img.externalLink').remove();
    $('#banner a[href^="http"] + img.externalLink').remove();
    $('#banner a[href^="https"] + img.externalLink').remove();
    $('a[href^="http"].nojumpicon + img.externalLink').remove();
    $('a[href^="https"].nojumpicon + img.externalLink').remove();
    $('#recaptcha_table img.externalLink').remove();
    
    
    $('a[href^="http"]').
    	add('a[href^="https"]').
    	not('a[href^="http://' + window.location.host + '"]').
    	not('a[href^="https://' + window.location.host + '"]').
    	not('a[href^="http"].nopopup').
    	click(function(){
			var rel = $(this).attr('rel');
			var href = $(this).attr('href');

			if(!rel || rel == 'nofollow'){
				window.open(href,'newWindow');
			} else {
				var rel_split = rel.split('|');
				var w = rel_split[0];
				var h = rel_split[1];
				var scrollbars = rel_split[2];
				var leftPos=(screen.width-w)/2;
				var topPos=(screen.height-h)/2;
				window.open(href,'newWindow','width='+w+',height='+h+',left='+leftPos+',top='+topPos+',scrollbars='+scrollbars+',resizable=no,statusbar=no,menubar=no,toolbar=no');
			}

			try
			{
				//track explicitly coded links (matches "tracking_path_to_whatever")
				var classes = $(this).attr('class').split( " " );
				if ( classes != null )
				{
					for ( var x=0; x<classes.length; x++ )
					{
					   var theclass = classes[x];
					   if ( theclass != null && theclass.indexOf("tracking") == 0 )
					   {
					   	var trackpath = '/' + theclass.split("-").join("/");
						//alert( 'testing: ' + trackpath );
						pageTracker._trackPageview(trackpath);
					   }
					}
				} 	
				//track all outbound links
				if ( href.indexOf("://") > 0 )
				{
					var outbound = '/tracking/outbound/' + href.split("://",2)[1];
					//alert('outbound: ' + escape(outbound));
					pageTracker._trackPageview(outbound);
				}
			}
			catch ( e )
			{
				//do nothing
			}

			return false;		
		});
     
    // Wrap each li's content with a span, which will show the text as black, b/c the list itself is given 
    // a color corrresponding to each section's color. This is the only way to get numbered lists to have a
    // different color numbers and text, which is purely presentational. If JS isn't available, all ol's on the
    // site are just displayed with as black.
    $('ol').addClass('orderedList').children('li').wrapInner('<span></span>'); 
    
    // Any image given this class will be wrapped in a div, and the ALT text will be displayed below the image
    // in a p tag. This is an easier way to manage callout images without having to manually add all the HTML 
    // necessary to get the style.
    $('img.callOutLeft, img.callOutRight, img.callOutMargin').each(function(){
        var styles = $(this).attr('class');   
        var text = $(this).attr('alt');      
        
        $(this).attr('class','').wrap('<div class="'+ styles +'"></div>').after('<p>'+ text +'</p>');
    });
    
    // Some content areas have anchored jump links. This is taking a UL in the content area, CSS is pushing it to the 
    // left of the content into it's own column, then we're collapsing all the LIs, and adding a first LI used
    // to show the rest of the links.
    $('ul.contentNav').show().prepend('<li class="first"><a href="#"><b>Jump To Content</b></a></li>');
    $('ul.contentNav li.first a').click(function(){ return false;});
    $('ul.contentNav li').not(':first-child').hide();
    $('ul.contentNav').hover(function(){
        $(this).children('li').fadeIn();
    }, function(){
        $(this).children('li').not(':first-child').hide();
    });
	
    // Same but State version
    $('ul.contentNav2').show().prepend('<li class="first"><a href="#"><b>Select a State</b></a></li>');
    $('ul.contentNav2 li.first a').click(function(){ return false;});
    $('ul.contentNav2 li').not(':first-child').hide();
    $('ul.contentNav2').hover(function(){
        $(this).children('li').fadeIn();
    }, function(){
        $(this).children('li').not(':first-child').hide();
    });	
    
    // The Accordian lists are used all over the site. This is just taking the 'more' link and making it toggle
    // the extra information that goes along with the content.
    $('ul.accordian dt a.more').not('a.blog_more').toggle(function(){
        var item = $(this).attr('rel');
        
        $(this).html('&laquo; close'); 
        $('#'+ item +'_more').show();
        
        return false;
        
    }, function(){ 
        var item = $(this).attr('rel');
        
        $(this).html('&raquo; more'); 
        $('#'+ item +'_more').hide();        
        
        return false;
    });
    
    $('a.popup').click(function(){
		var rel = $(this).attr('rel');
		var href = $(this).attr('href');
		if(!rel){
			window.open(href,'newWindow');
		} else {
			var rel_split = rel.split('|');
			var w = rel_split[0];
			var h = rel_split[1];
			var scrollbars = rel_split[2];
			var leftPos=(screen.width-w)/2;
			var topPos=(screen.height-h)/2;
			window.open(href,'newWindow','width='+w+',height='+h+',left='+leftPos+',top='+topPos+',scrollbars='+scrollbars+',resizable=no,statusbar=no,menubar=no,toolbar=no');
		}
		return false;		
	});
	
	// Stripe table rows
	$('.alternateRows tr').mouseover(function(){
		$(this).addClass('tableRowHover');
	}).mouseout(function(){
		$(this).removeClass('tableRowHover');
	});
	$('.alternateRows tr:even').addClass('altRow'); 
    
    
    /* For Master Casters */
    $('.gallery a').click(function(){
        $(this).addClass('active').siblings().removeClass('active');
        var img = $(this).children('img').attr('src');
        //var alt = $(this).children('img').attr('alt');

        var parts = img.split('_');
        var lg_img = parts.slice(0, parts.length - 1);
        lg_img = lg_img.join('_') + '_lg.jpg';
        
        ytplayer.pauseVideo(); 
        var wasPlaying = false;
        if(ytplayer.getCurrentTime() > 0){
            wasPlaying = true;
        }

        $('.mcContentMain .photo').hide().html('<img src="'+ lg_img +'" /><p><b>&laquo; BACK TO VIDEO</b></p>').fadeIn().bind('click', function(){
            $(this).fadeOut();
            $('.mcContentMain .video').css('visibility', 'visible');
            
            $('.gallery a').each(function(){
                $(this).removeClass('active');
            });
            
            if(wasPlaying){
                ytplayer.playVideo();
            } 
            
        });
        $('.mcContentMain .video').css('visibility', 'hidden');   
        
        return false;
    }); 



	$('#bannerSelectNav').change(function(){
		window.location = $(this).val();
	});
	$('#bannerSelectNav .active').attr('selected','selected');

    


   //Media Player
   //burn through all mediaplayer classes and aadd anchors to thumbs to trigger content change
    $('.mediacontainer .mediaitem .thumb').click(
      function(event){

        $('.mediawrapper li div.thumb').removeClass('active');
        $(this).addClass('active');

        $(this).parents('.mediawrapper').children('.mediatarget').html(
          $(this).next('.itemcontent').html()
        );           
      }                        
    );                                 

   //activate first item in each mediaplayer 
    $('.mediawrapper .mediatarget').each(       
     function(event) {
       $(this).parent().find('.mediaitem').eq(0).find('.thumb').addClass('active');
       $(this).html( $(this).parent().find('.mediaitem .itemcontent').eq(0).html() );
     }                                                  
    );   


	// due to how the background images with shadows are attached to the li we'd 
	// have to create another image with a vertical line down the middle, then
	// the two columns within the lis wouldn't be able to be resized without making
	// a new image. the ratings design calls for the ratings to be at the bottom of the
	// li, and line up horizontally with each other, thus breaking the vertical line/border
	// that is on the .contentSub div in the middle (visually very bad). so this bit o goodness
	// is making sure those two content divs are at least the same hight, so the verical
	// line is solid through the middle. did you actually read this whole comment? long isn't it.
	$('body#community ul.accordian li').not('li.noresize').each(function(){
		var h = $(this).children('.contentMain').height();
		$(this).children('.contentSub').css('min-height', h + 3);
	});

    
    
    
    // Eventually break these following rules into an IE 6 only JS file
    $('#subNav li:last-child').addClass('last-child');
    
    // make sure the li:hover works in IE 6 
    $('#sideNav li').hover(function(){
        $(this).addClass('hover');
    }, function(){
        $(this).removeClass('hover');
    });
    
});

function addMediaPlayer( id, numelems )
{
    //$('.mediacontainer').jCarouselLite({
    $('#' + id ).jCarouselLite({
    startID: 0,
    btnNext: "#" + id + "_next",
    btnPrev: "#" + id + "_prev",
    scroll: numelems,
    speed: 1000,
    visible: numelems,
    circular: false
   });
   //$('.mediacontainer').css('visibility', 'visible');
   $('#' + id).css('visibility', 'visible');

}

function onYouTubePlayerReady(playerId) 
{
    ytplayer = document.getElementById("myytplayer");
} 

 

