/******************************************************
    * jQuery plug-in
    * Easy Background Image Resizer
    * Developed by J.P. Given (http://johnpatrickgiven.com)
    * Useage: anyone so long as credit is left alone
	* Brutalized version by Friant 05/10/2011 for video control only
******************************************************/

(function($) {
    // Global Namespace
    var jqez = {};
    // Define the plugin
    $.fn.ezBgResize = function(options) {
        // Set global to obj passed
        jqez = options;
		//console.log("par ici!");
		 resizeImage();
    };
    $(window).bind("resize", function() {
        resizeImage();
    });
    // Actual resize function
    function resizeImage() {

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

        $("#video-container").children('video').css("position", "relative");

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

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

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

        // Center the image
        if (typeof(jqez.center) == 'undefined' || jqez.center) {
            if ($("#video-container").children('video').width() > $(window).width()) {
                var this_left = ($("#video-container").children('video').width() - $(window).width()) / 2;
                $("#video-container").children('video').css({
                    "top"  : 0,
                    "left" : -this_left
                });
            }
            if ($("#video-container").children('video').height() > $(window).height()) {
                var this_height = ($("#video-container").children('video').height() - $(window).height()) / 2;
                $("#video-container").children('video').css({
                    "left" : 0,
                    "top" : -this_height
                });
            }
        }
        $("#video-container").css({
            "visibility" : "visible"
        });
        $("body").css({
            "overflow":"hidden"
        });
    }
})(jQuery);

