$( document ).ready( function() {
	
	/* Content - FF gradient patch for old mozilla web browsers */
	if( $.browser.mozilla ) {
		if( parseFloat( $.browser.version.replace( /\./ig, '' ) ) < 19210 ) {
			var height = $( '#content' ).height();
			$( '#content' ).css( { 'background-image': 'url(/images/phpThumb.php/q=100;h=' + height + ';aoe=1;/share/images/site/content/gradient.png)' } );
			
			$( '#content #tabs .article' ).each( function() {
				var height = $( this ).height();
				$( this ).css( { 'background-image': 'url(/images/phpThumb.php/q=100;h=' + ( height + 25 ) + ';aoe=1;/share/images/site/content/gradient-reverse.png)' } );
			});
		}
	}
	
	/* Tabs */
	if( $( '#tabs' ).size() > 0 ) {
		$( '#tabs section:first' ).corner( { tl: 0, tr: 0 } );

		$( '#box #content #tabs ul li' ).click( function() {
			if( !$( '#tab-' + $( this ).children( 'a' ).attr( 'class' ) ).size() )
				return true;

			$( '#box #content #tabs ul li' ).removeClass( 'current' );

			$( this ).addClass( 'current' );

			$( '.tabs' ).hide();
			$( '#tab-' + $( this ).children( 'a' ).attr( 'class' ) ).show();

			document.location = document.location.toString().replace(/#(.*)/, '') + '#' + $(this).children( 'a' ).attr('class');

			if( $.browser.mozilla ) {
				if( parseFloat( $.browser.version.replace( /\./ig, '' ) ) < 19210 ) {
					$( '#content #tabs .article' ).each( function() {
						var height = $( this ).height();
						$( this ).css( { 'background-image': 'url(/images/phpThumb.php/q=100;h=' + ( height + 25 ) + ';aoe=1;/share/images/site/content/gradient-reverse.png)' } );
					});
				}
			}

			return false;
		});

		// $( '#box #content #tabs section article' ).corner();
	
		$( '.tabs .article' ).click( function() {
			var href = $( this ).children( 'h3' ).children( 'a' ).attr( 'href' );

			document.location = href;
		});
		
		$( '.tabs .article img:odd' ).addClass( 'right' );
	}

	if( document.location.toString().match(/#(.*)$/) ) {
		var tab = document.location.toString().replace(/(.*)#/, '');
		if( $('#tabs ul li .' + tab).size() > 0 )
			$('#tabs ul li .' + tab).parents( 'li:first' ).trigger( 'click' );
	}
	
	/* Newsletter */
	var $newsletter = $( '#form-newsletter' ).val();

	$( '#form-newsletter' ).focus( function() {
		if( $( this ).val() == $newsletter )
			$( this ).val( '' );
	});

	$( '#form-newsletter' ).blur( function() {
		if( $( this ).val().trim() == '' )
			$( this ).val( $newsletter );
	});
	
	$( '#form-newsletter-subscribe' ).submit( function() {
		if( validate.empty( $( '#form-newsletter' ).val() ) || !validate.email( $( '#form-newsletter' ).val() ) ) {
			$( '#form-newsletter' ).focus( );

			$.post( '/members/subscribe/', { action: 'subscribe', email: $( '#form-newsletter' ).val() }, function( r ) {
				if( r == '1' )
					jAlert( 'You have successfully subscribed to the 3dws newsletter.' );
				else if( r == 'unvalid email' )
					jAlert( 'Please type valid email address.', '', function() { $( '#form-newsletter' ).focus( ); });
				else
					jAlert( 'Your email address is already subscribed to the 3dws newsletter.' );
			});

			$( '#form-newsletter' ).val( '' );

			return false;
		}
	});
});
