if (typeof(Perch) == 'undefined') {
	Perch		= {};
	Perch.UI	= {};
}

Perch.UI.Global = function() {
	
	var timer;
	var bubble = false;
	
	var init	= function() {
		$('body').addClass('js');
		init_search();
		init_nav();
		animate_burds();
		init_text_formatting();
		init_video();
		init_carousel();
	};
	
	var init_search = function() {
		$('#searchbar').find('div').hide().end().hide().insertAfter('#header');
		
		if ($('body#search').length>0) {
			$('#searchbar').show().find('div').show();
		}
	
		$('#nav li.search a').click(function(e){
			e.preventDefault();
			$('#searchbar').slideDown(function(){
				$(this).find('div').fadeIn();
			});
		});
	};
	
	var init_nav = function() {
		
		var timer;
				
		$('#nav>ul>li').mouseenter(function(){
			clearTimeout(timer);
			var self = $(this);
			$('#nav>ul>li div:visible').each(function(i,o){
				var me = $(o);
				if (me.parents('li').not(self)) me.hide();
			});
			self.find('div').show();
		});
		
		$('#nav>ul>li').mouseleave(function(){
			timer = window.setTimeout(function(){
				$(this).find('div').hide();
			}, 1000);
		});
		
		$('#nav>ul>li div').mouseenter(function(){
			clearTimeout(timer);
			var self = $(this);
			$(self).show();
		});
		
		$('#nav>ul>li div').mouseleave(function(){
			var self = $(this);
			timer = window.setTimeout(function(){
				self.hide();
			}, 1000);
		});

	};
	
	var init_video = function() {
		// remove HTML5 video from Chrome *sadface*
		/*
		if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {
			$('.html5video video a').appendTo('.html5video:first');
			$('.html5video video').remove();
		}
		*/
		
		$('video').bind('ended', function(){
			$(this).parent('div').find('.videooverlay').fadeIn('slow');
		});
		
		$('video').each(function(i,o){
			var v = $(o);
			var overlay = v.parent('div').find('.videooverlay');
			if (overlay.length) {
				overlay.find('a.replay').click(function(e){
					e.preventDefault();
					overlay.fadeOut('slow');
					var video = v.get(0);
					video.currentTime=0;
					video.play();
				});
			}
		});
	};

	var init_text_formatting = function() {
		if ($('.post, .description').length){
			prettyPrint();
		}
	};

	var animate_burds = function() {
		$('#tree1 .inner').css('bottom', '-487px');
		$('#tree1').append('<img src="/img/ft-bird1.gif" width="45" height="51" alt="Bird" id="treeburd1" />').find('#treeburd1').hide();
		$('#tree2 .inner').css('bottom', '-373px');
		$('#tree2').append('<img src="/img/ft-bird2.gif" width="48" height="64" alt="Bird" id="treeburd2" />').find('#treeburd2').hide();
		animate_tree1();
	};
	
	var animate_tree1 = function() {
		$('#tree1 .inner').css('bottom', '-487px').show().animate({
			bottom: 20
		}, {
			easing: 'swing',
			complete: animate_tree2
		});
	};
	
	var animate_tree2 = function() {
		$('#tree2 .inner').css('bottom', '-342px').show().animate({
			bottom: 20
		}, {
			easing: 'swing',
			complete: animate_burd
		});
	};
	
	var animate_burd = function() {		
		$('#treeburd1').show().animate({
			top: '30px'
		}, 'fast',function(){
			$(this).css('z-index', 11);
			
			$('#treeburd2').show().animate({
				top: '98px'
			},function(){
				$(this).css('z-index', 11);
				burd_hover(true);
			});
		});	
	};
	
	var burd_hover = function(show) {
		if (show) {
			if (!bubble) {
				$('#treeburd2').before('<p id="twitterfollow"><a href="http://twitter.com/grabaperch">Follow me on Twitter!</a></p>');
				bubble = $('#twitterfollow').hide();
				bubble.hover(function(){
					clearTimeout(timer);
				});
			}
			bubble.fadeIn('fast');
		}else{
			timer = setTimeout(function(){
				bubble.fadeOut();
			}, 5000);
		}
	};
	
	var init_carousel = function() {
		var storyscroller = $('#storyscroller');
		if (storyscroller.length) {
			storyscroller.addClass('active');
			var truck = $('<div id="storytruck"></div>');
			storyscroller.wrapInner(truck);
			
			create_carousel_nav(storyscroller.find('#storytruck'));
			
			var lis = storyscroller.find('ul:not(.storynav)>li');
			var h = 0;
			var tall = false;
			lis.each(function(i,o){
				var item = $(o);
				var content = item.find('.storycontent');
				if (content.outerHeight() > 170) tall = true; 
				if (parseInt(item.outerHeight(), 10) > h) h = parseInt(item.outerHeight(), 10);
			});
			if (tall) h = h+30;
			lis.css('height', h-14);
			storyscroller.css('height', h-8);
			
		}
	};
	
	var create_carousel_nav = function(truck) {
		var lis = truck.find('li');
		var nav = $('<ul class="storynav"></ul>');
		var i, l;
		for(i=0, l=lis.length; i<l; i++) {
			var txt = lis.eq(i).find('h3 a').text();
			nav.append('<li><a href="#" title="'+txt+'"><span>'+txt+'</span></a></li>');
		}
		
		lis.each(function(i,o){
			var item = $(o);
			var thisnav = nav.clone();
			thisnav.find('li:eq('+i+')').addClass('selected');
			item.append(thisnav);
			
			thisnav.find('li a').click(function(e){
				e.preventDefault();
				var w = truck.find('li:first').width();
				var pos = thisnav.find('a').index($(this))+1;
				truck.animate({left: 0-((w*pos)-w)+'px'});
			});
		});
	};

	var init_facebook = function() {
		
		var fbroot = $('#fb-root');
		
		if (fbroot.length) {
			$('<fb:like href="http://www.facebook.com/grabaperch" send="false" width="230" show_faces="false" font="arial"></fb:like>').insertAfter(fbroot);
			window.fbAsyncInit = function() {
				FB.init({appId:'166395266762820', status: true, cookie: true, xfbml: true});
			};
			(function() {
				var e = document.createElement('script'); e.async = true;
				e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
				document.getElementById('fb-root').appendChild(e);
			}());
		};
	};


	return {
		init: init
	};
	


}();

jQuery(function($) {Perch.UI.Global.init();});

document.write('<style type="text/css">#tree1 .inner, #tree2 .inner {display: none;}</style>');
