﻿S.Service = function(config){
    jQuery.ajax({
      type:'POST',
      asynchronous : false,
      url:S.Service.Base[config.ns||'syz']+(config.service||'art')+'Service.asmx/'+(config.method||'getArticle'),
      data:S.JSON.encode(config.pm||{}),
      contentType:"application/json;",
      success:function(o){config.cb.call(config.scope,o.d==false? false:S.JSON.transDate(o.d||o));},
      error:function(e){var obj=S.JSON.decode(e.responseText);alert('警告：'+obj.Message);}
    });
};
var FileName = {syz:{art:{}}};
S.apply(S.Service,{
    Base : new S.Enum({
        syz : '/syz.Service/',
        csland : '/csland.Service/',
        cx : '/cslandnew.CX/'
    }),
    getFile : function(config){
        try{if(!config.getFileName()) throw '';}catch(e){
            config.ecb([config.ns,config.service,config.method,'的FileName未设置'].join('.'));
            return;
        }
        jQuery.ajax({
          type:'GET',
          url:S.Base['temp']+config.getFileName(),
          contentType:"application/json;",
          success:function(r){var o=S.JSON.decode(r);config.cb.call(config.scope,o.d||o);},
          error:config.ecb||function(e){alert('未发现文件');}
        });
    },
    getService : function(config){
        jQuery.ajax({
          type:'POST',
          asynchronous : false,
          url:S.Service.Base[config.ns||'syz']+(config.service||'art')+'Service.asmx/'+(config.method||'getArticle'),
          data:S.JSON.encode(config.pm||{}),
          contentType:"application/json;",
          success:function(o){config.cb.call(config.scope,o.d==false? false:S.JSON.transDate(o.d||o));},
          error:function(e){var obj=S.JSON.decode(e.responseText);alert('警告：'+obj.Message);}
        });
    },
    getArticle : function(pm,cp){
        S.Service.getData({
            ns:'syz',service :'art',method:'getArticle',cp:cp,
            pm:S.apply({dirs:"-1",dept:-1,topic:-1,pageSize:6,pageNo:1},pm)
        });
    },
    getData : function(config){
        config.cb = config.cb || function(arts){
            S.UI.Content(S.apply({data:arts},config.cp));
            S.UI.PageBar({
                pos:config.pos||'pageBar',
                artSum:arts.Sum,
                pageSize:config.pm.pageSize,
                pageNo:config.pm.pageNo
            });
            if(config.fn) config.fn();
        }
        S.Service.get(new S.config(config));
    },
    get : function(config){
        config = new S.config(config);
        config.ecb = function(){S.Service.getService(config);};
        S.Service.getFile(config);
    },
    login : function(name,pwd,code,cb){
        S.Service.getService(new S.config({ns:'refer',service:'guest',method:'login',pm:{userName:name,pwd:MD5(pwd),valcode:code},cb:cb}));
    },
    logout : function(cb){
        S.Service.getService(new S.config({ns:'refer',service:'guest',method:'logout',pm:{},cb:cb}));
    },
    cx : function(config){
        config = S.apply({ns:'cx',service:'cx',method:'cx'},config);
        config = new S.config(config);
        S.Service.getService(config);
    },
    Navi : function(pm,fn){S.Service.getService(new S.config({
        ns:'syz',service:'dir',method:'navigate',pm:{id:pm.id},
        cb:function(dirs){S.UI.Navi(pm.pos||'navi',dirs,fn);}
        }));
    },
    Menu : function(pm){S.Service.getService(new S.config({
        ns:'syz',service:'dir',method:'getDirs',pm:{id:pm.id},
        cb:function(dirs){S.UI.Menu(pm.pos||'menu',dirs);}
        }));
    },
    read : function(config){
        S.Service.getService(new S.config({ns:config.ns,service:config.service,method:'read',pm:config.pm,cb:function(){}}));
    },
    imgNews : function(config){
        S.Service.get(new S.config({
            ns:'syz',
            service:'art',
            method:'getMediaArt',
            pm:S.apply({type:1,dirs:-1,dept:-1,topic:-1,pageSize:5,pageNo:1},config.pm),
            cb:function(arts){
                var data = S.advt||[];
                jQuery(arts.List).each(function(){
                    if(!this.Attach) return;
                    data.push({
                        title:this.Title,
                        pic:[S.Base.files,this.Attach.Dir,'/news/',this.Attach.StoreName].join(''),
                        lk:S.Base.web+'articleRead.aspx?id='+this.ID
                    });
                });
                S.UI.ImgNews(config.pos||'flashNews',data,config.width,config.height);
            }
        }));
    },
    imgNews1 : function(pos){
        S.Service.get(new S.config({
            ns:'syz',
            service:'art',
            method:'getMediaArt',
            pm:{type:ArticleType.图片新闻,dirs:'41,42',dept:-1,topic:-1,pageSize:4,pageNo:1},
            cb:function(arts){
                var el = document.getElementById(pos);
                el.innerHTML = '';
                jQuery(arts.List).each(function(){
                    if(!this.Attach) return;
                    el.appendChild(S.UI.E({tag:'div',items:[
                        {tag:'a',href:S.Base.web+'articleRead.aspx?id='+this.ID,target:'_blank',items:[
                            {tag:'img',src:S.Base.files+this.Attach.Dir+"/small/"+this.Attach.StoreName},{tag:'br'},{text:this.Title}
                        ]}
                    ]}));
                });
            }
        }));
    },
    getXC : function(pos,pm){
        S.Service.getData({
            ns:'syz',service:'att',method:'getByDir',pm:pm,
            cb:function(atts){
                if(!atts.List) return;
                var tab = [];
                $(atts.List).each(function(i){
                    tab.push({tag:'td',items:[
                        {tag:'a',href:S.Base.web+'/xc.aspx?dirid='+pm.dirs,items:[
                            {tag:'img',alt:this.FileName,src:S.Base.files+this.Dir+'/small/'+this.StoreName}
                        ]}
                    ]});
                });
                S(pos).innerHTML = '';
                S(pos).appendChild(S.UI.E({tag:'table',items:[{tag:'tbody',items:[{tag:'tr',items:tab}]}]}));
            }
        });
    }
});

