$(function() {
	$("#main_video div.thumb a").hover(
		function () {
        	$(this).css("opacity","0.5");
      	}, 
      	function () {
      		$(this).css("opacity","0.6");
      	}
	);
	if ($("#element_email_captcha").length>0) {
		$.get("/elem/email_captcha", function(data) {
			$("#element_email_captcha").replaceWith(data);
		});
		/*
		$('#email_captcha_value').bind( 'keydown', 'return', function() {
			$('#email_captcha_submit').trigger('click');
		});
		*/
	}	
	$('#email_captcha_new_code').live("click", function() {
		$('#email_captcha_errors').text('');
		$('#email_captcha_image').attr('src', '/captcha/?rand=' + Math.random());
		return false;
	});
	$('#email_captcha_submit').live("click", function() {
		var email_check = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var email = $('#form_subscribe_email').val();
		var code = $('#email_captcha_value').val();
		if ( code == '' )
		{
			$('#email_captcha_errors').text( lang['subscribe_error_captcha_invalid'] );
		}
		else if (!email_check.test(email))
		{
			$('#email_captcha_errors').text( lang['subscribe_error_email_invalid'] );
		}
		else
		{
			$('#email_captcha_loader').show();
			$.post("/subscribe/add",
				{
					'data[code]': code,
					'data[email]': email
				},
				function( data )
				{
					$('#email_captcha_errors').text('');
					if ( data == 'YES' )
					{
						$('#email_captcha').hide();
						messageShow(lang['subscribe_success_text']);
						return;
					}
					else if ( data != '' )
					{
						$('#email_captcha_errors').text(data);
					}
					else
					{
						// smthng is wrong!
					}
					$('#email_captcha_loader').hide();
				}
			);
		}
	});
	$('#form_subscribe_email').bind( 'keydown', 'return', function() {
		$('#form_subscribe_submit').trigger('click');
	});
	$('#form_subscribe_email')
	.focus(function() {
		if(this.value == this.defaultValue) {
			this.value = '';
		}
	})
	.blur(function() {
		if(!this.value.length) {
			this.value = this.defaultValue;
		}
	});
	$('#form_subscribe_submit').click( function () {
		$('#email_captcha_errors').text('');
		var email_check = /^([\w-\.]+@([\w-]+\.)+[a-z]{2,4})?$/;
		var email = $('#form_subscribe_email').val();
		if ( email == '' )
		{
			alert( lang['subscribe_error_no_email'] );
		}
		else if (!email_check.test(email))
		{
			alert( lang['subscribe_error_email_invalid'] );
		}
		else
		{
			$.post("/subscribe/check",
				{
					'data[email]': email
				},
				function( data )
				{
					if ( data == 'YES' )
					{
						$('#email_captcha_value').val('');
						jqmShow( '#email_captcha' );
					}
					else if ( data == 'EXISTS' )
					{
						alert( lang['subscribe_error_email_subscribed'] );
					}
				}
			);
		}
		return false;
	});
});