
var g_songs=[];var g_timers={};var g_boxes=null;var g_stream=null;var g_cookie_options=null;var g_title_toggle=false;(function($){$(document).ready(function(){var date=new Date();date.setTime(date.getTime()+(3*24*60*60*1000));g_cookie_options={path:'/',expires:date};$('.donate').click(function(){window.open($(this).attr('href'),"Support","scrollbars=yes,resizable=yes,width=568,height=450");return false;});initStyleSwitcher();initBoxes();initSoundManager();initSync();initPoll();initTwitter();initRequest();setTimeout("updateIsPlaying(0)",100);setTimeout("updateBoxes()",2000);setTimeout("init_shoutbox()",3000);});})(jQuery);function initStyleSwitcher(){var cookie=$.cookie("style");if(cookie&&cookie!="classic")
switchStyle(cookie);$("#style").children().remove();$("link[@rev=*style*][title]").each(function(i){var title=this.getAttribute("title");if(title==cookie)
var option=$("<option selected>"+title+"</option>");else
var option=$("<option>"+title+"</option>");$("#style").append(option);});$("#style").change(function(selected){var style=$("#style option:selected").text();switchStyle(style);$.cookie("style",style,g_cookie_options);return false;});$("#style").hover(function(){$(this).fadeTo(100,0.5);},function(){$(this).fadeTo(200,0.1);});$("#style").fadeTo(0,0.1);}
function collapse_boxes(){if(g_title_toggle)
g_boxes=JSON.parse(unescape($.cookie("collapsed")));else
$(".collapsible").each(function(){g_boxes[$(this).attr("id")]=true;})
g_title_toggle=!g_title_toggle;updateBoxes();return false;}
function initBoxes(){var default_boxes={"box_news":true,"box_chat":true,"box_request":true,"box_about":false};var cookie=$.cookie("collapsed");if(!cookie)
$.cookie("collapsed",escape(JSON.stringify(default_boxes)),g_cookie_options);else
g_boxes=JSON.parse(unescape($.cookie("collapsed")));if(!g_boxes)
g_boxes=default_boxes;$(".collapsible h2").click(function(){var id=$(this).parent().parent().attr("id");var content=$(this).parent().parent().children(".content");g_boxes[id]=content.is(":visible");$.cookie("collapsed",escape(JSON.stringify(g_boxes)),g_cookie_options);updateBoxes();return false;});var konami=new Konami()
konami.code=function(){window.open('http://www.wikihost.org/w/noliferadio/');return false;}
konami.load()
$("#title").click(collapse_boxes);}
function initSync(){var cookie=$.cookie("cookie_password");if(!cookie){$("#sync form").ajaxForm(function(){$("#sync").hide();return false;});$("#sync").show();}
$(".password").focus(function(){$(this).attr("value","");});}
function initRequest(){bindFilter('filter_songs','php/get_songs.php','songs[]','#filtered_songs');$("#box_request form").ajaxForm(function(){return false;});}
function initPoll(){for(var i=0;i<4;++i)
$(".song_"+i+" .poll_rating").attr("rel",i);$(".poll_rating").click(function(){if(g_songs.length==0)
return false;var id=$(this).attr("rel");$(".song_"+id+" .poll_question").hide();$.getJSON("php/poll/set.php",{path:g_songs[id].escaped_song,rating:$(this).attr("rev"),nt:(new Date()).getTime()});setTimeout("updatePoll("+id+")",500);return false;});}
function initTwitter(){$("#box_news .news").tweet({username:["noliferadio"],count:2});$("#box_news .content").tweet({username:["noliferadio"],count:8});}
function initSoundManager(){soundManager.url="swf/";soundManager.debugMode=false;soundManager.waitForWindowLoad=true;soundManager.onload=function(){$("#player").fadeIn("slow");$("#player .stop").hide();}
$("#player .play").click(function(){if(!g_stream){g_stream=soundManager.createSound({id:'stream',volume:$("#player .volume").slider("value"),url:'http://radio.nolife-radio.com:9000/stream'});}
g_stream.play();$(this).hide();$("#player .stop").show();return false;});$("#player .stop").click(function(){if(g_stream){g_stream.destruct();g_stream=null;}
$(this).hide();$("#player .play").show();return false;});$("#player .volume").slider({min:0,max:100,value:100,slide:function(event,ui){if(g_stream)
g_stream.setVolume(ui.value);}});}
function switchStyle(style){if(style=="")
style="classic"
$('link[@rel*=style][title]').each(function(){this.disabled=true;if(this.title==style)
this.disabled=false;});}
function showBox(box){box.children(".header").children(".minimized").fadeOut("fast");box.children(".content").slideDown("fast");box.removeClass("collapsed");}
function hideBox(box){box.children(".header").children(".minimized").fadeIn("fast");box.children(".content").slideUp("fast");box.addClass("collapsed");}
function updateBoxes(){for(var id in g_boxes){var box=$("#"+id);if(g_boxes[id])
hideBox(box);else
showBox(box);}}
function updateSong(id){var tag=$(".song_"+id+" .radio_song")
tag.html(g_songs[id].song);tag.attr("href","http://www.google.com/search?q="+g_songs[id].song);updatePoll(id);}
function updateIsPlaying(delay){$.getJSON("server_info.json",{nt:(new Date()).getTime()},function(json){if(json.online){json.escaped_song=escape(json.song);if(g_songs.length>0&&g_songs[0].escaped_song==json.escaped_song)
return;$(".timeout").animate({opacity:0.2,width:"0px"},0).animate({opacity:1.0,width:"100%"},delay,"linear",function(){g_songs.unshift(json);if(g_songs.length>4)
g_songs.pop();$(".radio_status").html("Servers online - ");$(".radio_listeners").html(json.listeners+' listeners');for(id in g_songs)
updateSong(id);$(".radio").show();}).animate({opacity:0.0},500);}
else{$(".radio_status").html("Server is offline.");$(".radio").hide();$(".poll").hide();}});setTimeout("updateIsPlaying(2000)",5000);}
function setPollStyle(obj,width,percent,inc){if(percent==0)
obj.hide();else{obj.css("display","inline-block");obj.html(percent+"%");if(percent==100)
obj.width(""+width+"px");else
obj.width(""+(width*percent/100.0)+"px");}}
function updatePollBar(element,width,total,positive){var pos=Math.round(positive*100/total);var neg=Math.round((total-positive)*100/total);setPollStyle(element.find(".poll_positive"),width,pos);setPollStyle(element.find(".poll_negative"),width,neg);}
function updatePoll(id){var song=$(".song_"+id);$.getJSON("php/poll/has_voted.php",{path:g_songs[id].escaped_song,nt:(new Date()).getTime()},function(json_voted){$.getJSON("php/poll/get.php",{path:g_songs[id].escaped_song,nt:(new Date()).getTime()},function(json){song.find(".poll").show();if(json_voted.has_voted){if(id==0){document.title="* "+g_songs[id].song+" - NoLife-radio";updateFavicon('favicon_voted.png');}
song.find(".poll_question").hide();}
else{if(id==0){document.title=g_songs[id].song+" - NoLife-radio";updateFavicon('favicon.png');}
song.find(".poll_question").show();}
if(json.total>0){var width=$("#content_bar_size").width();var subsong=$(".content .song_"+id);updatePollBar(subsong,width,json.total,json.positive);if(id==0){subsong=$(".minimized .song_"+id);width=$("#minimized_bar_size").width();updatePollBar(subsong,width,json.total,json.positive);}
song.find(".poll_total").html(json.total+" vote"+(json.total>1?"s":""));song.find(".poll_result").show("fast");}
else{song.find(".poll_result").hide("fast");}});});}
function bindFilter(filter_id,php_file,input_name,container_id){g_timers[filter_id]={id:-1,self:null}
$('input#'+filter_id).keyup(function(){var timer=g_timers[filter_id];if(timer.id!=-1)
clearTimeout(timer.id);timer.self=this
timer.id=setTimeout(function(){var timer=g_timers[filter_id];if($(timer.self).val().length<2)
return false;$.getJSON(php_file,{filter:$(timer.self).val()},function(json){timer.id=-1;var options='';var width=$("#content_bar_size").width();$(container_id).html('');for(var i in json){var text=json[i].value;if(json[i].total>0)
text=json[i].positive+'/'+json[i].total+' g: '+text;$(container_id).append($('<li>').append(text));}});},500);});}
function updateFavicon(favicon){var canvas=document.createElement('canvas');if(canvas.getContext){canvas.height=canvas.width=16;var ctx=canvas.getContext('2d');var img=new Image();img.onload=function(){ctx.drawImage(img,0,0);var link=$('#favicon');link.replaceWith(link.clone().attr('href',canvas.toDataURL('image/png')));};img.src=favicon;}}
