$( function () {
	SearchInit();
	EmailSubscribeInit();
    });

function SearchInit() {
	$('#search-query').focus( function () {
			if ( this.value == 'Enter Keywords') { 
				this.value='';
			} else { 
				this.select();
			}
		});

	// 		$('#search-query').blur( function () {
	// 				if ( this.value == '' ) { 
	// 					this.value = 'Enter Keywords';
	// 				}
	// 			});

	$('#search-submit').click( function () {
			if ( $('#search-query').val() == 'Enter Keywords' || $('#search-query').val() == '' ) { 
				$('#search-query').focus();
				return false;
			}
		});

}

function EmailSubscribeInit() {

	if ( ! $("#EmailSubscribe").length > 0 ) {
		return false;
	}

    var s = '';

    s += '<form action="/email_subscribe" method="post">' + "\n";
    s += '<input type="text" name="email"value="Email" />' + "\n";
    s += '<input type="submit"value="Subscribe" />' + "\n";
    s += '</form>' + "\n";

	s += '<p>Enter your address to get our weekly Latest Additions</p>' + "\n";

    // s += '<div id="EmailSubscribeProcessing">' + "\n";
    // s += '<span>Saving your details...</span>' + "\n";
    // s += '</div>' + "\n";

    // s += '<div id="EmailSubscribeProcessed">' + "\n";
    // s += '<span>You have been added to our weekly Latest Additions email list!</span>' + "\n";
    // s += '</div>' + "\n";

    $('#EmailSubscribe').html(s);

	$('#EmailSubscribe input[type=text]')
		.focus( function () {
				if ( this.value == 'Email') { 
					this.value=''; 
				} else { 
					this.select();
				}
			})
		.blur( function () {
				if ( this.value == '' ) { 
					this.value = 'Email';
				}
			});

	$('#EmailSubscribe input[type=submit]')
		.bind('click', function() {

				$('#EmailSubscribe p').removeClass('invalid');
				$('#EmailSubscribeError').html('Enter your address to get our weekly Latest Additions');

				var valid = true;
				var email = $('#EmailSubscribe input[type=text]').val();
				if ( email == '' || ! validateEmail ( email ) ) {
					$('#EmailSubscribe input[type=text]').addClass('invalid').focus();
					$('#EmailSubscribe p').html('Please enter a valid email address');
					setTimeout ( "$('#EmailSubscribe input[type=text]').removeClass('invalid'); $('#EmailSubscribe p').html('Enter your address to get our weekly Latest Additions');", 3000 );
					return false;
				}

				var request = {
					m: 'email_subscribe',
					rpc: 'email_subscribe',
					email: $('#EmailSubscribeEmail').val()
				};

				$('#EmailSubscribe form').fadeOut( function() {
						$('#EmailSubscribe p').html('').addClass('processing');
						$.post( '/email_subscribe', { 'rpc':'email_subscribe', email: email }, function () {	
								$('#EmailSubscribe p').html('You are now subscribed to Latest Additions').removeClass('processing');
								setTimeout ( "EmailSubscribeInit();", 3000 );
							});
					});
				return false;
			});
	

}

function validateEmail(str) {

	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1) {
		return false; 
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) {
		return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) {
		return false;
	}

	if (str.indexOf(at,(lat+1))!=-1) {
		return false;
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) {
		return false;
	}

	if (str.indexOf(dot,(lat+2))==-1) {
		return false;
	}
		
	if (str.indexOf(" ") != -1) {
		return false;
	}

	return true;
}

