var view = new (function() {
    this.tpl = {'photo':'<li><a href="{url}" target="_blank"><img src="{img}" height="150" alt="{title}" title="{title}" style="-ms-interpolation-mode: bicubic" /></a></li>',
        'news':'<li><a href="{url}" title="{title}-{date}">{title}</a></li>'};

    this.callback = function(t, u, r) {
        if (r.length == 0) {
            return;
        }
        var d = function(x) {
            return document.getElementById(x);
        };
        d('link-' + t).style.display = "block";
        d('external-' + t).style.display = "block";
        d('data-' + t).innerHTML = r;
        d('url-' + t).href = u;
    };

    this.render = function(json) {
        for (var j = 0; j < json.length; j++) {
            (function(s, p) {
                var res = p.results;
                var r = '';
                var t = s.tpl[p.type];

                for (var i = 0; i < res.length; i++) {
                    r += t.replace(/\{([a-z0-9]+)\}/ig, function(m, k) {
                        return res[i][k];
                    });
                }
                s.callback(p.type, p.url, r);
            })(this, json[j]);
        }
    };
})();

(function() {
    $('a.jfav').click(function(){
        try{
		    window.tobox(this.href,'_box');
        }catch(e){}
		return false;
    });

    $('#show-by-condition').change(function() {
        [function(id) {
            $('#by-trend').hide();
            $('#by-date').show('fast');
        },function(id) {
            if (document.getElementById('by-trend').getAttribute('loaded') != 'OK') {
                var u = '/albumJson.php?sort=hot&reverse=true&size=12&start=0&prefix=JSONCallback(&suffix=);&q=' + id;
                document.getElementsByTagName('head')[0].appendChild(document.createElement('script')).src = u;
            }
            $('#by-date').hide();
            $('#by-trend').show('fast');
        }][this.value](this.getAttribute('for'));
    });

    window.JSONCallback = function (json) {
        var t = '<div class="album" id="talbum-{albumId}"><div class="pic"><a href="{albumUrl}" target="_self" title="专辑: &#34;{albumName}&#34;"><img src="{albumImage150}" width="150" height="150" alt="{albumName}" /></a><div class="play j"><a href="/album_{albumId}.html" title="播放整张专辑" target="_1ting">播放整张专辑</a></div></div><p><a href="{albumUrl}" target="_self" title="专辑: &#34;{albumName}&#34;">{albumName}</a></p><p title="发行日期: &#34;{albumIssueDateTime}&#34;">{albumIssueDateTime}</p></div>';
        var res = json.results;
        var r = '';

        for (var i = 0; i < res.length; i++) {
            r += t.replace(/\{([a-z0-9]+)\}/ig, function(m, k) {
                return res[i][k];
            });
        }
        var d = document.getElementById('by-trend');
        d.innerHTML = r;
        d.setAttribute('loaded', 'OK');
    }
})();

