/**
 * ZETNews
 *
 * Zarządzanie dj'ami na stronie
 *
 * @autor Tomasz Pietrzak
 */
objKlubowe = {

    // konfiguracja
    xml: '/externals/xml/klubowe/%s.xml',
    city: '',

    init: function(site) {
        objKlubowe.city = $('body').attr('class').split(' ')[1].substring(7);
        objKlubowe.getPersonList(site);
        objKlubowe.navigation();
    },

    navigation: function() {

       $('#klubowePlayer .navigation a').click(function(){
           switch( $(this).attr('class') )
           {
               case 'previous':
                   $('#kluboweDJe .content .nodesList a.active').parent().prev().find('a').click()
                   break;
               case 'next':
                   $('#kluboweDJe .content .nodesList a.active').parent().next().find('a').click()
                   break;
           }
           return false;
       });

       $('#klubowePlayer.domowa').hover(function(){
            $('#klubowePlayer .detail').show();
        }, function(){
            $('#klubowePlayer .detail').hide();
        });

    },

    getPersonList: function(site) {
        $.ajax({
            type: 'GET',
            url: objKlubowe.xml.replace('%s', 'lista'),
            dataType: 'XML',
            success: function(xml) {

                // generowanie html'a
                objKlubowe.makePersonalList(xml, site);
            }
        });
    },

    makePersonalList: function(xml, site){
        var person, html = $('<ul/>').attr('class', 'nodesList');
        $(xml).find('person').each(function(i){

            person              = {};
            person.id           = $(this).find('id').text();
            person.name         = $(this).find('name').text();
            person.link         = $(this).find('link').text();
            person.image        = {};
            person.image.url    = $(this).find('image url233').text();
            person.image.text   = $(this).find('image text').text();
            person.image.width  = 233;
            person.image.height = 233;

            if ($(this).find('show '+objKlubowe.city).text())
            {
                person.name = person.name + '<br />' + $(this).find('show '+objKlubowe.city).text();
            }

            $(html).append(
                $('<li></li>').addClass('content-'+person.id).html(
                    $('<a></a>').attr('href', person.link).html(
                        $('<img />').attr('src', person.image.url).attr('alt', person.image.text).attr('width', person.image.width).attr('height', person.image.height)
                    ).click(function(){
                        $(this).parent().parent().find('li a').removeClass('active');
                        $(this).addClass('active');
                        objKlubowe.getPersonDetail();
                        return false;
                    })
                ).append(
                    $('<h3/>').html(person.name)
                )
            );
        });

        $('#kluboweDJe .content').html(html);
        $('#kluboweDJe .content .nodesList a:first').click();
    },

    getPersonDetail: function() {
        var nodeId = parseInt($('#kluboweDJe .content .nodesList .active').parent().attr('class').split(' ')[0].substring(8));

        $.ajax({
            type: 'GET',
            url: objKlubowe.xml.replace('%s', nodeId),
            dataType: 'XML',
            success: function(xml) {

                // generowanie html'a
                objKlubowe.makePersonalDetail(xml);
            }
        });
    },

    makePersonalDetail: function(xml) {
        var html, track;

        var name = $('#kluboweDJe .content .nodesList .active').parent().find('h3').html();
        var src  = $('#kluboweDJe .content .nodesList .active img').attr('src').replace('233x233', '180x180');
        var href = $('#kluboweDJe .content .nodesList .active').attr('href');

        $('#klubowePlayer .artist').html(
            $('<span />').html(name)
        ).append(
            $('<img />').attr('src', src).attr('alt', name)
        ).append(
            $('<a />').attr('href', href).attr('title', name)
        );

        $('#klubowePlayer .detail .album').html('');
        if ($(xml).find('person tracklist song').length>0){   // prevka
            // tracklist
            html = $('<ul class="playlist"></ul>');
            $(xml).find('person tracklist song').each(function(i){

                track           = {};
                track.artist    = $(this).find('artist').text();
                track.title     = $(this).find('title').text();
                track.audio     = $(this).find('audio').text();

                $(html).append(
                    $('<li></li>').html(
                        $('<a class="inline-exclude"></a>').attr('href', track.audio).html(track.artist+' - '+track.title)
                    )
                );
                if ( i == 10 )
                  return false;
            });

            $('#klubowePlayer .detail .album').html(
                $('<div></div>').addClass('tracklist').html(html)
            );

            pagePlayer.searchForLinks();

        }
    }
}

