var _yaq = _yaq || [];
(function(){
    var typeOf = function (o) {
        var _t;
        return ((_t = typeof(o)) == "object" ? o==null && "null" ||
                Object.prototype.toString.call(o).slice(8,-1):_t).toLowerCase();
    },
    dispatcher = function(q){
        for(var i = 0,l = q.length; i< l ;i++){
            var f = q[i];
            if(typeOf(f) ==='function'){
                f();
            }else if(typeOf(f)==='array'
                     && typeOf(this[f[0]]) =='function'){
                this[f[0]].apply(this,f.slice(1));
            }else{
                continue;
            }
        }
    },
    camelcase = function(s){
        return s.replace(/\-(.)/g,
                         function(m, l){
                             return l.toUpperCase();
                         });
    };
    window._yat = new function(){
        var a = this;
        a.server = 'http://ato.geci.cn/';
        a.set = function(attr, value){
            a[attr] = value;
            return a;
        };

        a.track = function(act,id,rid){
            var i = new Image,h=(new Date()).getTime()/1000,rid = rid||0;
            i.src = a.server +  act + '.' + id + '.' + rid +'.'+ h + '.gif';
        };

        var acts = ['view|favorite|download|rate|search','view|favorite|rate|search','view|favorite|fan|rate|search'],
            ts = ['song','album','singer'],
            re = /\/(player|album|singer)\/([0-9a-z]{2,2})\/(player|album|singer)_(\d+)\.html/i,
            ms = document.location.pathname.match(re),
            rid = 0,
            ns,act='view';

        for(var i = 0, l = ts.length;i<l;i++){
            var s = ts[i],ats = acts[i].split('|');
            for(var j = 0, n = ats.length; j<n ;j++){
                (function(s,k){
                    a[camelcase('track-'+s+'-'+k)] = function(){
                        this.track.apply(this,[s+'.'+k].concat([].slice.call(arguments)));
                    };
                })(s,ats[j]);
            }
        };

        if (ms) {
            if(document.referrer){
                s = document.referrer;
                if(ns = s.replace('http://www.1ting.com','').match(re)){
                    if(ns[1]==ms[1]){
                        rid = ns[4];
                    }
                }else if(s.match(/^http:\/\/(www\.)?(baidu|yahoo|google|sogou)/gi)){
                    act = 'search';
                }
            }
            if(parent==self){
                var s = ms[1]== 'player' ? 'song' : ms[1];
                a.track(s + '.' + act, ms[4], rid);
            }
        }
    };

    jQuery(function(){
        dispatcher.call(_yat, _yaq);
        _yaq = {
            push:function(){
                var b = Array.prototype.slice.call(arguments);
                if(b.length >0){
                    dispatcher.call(_yat, b);
                }
            }
        };

        if(window.yiting && yiting.player){
            yiting.player.hook('end',function(){
                var a = this,t = Math.ceil(a.position()/(a.duration()||1)*100);
            }).hook('play',function(){
                var a = this;
                _yat.trackSongView(a.get('song-id'),a.number==a.pnumber ? 0 : a.get('song-id',true)||0);
            });
        }
    });

})();

