/*
 * t8y.com 2011
 *
 * Developed by:
 * - Cédric Trigoso
 */

var drix = {};

drix.Background = (function() {

    var node;
    var parentNode;
    var imageNode;
    var url;

    var align = 'center';
    var valign = 'middle';
    var minWidth = 0;
    var minHeight = 0;

    var image;
    var imageWidth;
    var imageHeight;
    var loaded = false;

    var createNodes = function(params) {
        // Creating the div node containing the background
        node = $('<div></div>');
        node.css({
            position: 'fixed',
            zIndex: ('undefined' != typeof(params.zIndex)) ? params.zIndex : 0,
            overflow: 'hidden',
            width: 100 + '%',
            height: 100 + '%',
            backgroundColor: ('undefined' != typeof(params.backgroundColor)) ? params.backgroundColor : '#fff'
        });

        // Creating the image node
        imageNode = $('<img alt=""/>');
        imageNode.css('position', 'absolute');

        // Adding the nodes to the page
        node.prepend(imageNode);
        parentNode.prepend(node);
    };

    var loadImage = function() {
        image = new Image();
        $(image).load(onLoad);
        image.src = url;

        if (image.complete && !loaded) {
            // Image is in cache (IE6 & IE7 ... Firefox can handle the onload well even if the file was in cache)
            onLoad();
        }
    };

    var onLoad = function() {
        if (!loaded) {
            loaded = true;

            // Displaying the image
            imageNode[0].src = image.src;

            // Getting the size of the image
            imageWidth = image.width;
            imageHeight = image.height;

            // Setting the correct size for the image
            resize();

            // Connecting the resize method to the window.onresize function to fire when the user resizes the window
            $(window).resize(resize);
        }
    };

    var resize = function() {
        // Defining the minimal width
        if ($(window).width() < minWidth) {
            node.css('width', minWidth + 'px');
        } else {
            node.css('width', '100%');
        }

        // Defining the minimal height
        if ($(window).height() < minHeight) {
            node.css('height', minHeight + 'px');
        } else {
             node.css('height', '100%');
        }

        // Resizing the background image
        var imageProportion = imageHeight / imageWidth;

        if (node.height() / node.width() > imageProportion) {
            if (node.height() > imageHeight) {
                // The image needs to be stretched
                imageNode[0].height = node.height();
            } else {
                // Keeping the original size of the image
                imageNode[0].height = imageHeight;
            }
            imageNode[0].width = imageNode[0].height / imageProportion;
        } else {
            if (node.width() > imageWidth) {
                // The image needs to be stretched
                imageNode[0].width = node.width();
            } else {
                // Keeping the original size of the image
                imageNode[0].width = imageWidth;
            }
            imageNode[0].height = imageNode[0].width * imageProportion;
        }

        // Aligning the image
        alignBackground();
    };

    var alignBackground = function() {
        // Horizontal alignment
        if ('center' == align) {
            imageNode.css('left', - Math.ceil((imageNode[0].width - node.width()) / 2) + 'px');
        } else if ('right' == align) {
            imageNode.css('right', 0);
        } else {
            imageNode.css('left', 0);
        }

        // Vertical alignment
        if ('middle' == valign) {
            imageNode.css('top', - Math.ceil((imageNode[0].height - node.height()) / 2) + 'px');
        } else if ('bottom' == valign) {
            imageNode.css('bottom', 0);
        } else {
            imageNode.css('top', 0);
        }
    };

    // Public interface
    return {
        init: function(parentNodeParam, urlParam, params) {
            parentNode = $(parentNodeParam);
            url = urlParam;
            params = (!params) ? {} : params;

            if (0 == parentNode.length) {
                throw 'drix.Background.init() - The first parameter "parentNode" is mandatory and should be a node reference.';
            }

            if (!url) {
                throw 'drix.Background.init() - The second parameter "url" is mandatory.';
            }

            // Horizontal alignment
            if ('undefined' != typeof(params.align) && ('left' == params.align || 'center' == params.align || 'right' == params.align)) {
                align = params.align;
            } else if ('undefined' != typeof(params.align)) {
                throw 'drix.Background.init() - The third parameter "params.align" has to have one of these values: "left", "center" or "right".';
            }

            // Vertical alignment
            if ('undefined' != typeof(params.valign) && ('top' == params.valign || 'middle' == params.valign || 'bottom' == params.valign)) {
                valign = params.valign;
            } else if ('undefined' != typeof(params.valign)) {
                throw 'drix.Background.init() - The third parameter "params.valign" has to have one of these values: "top", "middle" or "bottom".';
            }

            // Minimal width
            if ('undefined' != typeof(params.minWidth) && !isNaN(params.minWidth)) {
                minWidth = parseInt(params.minWidth);
            } else if ('undefined' != typeof(params.minWidth)) {
                throw 'drix.Background.init() - The third parameter "params.minWidth" must be an integer.';
            }

            // Minimal height
            if ('undefined' != typeof(params.minHeight) && !isNaN(params.minHeight)) {
                minHeight = parseInt(params.minHeight);
            } else if ('undefined' != typeof(params.minHeight)) {
                throw 'drix.Background.init() - The third parameter "params.minHeight" must be an integer.';
            }

            // Create the nodes
            createNodes(params);
            // Load the image
            loadImage();
        }
    };
})();

