var bg_prop = {};

function resizeBG() {
    setTimeout( function(){

        var is_front = $('.front').length;
        var is_front = 1;

        var bg = $('#page_bg');
        var f = $('#footer');
        //var c = $('#footer');

        f.hide(1,function(){

            if( is_front > 0 ) {
                bg.css('height', 'auto' );
                bg.css('width', 'auto' );
                var w = bg.width();
                var h = bg.height();
                var r = w/h;

                //f.css('top', 'auto');
                //f.css('width', 'auto');

                bg.css('height', '0' );
                bg.css('width', '0' );
            }

            var dw = $(document).width();
            var dh = $(document).height();
            var ww = $(window).width();
            var wh = $(window).height();

            if ( dh > wh ) {
                // needed for proper coverage of faded mask logo
                $('#page').height( dh + 60 );
                // needed for proper coverage of left and right opaque margins
                $('#margin_left').height( dh + 60 );
                $('#margin_right').height( dh + 60 );
            } else {
                // needed for proper coverage of faded mask logo
                $('#page').height( dh );
                // needed for proper coverage of left and right opaque margins
                $('#margin_left').height( dh );
                $('#margin_right').height( dh );
            }

            var mw = 0;
            mw = ww - $('#main').width();
            if ( mw > 0 ) {
                mw = Math.round( mw / 2);
            } else {
                mw = 0;
            }
            $('#margin_left').width( mw );
            $('#margin_right').width( mw );

            if( is_front > 0 ) {
                var ch = null;
                var cw = null;

                var _h = null;
                var _w = null;

                // clip h and w
                //if ( ww >= dw ) {
                    cw = ww;
                //} else {
                //    console.log('b');
                //    cw = dw;
                //    if ( jQuery.browser.msie && jQuery.browser.version == 8) {
                //        cw = cw - 21;
                //    }
                //}
                if ( wh >= dh ) {
                    ch = wh;
                } else {
                    ch = dh + 40;
                }


                if ( ww >= dw ) {
                    _w = ww;
                    _h = wh;
                } else {
                    _w = dw;
                    _h = dh;
                    //_h = dh + 40; // give the footer some room
                    //if ( jQuery.browser.msie && jQuery.browser.version == 8) {
                        //_w = _w - 21;
                    //}
                }

                _h = _w / r;

                bg.css('clip', 'rect( 0px ' + cw + 'px ' + ch + 'px 0px )');

                //f.css('top', _h-28 + 'px');
                //f.css('width', _w + 'px');

                bg.height( _h );
                bg.width( _w );

                //bg.height( h );
                //bg.width( w );

                bg.show();
                f.show();
            }
        });

    }, 250);

}


$().ready(function(){

    if( $('.front').length > 0 ) {
        swfobject.embedSWF(
            "/sites/all/themes/jhd_theme/swf/mask360.swf",
            "swfContent",
            "196",
            "330",
            "9.0.0",
            "/sites/all/themes/jhd_theme/swf/expressInstall.swf"
        );

    }

    var bgs = new Array();
    bgs[0] = new Array( '/sites/all/themes/jhd_theme/images/bg_01.jpg', 'dark' );
    bgs[1] = new Array( '/sites/all/themes/jhd_theme/images/bg_03.jpg', 'light' );
    bgs[2] = new Array( '/sites/all/themes/jhd_theme/images/bg_04.jpg', 'light' );
    bgs[3] = new Array( '/sites/all/themes/jhd_theme/images/bg_05.jpg', 'light' );
    //bgs[4] = new Array( '/sites/all/themes/jhd_theme/images/bg_02.jpg', 'dark' );

    var r = Math.floor( Math.random() * bgs.length );
    var bg = $('<img alt="" id="page_bg"/>');
    bg.attr('src', bgs[r][0] );

    $('body').addClass( bgs[r][1] );

    $('body').prepend( bg );

    //setTimeout( function() {
        //Cufon.replace('h1',{ fontFamily: 'gothamlight' });
        //Cufon.replace('h2',{ fontFamily: 'gothambook' });
        ////Cufon.replace('p',{ fontFamily: 'gotham' });
        //Cufon.replace('#top_links li',{ fontFamily: 'gotham' });
        //Cufon.replace('#navigation li',{ fontFamily: 'gotham' });
    //}, 250 );

    //resizeBG();

});


$(window).load(function(){
    setTimeout( function() {
        Cufon.replace('h1',{ fontFamily: 'gothamlight' });
        Cufon.replace('.home_main h2',{ fontFamily: 'gothambook' });
        //Cufon.replace('p',{ fontFamily: 'gotham' });
        Cufon.replace('#top_links li',{ fontFamily: 'gotham' });
        Cufon.replace('#navigation li',{ fontFamily: 'gotham' });
        Cufon.replace('.view-Product-Listing .views-field-title', {fontFamily: 'gothambook'});

        $('#page').css('left','0');
        $('#page').css('position','static');

        resizeBG();

    }, 100 );


});




$(window).resize(function() {
    resizeBG();
});

