$(function() {
	
	/*-------------------------------------------------------
	*	Main Variables
	-------------------------------------------------------*/
	
	agent = navigator.userAgent.toLowerCase();
	url = document.URL;
	root = url.substring(0,url.indexOf('/',7));

	/*-------------------------------------------------------
	*	Add Missing Paragraph Tags
	*
	*	(via matthewbuchanan.name)
	-------------------------------------------------------*/
	
	$("div.body:not(:has(p,ol,ul,h1,h2,h3,h4,h5,h6))").wrapInner("<p></p>");
	
	/*-------------------------------------------------------
	*	Remove meta data from Ask/Submit posts
	-------------------------------------------------------*/
	
	if(url.match(root + "/ask") || url.match(root + "/submit"))
	{
		$('div.post_meta').remove(); 
	}
	
	/*-------------------------------------------------------
	*	Quotes around quotes :)-
	-------------------------------------------------------*/
	
	$('h1.quote').each(function(){
		
		if($(this).find("p").length == 0) $(this).wrapInner("<p></p>");

		if($(this).find("br").length > 0)
		{
			var html = $(this).html().replace("<br>\n", "</p><p>");
			$(this).html(html);
		}
		
		$(this).find("p").prepend('"');
		
		$(this).find("p:last").append('"');
	});
	
	/*-------------------------------------------------------
	*	Post Notes/Comments Full Display
	-------------------------------------------------------*/
	
	$('.post_notes, .disqus_comments').hover(function(){
		$(this).attr('id', 'full_display');		
	});
	
	$('.post_meta').hover(function(){
		if($(this).hasClass('permalink')) $(this).attr('id', 'hovered');
	});
	
	if(url.indexOf('#') > 0)
	{
		var urlsplit = url.split('#', 2);
		
		if(urlsplit[1] == 'notes') $('.post_notes').attr('id', 'full_display');
				
		if(urlsplit[1] == 'disqus_thread') $('#disqus_thread').attr('id', 'full_display');
		
		$('.post_meta').attr('id', 'hovered');
	}
	
	$('a.notes').click(function(){
		$('.post_notes').attr('id', 'full_display');
	});
	
	$('a.dsq-comments-count').click(function(){
		$('#disqus_thread').attr('id', 'full_display');
	});
	
	/*-------------------------------------------------------
	*	Tag Formatting
	*
	*	This keeps tags looking good when there are too
	*	many of them
	-------------------------------------------------------*/
	
	$('li.tags').each(function(){
		
		if($(this).height() > 25)
		{
			var total_tags = $(this).find('a').length;
			var half = Math.round(total_tags / 2) - 1;
			$(this).find('a:eq(' + half + ')').after("<br />");
		}

	});
	
	/*-------------------------------------------------------
	*	Search/Tag Pages
	-------------------------------------------------------*/
	
	if(url.match(root + "/search") || url.match(root + "/tagged"))
	{	
		if(!$("#page_info").hasClass("no_results"))
		{
			$("#page_info").prepend('<p class="close"><a href="#">X</a></p>');
		}
	}
	
	$("p.close a").click(function(){	
		$("#page_info").remove();
	});
	
	/*-------------------------------------------------------
	*	Internet Explorer Notice
	-------------------------------------------------------*/
	
	if(agent.match(/msie/i))
	{
		$('#header').after('<div id="page_info"><p>You are using Internet Explorer. Please download a <a id="safari" href="http://www.apple.com/safari/" title="Safari">better</a> <a id="firefox" href="http://www.mozilla.com/en-US/firefox/ie.html" title="Firefox">browser</a>.</p><div class="clear"></div></div>');
		
		$('a#safari, a#firefox').click(function(e) {
			e.preventDefault();
			pageTracker._trackPageview("/outgoing/ie_warning/" + $(this).attr('id'));
			window.location.href = $(this).attr('href');
		});

		return;
	}
	
	/* ******************************************************
	*
	*	NON_INTERNET EXPLORER BELOW HERE
	*
	*********************************************************
	
	/*-------------------------------------------------------
	*	Video Posts
	-------------------------------------------------------*/
	
	$("object").each(function(){
	
		/*  Bigger Videos
		-------------------------------------------------------*/
		
		var new_width = 700,
			height = $(this).attr("height"),
			width = $(this).attr("width"),
			new_height = Math.round((height / width) * new_width);
		
		$(this).attr("width", new_width).attr("height", new_height);
		
		$(this).parent().find("embed").attr("width", new_width).attr("height", new_height);
			
		/*  Embed Options (via matthewbuchanan.name)
		-------------------------------------------------------*/
			
		var parent = $(this).parent(),
			videoCode = parent.html(),
			oldOpts = "",
			newOpts = "",
			params = "";
			
		if ($(this).find("param[value^='http://www.youtube.com']").length > 0) 
		{
			var oldOpts = /rel=0/g,
				newOpts = "rel=0&amp;hd=1&amp;color1=0xFFFFFF&amp;color2=0xFFFFFF";
		}
		
		if(parent.find("object[data^='http://vimeo.com']").length > 0)
		{
			var oldOpts = /show_title=[01]&amp;show_byline=[01]&amp;show_portrait=[01]&amp;color=([a-fA-F0-9]){1,6}/g,
				newOpts = "show_title=0&amp;show_byline=0&amp;show_portrait=1&amp;color=4BCEFD";
		}
		
		videoCode = videoCode.replace(oldOpts, newOpts);
		
		parent.html(videoCode);
		
	});
	
});

/*-------------------------------------------------------
*	New Audio Player
-------------------------------------------------------*/

function newAudioPlayer(color, player, id, randomize)
{
	if(!player)
	{
		return;
	}
	
	color = color.replace("#", "");
	player = player.replace("color=FFFFFF", "color=" + color);
	
	replaceIfFlash(9, id, player);
	
	if(randomize == true)
	{
		var style_color = "#" + color;
		var parent = document.getElementById(id).parentNode;
		parent.style.borderStyle = "solid";
		parent.style.borderWidth = "3px";
		parent.style.borderColor = "#" + color;
	}
	
}

/*-------------------------------------------------------
*	Tumblr Audio Player (tumblr.com's script)
-------------------------------------------------------*/

function flashVersion()
{
    if (navigator.plugins && navigator.plugins.length > 0) {
        var mimeData = navigator.mimeTypes;
        if (mimeData && mimeData['application/x-shockwave-flash'] && mimeData['application/x-shockwave-flash'].enabledPlugin && mimeData['application/x-shockwave-flash'].enabledPlugin.description) {
            return parseInt(mimeData['application/x-shockwave-flash'].enabledPlugin.description.split(' ')[2].split('.')[0], 10)
        }
    } else if (navigator.appVersion.indexOf("Mac") == -1 && window.execScript) {
        try {
            var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
            var version = axo.GetVariable("$version");
            return version.split(',')[0].split(' ')[1]
        } catch(e) {}
        return 0
    }
}

function replaceIfFlash(version, element_id, replacement_string)
{
    if (flashVersion() >= version) document.getElementById(element_id).innerHTML = replacement_string
}

function renderVideo(element_id, src, width, height, flashvars)
{
    var agent = navigator.userAgent.toLowerCase();
    var is_iphone = (agent.indexOf('iphone') != -1);
    if (is_iphone)
    {
        document.getElementById(element_id).innerHTML = '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" width="' + width + '" height="' + height + '" codebase="http://www.apple.com/qtactivex/qtplugin.cab"><param name="src" value="' + src + '"><param name="qtsrc" value="' + src + '"><param name="autoplay" value="false"><embed src="' + src + '" qtsrc="' + src + '" width="' + width + '" height="' + height + '" pluginspage="http://www.apple.com/quicktime/"></embed></object>'
    }
    else
    {
        replaceIfFlash(10, element_id, '<embed type="application/x-shockwave-flash" src="http://www.tumblr.com/swf/video_player.swf?16" bgcolor="#000000" quality="high" class="video_player" allowfullscreen="true" height="' + height + '" width="' + width + '" flashvars="file=' + encodeURIComponent(src) + (flashvars ? '&amp;' + flashvars : '') + '"></embed>')
    }
}