/**
 * home,genre,album
 *
 */

document.domain='1ting.com';

(function() {
    var tc = window.setInterval(function() {
        var is = $('#btn-items');
        if (is.size() == 0) {
            return false;
        } else {
            window.clearInterval(tc);
        }

        var bs = $('#btn-search');
        var val = bs.val();

        is.find('a').each(function() {
            $(this).click(function() {
                bs.val(this.innerHTML);
                is.toggle();
                return false;
            });
            
            if(this.parentNode.className=='current'){
                bs.val(this.innerHTML);  
            }
        });

        bs.click(function() {
            is.toggle().find('a').each(function() {
                $(this).parent().attr('className', $(this).text() == bs.val() ? 'current' : '');
            });
        });

        $('#frm-search').submit(function() {
            var qs = {'歌曲':'song','专辑':'album','歌手':'singer','歌词':'lyric','铃声':'ring','全部':'all'};
            var k = $.trim($('li.search_key input').val());
            var d = $.trim($('#btn-search').val());
            if (k.length > 0 && k != '在此输入' && qs[d]) {
                $(this).attr('action', 'http://so.1ting.com/' + qs[d] + '.do');
                return true;
            } else {
                return false;
            }
        });

        $('input[type=text]').each(function(i){
            $(this).one('click',function(){
                if(this.value!=this.getAttribute('default')){
                    this.value='';
                }
            });
        });

    }, 1000);
})();

$(document).ready(
    function(){
        var Dict = Dict || window;
        Dict.get = function(k){
            return this['_'+k];
        };
        Dict.set = function(k,v){
            this['_'+k] = v;
        };

        window.callback_play_list = function(a){
            Dict.set('called',a);
        }

        var fns = {select:function(selector,o){
                        if(typeof selector !=='string'){
                            selector = '#song-list input[name=checked]';
                        }
                        if(o){
                             var b = o.getAttribute('type')=='checkbox'?o.checked:true;
                        }else{
                            var b = this.checked;
                        }
                        $(selector).each(function(){
                            this.checked = b;
                        });
                    },
                    play:function(s){
                        var vs = fns['check'](s);
                        vs.length>0 ? window.open('http://www.1ting.com/p_'+vs.join('_')+'.html','_1ting') : alert('请选择歌曲！');
                    },
                    fav:function(s){
                        var vs = fns['check'](s);
                        vs.length>0 ? window.tobox('http://box.1ting.com/service/addToFavorite.jsp?t=1&rID='+vs.join(','),'_box') : alert('请选择歌曲！');
                    },
                    add:function(s,o){
                        var vs = fns['check'](s);
                        if(vs.length>0){
                            try{
                                var fl =(function(n){
                                if (navigator.appName.indexOf("Microsoft") != -1) {
                                     return window[n];
                                 } else {
                                     return document[n];
                                 }
                                })('play_list_send');
                                fl.add_to_play_list(vs);
                            }catch(e){
                                Dict.set('called',e.message);
                            }
                        }else{
                            Dict.set('called','请选择歌曲！');
                        }
                    },
                    check:function(selector){
                        var vs = [];
                        if(typeof selector !=='string'){
                            selector = '#song-list input[name=checked]';
                        }

                        $(selector).each(function(){
                            if(this.checked){
                                vs.push(this.value);
                            }
                        });
                        return vs;
                    }
                };
        $('#song-list tbody tr').mouseover(function(){
            $(this).css('background','#EAFAFF');
        }).mouseout(function(){
            $(this).css('background','#fff');
        });

        $('a.jfav').each(function(i){
            $(this).click(function(){
                window.tobox(this.href,'_box');
                return false;
            });
        });

        var explain = $('#explain');
        if(explain.get(0)){
            var oh = explain.height();
            explain.css('height','auto');
            var mh = explain.height();
            explain.css('height',oh+'px');
            $('.open span').click(function(){
                var nh = explain.height();
                explain.animate({height:nh==oh?mh:oh+'px'}, 500);
                $('.open span').toggle();
                return false;
            });
        }

        var artists = $('li.simple');
        if (artists.size()>0){
            $('#jopen').click(function(){
                $('#menu').show();
                return false;
            });

            $("#menu a").each(function(i){
                if(document.location.href.indexOf(this.getAttribute('href')) >=0 && i>0){
                    this.parentNode.className+=" now";
                }
            });

            $(document).click(function(e){
                if((e.target||e.srcElement).parentNode != $('#jopen').get(0)){
                    $('#menu').hide();
                }
            });

            $('#jclose').click(function(){
                $('#menu').hide();
                return false;
            });

            artists.each(function(){
                var p = this.parentNode.parentNode;
                var c = this.parentNode;
                $(this).click(function(){
                    $(c).removeClass('tagb');
                    $(p).next().show().next().hide().end();
                }).next().click(function(){
                    $(c).addClass('tagb');
                    $(p).next().hide().next().show().find('a.lazy').each(function(){
                        this.removeAttribute('className');
                        this.removeAttribute('class');
                        var ca = this.cloneNode(true);
                        ca.innerHTML = '<img src="http://img.1ting.com/images/singer/s75_'+this.id+'.jpg" width="75" height="100" alt="'+ca.innerHTML+'" title="'+ca.innerHTML+'" />';
                        this.parentNode.parentNode.insertBefore(ca,this.parentNode);
                    }).end();
                });
            });

            $('#jkeyboard a').each(function(i){
                $(this).click(function(){
                    $('#meun').hide();
                    
                    var pre = this.innerHTML.toLowerCase();
                    if(pre.length>1){
                        pre = this.getAttribute('for');
                    }
                    
                    if(pre=='all'){
                        $('.jartist').removeClass('jashow').show();
                        $('.word_list').show();
                        $('.simple').hide();
                        $('.pic_list').hide();
                    }else{
                        $('.jartist').hide();
                        $('.simple').show();
                        $('#jk'+pre).addClass('jashow').show();
                    }
                    $('#jkeyboard a.now').removeClass('now');
                    $(this).addClass('now');
                    return false;
                });
            });
        }

        var faq = $('.faq');
        if(faq.get(0)){
            faq.click(function(){
                $('div.about').show();
            });
            $('div.close a').click(function(){
                $('div.about').hide();
                return false;
            });

            var us =[];
            var ul = $('ul.row ul.has');
            var im = $('ul.row h3 img.has');

            function close_all(j){
                ul.each(function(i){
                    us[i]=this;
                    if(i !==j){this.style.display='none';}
                });
                im.each(function(i){
                    if(i !== j){
                        toggle(this,false);
                    };
                });
            }

            function toggle(o,b){
                o.src= b ?'/v5/images/genre/open.gif':'/v5/images/genre/close.gif';
                o.alt = b ?'点击收回':'点击展开';
            }

            function open_all(j){
                ul.each(function(i){
                    us[i]=this;
                    this.style.display='';
                });
                im.each(function(i){
                    toggle(this,1);
                });
            }

            ul.each(function(i){
                us[i]=this;
                this.style.display='none';
            }).end();

            im.each(function(i){
                $(this).click(function(){
                    toggle(this,this.src.indexOf('close.gif') !=-1);
                    $(us[i]).toggle('fast');
                });
            }).end();

            $('ul.tags li').each(function(i){
                $(this).click(function(){
                    if(i==0){
                        open_all();
                        $(this).attr('className','simple').next().attr('className','all');
                    }else{
                        close_all();
                        $(this).attr('className','simple').prev().attr('className','all');
                    }
                });
            });

            /*<genre>*/
            var linkmap = {};
            var indexes = '@';
            function before(i){
                var s = i;
                var b = i;
                while(s >= i){
                    s = indexes.indexOf('@',b--);
                }

                return s+1;
            }

            function after(i){
                var s = indexes.indexOf('@',i);
                return s;
            }

            $('#genre_s_form').submit(function(){
                if (indexes.length < 2) {
                    $('ul.row a').each(function() {
                        var c = this.innerHTML.toLowerCase();
                        linkmap[c] = this.getAttribute('href');
                        indexes += c + '@';
                    });
                }
                var k = $.trim($('#genre_s_key').val());
                k = k.replace('音乐','');
                if (k.length > 0) {
                    k = k.toLowerCase().replace('@', '');

                    var i = indexes.indexOf(k);
                    if (i != -1) {
                        window.location.href=(linkmap[indexes.substring(before(i), after(i))]);
                        return false;
                    }
                }
                return false;
            });
            /*</genre>*/
        }

        $('input.btn').each(function() {
            var k = this.getAttribute('name');
            var prefix = this.getAttribute('for');
            var id = prefix?prefix + 1:'song-list';
            if(k === 'add'){
                $(this).mousedown(function() {
                    id = Dict.get(prefix) || id;
                    fns[k]('#' + id + ' input[type=checkbox]', this);
                }).mouseup(function(){
                    switch (Dict.get('called').constructor)
                    {
                        case Array:
                            window.open('http://www.1ting.com/p_'+Dict.get('called').join('_')+'.html','_1ting');
                            break;
                        case Boolean:
                            alert('添加到列表!');
                            break;
                        case String:
                            alert(Dict.get('called'));
                            break;
                    }
                });
            }else{
                $(this).click(function() {
                    id = Dict.get(prefix) || id;
                    fns[k]('#' + id + ' input[type=checkbox]', this);
                });
            }
        });
        //全选 按钮事件 add 10:40 2009-11-24 @coolmoo
        $("input[name=select]").unbind();//反绑定 fns[select]事件
        $('input[name=select]').click(function(){
	        $('#song-list input[name=checked]').each(function(){
	        	this.checked=this.checked?false:true;
	       	});
        });
        //全选 按钮事件 End
    }
);


