$(window).resize( function() {
  setBgElements();
});


function setBgElements() {
  var stop = false
  $('#bg').css( 'visibility', 'hidden' );
	$('#bg').height( '0px' );
	$('#bg-top').height( '0px' );
	$('#bg-bottom').height( '0px' );

  centerElements();
  
  setInterval( function() {
    if( stop )
      return;

  	$('#bg').height( $(document).height() );
  	var h = $('#bg-top').parent().height() - 3 /*bg-middle margin-top*/;
  	if( isOldIE() )
  	  h = $(document).height()-3;
  	 
  	if( h%2 == 1 )
  	 h--;
  
  	if (h > 0) {
  	  if( h < 480 )
  	    h = 480 - 3;
  	  h = (h-30)/2;
  
  	  $('#bg-top').css({ 'height': h+'px' });
  	  //$('#bg-bottom').css({ 'top':h+30 });
  	  $('#bg-bottom').css({ 'height': h+'px' });
    }
  
    $('#bg').css( 'visibility', 'visible' );
    stop = true;
  }, 0 );
  
  //$('.wrapper').height( '100%' );
  //$('.wrapper').height( $(document).height() );
}


/*$(window).resize( function() {  //alert($(document).height());
  var stop = false;
  
  $('#bg').height( '0px' );  //alert($(document).height());

  // Hack, to allow browser update height.
  setInterval( function() {
    if( stop )
      return;

    $('#bg').height( $(document).height() );
    stop = true;
  }, 1 );
})*/

