jQuery(document).ready(function($) {

	$('a[rel*=external]').click( function() {
	   window.open(this.href);
	   return false;
   });


	$('#dm_user_form_email').keyup(function() {
		$('#dm_user_form_username').val($(this).val());
	});
	
	$('.password-password input').ready(function() {
		restoreBox();
	});
	
	$('.drop-down-box, .close-this').click(function() {
		$('.engineers-login-box').slideToggle();
		return false;
	});

	$('.subnav').each(function() {
		$(this).find('ul').children('li:last-child').css('background','0');
	})

	$('.navigation_menu ul:first').superfish();
	
	$('.navigation_menu ul li').each(function() {
		$(this).children('ul').children('li').children().prepend('<span></span>');
		$(this).children('ul').children('li:last-child').children('a, span').css({
			'-moz-border-radius':'0 0 4px 4px',
			'-webkit-border-radius': '0 0 4px 4px',
			'border-radius': '0 0 4px 4px'
		});
	});

	$('.edit').find('.navigation_menu').append('<div class="clear"></div>');

	if ($('.schematic').length) {
		$('.schematic').dialog({
			autoOpen: false,
			draggable: true,
			width: $('.schematic').children('img').width() + 28, 
			resizable: true,
			position: 'right'
		});

		$('.dialogerise').click(function() {
			$(".schematic").dialog('open');
			$(".schematic").dialog( {title:'Schematic'} );
			return false;
		});

		/* Scroll the schematic dialog upon scroll. The if() clause prevents a
		 * strange flickering effect, presumably because it re-renders then it
		 * applies the display:none attribute. But if it's already hidden, we
		 * don't care where it is; and it already repositions itself on open.
		 */
		$(window).scroll(function () {
			if ($('.schematic').closest('.ui-dialog').css('display') != 'none') {

				//var x = $('.schematic').parent().position().left;
				var x = $(window).width();
				var y = "100%";

				$('.schematic').dialog('option', 'position', [x,y]);
			}
		});

		/* Reposition the schematic dialog if the viewport gets resized
		 */
		$(window).resize(function () {
			if ($('.schematic').closest('.ui-dialog').css('display') != 'none') {
				var x = $(window).width();
				var y = "100%";

				$('.schematic').dialog('option', 'position', [x,y]);
			}
		});
	}

	if ($('.cycle').length) {
		$('.cycle').cycle({timeout:6000, random: 1,cleartypeNoBg: true});
	}
	
	$('#query').attr('title', "Search");
	
	
	$('form input.autoclear-it, form textarea').autoclear();
	
	if ($('.engineers-login-box .error_list').length > 0) {
		$('.engineers-login-box').show();

		$('section#content div.container').html('<h1>Username/password incorrect.</h1><p>Please try entering your details again using the Engineers Login.</p>');
	}
	else  {
	}
	
	$('.password-text input').focus(function() {
		changeBox();
	})
	
	$('.password-password input').blur(function() {
		restoreBox();
	})

	function changeBox()
	{ 
		$('.password-text').hide();
		$('.password-password').show();
		$('.password-password input').focus();
	}
	
	function restoreBox()
	{
		if($('.password-password input').val()=='')
		{
			$('.password-text').show();
			$('.password-password').hide();
			$('.password-text input').autoclear();
		}
	} 
	
	if ($('.legend').length) {
		$('.legend input').each(function() {
			$(this).attr("checked", "checked");
		})

		/* Turn on/off all rows in all unit type tables with one click. Toggle()
		 * doesn't work in this scenario
		 */
		$('.legend input').click(function() {
			if ($(this).is(':checked')) {
				$('tr.' + $(this).attr('class')).show();
			}
			else {
				$('tr.' + $(this).attr('class')).hide();
			}
		});

		/* Show current unit selection
		 */
		$('select[name=unitSelector]').change(function() {
			$('table.specs').addClass('hidden');
			$('table.specs.' + $(this).find('option:selected').val()).removeClass('hidden');
		});	
	}

	if ($('table.specs').length) {
		$('table.specs tbody tr:nth-child(even)').addClass('dark');
	}
	
	$('form').each(function() {
		$(this).validate();
	});



});
