(function(){
        var isReady=false;//判断onDOMReady方法是否已经被执行过
        var readyList= [];//把需要执行的方法先暂存在这个数组里
        var timer;//定时器句柄
        ready=function(fn) {
               if (isReady )
                      fn.call( document);
               else
                      readyList.push( function() { return fn.call(this);});
               return this;
        }
        var onDOMReady=function(){
               for(var i=0;i<readyList.length;i++){
                      readyList[i].apply(document);
               }
               readyList = null;
        }
        var bindReady = function(evt){
               if(isReady) return;
               isReady=true;
               onDOMReady.call(window);
               if(document.removeEventListener){
                      document.removeEventListener("DOMContentLoaded", bindReady, false);
               }else if(document.attachEvent){
                      document.detachEvent("onreadystatechange", bindReady);
                      if(window == window.top){
                             clearInterval(timer);
                             timer = null;
                      }
               }
        };
        if(document.addEventListener){
               document.addEventListener("DOMContentLoaded", bindReady, false);
        }else if(document.attachEvent){
               document.attachEvent("onreadystatechange", function(){
                      if((/loaded|complete/).test(document.readyState))
                             bindReady();
               });
               if(window == window.top){
                      timer = setInterval(function(){
                             try{
                                    //在IE下用能否执行doScroll判断dom是否加载完毕
                                   isReady||document.documentElement.doScroll('left');
                             }catch(e){
                                    return;
                             }
                             bindReady();
                      },5);
               }
        }
})();

var AutoAdjustIframe = function() {
    var autoSecond = 1;
    this.autoAdjust = function(iframeId) {
        setInterval(function() {
            try {
                var height = parseFloat(window.frames[iframeId].frames[iframeId + '-proxyiframe'].location.hash.replace(/^#/, '')) || 100;
                document.getElementById(iframeId).style.height = height + 'px';
            } catch (e) { };
        }, autoSecond);
    };
    var getHeight = function() {
        //return Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,
        //document.documentElement.clientHeight,document.body.clientHeight);
        return Math.max(document.documentElement.scrollHeight);
    };
    /*
    * 设置代理页的hash值，需要A域传给B域hostname
    */
    this.setHash = function() {
        var aSearch = document.location.search.match(/hostname=([^&]+)/);
        //if(!!aSearch){
        //设定代理页面url
        var proxyUrl = 'http://www.gialen.com/agent.html';
        var height = getHeight();
        try {
            console.log('proxyUrl:' + proxyUrl + '\nThe Iframe\'s height:' + height);
        } catch (e) { };
        //生成代理iframe
        var iframe = document.createElement('iframe');
        iframe.src = proxyUrl + '#' + height;
        iframe.id = 'iframeC';
        iframe.name = 'iframeC';
        iframe.style.display = 'none';
        document.body.appendChild(iframe);
        //动态设置代理iframe的hash，以便重新获取新的高度
        var interval = setInterval(function() {
            if (getHeight() != height) {
                height = getHeight();
                iframe.src = proxyUrl + '#' + height;
                try {
                    console.log('Reloading,The Iframe\'s height:' + height);
                } catch (e) { };
            }
        }, autoSecond);
        //}
    };
};

try
{
var aai=new AutoAdjustIframe();
ready(aai.setHash);
}
catch(e){};
