// JavaScript Document
$(document).ready(function () {
	// turn current pages on
	var location = window.location.href;
	var url = location;
	location = location.split('/');
	var page = location[location.length-1];
	
	// ignore variables on community page
	url = url.replace(/\?action=photos/, '');
	url = url.replace(/\?action=videos/, '');
	
	
	$("a").each(function () {
		var href = $(this).attr('href');
		if (href == page || href == '/' + page || href == './' + page || href == url || href == url.replace('http://', 'http://www.')) {
			$(this).addClass('on');
			
			// slightly smoother dog ears 18 pixels removed again after dog ear added.
			$(this).not(".blog a").not(".subnav a").width($(this).width() + 18);
		}
	});
	
	// turn on blog page
	$("body.blog #nav div.blog a").addClass('on');

	
	
	// roll over effects
	function initNav() {
		$("div#nav div a").not("a.on").not(".subnav a").hover(
			function () { 
				$(this).clearQueue();
				if (!$(this).hasClass('on')) {
					$(this).animate({'padding-right': '17px'}, 200); 
				}
			},
			function () {
				$(this).animate({'padding-right': '0px'}, 200);
			});
	}
	
	// dog ear stuff
	function dogEarIt()
	{
		$("div#nav div a.on").not(".subnav a").each(function () {
			var ear_src = $(this).children("img").attr('src').replace('.gif', '_ear.png');
			$(this).css({'padding-right': '0px'});
			$(this).after("<img class='ear' src='" + ear_src + "' width='18' height='58' />");
			// wait 0.5 seconds then remove the 18 pixels we added above
			var onlink = $(this).not('.blog a');
			setTimeout(function () {
				onlink.width(onlink.width() - 18);
			}, 200);
		});
	}
	
	// mark parent nav as on for dog ears (community subnav)
	$("div#nav .subnav a.on").each(function () {
		$(this).parent().parent().find('a:first').addClass('on');
	});
	
	// preload all of the dog ears
	$("body").append('<div id="preloader" style="display:none"></div>');
	$("div#nav div a").not(".subnav a").each(function () {
		var ear_src = $(this).children("img").attr('src').replace('.gif', '_ear.png');
		$("#preloader").append('<img src="' + ear_src + '" />');
	});
	
	// temporary function to handle dog ear on click
	/*
	$("div#nav div a").click(function () {
		$("div#nav div a").removeClass("on");
		$("div#nav div").children().not('a').remove();
		$(this).addClass("on");
		
		dogEarIt();
		initNav();
		//return false;
	});
	*/
	
	dogEarIt();
	initNav();
});