// Copyright (R) 2009 Intel Corporation, All Rigths Reserved.
/*members addEventListener, align, appendChild, attachEvent, constructor, 
createElement, divId, frameBorder, getElementById, hasOwnProperty, 
height, href, id, indexOf, lastIndexOf, length, location, marginHeight, 
marginWidth, name, proxy, register, scrolling, src, substr, width */
var widget = function (spec, defaultProxy) {
    var that = {},
    widgets = {},
    divIds = {},
    create = function (instance) {
        var div, indexQuery, source,
        iframe = document.createElement("iframe"),
        beforeHash = "",
        hash = "",
        beforeQuery = "",
        afterQuery = "",
        proxyName = "",
        src = instance.src,
        href = window.location.href,
        instanceProxy = instance.proxy,
        indexHash = src.indexOf("#");
        if (indexHash >= 0) {
            beforeHash = src.substr(0, indexHash);
            hash = src.substr(indexHash, src.length);
        } else {
            beforeHash = src;
        }
        indexQuery = beforeHash.indexOf("?");
        if (indexQuery >= 0) {
            beforeQuery = beforeHash.substr(0, indexQuery);
            afterQuery = beforeHash.substr(indexQuery + 1, beforeHash.length);
        } else {
            beforeQuery = beforeHash;
        }
        if (instanceProxy) {
            if (instanceProxy.substr(0, 7) !== "http://" && instanceProxy.substr(0, 8) !== "https://") {
                instanceProxy = href.substr(0, href.lastIndexOf("/")) + "/" + instanceProxy;
            }
            proxyName += "iframeProxy=" + encodeURIComponent(instanceProxy);
        }
        source = beforeQuery;
        if (afterQuery || proxyName) {
            source += "?";
        }
        source += afterQuery;
        if (afterQuery && proxyName) {
            source += "&";
        }
        source += proxyName + hash;
        iframe.src = source;
        iframe.align = instance.align || "";
        iframe.frameBorder = instance.frameBorder || "0";
        iframe.height = instance.height || "1";
        iframe.hspace = instance.hspace || "0";
        iframe.id = instance.id || "";
        iframe.marginHeight = instance.marginHeight || "0";
        iframe.marginWidth = instance.marginWidth || "0";
        iframe.name = instance.name || "";
        iframe.width = instance.width || "1";
        iframe.wspace = instance.wspace || "0";
        iframe.scrolling = instance.scrolling || "no";
        div = document.getElementById(instance.divId);
        if (div === null) {
            throw new Error("The element with id " + instance.divId + " does not exist.");
        }
        div.appendChild(iframe);
    },
    register = function (args, instanceProxy) {
        var i, arg;
        if (args && typeof args === "object") {
            if (args.constructor !== Array) {
                args = [args];
            }
        } else {
            throw new Error("Invalid arguments.");
        }
        for (i = 0; i < args.length; i += 1) {
            arg = args[i];
            if (arg.src === undefined || arg.divId === undefined) {
                throw new Error("Required arguments are missing.");
            }
            if (widgets[arg.src] || divIds[arg.divId]) {
                throw new Error("Each iframe src and div id must be unique.");
            }
            if (instanceProxy && !arg.proxy && arg.proxy !== null) {
                arg.proxy = instanceProxy;
            } else if (defaultProxy && !arg.proxy && arg.proxy !== null && instanceProxy !== null) {
                arg.proxy = defaultProxy;
            }
            widgets[arg.src] = arg;
            divIds[arg.divId] = true;
        }
    },
    onLoad = function () {
        var src;
        for (src in widgets) {
            if (widgets.hasOwnProperty(src)) {
                create(widgets[src]);
            }
        }
    };
    that.register = register;
    if (spec) {
        register(spec, defaultProxy);
    }
    if (typeof (window.addEventListener) === "undefined") {
        window.attachEvent("onload", onLoad);
    } else {
        window.addEventListener("load", onLoad, false);
    }
    return that;
};
