/**
 * @author R.A. Ray
 */

$("html").addClass('js');

var setCornerz = function ()
{
	$( '#promo' ).cornerz( { radius: 5 } );

	$( '#actions .button' ).cornerz( { radius: 2, background: '#496DB5' } );
	
	$( '#book .button' ).cornerz( { radius: 2, background: '#6987C2' } );
	
	$( '#aside .col-4' ).cornerz( { radius: 5 } );
	
	$( '#content h1' ).cornerz( { radius: 5 } );
};

var fixpngs = function ()
{
	
		$( '#actions li a' ).pngFix();

};

var setLabels = function () {
	$('#aside form dt').addClass('on-top');
	$('#aside input')
		.each(function () {
			if ($(this).attr('value') != '') {
				$('label[for='+ $(this).attr('id') + ']').html('&nbsp;');
			}	
		})
		.focus(function () {
			$('label[for='+ $(this).attr('id') + ']').html('&nbsp;');
		})
		.blur(function () {
			if ($(this).attr( 'value' ) == '') {
				var label = $('label[for='+ $(this).attr('id') + ']');
				label.html(label.attr('title'));
			};
		}
	);
};

var deobfuscation = function ()
{
	$('.smurf').wrapInner('<a href="#"></a>');
	$('.smurf a').hover( 
		function () 
		{
			var email = $(this).text();
			email = email.replace(' [at] ', '@').replace(' [dot] ', '.');
			$(this).attr('href', 'mailto:' + email);	
			$(this).unbind();
		}
	).focus( 
		function () 
		{
			var email = $(this).text();
			email = email.replace( ' [at] ', '@' ).replace( ' [dot] ', '.' );
			$(this).attr( 'href', 'mailto:' + email );
			$(this).unbind();
		}
	);
};

$( document ).ready(function () {
	setCornerz();
	fixpngs();
	setLabels();
	deobfuscation();
	
	// Added by Matt to open external links in new windows. Credit to Karl Swedberg.
	$("#content .alpha a").filter(function(index) {
		return this.hostname && this.hostname !== location.hostname;
	}).attr("target","_blank");
	
	$("a.whats_this_link").click(function() {
		var entry_id = $(this).attr("rel");
		var expander_id = "#whats_this_expander" + entry_id;
		var expander = $(expander_id);
		expander.slideToggle("medium");
		return false;
	});
	
	if ($("#content form#register_form").length > 0) {
		$("#content form#register_form").validate({
			rules: {
				password_confirm: {
					equalTo: "#password"
				},
				email_confirm: {
					equalTo: "#username"
				}
			},
			messages: {
				password_confirm: {
					equalTo: "The two passwords must match."
				},
				email_confirm: {
					equalTo: "The two email addresses must match."
				},
				accept_terms: "You must accept the Terms &amp; Conditions to register."
			}
		});
	};
});
