// if the min screen size not defined in child page, set default here
if(!minScreenSize) { var minScreenSize = {width: 1020,height:768}; }
if(!layoutMode) { var layoutMode = ""; }

// initialize the layout
$.layout({mode: layoutMode, screen: minScreenSize});

var BgImgRatioHeightOverWidth;
var BgImgRatioWidthOverHeight;


function resizeBackgroundImage() {
    if (!BgImgRatioHeightOverWidth) {
        BgImgRatioHeightOverWidth = $('div.background-container img').height() / $('div.background-container img').width();
        BgImgRatioWidthOverHeight = $('div.background-container img').width() / $('div.background-container img').height();
    }
    var h = BgImgRatioHeightOverWidth * $(window).width();
    
    $('div.background-container').css({
        height:$(window).height(),
        width:$(window).width()
    });
    
    $('div.main-container').css('width', $(window).width());
    
    if ((h < $(window).height()) || (h < $(document).height())) {
        var w = BgImgRatioWidthOverHeight * $(window).height();
        if (w >= $(window).width()) {
            $('div.background-container img').css({
                width: w + 'px',
                height: $(window).height() + 'px'
            });
        }
        else {
            $('div.background-container img').css({
                width: '100%',
                height: 'auto'
            });
        }
    }
    else {
        $('div.background-container img').css({
            width: $(window).width() + 'px',
            height: h + 'px'
        });
    }
}


function headerCenter() {
    //vertical center
    /*
    $(".header").verticalCenter({
        minH: globalSettings.centerBoxDefault.minH,
        maxH: globalSettings.centerBoxDefault.maxH,
        offsetTop:40,
        offsetBot:0     
    }).css("visibility","visible");     
    */
    var header = $('.header');
    header.css({
        'visibility': 'visible',
        'left': '50%'
    });
    if (header.position().left < 470) header.css('left', 470);
}

//*** Event Handlers ***

// wait for background image to load
$(document).bind('backgroundLoaded', function() {
    BgImgRatioHeightOverWidth = $('div.background-container img').height() / $('div.background-container img').width();
    BgImgRatioWidthOverHeight = $('div.background-container img').width() / $('div.background-container img').height();
    resizeBackgroundImage();
});

$(document).bind('newBackgroundLoaded', function(event, i) {
    BgImgRatioHeightOverWidth = $('div.background-container img.image' + i).height() / $('div.background-container img.image' + i).width();
    BgImgRatioWidthOverHeight = $('div.background-container img.image' + i).width() / $('div.background-container img.image' + i).height();
    resizeBackgroundImage();
});


$(document).ready(function() {
    resizeBackgroundImage();
    $(window).bind("resize", function() {
        resizeBackgroundImage();
    });
    $('.subscribe-email-enter').click(function() {
        var showMsg = function(msg)
        {
            $('.subscribe-email-message').html(msg).fadeIn(400, function()
            {
                setTimeout(function(){
                    $('.subscribe-email-message').fadeOut(400);        
                }, 3000);
            });
        }
        var emailVal = $('.subscribe-email').val();
        if(emailVal == ''){
            showMsg('Please enter an Email Address');
            
        } else if(!/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/.test(emailVal)) {
            showMsg('Your email is in an invalid format');
        }
        else
        {
            $.ajax({
                url: '/CodeBase/WebServices/WebRegistrationService.asmx/RegisterEmail',
                data: {
                    'email': emailVal
                },
                type: $('.subscribe-email-form').attr('method'),
                success: function(d, t, x) {
                    var message = 'Thank you for subscribing!';
                    showMsg(message);
                    $('.subscribe-email').val('');
                }
            });
        }
        //$('.subscribe-email-form').get(0).submit();
    });
    //if(!$.browser.msie) {Cufon('h1')('h2'); } //Fonts
});

$(window).resize(headerCenter);

headerCenter();

