﻿var keyEnum={"enter":"13","ctrl":"17","space":"32","backspace":"8","shift":"16","esc":"27"}
String.prototype.contains = function(str) { return (this.indexOf(str) > -1);};
String.prototype.trim = function (s) {if(s) return this.trimEnd(s).trimStart(s);else return this.replace( /(^[ \t\n\r]*)|([ \t\n\r]*$)/g, '' ) ;};
String.prototype.trimEnd=function(s){ if(this.endsWith(s)) { return this.substring(0,this.length-s.length);} return this;};
String.prototype.trimStart=function(s){ if(this.startsWith(s)){ return this.slice(s.length);}return this;};
String.prototype.startsWith = function(str) {return (this.indexOf(str) == 0);};
String.prototype.endsWith = function(str) { return (str.length <= this.length && this.substr(this.length - str.length, str.length) == str); };
String.format=function(){var str=arguments[0];for(var i=1;i<arguments.length;i++){var reg=new RegExp("\\{"+(i-1)+"\\}","ig"); str=str.replace(reg,arguments[i]);}return str; };
String.isNullOrEmpty = function(str){ return str;};

var max = {   
    browser : {
        isIE     : navigator.userAgent.toLowerCase().contains('msie'),
        isIE5    : navigator.userAgent.toLowerCase().contains('msie 5'),
        isIE6    : navigator.userAgent.toLowerCase().contains('msie 6'),
        isIE7    : navigator.userAgent.toLowerCase().contains('msie 7'),
        isGecko  : navigator.userAgent.toLowerCase().contains('gecko'),
        isSafari : navigator.userAgent.toLowerCase().contains('safari'),
        isOpera  : navigator.userAgent.toLowerCase().contains('opera')
    },
    global : {
        getClientWidth  : function() { return ((document.documentElement && document.documentElement.clientWidth) || document.body.clientWidth); },
		getClientHeight : function() { return ((document.documentElement && document.documentElement.clientHeight) || document.body.clientHeight); },
		getScrollTop    : function() { return ((document.documentElement && document.documentElement.scrollTop) || document.body.scrollTop); },
		getScrollLeft   : function(){ return ((document.documentElement && document.documentElement.scrollLeft) || document.body.scrollLeft); },
		getFullHeight   : function(){ if(document.documentElement.clientHeight>document.documentElement.scrollHeight) return document.documentElement.clientHeight;else return document.documentElement.scrollHeight;},
		getFullWidth    : function(){ return document.documentElement.scrollWidth;},
		getBrowserRect  : function(){ var r=new Object(); r.left = this.getScrollLeft();  r.top = this.getScrollTop(); r.width = this.getClientWidth(); r.height = this.getClientHeight(); r.bottom = r.top + r.height; r.right = r.left + r.width; return r; }      
		},
    coor : {
       left    : function(e,left){if(typeof(left)=="number"){e.style.position="absolute";e.style.left=left+"px";}else{var offset=e.offsetLeft; if(e.offsetParent!=null) offset+=Left(e.offsetParent); return offset; }},
       top     : function(e,top){if(typeof(top)=="number"){e.style.position="absolute";e.style.top=top+"px";}else{var offset=e.offsetTop; if(e.offsetParent!=null) offset += Top(e.offsetParent); return offset;}},
       width   : function(e,w){ if(typeof(w)=="number"){e.style.width= w+"px";}else{return e.offsetWidth;}},
       height  : function(e,h){   if(typeof(h)=="number"){e.style.height= h+"px";}else{return e.offsetHeight;}},
       getRect : function(e){var r=new Object();r.left  = getLeft(e); r.top   = getTop(e); r.width = getWidth(e); r.height= getHeight(e);r.bottom=r.top+r.height;r.right =r.left+r.width;return r;}
    }
};

//根据name得到一组对象
function $N(name) { return (typeof name == 'string' ? document.getElementsByName(name) : null);}

//根据tagname得到一组对象
function $T(name) { return (typeof name == 'string' ? document.getElementsByTagName(name) : null);}

//设置对象的样式
//例如 : setStyle($('id'),{ display:'none',position : 'absolute',zIndex : 999 });
function setStyle(e,s){ for(var k in s){e.style[k]=s[k];}}

//新建一个HTML节点
//tagName : 
//parent  : 父节点
function addElement(t,p){ var e= document.createElement(t); if(typeof(p)!='undefined') p.appendChild(e);else document.body.appendChild(e); return e;}


//停止事件冒泡
endEvent = function(ev) {
	if (max.browser.isIE) {
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	} 
	else {
		ev.preventDefault();
		ev.stopPropagation();
	}
	return false;
};

//对象事件注册
//elm   :节点
//n     :事件名称比如click,dbclick,change,可以在前面带on也可以不带
//h     :事件处理函数
function addHandler(elm,n,h)
{
    if(n.indexOf('on',0)>=0)
    {
        n=n.substring(2,n.length);
    }
    if(!max.browser.isIE)
    {
        elm.addEventListener(n,h,false);
    }
    else
    {
        elm.attachEvent("on"+n,h);
    }
};
function hideElement(e,callback)
{
    var ivtO=5,ivt=50;
    var t=new timer(ivt, function(n){
    if(n<=ivtO)
    {
        opacity(e,100-n*(100/ivtO));
    }
    else
    {
        if(callback)callback();
        t.stop();
        t=null;
    }});
    t.start();
}

//刷新页面
function refresh(){var href = location.href;if(href.indexOf('#',0)>-1){    href=href.substring(0,href.indexOf('#',0))}location.replace( href );}

function hide(e,callback){
    hideElement(e,function(){
    var h=max.coor.height(e);
    if(e.style.borderTopWidth!=""&&!isNaN(e.style.borderTopWidth))
        h-=parseInt(e.style.borderTopWidth);
    if(e.style.borderBottomWidth!=""&&!isNaN(e.style.borderBottomWidth))
        h-=parseInt( e.style.borderBottomWidth);
    if(e.style.paddingTop!=""&&!isNaN(e.style.paddingTop))
        h-=parseInt( e.style.paddingTop);
    if(e.style.paddingBottom!=""&&!isNaN(e.style.paddingBottom))
        h-=parseInt(e.style.paddingBottom);
    _buf.push({id:e.id,ov:e.style.overflow,height:h,ds:e.style.display});
    setStyle(e,{overflow:'hidden'});
    var nh;
    var t=new timer(10,function(n){
    if(n<=10){
    nh=((10-n)/10*h)+"px";
    setStyle(e,{height:nh})}
    else{t.stop();t=null;setStyle(e,{display:"none"}); if(callback)callback();}
    });
    t.start();
    })
}

var _buf=new Array();
function show(e)
{
    var b=null,i;
    for(var k in _buf)
    {
       if( _buf[k].id==e.id)
       {
            i=k;
            b=_buf[k];
            break;
       }
    }
    if(b==null){setStyle(e,{display:''}); return}
    setStyle(e,{display:b.ds});
      //opacity(e,100);
    var t=new timer(10,function(n){
    if(n<=10) {
        setStyle(e,{height:(b.height*(n/10))+"px"});
        }
    else if(n<=20)
        opacity(e,(n-10)*10);
    else{
            t.stop();t=null;
            setStyle(e,{overflow:b.ov,height:b.height+"px"});
            _buf.splice(i,1);
        }
    });
    t.start();
}

//设置对象的透明度
function opacity( e,o){ setStyle(e,{opacity :o / 100  ,MozOpacity : (o / 100) ,KhtmlOpacity : (o / 100) , filter : "alpha(opacity=" + o + ")"} );};

///移除事件注册
//e :节点
//n :事件名称比如click,change
//h :事件处理函数
function removeHandler(e,n,h)
{
    if(n.indexOf('on',0)>=0)
    {
        n=n.substring(2,n.length);
    }
    if(!max.browser.isIE)
    {  
        e.removeEventListener(n,h,false);
    }
    else 
    { 
        e.detachEvent("on"+n,h);
    }
}

///判断一个变量或者函数是否已经定义
//variable : 
function isUndefined(v) {return typeof(v) == 'undefined' ? true : false;}

//获取元素的纵坐标
//顶部距离文档顶部的高度 
function getTop(e){ var offset=e.offsetTop;  if(e.offsetParent!=null) offset+=getTop(e.offsetParent);  return offset; } 

//获取元素的横坐标 
//左边距
function getLeft(e){ var offset=e.offsetLeft;  if(e.offsetParent!=null) offset+=getLeft(e.offsetParent);  return offset; }

//获取一个对象的宽度
function getWidth(obj) { if (isUndefined(obj)) return 0; else return obj.offsetWidth; }
//获取一个对象的高度
function getHeight(obj) { if (isUndefined(obj)) return 0; else return obj.offsetHeight; }

//时钟
var timer=function( i,ontick ){
    this.counter=0;
    this.interval=i;
    this.ontick=ontick;
}
timer.prototype.start = function(){ var _this=this; if(this.interval>0){ if(this.ontick!=null)this.ontick(++this.counter); window.setTimeout(function(){_this.start()},_this.interval);}}
timer.prototype.stop  = function(){ this.interval=0; }

//等比缩小图片
//e：img节点
//w：宽度限制
//h：高度限制
function imageScale(e, w,h){var width =e.width; var height=e.height; var scale= width / height;if(width>w){e.width=w;e.height= w / scale;}if(height>h){ e.height=h; e.width= h * scale; }}
function scrollToBottom(id){    var l=$(id);    l.scrollTop = 65535;}

function ctrlEnterEvent(callback, controlid1, controlid2) {
    
    for(var i = 1; i < arguments.length; i++){
        var c = document.getElementById(arguments[i]);
        if (c) c.onkeydown = function(e){
			e=e||window.event;
            if(e.ctrlKey&&e.keyCode==13) { 
                callback && callback();
            }
        };
    }
}/******************************* 弹出菜单容器 *********************************/
var popupCollection=function(){var c=popupCollection;if(!c.innerList)c.innerList=new Array();}
popupCollection.prototype.add=function(obj){obj.onShow=this.onshow;popupCollection.innerList.push(obj);} 
popupCollection.prototype.onshow=function(obj){var cc=popupCollection;for(var i=0;i<cc.innerList.length;i++){if(cc.innerList[i]!=obj){cc.innerList[i].hide();}}}
popupCollection.instance = function(){if(!window.dropCollection)window.dropCollection=new popupCollection();return window.dropCollection;}

//**弹出菜单基类
var popupBase =function(listid,trigger,autopopup){this.list=$(listid);this.auto = autopopup;if(trigger instanceof Object)this.trigger = trigger; else this.trigger=$(trigger);}
popupBase.prototype.show=null;
popupBase.prototype.hide=null;
popupBase.prototype.onShow=null;
popupBase.prototype.onHide=null;

/******************************* 下拉菜单 *********************************/
//下拉菜单
//listid:下拉菜单的那个容器ID
//triggerid:触发下拉菜单弹出的那个对象的ID
//autoPop:如果是true那么只要鼠标滑过就弹出， 否则需要点击才弹出
//position:位置目前只支持'auto'和'center'
var popup=function( listid, trigger,autoPop,onSelectClass,pos)
{
    this.focus=false;      
    popupBase.call(this,listid,trigger,autoPop);
    
    this.triggerStyle=onSelectClass;
    if(pos)
        this.position = pos;
    else
        this.position='auto';
        
    var _this=this;
    var s=this.list.style;
    s.position='absolute';
    s.zIndex=50;

    addHandler(this.trigger,this.auto?'mouseover':'click',function(e){ _this.show();});
    if(this.auto)
    {
        addHandler(this.list,'mouseover',function(e){_this.focus=true;});
        addHandler(this.trigger,"mouseout",function(){ hideList(_this)});
        addHandler(this.list,"mouseout",function(){ hideList(_this)});
    }
    else
    {
        addHandler(this.trigger,"mouseout",function(){ _this.focus=false;});
        addHandler(this.list,"mouseout",function(){ _this.focus=false;});
        addHandler(this.trigger,"mouseover",function(){_this.focus=true;});
        addHandler(this.list,"mouseover",function(){ _this.focus=true;});
        addHandler(document.documentElement,"click",function(){if(_this.focus==false)_this.hide();})
    }
    popupCollection.instance().add(this);
}
function hideList(e)
{
    window.setTimeout(function(){if(e.focus==false)e.hide(); },1000);
    e.focus=false;
}

popup.prototype.show=function()
{
    var s=this.list.style;
     s.display='block';
    var t,l;
     rt = max.coor.getRect(this.trigger)
     rl = max.coor.getRect(this.list);
     rw = max.global.getBrowserRect();
   
    
    if( rt.bottom+rl.height>rw.bottom)
        t=rt.top-rl.height;
    else
        t=rt.bottom;
    if(this.position=='auto')
    {
        if(rt.left+ rl.width>rw.right)
            l=rt.right-rl.width;
        else
            l=rt.left;    
    }
    else
    {
            l=(rt.left + rt.width / 2)-rl.width/2 ;
    }
    
    this.focus=true;
    s.left = l+"px";
    s.top  = t+"px";

    var cn=this.triggerStyle;
    if(cn)
        if(this.trigger.className.indexOf(cn)<0)
            this.trigger.className+=" "+cn;

    if(this.onShow)this.onShow.call(popupCollection.instance(),this);
}

popup.prototype.hide=function()
{
    var s=this.list.style;
    s.display='none';
    var cn=this.triggerStyle;
    if(cn)
        if(this.trigger.className.indexOf(cn)>=0)
            this.trigger.className=this.trigger.className.replace(cn,'').trim();
    
    if(this.onHide)this.onHide.call(popupCollection.instance(),this);
}
