$j = jQuery;
var contactSubject = '';

$j(document).ready(function() {
	$j('a.contact-link').click(function() { showContactForm($j(this).data('subject')); return false; }).each(function() {
		var t = $j(this);
		t.data('subject', t.attr('title'));
		t.attr('title', '');
	});
	// center the contact form, if it's visible
	if ($j('body').hasClass('contact')) {
		$j(window).resize(function() { window.parent.centerForm($j('body').outerHeight()); }).resize();
		var subject = window.parent.getContactSubject();
		if (!subject || subject == 'undefined' || subject == undefined) subject = '';
		$j('input[name=your-subject]').val(subject);
	}
});

function showContactForm(subject) {
	hideContactForm();
	contactSubject = subject;
	$j('body').append('<div id="contact"><div class="background"></div><iframe class="form" src="/contact-form/" frameborder="0" scrolling="no"></iframe></div>');
	$j('#contact .background').click(function() { hideContactForm(); });
}

function getContactSubject() {
	return contactSubject;
}

function hideContactForm() {
	$j('body #contact').remove();
}

function closeExternal() {
	window.parent.hideContactForm();
}

function centerForm(height) {
	var form = $j('.form').css('height', height + 'px'),
		height = height,
		width = form.outerWidth(),
		glamdringHeight = $j('#glamdring').outerHeight(),
		body = $j(window),
		bodyWidth = body.width(),
		bodyHeight = body.height() - glamdringHeight,
		top = Math.max(0, Math.floor((bodyHeight - height) / 2)) + glamdringHeight,
		left = Math.floor((bodyWidth - width) / 2);
	form.css({ top : top + 'px', left : left + 'px' });
}

// search box. not really contact but this is included every page and i don't want to make a new file

$j(document).ready(function() {
	var t = $j('#glamdring input[type=text]').focus(function() {
		var t = $j(this);
		if (this.value == t.data('default')) {
			this.value = '';
			t.removeClass('empty');
		}
	}).blur(function() {
		var t = $j(this);
		if (this.value.replace(/^\s\s*/, '').replace(/\s\s*$/, '') == '') {
			this.value = t.data('default');
			t.addClass('empty');
		} else if (this.value != t.data('default')) {
			t.removeClass('empty');
		}
	}).data('default', 'try moods, styles, genres, and more').blur();
});
