/* RESEN */
/* Javascript Functions
----------------------------------------------------------------------------- */


	$(document).ready(function() {
	
		markup();
		Cufon.replace('#main h3,#sidebar h3,.tabs a', { hover: true }); 
		lightbox();
		actions();
		replaceSubmitButtons();
		ads();
		ajaxContent();
		
	});
	
	$(window).load(function() {

		tabs();
		alerts();
		if (section == "trackmap") tracks();
		
		// Unhide Featured News Story
		$('#page-index #news ul.content > li:first-child').find('img').css('visibility','visible');
			
	});
		

/* FUNCTION: LINK BEHAVIORS
----------------------------------------------------------------------------- */

	
	function actions() {

		
/* Back Link */


		$('a[href=#back]').click(function() {
		
			history.go(-1);
			return false;
		
		});
		
		
/* Social Bookmarks */
			

		if ($('ul.share').length > 0) {
		
			var itemTitle = $('ul.share').parent().children('h4').text();
			$('ul.share .facebook a').attr('href', 'http://www.facebook.com/sharer.php?u=' + location.href);
			$('ul.share .delicious a').attr('href', 'http://del.icio.us/post?v=4&noui&jump=close&url=' + location.href + '&title=' + itemTitle);
			$('ul.share .stumbleupon a').attr('href', 'http://www.stumbleupon.com/submit?url=' + location.href + '&title=' + itemTitle);
			$('ul.share .digg a').attr('href', 'http://digg.com/submit?phase=2&url=' + location.href + '&title=' + itemTitle);

		}
		
	}
	
	
/* FUNCTION: TABS
----------------------------------------------------------------------------- */


	function tabs() {
	
	
/* Remove Empty Tabs */


		$('.tabbed > ul.tabs').children('li').each(function() {
		
			var pane = $(this).find('a').attr('href');
			if ($(this).parent().siblings(pane).children().length == 0) { 
			
				$(this).parent().siblings(pane).remove();
				$(this).remove();
				
			}
	
		});
		
	
/* Initiate Tabs */


		$('.tabbed > ul.tabs').tabs({
		
			show: function() {
			
				Cufon.refresh();
			
			}
		
		});
		
		
/* Fix Tabbed Paging */

		
		$('.tabbed .pane ul.paging a, .tabbed .pane ul.pages a').each(function() { 
		
			var pane = $(this).closest('.pane').attr('id');
			$(this).attr('href', $(this).attr('href') + '#' + pane); 
	
		});
		
		
	}
	
	
/* FUNCTION: REPLACE SUBMIT BUTTONS
----------------------------------------------------------------------------- */


	function replaceSubmitButtons() {
	
	
/* Replace Buttons */

	
		$('ul.actions input[type=submit]').each(function() {
					
			var language = $(this).attr('value');
			$(this).parent().addClass('submit');
			$(this).after('<a class="action submit" href="#submit">' + language + '</a>');

		});
		

/* Button Click */

	
		$('a.submit').click(function() {

			$(this).parents('form').submit();
			return false;
			
		});
		
		
/* Form Submit */


		$('form').submit(function() {
			
			if ($(this).attr('action').match('actions')) {
			
				var action = window.location.href.replace(window.location.protocol + "//" + window.location.hostname, '');
				$(this).attr('action', action);
				
			}
			
		});
		
	
	}
	
	
/* FUNCTION: ALERT
----------------------------------------------------------------------------- */


	function alerts() {
	
		$('div.alert').each(function() {
		
			$(this).prependTo('body').slideDown('fast');
		
		});
		
		$('div.alert').click(function() {
		
			$(this).slideUp('fast');
		
		});
			
	}
	
	
/* FUNCTION: MARKUP CHANGES
----------------------------------------------------------------------------- */

	
	function markup() {
	
	
/* Classes */
	
	
		$('#page-index #news ul.content > li:first-child').addClass('emphasis').find('img').css('visibility','hidden');
		$('#main #videos ul.content > li:nth-child(3n+1)').addClass('clear');

	
/* Markup Changes */


		$('.editorial.browse .emphasis').children('h4, span, em, div').wrapAll('<div class="lead"></div>');
		$('.editorial.browse .emphasis a.image img').each(function() { $(this).attr('src', $(this).attr('src').replace('thumb_', 'img_')); });		
	
		// Move Reteweet to message body
		$('#page-story #main iframe').insertAfter('#story > em');
	
	}
	
	
/* FUNCTION: TRACK LISTINGS
----------------------------------------------------------------------------- */


	function tracks() {
		
		$('ul#trackname a').click(trackFilter);
		
		$('ul#trackname > li').each(function() {
		
			var track = $(this).children('a').text().toLowerCase().replace(' ','-').replace('#','numeric');
			if ($('ul#tracks').find('li.' + track).length == 0 && track != "all") $(this).remove();
		
		});
	

	}
	
	function trackFilter() {
	
		$('ul#trackname li').removeClass('current');
		$(this).parent().addClass('current');
		
		var filterValue = $(this).text().toLowerCase().replace(' ','-').replace('#','numeric');
		$('ul#tracks li').addClass('hide');

		if (filterValue == "all") {
		
			$('ul#tracks li').removeClass('hide');
			
		} else {
		
			$('ul#tracks li.' + filterValue).removeClass('hide');
			
		}
		
		return false;
	
	}
	
	
/* FUNCTION: EMAIL PRINTER
----------------------------------------------------------------------------- */


	function camo(never, eat, shredded) {
	
		return '<a href="mailto:' + never + '@' + eat + '.' + shredded + '">' + never + '@' + eat + '.' + shredded + '</a>';
	
	}
	
	
/* FUNCTION: CUFON
----------------------------------------------------------------------------- */


	function cufon() {
	
		var replace = [
			'li',
			'h1'
		];
		
		Cufon.replace(replace.toString());
	
	}
	
	
/* FUNCTION: ADS
----------------------------------------------------------------------------- */


	function ads() {
	
		$('div.ad:not(.rectangle) ul.content').each(function() {
		
			if ($(this).children().length > 1) {
			
				$(this).cycle({
				
					speed: 300, 
					timeout: 10000
			
				});
				
			}
		
		});
	
	}
	
	
/* FUNCTION: AJAX CONTENT
----------------------------------------------------------------------------- */


	function ajaxContent() {
	
		$('div.ajax').each(function() {
		
			var content = $(this).attr('rel');
			$(this).load(content);
		
		});
	
	}
