/*script 4 resizing bg*/

function get_window_size() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  var sizes = new Array;
  sizes[0] = myWidth;
  sizes[1] = myHeight;
  return sizes;
} 

jQuery(document).ready(function($) {      
    regexp = /(http:.*\.[A-Za-z]{1,4})/;
    str = $('body').css('background-image');
    result1 = regexp.exec(str); 
    $("body").css( 'background-image' , 'none' );     

    if( result1 instanceof Array && result1.length > 0 ){
        var img = new Image(); 
     
        img.onload = function() { 
            current_img = $(this).css( 'display', 'none' );
            $("#bg").append(current_img); 

            
            img_resize( current_img );
            
            current_img.show();
            
            $(window).resize(function() {
                img_resize( current_img );    
            })
            
            //alert( img_resize + "~" + window_size );
        }; 
        
        img.src = result1[0];  
        
        function img_resize( current_img ){
            var img_size, window_size, img_resize, K_w, K_h, koef;
            img_size = new Array;
            img_resize = new Array;
                        
            
            img_size[0] = current_img.width();
            img_size[1] = current_img.height();   
            window_size = get_window_size();
            
            K_w = window_size[0] / img_size[0];
            K_h = window_size[1] / img_size[1];
            
            koef = K_w > K_h ? K_w :  K_h ;
            
            img_resize[0] = Math.ceil( koef * img_size[0] );
            img_resize[1] = Math.ceil( koef * img_size[1] );
            
            current_img.css( 'width' , img_resize[0] + "px" ).css( 'height' , img_resize[1] + "px" );            
        }           
        
    }


})
