﻿/*!
 * jQuery loader plugin
 * Version BETA 3 (03-AUGUST-2011)
 * Copyright (c) 2011 Guilherme Mori {guilherme.dannaa.mori}@gmail.com
 * 
 * http://code.google.com/p/jquery-preloader/
 */
(function (a) {
    var b = {
        bgColor: "#000000",
        bgOpacity: .90,
        load: [],
        message: "Loading file %c of %t | %p%",
        ending_message: "Welcome!",
        fadeOutSpeed: "slow"
    };
    var d = {
        init: function (c) {
            if (c) {
                a.extend(b, c)
            }
            if (!a.isArray(b.load)) {
                return a.error("Options must have at least load contents array on jQuery.preLoadGUI().")
            }
            d.show(b.load)
        },
        show: function (e) {
            for (var f in e) {
                if (a.isArray(e[f])) {
                    i = "preLoadGUIBeta3";
                    c = a("#" + i).size();
                    if (c == 0) {
                        a("<div></div>").attr("id", i).css({
                            width: "100%",
                            height: "100%",
                            backgroundColor: b.bgColor,
                            zIndex: 99999999,
                            position: "fixed",
                            opacity: b.bgOpacity,
                            left: 0,
                            top: 0
                        }).appendTo("body");
                        a("<div></div>").attr("id", i + "loader").attr("rel", e.length).attr("loaded", 0).css({
                            width: "0",
                            height: "1px",
                            backgroundColor: "#ffffff",
                            zIndex: 999999999,
                            position: "fixed",
                            left: 0,
                            top: "50%"
                        }).appendTo("body");
                        a("<div></div>").attr("id", i + "loaderText").css({
                            fontSize: "11px",
                            fontFamily: "Verdana, Geneva, sans-serif",
                            width: "100%",
                            color: "#ffffff",
                            height: "12px",
                            zIndex: 999999999,
                            position: "fixed",
                            left: 0,
                            top: "50%",
                            marginTop: "-20px"
                        }).appendTo("body")
                    }
                    switch (e[f][1]) {
                    case "js":
                        a.getScript(e[f][0], function () {
                            d.show(e)
                        });
                        break;
                    case "img":
                        a("<img />").css({
                            visibility: "hidden"
                        }).attr("src", e[f][0]).load(function () {
                            d.show(e)
                        });
                        break;
                    case "css":
                        a("<style></style>").attr({
                            type: "text/css"
                        }).load(e[f][0], function () {
                            a(this).appendTo("head");
                            d.show(e)
                        });
                        break;
                    case "html":
                        a("<div></div>").attr("rel", e[f][2]).load(e[f][0], function () {
                            a(a(this).attr("rel")).append(a(this).html());
                            d.show(e)
                        });
                        break;
                    case "run":
                        a.getScript(e[f][0], function () {
                            d.show(e)
                        });
                        break;
                    default:
                        delete e[f];
                        d.show(e);
                        return;
                        break
                    }
                    d.update(e[f][0]);
                    delete e[f];
                    return
                }
            }
            a("#" + i + "loaderText").text(b.ending_message);
            a("#" + i + "loader").animate({
                width: "100%"
            }, 0, "swing", function () {
                i = "preLoadGUIBeta3";
                a("#" + i + "loader, #" + i + ", #" + i + "loaderText").fadeOut(b.fadeOutSpeed)
            })
        },
        update: function (c) {
            i = "preLoadGUIBeta3";
            ld = a("#" + i + "loader").attr("loaded") * 1;
            t = a("#" + i + "loader").attr("rel") * 1;
            msg = b.message;
            msg = msg.replace(/(%f)|(%p)|(%c)|(%t)/g, function (a) {
                return a == "%f" ? c : a == "%p" ? Math.round(ld / t * 100) : a == "%c" ? ld : t
            });
            a("#" + i + "loader").attr("loaded", ld + 1).css({
                width: ld / t * 100 + "%"
            });
            a("#" + i + "loaderText").html(msg)
        }
    };
    a.preLoadGUI = function (b) {
        if (typeof b === "object") {
            return d.init(b)
        } else {
            a.error("Options must have at least load contents array on jQuery.preLoadGUI().")
        }
    }
})(jQuery)

