﻿//---------------   跨域代理:ajax,cookie   ----------//
document.domain = 'expo.cn';

Expo.apply(Expo, {
    dataUrl: "http://cgi.expo.cn/cgi-bin/",
    proxyUrl: 'http://cgi.expo.cn/proxy/proxy.htm'
});

Expo.apply(Expo, new Expo.util.Observable);

//用代理页的Ajax
function doExpoProxy(){
    var im = document.createElement('iframe');
    im.id = im.name = 'expo_proxy';
    im.src = Expo.proxyUrl;
    im.style.display = 'none';
    document.body.appendChild(im);
}
//确保代理页初始化完毕!
var __proxyLoaded = false;
function proxyLoaded(ajax, cookie){    
    __proxyLoaded = true;
    Expo.util.Ajax = ajax;
    //Expo.util.Cookie = cookie;
    
    setTimeout(function(){
        Expo.fireEvent('load');
    }, 200);
}
Expo.util.Dom.on(window, 'load', function(e){              
    if(!__proxyLoaded) doExpoProxy();
});
setTimeout(function(){
    Expo.util.Dom.fireEvent(window, 'domload');
}, 3000);
//---------------   功能和组件扩展  ----------------//
function thisMovie(movieName) {
	 if (navigator.appName.indexOf("Microsoft") != -1) {return window[movieName]; } else { return document[movieName]; }
}
Expo.getParams = function(s){
    s = s || location.search;
    var o = {};
    if(s){
        var ss = s;
        if(s.indexOf('#') == 0 || s.indexOf('?') == 0){
            ss = s.substring(1)
        }        
        ss = ss.split('&');
        for(var i=0; i<ss.length; i++){
            var pv = ss[i].split('=');
            o[pv[0]] = pv[1];
        }
    }    
    return o;
}
Expo.setParams = function(ps){    
    ps = ps || {};
    var sb = [];
    for(var p in ps){
        sb[sb.length] = p+'='+ps[p];
    }
    return sb.join('&');    
}

//10位转到2位 补齐16位
function HtoT16(num){
	var twnum=Number(num).toString(2);
	if(twnum.length<16){
	var twnuml = twnum.length;
		for(var i=0;i<(16-twnuml);i++){
			twnum="0"+twnum;
		}
	}
    return twnum;
}
function checkTime(time){
	var ptime = new Date(parseInt(time)*1000);
	var mtime = new Date();
	var d = (Date.parse(mtime)-Date.parse(ptime))/1000;
	if(d<0){
		return "刚刚";
	}else if(d<60){
		return Math.floor(d/60)+" 分钟前";
	}else if(d<3600){
		return Math.floor(d/60)+" 分钟前";
	}else if(d<86400){
		return Math.floor(d/3600)+"小时前";
	}else{
		//return ptime.toLocaleDateString();
		return ptime.getFullYear()+"年"+(ptime.getMonth()+1)+"月"+ptime.getDate()+"日";
	}
}
showTitle = function (title){

    title = HTMLDecode(title);
	var mtitle = title.substr(0,23);
	return mtitle.replace(/</g,"").replace(/>/g,"")
}
function isEmail(s){
	var pattern = /^([a-zA-Z0-9._-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/; 
	return pattern.test(s); 

}
/*
    数据服务
        实现业务逻辑级别的数据交互控制
        如
            errField: 'err'
            ajax封装
*/
Expo.DataServer = function(option){
	
	Edo.apply(this, option);
	
	this.ajax;
	this.data;
    this.addEvents(
         'load',
         'fail',
         'loading'
    );
    this.onSuccess;
    this.onFail;
    Expo.DataServer.superclass.constructor.call(this);
};
Expo.extend(Expo.DataServer, Expo.util.Observable, {
    errField: 'err',
    //包装一个ajax组件:todo 这个也可以抽象一下
    build:function(url,params,onSuccessfn,onFailfn){
        this.fireEvent('loading', this); 
        this.onSuccess = onSuccessfn;
        this.onFail= onFailfn;
        var o = {
            type: this.method || 'post',
            url: url,
            params: params,
            onSuccess: this._onSuccess.bind(this),
            onFail: this._onFail.bind(this)
        }        
        this.ajax = Expo.util.Ajax.request(o);        
    },
    _onFail: function(err){
         if(typeof(err)=='undefined') err ='-10000'         
         if(typeof(this.onFail)!='undefined') this.onFail(err); 
         this.fireEvent('fail', err);
         //throw new Error("自定义：网络连接失败。"); 
    },
    _onSuccess:function(text){    
        try{
            this.data = Expo.util.XmlToJson(text);
        }catch(e){
            throw new Error("xml格式错误:"+e);
        }
        if(Number(this.data[this.errField])!==0){
            this._onFail(this.data[this.errField]);
            return;
        }
        if(typeof(this.onSuccess)!='undefined') this.onSuccess(this.data); 
        this.fireEvent('load', this.data);
    }
});


/*
模板服务
*/
Expo.DataView = function(o){
    Expo.apply(this, o);
    this.dataserver;
    this.view;
    this.addEvents(
         'load',
         'fail',
         'loading',
         'render'
    );
}
Expo.extend(Expo.DataView, Expo.util.Observable, {
    //装配事件和模板 数据地址 模板字符 渲染对象
    build:function(url,pars,atplstr,arender){       
        //定义ajax
        this.dataserver  = new Expo.DataServer();
        this.dataserver.on('load',function(){
            this.fireEvent('load', this);
        },this);
        this.dataserver.on('loading',function(){
            this.fireEvent('loading', this);
        },this);
        this.dataserver.on('fail',function(e,err){
            this.fireEvent('fail',this,err);
        },this);
        this.dataserver.build(url,pars,this.__onSuccess.bind(this));
    },
    //dataserver成功
    __onSuccess:function(){
        var data = this.dataserver.data;
        this.refresh(data);        
    },
    getData:function(){
        return this.dataserver.data;
    },
    refresh: function(data){
        if(this.tpl && this.render){
            if(typeof this.tpl === 'string') this.tpl = new Expo.util.Template(this.tpl);
            var render = this.render;
            this.render = $(render);            
            this.data = data;
            if(this.data){
                if(this.fireEvent('beforerender', this) !== false){
                    var html = this.tpl.run(this.data);
                    
                    this.render.innerHTML = html;
                    this.fireEvent('render', this)
                }
            }
        }
    }
})

Expo.Pager = function(o){
    Expo.apply(this, o);
    this.addEvents(
        'change'
    );
    Expo.Pager.superclass.constructor.call(this);
    this.render = $(this.render);
    if(!this.render) throw new Error("必须为分页组件指定render");
    Expo.util.Dom.on(this.render, 'click', function(e){
        var t = e.target;
        var nav = t.className && t.className.toLowerCase();
        var fn = this[nav];
        if(fn) fn.call(this);        
    }, this);
}
Expo.extend(Expo.Pager, Expo.DataView, {
    tpl: '<div class="pager">\
    <%if(this.index < this.allPage-1){%>\
    <a class="last" href="javascript:;"><%=LOCAL.last%></a>\
    <a class="next" href="javascript:;"><%=LOCAL.next%></a>\
    <%}%>\
    <%if(this.index!=0){%>\
    <a class="pre" href="javascript:;"><%=LOCAL.pre%></a>\
    <a class="first" href="javascript:;"><%=LOCAL.first%></a>\
    <%}%>\
    <div style="padding-bottom:2px; line-height:22px;margin-right:10px;">每页<span style="color:#ff9129;"><%=this.size%></span>条,共<span style="color:#ff9129;"><%=this.total%></span>条,当前第 <input style="width:30px;border:solid 1px #bec599;height:16px;margin-left:2px;margin-right:2px;color:#ff9129; text-align:center;"value="<%=this.index+1%>" id="<%=this.sizeid%>"/>/<span style="color:#ff9129;"><%=this.allPage%></span> 页</div>   \
</div>',
    total: 0,    
    start: 0,
    size: 10,
    index: 0,
    allPage: 0,
    compute: function(){            
        this.total = this.total || 0;
        this.allPage = parseInt(this.total / this.size);
        if(this.total % this.size !== 0) this.allPage += 1;        
        
        if(this.start >= this.total) this.start = (this.allPage - 1) * this.size;
        if(this.start <= 0) this.start = 0;
        
        this.index = parseInt(this.start / this.size);
        if(this.allPage == 0 && this.total != 0) this.allPage = 1;
        
        if(this.allPage == 0 ) this.allPage = 1;
        
    },
    changeIndex: function(index){
        index = parseInt(index);
        if(isNaN(index)) {
            alert('请输入正确的页码数字');            
        }else{
            this.change(index * this.size, this.size);
        }
    },
    changeSize: function(size){
        
        this.fireEvent('sizechange', this);
    },
    first: function(){
        this.change(0, this.size);
    },
    pre: function(){
        this.change(this.start - this.size, this.size);
    },
    next: function(){
        this.change(this.start + this.size, this.size);
    },
    last: function(){
        this.change((this.allPage-1)*this.size, this.size);
    },
    change: function(start, size){
        this.start = !start && start !== 0 ? this.start : parseInt(start);
        this.size = parseInt(size) || this.size;
        this.index = parseInt(this.start / this.size);
        this.fireEvent('change', this);
    },
    refresh: function(){    //刷新
        this.compute();
        this.sizeid = Expo.id(null, 'pagersize');
        Expo.Pager.superclass.refresh.call(this, this);
        
        this.doSizeChange();  
    },
    doSizeChange: function(){
        var sf = this;
        
        $(this.sizeid).onblur = function(){
            sf.changeIndex(parseInt(this.value) - 1);
        }
    }
});

Expo.Pager2 = function(o){
    
    Expo.Pager2.superclass.constructor.call(this, o);
    
    Expo.util.Dom.on(this.render, 'click', function(e){
        var t = e.target;
        if(Expo.util.Dom.hasClass(t, 'pager-go')){
            var index = parseInt($(this.sizeid).value);
            if(isNaN(index)){
                alert("请输入数字页码");
                $(this.sizeid).value = '';
                return;
            }
            
            if(index > this.allPage || index < 0){
                alert("超出页码,请重新输入");
                $(this.sizeid).value = '';
                return;
            }
            this.changeIndex(index-1);
        }
        if(Expo.util.Dom.hasClass(t, 'pager-goindex')){
            this.changeIndex(parseInt(t.id)-1);
        }
        
        
    }, this);
}
Expo.extend(Expo.Pager2, Expo.Pager, {
    tpl: '<%%>\
    <div class="pager" ><a class="pager-go" href="javascript:;">确定</a><div style="padding-bottom:2px; line-height:22px;margin-right:10px;">到第<input style="width:30px;border:solid 1px #bec599;height:16px;margin-left:2px;margin-right:2px;color:#ff9129; text-align:center;" id="<%=this.sizeid%>"/>页</div><div><%=this.createPagerHtml()%></div><div style="color:#464646;"><%=this.index+1%>/<%=this.allPage%>页</div></div>',
    doSizeChange: function(){},
    viewNum: 2, //前后都有2个
    createPagerHtml: function(index, allPage, viewNum){
        var html = '';
        var allPage = this.allPage - 1;
        
        var pre = -100;
        for(var i=0; i<=allPage; i++){
            if(i == 0 || i == allPage || (i >= this.index-this.viewNum && i <= this.index+this.viewNum)){      
                if(pre >= 0 && pre != i - 1){
                    
                    html += '<a id="'+Math.floor((i-pre)/2+pre+1)+'" class="pager-goindex" style="padding:0;" href="javascript:;">...</a>';                    
                }
                html += '<a id="'+(i+1)+'" href="javascript:;" class="pager-goindex" style="'+(i == this.index ? 'color:black;font-weight:bold;': '')+'">'+(i+1)+'</a>';                
                
                pre = i;
            }
        }
        
        return html;   
    }
    
});


/*错误代码*/
Expo.ERROR = function(num){

    num = num.toString();
   var dep=num;
   switch(num)
   {
    case "-20050":
     dep=LOCAL.tzbcz;//"帖子不存在";
     break
    case "-20051":
     dep=LOCAL.yhbsgly;//"用户不是管理员";
     break
     case "-20052":
     dep=LOCAL.bmwk;//"版面未开";
     break
     case "-20053":
     dep=LOCAL.yhbpb;//"用户被屏蔽";
     break
     case "-20991":
     dep=LOCAL.csbnwk;//"参数不能为空";
     break
     case "-20990":
     dep=LOCAL.cswx;//"参数无效";
     break
     case "-20977":
     dep=LOCAL.dlsb;//"登录失败";
     break
      case "-20976":
     dep=LOCAL.yhmycz;//"用户名已存在";
     break
      case "-20973":
     dep=LOCAL.yhmhmmwk;//"用户名或密码为空";
     break
      case "-20971":
     dep=LOCAL.scsb;//"上传失败";
     break
      case "-20970":
     dep=LOCAL.mydl;//"没有登录";
     break
      case "-20965":
     dep=LOCAL.mmbd;//"密码不对";
     break
     case "-20964":
     dep=LOCAL.ipsx;//"ip受限";
     break
      case "-20963":
     dep=LOCAL.yhmxz;//"用户名在5到15个字符之间";
     break
      case "-20962":
     dep=LOCAL.yhmgsbd;//"用户名格式不对";
     break
      case "-20961":
     dep=LOCAL.wzbtxz;//"文章标题在1到100个字符之间";
     break
      case "-20960":
     dep=LOCAL.nrxz;//"内容在1到10000个字符之间";
     break
     case "-20986":
     dep=LOCAL.yzmcw;//"验证码输入错误，请重新再试";
     break
     case "-23002":
     dep=LOCAL.yhbcz;//"用户不存在";
     break
     case "-23009":
     dep=LOCAL.yhycz;//"用户已存在";
     break
     case "-23013":
     dep=LOCAL.mmbzq;//"密码不正确";
     break
     case "-28002":
     dep=LOCAL.yhmdl;//"用户没登录, 或者重复登录，另一个页面注销了 ";
     break
      case "-28003":
     dep=LOCAL.mmcw;//"密码错误";
     break
      case "-28005":
     dep=LOCAL.cfdl;//"一般重复登陆，会出现这个错误";
     break
      case "-28007":
     dep=LOCAL.zhbf;//"QQ号码ip被封";
     break
      case "-28008":
     dep=LOCAL.dlsb;//"登录失败";
     break
      case "-28010":
     dep=LOCAL.dltj;//"登录太久";
     break
      case "-30003":
     dep=LOCAL.bmbcz;//"版面不存在";
     break
      case "-30004":
     dep=LOCAL.ysgly;//"已是本版管理员";
     break
      case "-30005":
     dep=LOCAL.bmyg;//"版面id已使用过";
	  case "0":
     dep=LOCAL.dltj;//"版面id已使用过";
     break
   default:
     dep;
   }
   return dep;
}

if(typeof EXPO_LANG === 'undefined'){
    EXPO_LANG = 'cn';
}
document.write('<script src="/scripts/local/'+EXPO_LANG+'.js" type="text/javascript"></s'+'cript>');

//更新一些用户登录信息
function doLoginUpdate(){

    var logininfo = $('logininfo');
    if(logininfo){   
        logininfo.style.display = 'block'; 
        var user = Expo.User.getLocalUser();
        if(user){
            var nick = user.nick || user.uin;
            logininfo.innerHTML = '<span style="color:#0158a7;font-weight:bold;font-size:13px;">欢迎您，'+nick+'</span>&nbsp;&nbsp;<a href="/profile/index.html">个人中心</a>&nbsp;&nbsp;<a href="javascript:logoutExpo();">退出</a>&nbsp;&nbsp;<a href="/help.html" target="_blank">帮助</a>&nbsp;&nbsp;<a href="http://en.expo.cn/index.html#lang=t" target="_blank">English</a>&nbsp;';
        }else{
            
            logininfo.innerHTML = '<a href="/login.html" onclick="Expo.util.Cookie.set(\'returnurl\', location.href);">登录</a>&nbsp;&nbsp;<a href="/register.html" onclick="Expo.util.Cookie.set(\'returnurl\', location.href);">注册</a>&nbsp;&nbsp;<a href="/help.html" target="_blank">帮助</a>&nbsp;&nbsp;<a href="http://en.expo.cn/index.html#lang=t" target="_blank">English</a>&nbsp;';
        }
    }
}

Expo.on('load', function(e){
    doLoginUpdate();
});

function logoutExpo(){
    Expo.User.logout(function(){
        alert("您已退出登录网上世博");
        location.reload();
    }, function(){
        alert("您已退出登录网上世博");
        location.reload();
    });
}
//搜索按钮
searchExpo = function(){
    var key = $('search-text').value;
    if(key === '') {
        //alert('请输入搜索关键字');
        return;
    }    
    location.href = 'http://www.expo.cn/search/search.html#key='+key;
}
var searchBtn = $('search-trigger');
if(searchBtn){
    $('search-trigger').href = 'javascript:searchExpo()';
    Expo.util.Dom.on(searchBtn, 'keyup', function(e){    
        if(e.keyCode == 13){
//            searchExpo();
        }
    });
}
function getSWF(movieName)
{
    if (navigator.appName.indexOf("Microsoft") != -1)
    {
        return window[movieName];
    }
    else
    {
        return document[movieName];
    }
}

ERRHANDER = function(errCode){    
    alert(Expo.ERROR(errCode));
}


document.title = '::网上中国2010年上海世博会::';

//当go(-1)时,如果没有继续后退(hash不改变),则自动跳转到论坛分类首页
var deferTimer = null;
function deferGo(){
    var nowHash = location.hash;
    deferTimer = setTimeout(function(){    
        var deferHash = location.hash;
        if(nowHash == deferHash){
            var o = Expo.getParams(nowHash);
            var fid = o.fid || 11;
            location.href = "http://www.expo.cn/bbs/index.html#fid="+fid;
        }
    }, 1000);
}

var expotianct = document.getElementById('expotianct');
if(expotianct){
    var dayTime = new Date(2010,4,1) - new Date();
    var day = Math.ceil(dayTime / (3600*24*1000));
	var strday;
    document.getElementById('expotian').innerHTML = day;
    if(day<=0){
		day--;
		if(Math.abs(day)<1000){
			strday = Math.abs(day)
		}
		if(Math.abs(day)<10){
			strday = "00"+Math.abs(day)
		}
        expotianct.innerHTML = "<div id=\"newtopnav\" style=\"line-height:25px\">&nbsp;<a href='http://www.expo.cn/jnh/hznew/index.html' target='_self'><b>我的护照</b></a>| <a href='http://www.expo.cn/pavilion/plugin.html' target='_self'><b>插件下载</b></a>  | <a href='http://www.expo.cn/mrtj/pavilionlist_cn_pinyin.html' target='_self'><b>直达展馆</b></a> | <a href='http://www.expo2010.cn' href='_blank'>世博网</a> | <a href='http://www.expo.cn/mrtj/wapexpo.html' href='_blank'>掌上世博</a>&nbsp;&nbsp;</div>";
    }
}
/*  |xGv00|40ab951a1561a0c55d269b9cf47f4e34 */
