$(document).ready(function(){
	
	// Input default value replace
	$("input, textarea").focus(function() {
		// only select if the text has not changed
		if(this.value == this.defaultValue) {
			this.select();
		}
	});
	$("input, textarea").keyup(function() {
		if(this.value != this.defaultValue) {
			$(this).removeClass("example");
		}
	});

	
	// Show in-page nav if more than one anchor link and if div exists (doesn't show on posts)
	if (($('.post h3').size() > 1) && ($('#page_nav_container').length)) {
		// Generate anchor links from H3's in posts
		$(".post h3").each(function()
		{
			var thisText = $(this).text();
			var anchorText = thisText.replace(/ /g, "-");
			var anchorLink = '<a name="' + anchorText + '"></a>';
			var anchorText = '<li><a href="#' + anchorText + '" class="anchor">' + thisText + '</a></li>';
			$(this).before(anchorLink);
			//$("ul.page_nav").append(anchorText);
			//$("</li>").appendTo(".page_nav").html("Please show up");
			$(anchorText).appendTo('ul.page_nav'); 
		});
		$('#page_nav_container').fadeIn(500);
	};
	

	// Scroll to top anchor
	var scroll_timer;
	var displayed = false;
	var $window = $(window);
	var top = $(document.body).children(0).position().top;
	$window.scroll(function () {
		window.clearTimeout(scroll_timer);
		scroll_timer = window.setTimeout(function () { // use a timer for performance
			if($window.scrollTop() <= top) // hide if at the top of the page
			{
				displayed = false;
				$('#scroll_top_message a').fadeOut(500);
			}
			else if(displayed == false) // show if scrolling down
			{
				displayed = true;
				$('#scroll_top_message a').stop(true, true).fadeIn(250).click(function () { $('#scroll_top_message a').fadeOut(500); });
			}
		}, 100);
	});
	
	
	// Scroll all anchor links smoothly
	$(function(){
		$('a[href*=#]').click(function() {
			if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
			&& location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target
			|| $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				var targetOffset = $target.offset().top;
				$('html,body')
				.animate({scrollTop: targetOffset}, 500);
				return false;
				}
			}
		});
	});
	

	// Fade front images
	$('#welcome_photos').cycle({fx:'fade',speed:2500,timeout:1500});
	
	
	// Vimeo embed
	var video_id = 6798947;
	function vimeo_player_loaded(swf_id) {
		//welcome_vid = document.getElementById(swf_id);
		//document.getElementById('controls').style.display = '';
		api_addEventListener('onProgress', 'vimeo_on_progress');
		api_addEventListener('onLoading',  'vimeo_on_loading');
		api_addEventListener('onFinish',   'vimeo_on_finish');
		api_addEventListener('onPlay',     'vimeo_on_play');
		api_addEventListener('onPause',    'vimeo_on_pause');
		api_addEventListener('onSeek',     'vimeo_on_seek');
		//document.getElementById('vimeo_duration').innerHTML = moogaloop.api_getDuration();
	}
	function vimeo_on_play(swf_id) {
		//document.getElementById('state').innerHTML = 'Playing';
		alert('playin');
	}
	function vimeo_on_pause(swf_id) {
		//document.getElementById('state').innerHTML = 'Paused';
	}
	function vimeo_on_finish(swf_id) {
		//document.getElementById('state').innerHTML = 'Finished';
	}
	
	$('#welcome_vid').flashembed({
		src: 'http://vimeo.com/moogaloop.swf',
		height:220,
		width:400,
		wmode:'opaque',
		w3c:true,
		version:[9,0],
		onFail: function() { 
   			$('#welcome_vid_noflash').show(); 
		}
	},{
		clip_id: video_id,
		show_portrait: 0,
		show_byline: 1,
		show_title: 1,
		//fullscreen: true,
		color:'D58600',
		js_api: 1
	});
		
	
	// Load in Analytics
	$.gaTracker('UA-11141170-1');
	

});