(function ($) {
    $.fn.fullScreen = function (args) {
        var defaults = {
            maintainAspectRatio: true,
            resize: true,
            reposition: true,
            expand: true,
            contract: true,
            nativeSceneHeight: 800,
            nativeSceneWidth: 1200,
            align: "top left",
            leftOfCenter: 0,
            rightOfCenter: 0
        };
        $(this).each(function () {
            var options = $.extend(defaults, args),
            obj = $(this),            
            oHeight, oWidth, oTop, oRight, oBottom, oLeft, w, alignLeft, alignRight, alignCenter, alignTop, alignBottom, alignMiddle;

            //figure out alignment configureation
            options.align = options.align.toLowerCase();
            alignTop = (options.align.search("top") != -1);
            alignRight = (options.align.search("right") != -1);
            alignCenter = (options.align.search("center") != -1);
            alignBottom = (options.align.search("bottom") != -1);
            alignLeft = (options.align.search("left") != -1);
            alignMiddle = (options.align.search("middle") != -1);
            
            //get original size
            oHeight = obj.css('height');
            oHeight = Number(oHeight.replace("px", ""));
            oWidth = obj.css('width');
            oWidth = Number(oWidth.replace("px", ""));
            oTop = obj.css("top");
            oTop = Number(oTop.replace("px", ""));
            oRight = obj.css("right");
            oRight = Number(oRight.replace("px", ""));
            oBottom = obj.css("bottom");
            oBottom = Number(oBottom.replace("px", ""));
            oLeft = obj.css("left");
            oLeft = Number(oLeft.replace("px", ""));
            
            w = $(window);
            $(window).resize(onResize);
            onResize();
            obj.load(function () {
                onResize();
            });

            function onResize(event) {
                var xRatio, yRatio, cssObj = {};
                xRatio = w.width() / options.nativeSceneWidth;
                yRatio = w.height() / options.nativeSceneHeight;
                if (options.maintainAspectRatio) {
                    xRatio = (xRatio > yRatio) ? xRatio : yRatio;yRatio = (xRatio > yRatio) ? xRatio : yRatio;
                }
                if (!options.contract && xRatio < 1) {
                    xRatio = 1;
                }
                if (!options.expand && xRatio > 1) {
                    xRatio = 1;
                }
                if (!options.contract && yRatio < 1) {
                    yRatio = 1;
                }
                if (!options.expand && yRatio > 1) {
                    yRatio = 1;
                }
                if (options.resize) {
                    var newHeight, newWidth;
                    newHeight = yRatio * oHeight;
                    newWidth = xRatio * oWidth;
                    cssObj.width = newWidth;
                    cssObj.height = newHeight;
                } else {
                	//just use original size
                    cssObj.width = obj.css("width");
                    cssObj.height = obj.css("height");
                }
           
                if (options.reposition) {
                    var newTop, newRight, newBottom, newLeft;
                    var string = "";
                    if (!isNaN(oTop) && alignTop) {
                        newTop = oTop * yRatio;
                        cssObj.top = newTop;
                    } else if (!isNaN(oBottom) && alignBottom) {
                        newBottom = oBottom * yRatio;
                        cssObj.bottom = newBottom;
                    } else if (alignMiddle) {
                        newTop = (w.height() - cssObj.height) / 2;
                        cssObj.top = newTop;
                    }
                    if (!isNaN(oRight) && alignRight) {
                        newRight = oRight * yRatio;
                        cssObj.right = newRight;
                    } else if (!isNaN(oLeft) && alignLeft) {
                        newLeft = oLeft * xRatio;
                        cssObj.left = newLeft;
                    } else if (alignCenter) {
                        newLeft = (w.width() - cssObj.width) / 2;
                        cssObj.left = newLeft - options.leftOfCenter + options.rightOfCenter;
                    }
                }
                try {
                    obj.css(cssObj);
                } catch (e) {
                    if (console != null) {
                        console.log(cssObj);
                        console.log(e);
                        console.log(obj);
                    }
                }
            }
        });

        function setOptions(value) {
            options = value;
        }

        function getOptions() {
            return options;
        }
    };
})(jQuery);
