﻿var widgetID = null;
var countTry = 0;
var iframeDocument = null;

function afterAddWidget(_widgetID)
{
    widgetID = _widgetID;
    setTimeout("afterAddWidgetSUSPEND();",3000);
}

function afterAddWidgetSUSPEND()
{
    countTry += 1;
    var widgetObj = findObjectNodeInFrame('frame'+widgetID);
    if (widgetObj==null || widgetObj.clientWidth==0) 
    {
        if (countTry<7)
        {
         setTimeout("afterAddWidgetSUSPEND();",3000);
         return;
         }
        else
        {
            alert('Failed to adjust widget size. Please resize the widget frame with the mouse.');
            return;
        }
    }
    var sheet = document.getElementById('win'+widgetID+'_resizeSheet');
    resizeSheet(sheet,widgetObj.clientWidth+15,widgetObj.clientHeight+15);
}

function resizeSheet(thesheet,width,height)
{
                var sheet = $(thesheet);
                var theDiv = $(thesheet).parents(".dragWin");
                var frameID = theDiv[0].id.replace("win","");
                var cell = $("#tdContentWin"+frameID);
                var row = $("#trContentWin"+frameID);
                
                minimizeElements(theDiv);
                SetSize(theDiv,width,height+25);
                FixTableSpecific(theDiv.children(":first"),$(thesheet),width-2,height-2);
                SetFrameSize(cell);
                //SetBannersSize(row,sheet);
                
                try
                {
                //ie
                 var borderDiv = document.getElementById('win_vml' + frameID);
               borderDiv.style.width = (width+10) + 'px';
                borderDiv.style.height = (height+40) + 'px';
                }
                catch(ee){}
                try
                {
                //chrome
                 var tableContent = document.getElementById('tableContentWin' + frameID);
               tableContent.style.width = (width) + 'px';
                tableContent.style.height = (height) + 'px';
                var cell = $('#tdContentWin'+frameID);
                 SetFrameSize(cell);
                }
                catch(ee){}
                                
                ajaxUpdateCall(
                    {
                        type      : "resize-stop",
                        id        : theDiv[0].id.replace("win",""),
                        frHeight  : height,
                        frWidth   : width
                    });
                    

}
function findObjectNodeInFrame(frameID)
{
   var iFrame =  document.getElementById(frameID);
var iFrameBody;
   if ( iFrame.contentDocument ) 
   { // DOM
    var iFrameBody = iFrame.contentDocument.body;
   }
   else if ( iFrame.contentWindow ) 
   { // IE
  //var iFrameBody = iFrame.contentWindow.document.getElementdByTagName('body')[0];
  var iFrameBody = iFrame.contentWindow.document.body;
   }
iframeDocument = iFrame.contentWindow.document;
    return findObjectChild(iFrameBody);
}

function findObjectChild(parent)
{
    var children=parent.childNodes;

    for (var i=0;i<children.length;i++)
    {
        var nodename = children[i].nodeName;
        if (nodename == "OBJECT" || nodename == "EMBED" || nodename == "IFRAME") return children[i];
    }
    for (var i=0;i<children.length;i++)
    {
        var result=findObjectChild(children[i]);
        if (result != null) return result;
    }

}
