$(function() {
 
  // Clear searchfield on focus
  var searchField = $('#searchfield').val();
  
  if (searchField == '') {
	  $('#searchfield').autofill({
	    value: 'Search..'
	  });
  }
  
  // Here we check for the current select value and updates the wrapping <span>
  var currentSelect = $('#contact select').val();
  
  $('#contact').find('span.recipient').append('<strong>' + currentSelect + '</strong>');
  
  $('#contact select').click(function(){
  	var currentValue = $('#contact select option:selected').text();
  	var pseudoSelect = $('#contact').find('span.recipient strong');
  	
  	$(pseudoSelect).empty().append(currentValue);
  });

  // List style elements
  $('.nav ul li:first, ul.children li:first, #posts li:first').addClass('first');
  $('.nav ul li:last, ul.children li:last, #posts li:last').addClass('last');
  
  $('#contentwrap .article:last').addClass('last');
  
  // Form style elements
  $('input:text').addClass('text');
  
  // Subscribe box
  var unsubscribe = $('div.subscribe').find('.unsubscribe');
  var subscribe = $('div.subscribe').find('.subscribefield');
  
  $('div.subscribe input.text').focus(function(){
  	subscribe.addClass('focus');
  	unsubscribe.fadeIn();  	
  }).blur(function(){
  	subscribe.removeClass('focus');
  	unsubscribe.fadeOut('fast');  	
  });  
  
  // Logo dropdown
  $('#logo').hover(
    function(){
      $('#logo .inner .dropdown').show();
    },
    function(){
      $('#logo .inner .dropdown').fadeOut('fast');
    }
  );
  
  // Modal contact
  // -------------------------
  // Is the contact modal active
  windowState = document.location.hash.substr(1,document.location.hash.length);
  
  if (windowState == 'contact') {
  	$('#outer-contact').addClass('active');
  	$('body').addClass('modal');
  }
  
  $("li.page-item-1110 a").click(function(){
  	$("#outer-contact").fadeIn('fast').addClass("active");
  	$('body').addClass('modal');
  	
  	return false;
  });
  
  $("a.close").click(function(){
  	$("#outer-contact").hide('fast').removeClass("active");
  	$('body').removeClass('modal');
  	
  	return false;
  });
  
  if ($('body').hasClass('single')) {
    // Add gallery classes for style control
    $('.gallery dl:nth-child(2n)').addClass('middle');
  }
  
  if ($('body').hasClass('blog') || $('body').hasClass('single')) {
  	$('a').find('img').parent().addClass('wp-image');
  
  	$('a.wp-image').fancybox({
  		'padding'		:		0,
  		'margin'		:		20
  	});
  	
  	$('.wp-caption').find('a').fancybox({
  		'padding'		:		0,
  		'margin'		:		20
  	});
  	
  	$('dt.gallery-icon').find('a').fancybox({
  		'padding'		:		0,
  		'margin'		:		20
  	});
  	
  	$('dt.gallery-icon').find('a').attr('rel', 'gallery');
  }
  
  $('a[rel="wp-video-lightbox"]').click(function() {
  	$.fancybox({
  			'padding'		: 0,
  			'autoScale'		: false,
  			'transitionIn'	: 'none',
  			'transitionOut'	: 'none',
  			'title'			: this.title,
  			'width'		: 680,
  			'height'		: 495,
  			'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
  			'type'			: 'swf',
  			'swf'			: {
  			   	 'wmode'		: 'transparent',
  				'allowfullscreen'	: 'true'
  			}
  		});
  
  	return false;
  });
  
	Cufon.refresh('.cufon, #post-header h1, .search #post-inner > h1, .article.page h1, .page #post h1, .widget-title, #respond label, #contact label');
	
	
});

