function getArgs()
{
    var args = new Object();
    var query = location.search.substring(1);
    var pairs = query.split("&");
    for(var i = 0; i < pairs.length; i++)
    {
        var pos = pairs[i].indexOf('=');
        if (pos == -1) continue;
        var argname = pairs[i].substring(0,pos);
        var value = pairs[i].substring(pos+1);
        args[argname] = unescape(value);
    }
    return args;
} 

function widgetFixAnchor(anchorObject, mode)
{
    if ((typeof mode == 'undefined') || (mode == 'top'))
    {
        var oldhref = anchorObject.href;
        
        var postBackTag = "javascript:__doPostBack(";
        if (oldhref.indexOf(postBackTag) != -1)
        {
            return;           
        }
        
        anchorObject.onclick = function () { window.top.location.href = oldhref };
        anchorObject.href = "#";
    }
}

function widgetFixAnchors(mode)
{
    theObjects = document.getElementsByTagName("a");
    for (var i=0; i<theObjects.length; i++)
        if ((theObjects[i].href != '') && (theObjects[i].href != '#') && 
            ((typeof theObjects[i].onclick == 'undefined') || (theObjects[i].onclick == null)) )
            widgetFixAnchor(theObjects[i], mode);
            
    // Resize the encapsulating iframe by using a hidden iframe that loads from the same domain as the one hosting the widget
    // this allows cross domain communication between the host site and the iframe of the widget
    // We do this in a timeout since otherwise Firefox perpetually loads the page - not sure why
    setTimeout(
        function() {
            var args = getArgs()
            if (args.resizeiframeurl)            
            {                                        
                var height = Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
                var width = Math.max(document.body.scrollWidth, document.documentElement.scrollWidth);
                var oIFrame = document.createElement("iframe");
                oIFrame.style.display = "none";
                oIFrame.setAttribute("scrolling", "no");
                oIFrame.setAttribute("frameborder", "0");
                oIFrame.setAttribute("src", args.resizeiframeurl +
                    "&width=" + width + "&height=" + height);
                document.body.appendChild(oIFrame);
            }
        },
        100 );
}
