/**
 * @author hangpeng
 */
(function(){
    var wF = wF || {}, _doc = document,    /**
     * 简单地加载js模块
     * @param {String} file's path which's correct absolutely!
     * @param {Function} callback
     */
    loader = function(path, callback){
        var _script = document.createElement("script"), _head = document.getElementsByTagName('head')[0];
        _script.src = path;
        _head.appendChild(_script);
        _script.onload = _script.onreadystatechange = function(){
            if ((!this.readyState) || this.readyState == "loaded" || this.readyState == "complete") {
                callback();
            }
        }
        _script.onerror = function(){
            _script.onload = _script.onerror = undefined;
            alert("Failed");
            _head.removeChild(_script);
        }
    };
    wF = {
        /**
         * 添加书签
         * @param {String} siteName
         * @param {String} siteAddr
         * */
        addBookMark: function(siteName, siteAddr){
            var nome_sito = siteName;
            var url_sito = siteAddr;
            if ((navigator.appName == "Microsoft Internet Explorer") &&
            (parseInt(navigator.appVersion) >=
            4)) 
                window.external.AddFavorite(url_sito, nome_sito);
            else 
                if (navigator.appName == "Netscape") 
                    window.sidebar.addPanel(nome_sito, url_sito, '');
                else 
                    alert("您的浏览器不支持该功能，请手动添加！");
        },
        /**
         * 设为首页
         * @param {String} url
         */
        setMyHomepage: function(url){
            if (!!(window.attachEvent && !window.opera)) {
                document.body.style.behavior = 'url(#default#homepage)';
                document.body.setHomePage(url);
            }
            else {
                if (window.clipboardData && clipboardData.setData) {
                    clipboardData.setData('text', url);
                }
                else {
                    alert('您的浏览器不支持该功能，请手动添加！');
                }
            }
        },
        /**
         * 自动检测输入框
         * @param {Object} elem
         * @param {String} val
         */
        autoInput: function(elem, val){
            if (!elem || elem.nodeName.toLowerCase() != 'input') 
                return;
            elem.onfocus = function(e){
                if (this.value == val) {
                    this.value = '';
                }
                if (elem.onblur) 
                    return;
                elem.onblur = function(e){
                    if (this.value.replace(/\s+/g, '') == '') {
                        this.value = val;
                    }
                }
            }
        },
        /**
         * 生成一个可拖动的窗体
         * @param {String} title
         * @param {Boolean} drag or not
         * @param {String} box ID flag
         * @param {Object} event Object
         * @param {String} innerHTML Structure
         * @return {Object} this box
         * @example
         * <div class="wF-box">
         * 	<h1 class="w-drag"><em class="w-title">title</em><em class="w-close">X</em></h1>
         * 	<div class="warea">
         * 		Here is Content
         * 	</div>
         * </div>
         */
        lightBox: function(title, drag, flag, e, structure){
            //console.log(arguments);
            if (_box = _doc.getElementById(flag)) {
                _box.style.display = 'block';
                return _box;
            }
            var _box = null, _btn_top, _btn_c = null, _e = e || window.event, _geBox = function(){
                var _str = '';
                _box = document.createElement('div');
                _box.className = 'wF-box';
                _box.id = flag;
                _str += '<h1';
                _str += drag ? ' class="w-drag">' : '>';
                _str += '<em class="w-title">' + title + '</em><em class="w-close">X</em></h1>';
                _str += '<div class="w-area">' + structure + '</div>';
                _box.innerHTML = _str;
                return _box;
            };
            //生成
            _geBox();
            //定位
            _box.style.position = "absolute";
            _box.style.left = (_e.clientX || _e.pageX) + "px";
            _box.style.top = (_e.clientY || _e.pageY) + "px";
            //关闭
            _btn_top = _box.children.item(0).getElementsByTagName("em");
            for (var i = _btn_top.length - 1; i >= 0; i--) {
                if (_btn_top[i].className == 'w-close') {
                    _btn_c = _btn_top[i];
                    break;
                }
                else {
                    continue;
                }
            }
            _btn_c.onclick = function(){
                _box.style.display = 'none';
            }
            //添加
            document.body.appendChild(_box);
            //加载拖动模块
            if (drag) {
                loader("js/jquery.event.drag-2.0.min.js", function(){
                    $('div.wF-box').drag(function(ev, dd){
                        $(this).css({
                            top: dd.offsetY,
                            left: dd.offsetX
                        });
                    }, {
                        handle: "h1.w-drag"
                    });
                })
            }
            return _box;
        },
        /**
         * 自适应iframe高度
         * @param {Object} id
         */
        adjustIframe: function(id){
        
            var iframe = document.getElementById(id)
            
            var idoc = iframe.contentWindow && iframe.contentWindow.document || iframe.contentDocument;
            
            //取得其文档对象  
            
            (function(){
            
                if (idoc.readyState !== "complete") {//如果没有加载完成，继续检测  
                    //javascrpt最短时钟间隔为16ms，这里取其倍数  
                    
                    setTimeout(arguments.callee, 64);
                    
                }
                else {
                
                    iheight = Math.max(idoc.body.scrollHeight, idoc.documentElement.scrollHeight);//取得其高  
                    iframe.style.height = iheight + "px";
                    
                }
                
            })();
            
        }
    };
    //绑定到全局
    window['wF'] = wF;
}(window))

