$(document).ready(function() {
	// newsletter-email
	// focus
	var emailVar = $('#newsletter-email');
	if(emailVar) {
		$(emailVar).focus(function(){
		if($(emailVar).val() == 'email') {
			$(emailVar).val('');
		}
		else if($(emailVar).val() == ''){
			$(emailVar).val('email');
		}
	});
	
	// blur
	$(emailVar).blur(function(){
		if($(emailVar).val() == 'email') {
			$(emailVar).val('');
		}
		else if($(emailVar).val() == '') {
			$(emailVar).val('email');
		}
	});
	}	
	
	// hideField();
	togglePassword('password');
	
	// Script by Jermy Kieth hacked
	function togglePassword(id) {
	    if (!document.getElementById(id)) { return; }

	    var password_field = document.getElementById(id);
	    
	//  Create the text field
	    var text_field = document.createElement('input');
	    text_field.setAttribute('type','text');
	    $(text_field).attr('class','login-field');
	    text_field.style.display = 'inline';
	    $('#password').css('display', 'none');
	    text_field.value = password_field.value;

	//  Create the container for the checkbox and label
	    var span = document.createElement('span');
	    span.className = 'passwordtoggle';

	//  Create the checkbox
	    var checkbox = document.createElement('input');
	    checkbox.setAttribute('type','checkbox');
	    checkbox.setAttribute('id',id+'-checkbox');

	//  Create the label
	    var label = document.createElement('label');
	    var label_text = document.createTextNode('show password');
	    label.setAttribute('for',id+'-checkbox');
	    label.appendChild(label_text);

	//  Put the checkbox and label into the container
	//  span.appendChild(checkbox);
	    span.appendChild(label);

	//  Toggle the display of the password and text fields
	    checkbox.onclick = function() {
	        if (this.checked) {
	            password_field.style.display = 'none';
	            text_field.style.display = 'inline';
	        } else {
	            text_field.style.display = 'none';
	            password_field.style.display = 'inline';
	        }
	    };

	//  Keep the values of the password and text fields in sync
	    var syncFields = function() {
	        if (password_field.style.display == 'none') {
	            password_field.value = text_field.value;
	        } else {
	            text_field.value = password_field.value;
	        }
	    };

	    password_field.onkeyup = syncFields;
	    text_field.onkeyup = syncFields;

	//  Insert the container with the checkbox and label after the password field
	    if (password_field.parentNode.lastChild == password_field) {
	        password_field.parentNode.appendChild(span);
	    } else {
	        password_field.parentNode.insertBefore(span,password_field.nextSibling);
	    }

	//  Insert the text field before the container
	    span.parentNode.insertBefore(text_field,span);

	    syncFields();
	    
	    
	    
	    
// my code	    
		// inputs
		var userVar = $('#username');
		var passVar = $('.login').find('.login-field');
		var passRealVar = $('#password');
		
		// username
		// focus
		if(userVar) {
			$(userVar).focus(function(){
			if($(userVar).val() == 'username') {
				$(userVar).val('');
			}
			else if($(userVar).val() == ''){
				$(userVar).val('username');
			}
		});
		
		// blur
		$(userVar).blur(function(){
			if($(userVar).val() == 'username') {
				$(userVar).val('');
			}
			else if($(userVar).val() == '') {
				$(userVar).val('username');
			}
		});
		}
		
		
		
		// password
		// focus
		if(passVar) {
			$(passVar).focus(function(){
			if($(passVar).val() == 'password') {
				$(passRealVar).val('');
				text_field.style.display = 'none';
	            password_field.style.display = 'inline';
	            password_field.focus();
			}
			else if($(passVar).val() == ''){
				$(passVar).val('password');
				password_field.style.display = 'none';
	            text_field.style.display = 'inline';
			}
		});
		// blur
		$(passVar).blur(function(){
			if($(passVar).val() == 'password') {
				$(passRealVar).val('');
				text_field.style.display = 'none';
	            password_field.style.display = 'inline';
			}
			else if($(passVar).val() == '') {
				$(passRealVar).val('password');
				password_field.style.display = 'none';
	            text_field.style.display = 'inline';
			}
		});
		}
		
		// focus
		if(passRealVar) {
/*			$(passRealVar).focus(function(){
				if($(passRealVar).val() == 'password') {
					$(passRealVar).val('');
					text_field.style.display = 'none';
		            password_field.style.display = 'inline';
				}
				else if($(passRealVar).val() == ''){
					$(passRealVar).val('password');
					password_field.style.display = 'none';
		            text_field.style.display = 'inline';
				}
			}); */
		// blur
		$(passRealVar).blur(function(){
			if($(passRealVar).val() == 'password') {
				$(passRealVar).val('');
				text_field.style.display = 'none';
	            password_field.style.display = 'inline';
			}
			else if($(passRealVar).val() == '') {
				$(passRealVar).val('password');
				password_field.style.display = 'none';
	            text_field.style.display = 'inline';
			}
		});
		}

	}

	togglePassword('login-password');
	
	// newsletter ajax
	// alert box
	function _alert(theCode, toHere) {
		jQuery('.overlay').html(theCode);
	
		jQuery('.alert').css("top", toHere).animate({'top' : toHere+30+'px'}, 500, 'swing');
		
		jQuery('.close').click(function(event) {
			jQuery('.overlay').remove();
		});	//end close
	} // end alert

	// submit pressed
	$('input.submit').click(function(event) {
		event.preventDefault();
		var toHere = $(document).scrollTop();
		// fields
		var POST = {}; 
		POST.email = $('input.email').val();
		
		$('<div class="overlay"></div>').appendTo('.wrapper');
		$('.overlay')
			.css({'height' : $(document).height()})
			;
		
		// ajax
		jQuery.post('wp-content/themes/worldwitness118/check-newsletter.php',{ email: POST.email}, function(data){ _alert(data, toHere)});	
		}); // end submit
	
});
