var Daylife = {};
var form_message = document.createElement('div');

$(function () {
	
	$('#menu-main-navigation li:last').addClass('last');
	//Daylife.evenColumns();
	$('#feature-banner h1').each(function () {
		$(this).html($(this).html().replace(/Daylife./,'<em>Daylife.</em>'));
		$(this).html($(this).html().replace(/Daylife/,'<em>Daylife</em>'));
	});
	$('#header form input').focus(function () {
		$(this).val() == 'Search' ? $(this).val('') : '';
	});
	$('#header form input').blur(function () {
		$(this).val() == '' ? $(this).val('Search') : '';
	});
	$(".more-colorbox").colorbox({iframe:true, innerWidth:400, innerHeight:300});
	Daylife.smartSuggest();
	Daylife.highlightArchive();
	Daylife.scrollToSetup();
	Daylife.sectionNav();
	Daylife.postForm();
});

Daylife.scrollToSetup = function () {
	$('a[href^="#"]').click(function () {
		var go_to_el = $(this).attr('href');
		$.scrollTo(go_to_el, 800);
		return false
	});
}

Daylife.postForm = function () {
	$('.contact-form form input[type="submit"]').click(function () {
		var form = $(this).closest('form');
		//this may help w/ spam
		$(form).append('<input type="hidden" value="ajax_post" name="ajax_post" />');
		$(form).find('input[type="submit"]').fadeTo('fast', .7);
		$.ajax({
			url: $(form).attr('action'), 
			data: $(form).serialize(),
			success: function (data) {
				$(form).find('input[type="submit"]').fadeTo('fast', 1);
				$(form_message).empty().append(data);
				$(form).before($(form_message));
				$.scrollTo($(form).closest('.widget'), 500, { axis: 'y' });
				if(!$(data).hasClass('error-block'))
				{
					$(form).find('input[type="text"]').each(function () {
						$(this).val('');
					});
					$(form).find('textarea').each(function () {
						$(this).val('');
					});
				}
				$(form).find('input[name="ajax_post"]').remove();
			}
		});
		return false;
	});
}

Daylife.highlightArchive = function () {
	if($('#main-content').hasClass('blog'))
	{
		$('#section-navigation ul li a').each(function () {
			$(this).attr('href') == document.URL ? $(this).closest('li').addClass('current_page_item') : '';
		});
	}
}

Daylife.evenColumns = function () {
	if($('.right-sidebar').size() > 0)
	{
		$('.right-sidebar').height() < $('#main-content').height() ? $('.right-sidebar').height($('#main-content').height() - $('h1.page-title:first').height()) : '';
	}
}

Daylife.smartSuggest = function() {
  var data_src = $('#site-search-form').attr('action');
  $('#search-site').smartSuggest({
    src: data_src,
	showImages: false,
	tempHeaderText: "Looking for our news and image search? It's moved. <a href='http://news.daylife.com'>Click here.</a>"
  });
  $('#main-navigation ul li a').mouseover(function () {
	$('#search-site').trigger('hideBox').blur();
  });
}

Daylife.sectionNav = function () {
	if($('#section-navigation').size() > 0)
	{
		var current_top_page = $('#section-navigation ul li:first a').attr('href');
		var current_section = '';
		
		$('#menu-main-navigation li a').each(function () {
			if($(this).attr('href') == current_top_page)
			{
				current_section = $(this).closest('li').find('ul').clone();
			}
		});
		if(current_section != '')
		{
			$('#section-navigation ul').append($(current_section).find('li'));
		}
		if ($('#section-navigation ul li').size() <= 1) {
			$('#section-navigation').hide();
		}
	}
}
