function Menu(idElement, selectedID) 
{	
    // conf
    this.nameAndArtistNameLength = 33;
    this.intervalPlaylistRefreshTime = 45000;
    this.intervalAutoRefreshTime = 10000;
	
    // setters
    this.idElement = idElement;
    this.selectedID = selectedID;
	
    // helpers
    this.currentAutoID = null;
    this.intervalRenderID = false;
    this.intervalAutoID = false;
    this.firstRun = true;
}

Menu.prototype.stop = function()
{
    if(this.intervalAutoID)
    {
        clearInterval(this.intervalAutoID);
        this.intervalAutoID = false;
    }
}

Menu.prototype.selectBySpcId = function(spcId)
{
    this.stop(); 
    $('#' + this.idElement + ' li').removeClass('active');
    $('#' + this.idElement + ' li.spc-id-' + spcId).addClass('active');
}

Menu.prototype.auto = function()
{
    // auto		
    var obj = this;

    obj.select();
    obj.render();
	
    if(PLAYLIST == null)
    {
        PLAYLIST = new SPPlaylist;
    }
	
    PLAYLIST.start(this.currentAutoID.split('-')[3]);

    // autopilot
    if(!this.intervalAutoID)
    {
        this.intervalAutoID = setInterval(function() {
            obj.select();
            PLAYLIST.start(obj.currentAutoID.split('-')[3]);
        }, obj.intervalAutoRefreshTime);
    }
	
    // render
    if(!this.intervalRenderID)
    {
        this.intervalRenderID = setInterval(function() {
            obj.render();
        }, obj.intervalPlaylistRefreshTime);
    }
}


Menu.prototype.show = function(spcID)
{
    // auto		
    var obj = this;
    obj.render();
	
    if($('.spc-id-' + spcID).length)
    {		
		
		
        if(PLAYLIST == null)
        {
            PLAYLIST = new SPPlaylist;
            PLAYLIST.redirect(true);
        }
		
		
        var streamID = $('.spc-id-' + spcID).attr('id').split('-')[3];
        $('.spc-id-' + spcID).addClass();
		
        $('#' + this.idElement + ' li ').removeClass('active'); 
        $('.spc-id-' + spcID).addClass('active');
		
        PLAYLIST.start(streamID);
    }
	
    // render
    if(!this.intervalRenderID)
    {
        this.intervalRenderID = setInterval(function() {
            obj.render();
        }, obj.intervalPlaylistRefreshTime);
    }
}

Menu.prototype.list = function()
{
    var obj = this;
    obj.render();

    if(!this.intervalRenderID)
    {
        this.intervalRenderID = setInterval(function() {
            obj.render();
        }, obj.intervalPlaylistRefreshTime);
    }
}

Menu.prototype.select = function()
{
    if(this.firstRun)
    {
        this.currentAutoID = $('#' + this.idElement + ' li:first').attr('id');
        this.firstRun = false;
    }
    else
    {
        this.currentAutoID = $('#' + this.currentAutoID).next().attr('id');
		
        if(!$('#' + this.currentAutoID).length)
        {
            this.currentAutoID = $('#' + this.idElement + ' li:first').attr('id');
        }
    }
	
    $('#' + this.idElement + ' li ').removeClass('active'); 
    $('#' + this.currentAutoID).addClass('active');
}

Menu.prototype.render = function()
{
    var idElement  					= this.idElement;
    var nameAndArtistNameLength 	= this.nameAndArtistNameLength;
    var request = PLAYLIST_DATA_BASE_URL + 'm/playlist/combine.jsonp';
    $.jsonp({
        url: request,
        callback: 'jsonp',
        'success'  : function(data) {
            $.each (data,
                function(key, channel)
                {
                    var id = channel.id;
                    var name = channel.name;
                    var song = '';
                    var artists = new Array();
                    
                    $.grep(channel.songs, function(s) {
                        var status = s.status;
                        if(status == 0)
                        {
                            song = s.name;
                    			
                            $.grep(s.artists, function(artist) {
                                artists.push(artist.name);
                            });
                        }
                    }
                    );
                    
                    // draw
                    
                    var tempSong = song;
                    var tempArtists = artists.join(', ');
                    var trimingArtists = false;
                    
                                                            
                    if((name + tempArtists).length > nameAndArtistNameLength)
                    {
                        tempArtists = tempArtists.substring(0, nameAndArtistNameLength - name.length) + '...';
                        trimingArtists = true;
                    }

                    if(!trimingArtists && (name + tempArtists + tempSong).length > nameAndArtistNameLength)
                    {
                        tempSong = tempSong.substring(0, nameAndArtistNameLength - (name.length + tempArtists.length)) + '...';
                    }
                    
                    if(trimingArtists)
                    {
                        tempSong = '';
                    }
                           
                    $('#' + idElement + ' #chl-li-item-' + id + ' .chl-artist').html(tempArtists);
                    $('#' + idElement + ' #chl-li-item-' + id + ' .chl-song').html(tempSong);
                    $('#' + idElement + ' #chl-li-item-' + id + ' a').attr('title', name + (artists.length ? ', ' + artists.join(', ') : '') +  (song.length ? ', ' + song : ''));
                    
                    if(!trimingArtists && tempSong.length)
                    {
                        $('#' + idElement + ' #chl-li-item-' + id + ' .chl-separator').html('-');
                    }            
                }
                );
        }
    });
	
}
