﻿/*滚动字幕*/
function getElementsByClass(searchClass,tagName) {
 var classElements = new Array();
 if ( tagName == null )
  tagName = '*';
 var els = document.getElementsByTagName(tagName);
 var elsLen = els.length;
 var pattern = new RegExp("(^|\s)"+searchClass+"(\s|$)");
 for (i = 0, j = 0; i < elsLen; i++) {
  if ( pattern.test(els[i].className) ) {
   classElements[j] = els[i];
   j++;
  }
 } 
 return classElements;
}
 
function ccMarquee(className){
 var a=getElementsByClass(className);
 for (i = 0; i < a.length; i++) {
  a[i].innerHTML="<marquee direction='left' onmouseover='javascript:this.stop();'onmouseout='javascript:this.start();' scrollamount='2' scrolldelay='50' width='100%'>"+ a[i].innerHTML+"</marquee>";
 }
}
function ffMarquee(className){
 var a=getElementsByClass(className);
 for (i = 0; i < a.length; i++) {
  a[i].innerHTML="<marquee direction='up' onmouseover='javascript:this.stop();'onmouseout='javascript:this.start();' scrollamount='2' scrolldelay='50' width='100%'>"+ a[i].innerHTML+"</marquee>";
 }
}
/*图片缩略图*/
function loaded(myimg,mywidth,myheight){
var tmp_img = new Image();
tmp_img.src=myimg.src;
image_x=tmp_img.width;
image_y=tmp_img.height;
	if(image_x > mywidth){
	tmp_img.height=image_y * mywidth / image_x;
	tmp_img.width=mywidth;
		if(tmp_img.height>myheight){
			tmp_img.width=tmp_img.width * myheight / tmp_img.height;
			tmp_img.height=myheight;
			}
	}else if(image_y > myheight)
	{
	tmp_img.width=image_x * myheight / image_y;
	tmp_img.height=myheight;
		if(tmp_img.width>mywidth){
			tmp_img.height=tmp_img.height * mywidth / tmp_img.width;
			tmp_img.width=mywidth;
			}
	}
	myimg.width=tmp_img.width;
	myimg.height=tmp_img.height;

}

var Dialog = {
    "init": ""
    , "dialogBox": null
    , "defWidth": 400
    , "width": 0
    /**
    * 显示窗口
    * @param title 窗口标题
    * @param url   窗口内容页面的URL
    * @param loadCss  是否加载CSS
    * @param loadJs  是否加载JS
    * @param callbac  是否加载JS
    **/
    , "show": function(title, url, loadCss, loadJs) {
        admin_select_yc(0);
        if (this.dialogBox == null) {
            //窗口不存在，自动创建
            this.create();
        }

        this.setWidth(this.defWidth);   //默认宽度



        this.dialogBox.style.display = "block";    //显示窗口

        this.setTitle(title); //设置标题

        this.loading = setTimeout("Dialog.setLoading()", 200);  //200毫秒内数据没有加载就会显示“数据加载中...”

        Drag.init(document.getElementById("draghead"), this.dialogBox);

        this.dialogBox.onDragEnd = function(x, y) {
            Dialog.dialogBox.ox = x - Dialog.getRange().left;
            Dialog.dialogBox.oy = y - Dialog.getRange().top;
        }
        this.center();   //当浏览器不是在第一屏的位置显示窗口会看不见，所以要让它自动在当前屏幕的中间显示
        this.loadContent(url, loadCss, loadJs);  //加载窗口内容
    }
    , "setWidth": function(width) {
        if (width != this.width) {
            this.width = width;
            this.dialogBox.style.width = width + "px";
            this.center();

        }

    }
    , "loading": null
    , "setLoading": function() {
        this.setContent(" &nbsp;&nbsp;&nbsp;&nbsp;数据加载中...");
    }
    , "clearLoading": function() {
        if (this.loading != null) {
            clearTimeout(this.loading);
            this.loading = null;
        }
    }

    /**
    *  加载窗口内容
    */
    , "loadContent": function(url, loadCss, loadJs) {
        url2 = url;
        if (url.indexOf("?") > -1) url += "&" + (new Date().getTime());
        else url += "?" + (new Date().getTime());
        var xhttp = makeRequest();
        xhttp.onreadystatechange = function() {
            if (xhttp.readyState == 4 && (xhttp.status == 200 || window.location.href.indexOf("http") == -1)) {
                Dialog.clearLoading();
                getElement("dialogBox_content").innerHTML = xhttp.responseText;
                else_czlx = 1;
                if (loadCss) {
                    Dialog.loadCss(url2 + ".css");
                }
                if (loadJs) {
                    Dialog.loadJs(url2 + ".js");
                }
            }
        }
        xhttp.open("GET", url, true);
        xhttp.send(null);
        //使用GET方式会有缓存问题，所以要使用POST
    }
    /**
    * 设置窗口内容（提示信息）.
    */
    , "setContent": function(content) {
        document.getElementById("dialogBox_content").innerHTML = "<div class='dialogBox_Content'>" + content + "</div>";
    }

    /**
    * 关闭窗口
    **/
    , "close": function() {
        if (this.dialogBox != null) {
            this.dialogBox.style.display = "none";
        }
        admin_select_yc(1);
    }
    /**
    * 设置窗口的标题
    * @html  窗口的标题，支持HTML
    */
    , "setTitle": function(html) {
        document.getElementById("dialogBox_title").innerHTML = html;
    }


    /**
    * 创建窗口
    */
    , "create": function() {
        this.loadCss("/css/dialog.css");
        var dialogBox = document.createElement("DIV");
        dialogBox.id = "dialogBox";
        dialogBox.className = "dialogBoxBg";
        var html = '<div class="dialogBox" style="cursor:pointer"><div id="draghead" class="title"><h2 id="dialogBox_title"></h2><span><a href="javascript:Dialog.close()" target="_self"><img src="http://cimg2.163.com/dl/bbs/login01_div.gif" style="cursor:pointer" height="18" width="49" border="0" alt="关闭窗口" /></a></span></div><div class="content" id="dialogBox_content"></div></div>';
        dialogBox.innerHTML = html;

        document.body.appendChild(dialogBox);

        this.dialogBox = document.getElementById("dialogBox");


        this.center();  //设置窗口位置

        window.onscroll = Dialog.onBodyScroll;
    }

    /**
    * 加载CSS
    */
    , "loadCss": function(filename) {
        var head = document.getElementsByTagName('HEAD').item(0);
        var style = document.createElement('link');
        style.href = filename;
        style.rel = 'stylesheet';
        style.type = 'text/css';
        head.appendChild(style);
    }
    /**
    * 加载JS
    * @param filename 脚本URL
    */
    , "loadJs": function(filename) {
        var script = document.getElementById("js1");   //应该将filename MD5编码后作为ID使用
        if (script == null) {
            script = document.createElement("script");
        }
        script.id = "js1";
        script.src = filename;

        //为什么要使用insertAdjacentElement？,因为appendChild在JS文件已经存在浏览器缓存时就会出现IE崩溃的情况(Bbs.editArticle方法就会出现,postArticle则正常)
        if (document.frames) {
            document.body.insertAdjacentElement("BeforeBegin", script);
        }
        else {
            document.body.appendChild(script);  //
        }
    }
    , "onBodyScroll": function() {
        var range = Dialog.getRange();
        var top = (range.top + Dialog.dialogBox.oy);
        var left = (range.left + Dialog.dialogBox.ox);
        if (top < 5) { top = 5; }
        if (left < 5) { left = 5; }

        Dialog.dialogBox.style.top = top + "px";
        Dialog.dialogBox.style.left = left + "px";

        //document.title = "scroll:"+top;
    }

    /**
    *  设置窗口居中显示
    */
    , "center": function() {
        var range = Dialog.getRange();

        var left = (range.width - this.width) / 2;
        var top = (range.height - 400) / 2;

        this.dialogBox.ox = left;
        this.dialogBox.oy = top;

        if (range.left > left) { left = range.left + left; }
        if (range.top > top) { top = range.top + top; }

        this.dialogBox.style.left = left + "px";
        this.dialogBox.style.top = top + "px";

        //document.title = "center:"+top;
    }
    /**
    * 移动窗口位置
    * @left 
    * @top 
    */
    , "move": function(left, top) {
        this.dialogBox.ox = left;
        this.dialogBox.oy = top;

        this.dialogBox.style.left = left + "px";
        this.dialogBox.style.top = top + "px";
    }


    , "getRange": function() {
        var top = document.documentElement.scrollTop;
        var left = document.documentElement.scrollLeft;
        var height = document.documentElement.clientHeight;
        var width = document.documentElement.clientWidth;

        if (top == 0 && left == 0 && height == 0 && width == 0) {
            top = document.body.scrollTop;
            left = document.body.scrollLeft;
            height = document.body.clientHeight;
            width = document.body.clientWidth;
        }
        return { top: top, left: left, height: height, width: width };
    }

}
var Drag = {
    "obj": null,
    "init": function(a, aRoot) {
        a.onmousedown = Drag.start;
        a.root = aRoot;
        if (isNaN(parseInt(a.root.style.left))) a.root.style.left = "0px";
        if (isNaN(parseInt(a.root.style.top))) a.root.style.top = "0px";
        a.root.onDragStart = new Function();
        a.root.onDragEnd = new Function();
        a.root.onDrag = new Function();
    },
    "start": function(a) {
        var b = Drag.obj = this;
        a = Drag.fixE(a);
        var c = parseInt(b.root.style.top);
        var d = parseInt(b.root.style.left);
        b.root.onDragStart(d, c, a.clientX, a.clientY);
        b.lastMouseX = a.clientX;
        b.lastMouseY = a.clientY;
        document.onmousemove = Drag.drag;
        document.onmouseup = Drag.end;
        return false;
    },
    "drag": function(a) {
        a = Drag.fixE(a);
        var b = Drag.obj;
        var c = a.clientY;
        var d = a.clientX;
        var e = parseInt(b.root.style.top);
        var f = parseInt(b.root.style.left);
        var h, g;
        h = f + d - b.lastMouseX;
        g = e + c - b.lastMouseY;
        b.root.style.left = h + "px";
        b.root.style.top = g + "px";
        b.lastMouseX = d;
        b.lastMouseY = c;
        b.root.onDrag(h, g, a.clientX, a.clientY);
        return false;
    },
    "end": function() {
        document.onmousemove = null;
        document.onmouseup = null;
        Drag.obj.root.onDragEnd(parseInt(Drag.obj.root.style.left), parseInt(Drag.obj.root.style.top));
        Drag.obj = null;
    },
    "fixE": function(a) {
        if (typeof a == "undefined") a = window.event;
        if (typeof a.layerX == "undefined") a.layerX = a.offsetX;
        if (typeof a.layerY == "undefined") a.layerY = a.offsetY;
        return a;
    }
};

Object.printAll = function(obj) {
    var msg = "";
    var i = 0;
    for (dd in obj) {
        msg += dd + "  ";
        if ((i + 1) % 5 == 0) {
            msg += "\n";
        }
        i++;
    }
    alert(msg);
}