(function($){

	$(function(){
		
		//Support sub menu
		var hasSubMenu = $('#header.has-submenu').length ? true : false;
		$('#main-menu > ul > li').hover(function() {
			$(this).children('ul').css({'display': 'block'}).stop().animate({'opacity': 1}, 'fast');
		}, function() {
			if( !hasSubMenu || !($(this).hasClass('current_page_item') || $(this).hasClass('current-menu-parent')) ) {
				$(this).children('ul').stop().animate({'opacity': 0}, 'fast', function(){
					$(this).css({'display': 'none'});
				});
			}
		}).each(function(){
			var li = $(this);
			li.find('ul > li:first').addClass('first');
			li.find('ul > li:last').addClass('last');
		}).not('.current-menu-item, .current-menu-parent').each(function(id){
			var li = $(this);
			li.children('ul').css({'opacity': 0});
		});
		
		//Open lightbox for homepage video
		$('#intro a.lightbox').click(function(){
			var id = this.href.split('=').pop();
			var cover = $('<div class="lightbox-cover"></div>').appendTo('body');
			var wrapper = $('<div class="lightbox-wrapper"></div>').appendTo('body');
			var closeBtn = $('<a class="close">X</a>').appendTo(wrapper);
			
			var close = function() {
				cover.remove();
				wrapper.remove();
				return false;
			}
			
			cover.css({
				'width': $(window).width(),
				'height': $(window).height(),
				'opacity': 0
			});
			cover.animate({'opacity': 0.75}, 'fast');
			
			
			wrapper.append('<iframe width="600" height="340" src="http://www.youtube.com/embed/'+ id +'?wmode=transparent&autoplay=1" frameborder="0" allowfullscreen></iframe>');
			wrapper.css({
				'margin-left': -wrapper.width() / 2,
				'top': $(window).scrollTop() + 100,
				'opacity': 0
			});
			wrapper.animate({'opacity': 1}, 'fast');
			
			cover.click(close);
			closeBtn.click(close);
			
			return false;
		});
		
		//Default text inputs
		$('input.default-text, textarea.default-text').each(function(){
			
			var $this = $(this);
			var type = this.nodeName.toLowerCase();
			var defaultValue = '';
			var className = 'has-default-text'; 
			$this.addClass(className);
			
			switch( type ) {
				case 'input':
						defaultValue = $this.attr('value');
					break;
				case 'textarea':
						defaultValue = $this.html();
					break;
			}
			
			$this.focus(function(){
				if( $this.val() == defaultValue ) {
					$this.val('');
					$this.removeClass(className);
				}
			}).blur(function(){
				if( $this.val() == '' ) {
					$this.val(defaultValue);
					$this.addClass(className);
				}
			});
			
		});
		
		//Post image mask
		$('.post').each(function(){
			var post = $(this);
			
			post.find('img').each(function(){
				if( this.width > 400 ) {
					$(this).addClass('large');
				}
			});
			
			post.find('a > img').parent().fancybox({
				'transitionIn'	:	'elastic',
				'transitionOut'	:	'elastic',
				'speedIn'		:	600, 
				'speedOut'		:	200, 
				'overlayShow'	:	true
			});
			
		});
		
		$('.post .wp-caption').each(function(){
			var wrapper = $(this);
			var img = wrapper.find('img');
			wrapper.width( img.width() );
			wrapper.attr('class', wrapper.attr('class') + ' ' + img.attr('class') );
		});
		
		$('.accordion-wrapper').each(function(){
			var wrapper = $(this);
			var trigger = wrapper.find('.accordion-trigger').remove();
			var content = $('<div class="accordion-content closed"></div>').html( wrapper.html() );
			
			wrapper.html('').append(trigger).append(content);
			
			trigger.html('<span>'+ trigger.html() +'</span>').click(function(){
				if( content.hasClass('closed') ) {
					content.slideDown('slow');
					content.removeClass('closed');
				} else {
					content.slideUp('slow');
					content.addClass('closed');
				}
				return false;
			});
			
		});
		
		
		
	});

})(jQuery);
