jQuery(document).ready(function($) {
	
	$('.station').hover(function() {
			$(this).children('.overlay, .social-overlay').fadeIn(500);
	}, function() {
		$(this).children('.overlay, .social-overlay').fadeOut(500);
	});

    $('#anythingFader').anythingFader({
        autoPlay: true,                 // This turns off the entire FUNCTIONALY, not just if it starts running or not.
        delay: 8000,                    // How long between slide transitions in AutoPlay mode
        startStopped: false,            // If autoPlay is on, this can force it to start stopped
        animationTime: 1000,             // How long the slide transition takes
        hashTags: true,                 // Should links change the hashtag in the URL?
        buildNavigation: true,          // If true, builds and list of anchor links to link to each slide
        pauseOnHover: true,             // If true, and autoPlay is enabled, the show will pause on hover
        navigationFormatter: formatText   // Details at the top of the file on this use (advanced use)
    });

	var id = 'post';

	$('a.toggleVisual').click(
		function() {
			tinyMCE.execCommand('mceAddControl', false, id);
		}
	);

	$('a.toggleHTML').click(
		function() {
			tinyMCE.execCommand('mceRemoveControl', false, id);
		}
	);
	
	$("#post").keydown(function() {
		result = $("#post").val();
		result = result.replace("</p>",/\r\n\r\n/g).replace("</p>",/\n\n/g);
		//result = result.replace(/\r\n/g, "<br />").replace(/\n/g, "<br />");
		$("#post").val(result);
	});

	var urlChunks = $(location).attr('href').split('/');
	var path = '';
	for (var i=3; i<urlChunks.length; i++) {
		path += '/' + urlChunks[i];
	}
	
    $('.menu li a').each(function() {
		if ($(this).attr('href') == path) {
			$(this).parent().addClass('menu-active');
		}
	});
	

	setTimeout(function() { $(".lavaLamp").lavaLamp({ fx: "backout", speed: 700 }); }, 1000);
	
	$("#location").change(function() {
		$("#location-form").submit();
	});
	
});

function cleanPost () {
	var blocklist1, blocklist2;
	var content = $("#post").val();
	
	// Protect pre|script tags
	content = content.replace(/<(pre|script)[^>]*>[\s\S]+?<\/\1>/g, function(a) {
		a = a.replace(/<br ?\/?>[\r\n]*/g, '<wp_temp>');
		return a.replace(/<\/?p( [^>]*)?>[\r\n]*/g, '<wp_temp>');
	});

	// Pretty it up for the source editor
	blocklist1 = 'blockquote|ul|ol|li|table|thead|tbody|tfoot|tr|th|td|div|h[1-6]|p|fieldset';
	content = content.replace(new RegExp('\\s*</('+blocklist1+')>\\s*', 'g'), '</$1>\n');
	content = content.replace(new RegExp('\\s*<(('+blocklist1+')[^>]*)>', 'g'), '\n<$1>');

	// Mark </p> if it has any attributes.
	content = content.replace(/(<p [^>]+>.*?)<\/p>/g, '$1</p#>');

	// Sepatate <div> containing <p>
	content = content.replace(/<div([^>]*)>\s*<p>/gi, '<div$1>\n\n');

	// Remove <p> and <br />
	content = content.replace(/\s*<p>/gi, '');
	content = content.replace(/\s*<\/p>\s*/gi, '\n\n');
	content = content.replace(/\n[\s\u00a0]+\n/g, '\n\n');
	content = content.replace(/\s*<br ?\/?>\s*/gi, '\n');

	// Fix some block element newline issues
	content = content.replace(/\s*<div/g, '\n<div');
	content = content.replace(/<\/div>\s*/g, '</div>\n');
	content = content.replace(/\s*\[caption([^\[]+)\[\/caption\]\s*/gi, '\n\n[caption$1[/caption]\n\n');
	content = content.replace(/caption\]\n\n+\[caption/g, 'caption]\n\n[caption');

	blocklist2 = 'blockquote|ul|ol|li|table|thead|tbody|tfoot|tr|th|td|h[1-6]|pre|fieldset';
	content = content.replace(new RegExp('\\s*<(('+blocklist2+') ?[^>]*)\\s*>', 'g'), '\n<$1>');
	content = content.replace(new RegExp('\\s*</('+blocklist2+')>\\s*', 'g'), '</$1>\n');
	content = content.replace(/<li([^>]*)>/g, '\t<li$1>');

	if ( content.indexOf('<object') != -1 ) {
		content = content.replace(/<object[\s\S]+?<\/object>/g, function(a){
			return a.replace(/[\r\n]+/g, '');
		});
	}

	// Unmark special paragraph closing tags
	content = content.replace(/<\/p#>/g, '</p>\n');
	content = content.replace(/\s*(<p [^>]+>[\s\S]*?<\/p>)/g, '\n$1');

	// Trim whitespace
	content = content.replace(/^\s+/, '');
	content = content.replace(/[\s\u00a0]+$/, '');

	// put back the line breaks in pre|script
	content = content.replace(/<wp_temp>/g, '\n');

	return content;
}

function formatText(index, panel) {
	  return "";
}
