var List = { empty: false, channel_listener: function() { socket.on("channel", function(msg){ List.channel_function(msg); }); }, channel_function: function(msg) { switch(msg[0]) { case "list": List.populate_list(msg[1]); break; case "added": List.added_song(msg[1]); break; case "deleted": List.deleted_song(msg[1]); break; case "vote": List.voted_song(msg[1], msg[2]); break; case "song_change": List.song_change(msg[1]); break; } }, populate_list: function(msg) { if(list_html == undefined) list_html = $("#list-song-html").html(); full_playlist = msg; List.sortList(); $("#wrapper").empty(); if(full_playlist.length > 1){ $.each(full_playlist, function(j, current_song){ if(!current_song.now_playing){ //check that the song isnt playing $("#wrapper").append(List.generateSong(current_song, false, lazy_load, true)); } }); if(lazy_load){ if(window.mobilecheck()) $(".list-image").lazyload({}); else{ $(".list-image").lazyload({container: $("#wrapper")}).removeClass("lazy"); document.getElementById('wrapper').scrollTop += 1; document.getElementById('wrapper').scrollTop += -1; } } }else{ List.empty = true; $("#wrapper").append("The playlist is empty."); } $("#settings").css("visibility", "visible"); $("#settings").css("opacity", "1"); $("#wrapper").css("opacity", "1"); }, added_song: function(added){ full_playlist.push(added); List.sortList(); $("#suggested-"+added.id).remove(); if(List.empty){ $("#empty-channel-message").remove(); List.empty = false; } List.insertAtIndex(added, true); }, deleted_song: function(deleted){ var index = List.getIndexOfSong(deleted); var to_delete = $("#wrapper").children()[index]; try{ to_delete.style.height = 0; setTimeout(function() { $("#"+deleted).remove(); full_playlist.splice(List.getIndexOfSong(deleted), 1); }, 305); document.getElementById('wrapper').scrollTop += 1; document.getElementById('wrapper').scrollTop += -1; }catch(err){ full_playlist.splice(List.getIndexOfSong(deleted), 1); if(!List.empty) $("#wrapper").children()[$("#wrapper").children().length-1].remove(); } if(full_playlist.length <= 2){ List.empty = true; $("#wrapper").append("The playlist is empty."); } $("#suggested-"+deleted).remove(); Suggestions.checkUserEmpty(); }, voted_song: function(voted, time){ var index_of_song = List.getIndexOfSong(voted); var song_voted_on = full_playlist[index_of_song]; full_playlist[index_of_song].votes += 1; full_playlist[index_of_song].added = time; List.sortList(); $("#"+voted).remove(); List.insertAtIndex(song_voted_on, false); }, song_change: function(time){ var length = full_playlist.length-1; full_playlist[0].now_playing = true; full_playlist[0].votes = 0; full_playlist[0].guids = []; full_playlist[0].added = time; full_playlist[length].now_playing = false; try{ full_playlist.push(full_playlist.shift()); if(!List.empty) $("#wrapper").children()[0].remove(); List.insertAtIndex(full_playlist[length-1], false); document.getElementById('wrapper').scrollTop += 1; document.getElementById('wrapper').scrollTop += -1; }catch(e){} }, vote: function(id, vote){ socket.emit('vote', [chan, id, vote, adminpass]); return true; }, skip: function(){ socket.emit('skip', {pass: adminpass, id:video_id}); return true; }, importOldList: function(chan){ var ids=""; var num=0; playlist_url = "lists/"+chan+".json"; list = $.parseJSON($.ajax({ type: "GET", url: playlist_url, async: false }).responseText); $.each(list.songs, function(i,data) { ids+=data.id+","; if(num>45){ Search.addVideos(ids); ids = ""; num = 0; } num++; }); Search.addVideos(ids); document.getElementById("search").value = ""; }, sortList: function() { full_playlist.sort(Helper.predicate({ name: 'votes', reverse: true }, 'added')); }, show: function(){ if(!window.mobilecheck()) { if(showToggle){ showToggle=false; $("#toptitle").empty(); $("#chan").addClass("bigChan"); //$("#chan").html("zoff.no/"+encodeURI(chan)); $("#chan").html("zoff.no/"+chan.toLowerCase()); }else{ showToggle=true; $("#toptitle").html("Zöff"); $("#chan").removeClass("bigChan"); $("#chan").html(chan); } } }, insertAtIndex: function(song_info, transition) { i = List.getIndexOfSong(song_info.id); if(i === 0) $("#wrapper").prepend(List.generateSong(song_info, transition, false, true, false)); else $("#wrapper > div:nth-child(" + (i) + ")").after(List.generateSong(song_info, transition, false, true, false)); var added = $("#wrapper").children()[i]; if(transition) { setTimeout(function(){ $(added).css("height", 66); },5); } }, generateSong: function(song_info, transition, lazy, list, user) { var video_id = song_info.id; var video_title = song_info.title; var video_votes = song_info.votes; var video_thumb = "background-image:url('//img.youtube.com/vi/"+video_id+"/mqdefault.jpg');"; var song = $("