/* National Park Theme functions by Matthew Buchanan v1.0.1 */

function webkitSearch() {
	var defaultValue = "Search all posts..."; // Default Value
	if ($.browser.webkit) {
		var sf = document.getElementById("searchfield");
		sf.setAttribute("type", "search");
		sf.setAttribute("autosave", "saved.data");
		sf.setAttribute("results", "10");
		sf.setAttribute("placeholder", defaultValue);
		$("#searchfield").addClass("webkit");
		$("#search .button").hide();
	} else {
		$("#searchfield").val(defaultValue);
		$("#searchfield").css("color", "#8a8a8a");
		$("#searchfield").focus( function() {
			if ($(this).val() == defaultValue) {
				$(this).val("");
			}
			$(this).css("color", "#334");
		});
		$("#searchfield").blur( function() {
			if ($(this).val() == "") {
				$(this).val(defaultValue);
				$(this).css("color", "#8a8a8a");
			}
		});
	}
}

function handleAudioArt() {
	// Adapted from an idea by Jarred Bishop (http://jarredbishop.info)
	$("div.audio ul.tags li a").each(function() {
		var tag = $(this).text();
		if (tag.indexOf("art:") != -1) {
			var imgUrl = tag.replace("art:","");
			$(this).parent().parent().parent().prev().prepend("<div class='album'><img src='"+imgUrl+"' width='70' height='70' border='0' alt='' /></div>");
			$(this).parent().hide();
		}
	});
	$("ul.albums,div.album").before("<div class='overlay'><img src='http://static.tumblr.com/gpln05e/gqEkzf95y/albums.png' width='170' height='155' border='0' usemap='#albumlinks' /></div>");
}

function fixVimeo() {
	/*
		Better Vimeo Embeds by Matthew Buchanan
		http://matthewbuchanan.name/141302328

		Released under a Creative Commons attribution license:
		http://creativecommons.org/licenses/by/3.0/nz/
	*/
	$("div.post object[data^='http://vimeo.com']").each(function() {
		var $parent = $(this).parent();
		var vimeoCode = $parent.html();
		var params = "";
		if (vimeoCode.toLowerCase().indexOf("<param") == -1) {
			$("param", this).each(function() {
				params += $(this).get(0).outerHTML;
			});
		}
		var oldOpts = /show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=00ADEF/g;
		var newOpts = "show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=55CC55";
		vimeoCode = vimeoCode.replace(oldOpts, newOpts);
		if (params != "") {
			params = params.replace(oldOpts, newOpts);
			vimeoCode = vimeoCode.replace(/<embed/i, params + "<embed");
		}
		$parent.html(vimeoCode);
	});
}

function fixYouTube() {
	/*
		Widescreen YouTube Embeds by Matthew Buchanan & Hayden Hunter
		http://matthewbuchanan.name/451892574
		http://blog.haydenhunter.me

		Released under a Creative Commons attribution license:
		http://creativecommons.org/licenses/by/3.0/nz/
	*/
	$("div.post object").each(function () {
		if ($(this).find("embed[src^='http://www.youtube.com']").length > 0) {
			// Identify and hide embed(s)
			var parent = $(this).parent();
			parent.css("visibility","hidden");
			var youtubeCode = parent.html();
			var params = "";
			if (youtubeCode.toLowerCase().indexOf("<param") == -1) {
				// IE doesn't return params with html(), so…
				$("param", this).each(function () {
					params += $(this).get(0).outerHTML;
				});
			}
			// Set colours in control bar to match page background
			var oldOpts = /rel=0/g;
			var newOpts = "rel=0&amp;color1=0xFFFFFF&amp;color2=0xFFFFFF"; /* 0xEBF9FE */
			youtubeCode = youtubeCode.replace(oldOpts, newOpts);
			if (params != "") {
				params = params.replace(oldOpts, newOpts);
				youtubeCode = youtubeCode.replace(/<embed/i, params + "<embed");
			}
			// Extract YouTube ID and calculate ideal height
			var youtubeIDParam = $(this).find("embed").attr("src");
			var youtubeIDPattern = /\/v\/([0-9A-Za-z-_]*)/;
			var youtubeID = youtubeIDParam.match(youtubeIDPattern);
			var youtubeHeight = Math.floor(parent.find("object").width() * 0.75 + 25 - 3);
			var youtubeHeightWide = Math.floor(parent.find("object").width() * 0.5625 + 25 - 3);
			// Test for widescreen aspect ratio
			$.getJSON("http://gdata.youtube.com/feeds/api/videos/" + youtubeID[1] + "?v=2&alt=json-in-script&callback=?", function (data) {
				oldOpts = /height="?([0-9]*)"?/g;
				if (data.entry.media$group.yt$aspectRatio != null) {
					newOpts = 'height="' + youtubeHeightWide + '"';
				} else {
					newOpts = 'height="' + youtubeHeight + '"';
				}
				youtubeCode = youtubeCode.replace(oldOpts, newOpts);
				if (params != "") {
					params = params.replace(oldOpts, newOpts);
					youtubeCode = youtubeCode.replace(/<embed/i, params + "<embed");
				}
				// Replace YouTube embed with new code
				parent.html(youtubeCode).css("visibility","visible");
			});
		}
	});
}

$(function() {
	/* Cufon.set("fontFamily", "Mirisch").replace("#header h1", { fontSize: "44px" });
	Cufon.set("fontFamily", "Aller").replace("#pagenav a", { hover: true });
	
	Cufon.set("fontFamily", "Aller").replace("div.description", { fontSize: "10px", hover: true });
	Cufon.set("fontFamily", "Aller").replace("#side h2", { fontStyle: "italic" });
	Cufon.set("fontFamily", "Aller").replace("#disqus_thread h3", { fontStyle: "italic", color: "#55CC55" });
	var iv = setInterval(function() {
		if ($("#disqus_thread h3").length) {
			Cufon.refresh("#disqus_thread h3");
			clearInterval(iv);
		}
	}, 100); */
	

	if ($("#pagenav").length == 0) $("#header").addClass("no-pagenav");
	$("a.notes-button").each(function() {
		var noteString = $(this).html().substring(0,$(this).html().indexOf(" "));
		var noteCount = parseInt(noteString);
		if (noteCount > 0 || noteString.indexOf(",") != -1) $(this).addClass("fave");
	});
	
	$("#followed-mask").addClass("shortpanel").after("<a href='#' id='morelink'>Show <span id='all'>All " + $(".followed").children().length + "</span><span id='some' class='hidden'>a few</span></a>");
	/*
	$("#morelink").click( function(event) {
		event.preventDefault();
		var listheight = $(".followed").height();
		var maskheight = $("#followed-mask").height();
		$("#followed-mask").animate({ height: maskheight == 188 ? listheight : 188 }, "slow");
		$("#morelink").toggleClass("some");
		$("#all").toggleClass("hidden");
		$("#some").toggleClass("hidden");
	});
	*/
	
	$(".quote .content > p:first-child").prepend("&mdash;&nbsp;");
	$("embed.photoset").parent().wrapInner('<div class="border"></div>');
	$("div.content:contains('The URL you requested could not be found.')").next().hide();
	$("a.permalink").each(function() {
		if ( $(this).attr("href") == "" ) $(this).parent().hide();
	});
	
	$(".content p").widont();
	$("ol.notes li:last-child").addClass("last");
	/* $("#fine-print").prepend('<a href="http://nationalparktheme.tumblr.com">National Park theme</a> by <a href="http://matthewbuchanan.name">Matthew Buchanan</a>.'); */

	/* Be That Woman Additions */
	
	$("a.home-swap").click(function() {     	
    	$("#video-img, #video-player").toggle();
	});
	
	if(window.location.href.match(/submit/) !== null ){ 
		$(".pagetype, .meta, .text h2:first-child").hide();
		$("#pagenav ul li").eq(1).addClass("selected");
	}
	
	if(window.location.href.match(/contest/) !== null ){ 
		$(".pagetype, .meta, .text h2:first-child, .content p").hide();
		$("#pagenav ul li").eq(2).addClass("selected");
	}

	if(window.location.href.match(/ask/) !== null ){ 
		$(".pagetype, .meta").hide();
		$(".text h2:first-child").click(function(e) {
        	e.preventDefault();
    	});
    	$(".content").append('<p style="font-style:italic;">Your contact information will be kept private.</p>');
    	$("#pagenav ul li").eq(4).addClass("selected");
	}
	
	if(window.location.href.match(/tagged/) !== null ){ 
		$("#home-video, .feed").hide();
	}
	
	if(window.location.href.match(/page/) !== null ){ 
		$("#home-video, .feed").hide();
		$("#main div.post-spacer").eq(0).hide();
	}
	
	$("#pagenav ul li a").eq(0).addClass("home-icon").css('padding-left','35px');
	
	
	$("a.fancybox-video").click(function() {
	$.fancybox({
			'padding'		: 10,
			'autoScale'		: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'hideOnOverlayClick'	: false,
			'title'			: this.title,
			'width'			: 640,
			'height'		: 385,
			'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
			'type'			: 'swf',
			'swf'			: {
			   	 'wmode'		: 'opaque',
				'allowfullscreen'	: 'true'
			}
		});

		return false;
	});
	
	webkitSearch();
	/* handleAudioArt(); */
	/* fixVimeo(); */
	/* fixYouTube(); */

});
