$(document).ready(function() {

    $("ul.sf-menu").superfish({
        dropShadows:false,
        autoArrows:false
    });

    
    $("body").ezBgResize({
        img : "http://images.champlainheights.com/pics/backgrounds/" + bg
    });
    

    $('.event-moredetails').click(function() {
        $(this).parent().next().toggle();
        return false;
    })

    $('#listings-iframe').load(function() {
        $("#loading-listings").hide();
    })

    //loadBg();

        //cycle();

})

function cycle() {
    $.get(base + '/bgs/randoms/',function(data) {
         $("#jq_ez_bg").html(data);
         resizeImage();
         $('#jq_ez_bg').cycle({
             fx: 'fade',
             after: resizeImage
         })
         $(window).bind("resize", function() {
            resizeImage();
        });
    })
}

function loopBg() {
    var date = new Date().getTime();

    var url = base + "/bgs/random?"+date;

    $("<img/>").attr("src", url).load(function() {
        var old = $("#jq_ez_bg").children();
        $("#jq_ez_bg").prepend(this);
        old.fadeOut(2000,function() {$(this).remove();});
        $("body").css({
            "overflow":"hidden"
        });
        resizeImage();
    })
}

function loadBg() {
    var url = base + "/bgs/random";

    $("<img/>").attr("src", url).load(function() {

        // Create a unique div container
        //$("body").append('<div id="jq_ez_bg"></div>');

        // Add the image to it.
        $(this).hide();
        
        $("#jq_ez_bg").html(this);

        // First position object
        $("#jq_ez_bg").css("visibility","hidden");

        // Overflow set to hidden so scroll bars don't mess up image size.
        $("body").css({
            "overflow":"hidden"
        });
        
        resizeImage();
        $(this).fadeIn(1000);
    });

/*

    // set up the node / element
    _im =$("<img>");

    $("body").append('<div id="jq_ez_bg"></div>');

    // hide and bind to the load event
    _im.hide();
    _im.bind("load",function(){
        $(this).fadeIn();
    });

    resizeImage();
    // append to target node / element
    $('body div#jq_ez_bg').append(_im);

    // set the src attribute now, after insertion to the DOM
    _im.attr('src',_url);
    */
    
    $(window).bind("resize", function() {
        resizeImage();
    });
}

function resizeImage() {

    $("#jq_ez_bg").css({
        "position":"fixed",
        "top":"0px",
        "left":"0px",
        "z-index":"-1",
        "overflow":"hidden",
        "width":$(window).width() + "px",
        "height":$(window).height() + "px",
        "opacity" : 1
    });

    // Image relative to its container
    $("#jq_ez_bg").children('img').css("position", "absolute");

    // Resize the img object to the proper ratio of the window.
    var iw = $("#jq_ez_bg").children('img').width();
    var ih = $("#jq_ez_bg").children('img').height();

    if ($(window).width() > $(window).height()) {
        //console.log(iw, ih);
        if (iw > ih) {
            var fRatio = iw/ih;
            $("#jq_ez_bg").children('img').css("width",$(window).width() + "px");
            $("#jq_ez_bg").children('img').css("height",Math.round($(window).width() * (1/fRatio)));

            var newIh = Math.round($(window).width() * (1/fRatio));

            if(newIh < $(window).height()) {
                var fRatio = ih/iw;
                $("#jq_ez_bg").children('img').css("height",$(window).height());
                $("#jq_ez_bg").children('img').css("width",Math.round($(window).height() * (1/fRatio)));
            }
        } else {
            var fRatio = ih/iw;
            $("#jq_ez_bg").children('img').css("height",$(window).height());
            $("#jq_ez_bg").children('img').css("width",Math.round($(window).height() * (1/fRatio)));
        }
    } else {
        var fRatio = ih/iw;
        $("#jq_ez_bg").children('img').css("height",$(window).height());
        $("#jq_ez_bg").children('img').css("width",Math.round($(window).height() * (1/fRatio)));
    }

    // Center the image
   
    if ($("#jq_ez_bg").children('img').width() > $(window).width()) {
        var this_left = ($("#jq_ez_bg").children('img').width() - $(window).width()) / 2;
        $("#jq_ez_bg").children('img').css({
            "top"  : 0,
            "left" : -this_left
        });
    }
    if ($("#jq_ez_bg").children('img').height() > $(window).height()) {
        var this_height = ($("#jq_ez_bg").children('img').height() - $(window).height()) / 2;
        $("#jq_ez_bg").children('img').css({
            "left" : 0,
            "top" : -this_height
        });
    }
    

    $("#jq_ez_bg").css({
        "visibility" : "visible"
    });

    // Allow scrolling again
    $("body").css({
        "overflow":"auto"
    });


}
