$(function () {
	// New windows
	$('a.new-window').attr('target', '_blank');
	
	// Tooltips on "i" buttons
	$('.moreinfo').tooltip({
		top: -20,
		left: 15,
		showURL: false,
		bodyHandler: function() {
			var list = $(this).parents('th:first,td:first').find('.extras').html();
			if(list !== null) {
				return list;
			} else {
				return '';
			}
		}
	});
	
	// Inputs with a start default value
	$('input.initvalue').each(function () {
		$(this).data('original', $(this).val());
		$(this).focus(function () {
			if ($(this).val() == $(this).data('original')) {
				$(this).removeClass('initvalue');
				$(this).val('');
			}
		});
		$(this).blur(function () {
			if ($(this).val() == '') {
				$(this).addClass('initvalue');
				$(this).val($(this).data('original'));
			}
		});
	});
	
	// Terms and conditions
	$('#info_about_page p:eq(0) a').show();
	$('#info_about_page p:eq(0) span, #info_about_page p:eq(1)').hide();
	$('#info_about_page a.readmore').click(function () {
		$('#info_about_page p:eq(0) span, #info_about_page p:eq(1)').show();
		$(this).remove();
		return false;
	});
	
	// Rollover animations on cards
	var ho = { 'sp': 300, 'st': 'swing' };
	$('a.card').hover(function () {
		$(this).find('span').stop(true, false).animate({ 'left': '-100px' }, ho.sp, ho.st);
	}, function () {
		$(this).find('span').animate({ 'left': '0' }, ho.sp, ho.st);
	});
		
	// Popular/Latest tabs
	$('div.more').each(function () {
		$(this).find('ul.more:gt(0)').hide();
		$(this).find('.morenav li:first').addClass('selected');
		var e = this;
		$(this).find('.morenav a').each(function (i) {
			$(this).click(function () {
				$(e).find('.morenav li').removeClass('selected').filter(':eq('+i+')').addClass('selected');
				$(e).find('ul.more').hide().filter(':eq('+i+')').show();
				return false;
			});
		});
	});
	
	// Accordion tree
	$('.tree').each(function () {
		var id = $(this).attr('id');
		var f = this;
		var opened = $.cookie(id) || 0;
		$('>ul>li', this).each(function (i) {
			var e = this;
			if (i == opened) { 
				$(this).find('ul').show();
			}
			$(this).find('h3 a').click(function () {
				if ($(this).attr('href').indexOf('guides/credit-card-glossary.html') === -1)
				{
					if (id != "")
					{
						$.cookie(id, i, { expires: 1 });
					}
					if ($(e).find('ul').is(':hidden')) {
						$(f).find('ul ul').slideUp();
						$(e).find('ul').slideDown();
					} else {
						$(e).find('ul').slideUp();
					}
					return false;
				}
			});
		});
	});
	
	// No widows plz, thx
	$('.big_head h2, .big_head h3, .latest_article h3').each(function () { 
		$(this).html( $(this).html().replace(/(\s+)([^\s<]+)((<\/[^>]+>)*)$/, '&nbsp;$2$3') );
	});
});
