$(document).ready(function () {
    $('#navigation ul li').hover(
        function () {
            //show its submenu  
            $('ul', this).slideDown(100);

        },
        function () {
            //hide its submenu  
            $('ul', this).slideUp(100);
        }
    );
        $("#MarketAnalysisForm td.recaptcha_image_cell").attr("width", '230');
        $("#MarketAnalysisForm div#recaptcha_image").css("width", '230');
        $("#MarketAnalysisForm #recaptcha_response_field").css("width", '230');
        $("ul.mg2 li:last-child").css("margin", "0");
   try {
        $("#rotator").tabs({ fx: { opacity: "toggle"} }).tabs("rotate", 7000, true);
        $("#rotator").hover(
            function () {
                $("#rotator").tabs("rotate", 0, true);
            },
            function () {
                $("#rotator").tabs("rotate", 7000, true);
            }
            );
        if ($(".thirdnav").html().toLowerCase().indexOf("li") < 0) {
            $(".contentbody").removeClass("contentbody").addClass("contentbodywide");
            $(".thirdnav").removeClass("thirdnav").addClass("thirdnavhide");
        }
        if ($(".thirdnav").height() > $(".contentbody").height()) {
            $(".contentbody").height($(".thirdnav").height());
        }
        $("a.iframe").fancybox({ 'hideOnContentClick': false, 'width': 720, 'height': 540 });

        pushdown();
    }

    catch (er) {

    }
    $("#recaptcha_table").removeClass("recaptchatable");
    $("#recaptcha_tagline").hide();
    $("#recaptcha_logo").hide();

    $("#rotator li").last().css("border-bottom", "0px solid #FFFAFA");
fixbg("#bssbgimg");
fixerror("#bsserrorimg");

});

$(window).resize(function () {
    fixbg("#bssbgimg");
    fixerror("#bsserrorimg");
});

function fixerror(obj){
  if ($(obj).length){
    var doc_width = $(document).width();
    var image_width = $(obj).width();
    var width_offset = Math.round((doc_width / 2)) + 100; 
    $(obj).css("right", width_offset + "px");
  }
}

function fixbg(obj){
    var doc_width = $(document).width();
    var doc_height = $(document).height();

    if (doc_width <= 1566) {
        //alert("Step 1: getting document size\n\nWidth: " + doc_width + "px\nHeight = " + doc_height + "px");
        var image_width = $(obj).width();
        var image_height = $(obj).height();
        //alert("Step 2: getting image size\n\nWidth: " + image_width + "px\nHeight = " + image_height + "px");
        var image_ratio = image_width / image_height;
        //alert("Step 3: getting image width/height ratio: " + image_ratio);
        $(obj).width(1566);
        $(obj).height(858);
        // do not resize, just center
        var width_offset = Math.round((image_width - doc_width) / 2) - 10;
        //alert("Step 6: moving the image left by " + width_offset + "px to have it centered");
        $(obj).css("left", "-" + width_offset + "px");
    } else {

        //alert("Step 1: getting document size\n\nWidth: " + doc_width + "px\nHeight = " + doc_height + "px");
        var image_width = $(obj).width();
        var image_height = $(obj).height();
        $(obj).css("left", "0px");
        //alert("Step 2: getting image size\n\nWidth: " + image_width + "px\nHeight = " + image_height + "px");
        var image_ratio = image_width / image_height;
        //alert("Step 3: getting image width/height ratio: " + image_ratio);
        var new_width = doc_width;
        var new_height = Math.round(new_width / image_ratio);
        //alert("Step 4: adapting the image to document width, mantaining the ratio\n\nWidth: " + new_width + "px\nHeight = " + new_height + "px");
        $(obj).width(new_width);
        $(obj).height(new_height);
        if (new_height < doc_height) {
            new_height = doc_height;
            new_width = Math.round(new_height * image_ratio);
            //alert("Step 5: the image isn't high enough\n\nAdapting the image to document height, mantaining the ratio\n\nWidth: " + new_width + "px\nHeight = " + new_height + "px");
            $(obj).width(new_width);
            $(obj).height(new_height);
            var width_offset = Math.round((new_width - doc_width) / 2);
            //alert("Step 6: moving the image left by " + width_offset + "px to have it centered");
            $(obj).css("left", "-" + width_offset + "px");
        }
    }
   
   
    
    //alert("resize bg fix done");
}
function pushdown() {
    var bottom = $(".lowerright").height()
    var goal
    var body
    if($(".contentbody").length != 0){
        $body = $(".contentbody");
    }
    if ($(".contentbodywide").length != 0) {
        $body = $(".contentbodywide");
    }
    goal = $body.height();
    if ($(".lowerright").length == 0) return;
    $(".lowerright").before("<div class='pushdown'></div>");
    while (bottom < goal) {
        $(".pushdown").height($(".pushdown").height() + 2);
        //goal = $body.height();
        bottom = $(".pushdown").height() + $(".lowerright").height()
        
    }
    
}





