/******************************************************
    * jQuery plug-in
    * Easy Background Image Resizer
    * Developed by J.P. Given (http://johnpatrickgiven.com)
    * Useage: anyone so long as credit is left alone
******************************************************/

(function($) {
	// Global Namespace
    var jqez = {};

    // Define the plugin
    $.fn.ezBgResize = function(options) {

		// Set global to obj passed
		jqez = options;

		// If img option is string convert to array.
		// This is in preparation for accepting an slideshow of images.
		if (!$.isArray(jqez.img)) {
			var tmp_img = jqez.img;
			jqez.img = [tmp_img]
		}

    //$("<img/>").attr("src", jqez.img).load(function() {
    $("<img/>").load(function() {
      jqez.width = this.width;
			jqez.height = this.height;

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

			// Add the image to it.
			$("#jq_ez_bg").html('<img src="' + jqez.img[0] + '" width="' + jqez.width + '" height="' + jqez.height + '" border="0">');

			// 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();

      if($("#content").length > 0 )
      { window.setTimeout("fleXenv.fleXcrollMain('content');", 200);
      }
		//});
    }).attr("src", jqez.img);
    };

	$(window).bind("resize", function() {
		resizeImage();

    //Hoehe des Inhaltscontainers und des Inhalts
    //padding-top: 55px; padding-bottom: 55px; logo-height: 123px; + border-top: 3px; + border-bottom: 3px; = 239px
    var contenthight = $(window).height() - 239;
    var contentinnerheight = $("#content_contentwrapper").height();

    //Content
    if(contentinnerheight < contenthight)
    { $("#content").css({"height":contentinnerheight + "px"});
    }
    else
    { $("#content").css({"height":contenthight + "px"});
    }

    $(handleLoad);
    function handleLoad()
    { var swf_width = $('#jq_ez_bg img').width();
      var swf_height = $('#jq_ez_bg img').height();

      if(!swf_width)
      { setTimeout(handleLoad, 5);
      }
      else
      { $('.video').flash({swf: '/fileadmin/user-files/backgrounds/v3.swf',
                                width: swf_width,
                                height: swf_height,
                                play: true,
                                allowFullScreen: false,
                                wmode: 'transparent',
                                quality: 'high',
                                align: 'middle',
                                loop: 'false',
                                scale: 'exactfit',
                          			bgcolor: '#ffffff',
                          			menu: 'false',
                          			allowScriptAccess:'sameDomain'
        });

        $('.video').css('left', jQuery('#jq_ez_bg img').css('left'));
        $('.video').css('top', jQuery('#jq_ez_bg img').css('top'));
      }
    }
	});

	// Actual resize function
    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" : jqez.opacity
        });

        //Content-, Brandinghoehe und swf auf Browserfenster anpassen
        //padding-top: 55px; padding-bottom: 55px; logo-height: 123px; + border-top: 3px; + border-bottom: 3px; = 239px
        var contenthight = $(window).height() - 239;

        //Content
        $("#content").css({
            "height":contenthight + "px"
        });

        //Branding
        $("#branding").css({
            "height":contenthight + "px"
        });

        //swf
        $("#swf_container").css({
            "width":$(window).width() + "px",
            "height":$(window).height() + "px"
        });

        $("#flash_id").css({
            "width":$(window).width() + "px"
        });
        $("#flash_id").css({
            "height":$(window).height() + "px"
        });
        $("#flash_id_2").css({
            "width":$(window).width() + "px"
        });
        $("#flash_id_2").css({
            "height":$(window).height() + "px"
        });
        $("#flash_id_3").css({
            "width":$(window).width() + "px"
        });
        $("#flash_id_3").css({
            "height":$(window).height() + "px"
        });

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

        // 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 (typeof(jqez.center) == 'undefined' || jqez.center) {
			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"
        });

    }
})(jQuery);
