﻿var jq = {
    $: function(id)
    {
        var obj = document.getElementById(id); //先取得对象,不用直接操作页面的html元素
        if (!obj)
            return false;

        //对象的事件代理关联函数.
        obj.observe = function(evname, bserver) { if (obj.attachEvent) this.attachEvent("on" + evname, bserver); else this.addEventListener(evname, bserver, false); }
        obj.dobserve = function(evname, bserver) { if (obj.attachEvent) this.detachEvent("on" + evname, bserver); else this.removeEventListener(evname, bserver, false); }

        //对象显示和隐藏,不用删除掉
        obj.isShow = function() { return this.style.display == "block"; }
        obj.isHide = function() { return this.style.display == "none"; }
        obj.show = function() { this.style.display = "block"; }
        obj.hide = function() { this.style.display = "none"; }

        //设置对象属性样式
        obj.setAttribs = function(attribs) { for (var x in attribs) obj[x] = attribs[x]; }
        obj.setStyle = function(styles) { for (var x in styles) { try { if (jq.isIE && x == 'opacity') obj.style.filter = (styles[x] == 1) ? '' : 'alpha(opacity=' + (styles[x] * 100) + ')'; else obj.style[x] = styles[x]; } catch (e) { } } }

        //设置值,不用删除掉
        obj.val = function() { return this.value; }
        obj.val = function(v) { this.value = v; }

        //获取第一层子元素的集合,不用删除掉
        obj.getChilds = function() { var arr = new Array(), cs = obj.childNodes; for (var i = 0; i < cs.length; i++) { if (cs[i].nodeType == 1) arr[arr.length] = cs[i]; } return arr; }

        return obj;
    },

    ///////////////////////////////////////////////////////////////////////////辅助函数,不用删除掉
    isIE: (document.all && !window.opera),
    pageBody: (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body,

    //创建元素并附加
    createElement: function(tag, attribs, styles, parent)
    {
        var el = document.createElement(tag);

        //设置属性
        if (attribs) { for (var x in attribs) el[x] = attribs[x]; }
        //设置样式
        if (styles) { for (var x in styles) { try { if (jq.isIE && x == 'opacity') el.style.filter = (styles[x] == 1) ? '' : 'alpha(opacity=' + (styles[x] * 100) + ')'; else el.style[x] = styles[x]; } catch (e) { } } }
        if (parent) parent.appendChild(el);
        return el;
    },

    //双向的,可以适用于带有id的,也可以纯输出,不用删除掉
    //url, w, h, vars, objid, bg, win
    flash: function(paras)
    {
        var flashStr = '\<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0"\
                            width="' + paras.w + '" height="' + paras.h + '" id="FLVPlayer">\
                            <param name="movie" value="' + paras.url + '" />\
                            <param name="wmode" value="Transparent" />\
                            <param name="salign" value="lt" />\
                            <param name="quality" value="high" />\
                            <param name="scale" value="noscale" />\
                            <embed src="' + paras.url + '" quality="high" wmode="Transparent" scale="noscale" menu="false" width="' + paras.w + '" height="' + paras.h + '" name="FLVPlayer" salign="LT" align="middle" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />\
                        </object>';

        if (paras.flv)
            var flashStr = '\
                    <object type="application/x-shockwave-flash" data="/new_images/player.swf" width="' + paras.w + '" height="' + paras.h + '" id="vcastr3">\
			        <param name="movie" value="/new_images/player.swf"/>\
			        <param name="wmode" value="Transparent" />\
			        <param name="allowFullScreen" value="true" />\
			        <param name="FlashVars" value="xml=\
				        <vcastr>\
					        <channel>\
						        <item>\
							        <source>' + paras.flv + '</source>\
							        <duration></duration>\
							        <title></title>\
						        </item>\
					        </channel>\
					        <config>\
					        </config>\
					        <plugIns>\
					        </plugIns>\
				        </vcastr>"/>\
		            </object>';

        if (paras.objID)
            jq.$(paras.objID).innerHTML = flashStr;
        else
            document.write(flashStr);
    },

    //获取绝对坐标
    getAbsPoint: function(el)
    {
        var l = el.offsetLeft, t = el.offsetTop;
        while (el = el.offsetParent) l += el.offsetLeft, t += el.offsetTop;
        return { left: l, top: t };
    },

    //定位
    position: function(el)
    {
        var p = { x: el.offsetLeft, y: el.offsetTop };
        while (el.offsetParent)
        {
            el = el.offsetParent;
            p.x += el.offsetLeft;
            p.y += el.offsetTop;
            if (el != document.body && el != document.documentElement)
            {
                p.x -= el.scrollLeft;
                p.y -= el.scrollTop;
            }
        }
        return p;
    },

    getPageAbsSize: function()
    {
        var documentHeight = Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
        var documentWidth = Math.max(document.body.scrollWidth, document.documentElement.scrollWidth);

        return { width: documentWidth, height: documentHeight }
    },

    //页面尺寸
    getPageSize: function()
    {
        var width = jq.isIE ? jq.pageBody.clientWidth : (document.documentElement.clientWidth || self.innerWidth),
		height = jq.isIE ? jq.pageBody.clientHeight : self.innerHeight;

        return {
            width: width,
            height: height,
            scrollLeft: jq.isIE ? jq.pageBody.scrollLeft : pageXOffset,
            scrollTop: jq.isIE ? jq.pageBody.scrollTop : pageYOffset
        }
    },

    //发送Json请求
    sendJson: function(url, onend, onbefore)
    {
        var xmlHttp;
        if (window.ActiveXObject)
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        else if (window.XMLHttpRequest)
            xmlHttp = new XMLHttpRequest();

        xmlHttp.open("GET", url, true);
        xmlHttp.onreadystatechange = function() { jq.getJson(xmlHttp, onend); }
        xmlHttp.send(null);

        if (onbefore != null)
            onbefore(); //处理预发消息
    },

    //getJSON函数解析JSON字符串
    getJson: function(xmlHttp, onend)
    {
        if (xmlHttp.readyState == 4)
        {
            if (xmlHttp.status == 200)
            {                
                var json = eval("(" + xmlHttp.responseText + ")");
                onend(json);
            }
        }

        xmlHttp = null;
    }
};

///////////////////////////////////////////////////扩展数组的foreach
Object.prototype.foreach = function(func)
{
    for (i in this)
        if (i != "foreach")
        func.call(null, this[i]);
}

var Class = { create: function() { return function() { this.initialize.apply(this, arguments); } } }
var Extend = function(destination, source) { for (var property in source) { destination[property] = source[property]; } }
var Bind = function(object, fun) { return function() { return fun.apply(object, arguments); } }
var Each = function(list, fun) { for (var i = 0; i < list.length; i++) { fun(list[i], i); } };