/*

js/listenlive.js

*/

if(typeof ll == "undefined") ll = {};

ll.showid = 0;
ll.nowplayingid = 0;
ll.evalid = 0;
ll.delay = 1000*30; // 30 sec
ll.autoplay = false;
ll.init = function() {
	ll.sync();
	if(window.location.href.indexOf("autoplay") != -1) ll.autoplay = true;
};
ll.sync = function() {
	ll.clearTimer();
	$.getJSON("/json/livestreams.php",{evalid:ll.evalid},function(response){ll.receive(response)});
};
ll.clearTimer = function(){
	if(ll.timer) clearTimeout(ll.timer);
	ll.timer = null;
};
ll.receive = function(response) {
	if(response.error) alert(response.error);
	else {
		if(response.showid != ll.showid) {
			if(response.showid) {
				var currentshow = "<div class='address'><a>Current Show</a></div>";
				currentshow += "<div style='padding:10px;'><table><tr>";
				currentshow += "<td style='padding-right:10px;'><img src='"+response.show.pic+"' alt='' /></td>";
				currentshow += "<td style='font-size:14px;'><dl>";
				currentshow += "<dt>Show</dt><dd>"+response.show.showname+"</dd>";
				currentshow += "<dt>Genre</dt><dd>"+response.show.genre+"</dd>";
				currentshow += "<dt>DJs</dt><dd>"+response.show.djs+"</dd>";
				currentshow += "</dl></td>";
				currentshow += "</tr></table></div>";
				$('#currentshow').slideUp().html(currentshow).slideDown();
				$('#automation').slideUp();
				$('#hotline').slideDown();
				$('#evals').html("<div class='address'><a href='/dj/eval' style='float:right;color:#848484;text-decoration:underline;cursor:pointer;' target='_blank'>chapmanradio.com/dj/evals</a><a>Evals</a></div><div class='inner'><p style='color:#848484;font-size:11px;text-align:center;'>You can see this show's feedback because you're logged in.</p></div>");
			} else {
				$('#currentshow').slideUp();
				$('#automation').slideDown();
				$('#hotline').slideUp();
				$('#evals').slideUp();
			}
		}
		if(response.nowplaying.nowplayingid) {
			if(response.nowplaying.nowplayingid != ll.nowplayingid) {
				var currentsong = "";
				if(response.nowplaying.type == "music") {
					currentsong = "<div class='address'><a>Current Song</a></div>";
					currentsong += "<div style='padding:10px;'><table><tr>";
					currentsong += "<td style='padding-right:10px;'><img src='"+response.nowplaying.pic+"' alt='' /></td>";
					currentsong += "<td style='font-size:14px;'><dl>";
					currentsong += "<dt>Track</dt><dd>"+response.nowplaying.track+"</dd>";
					currentsong += "<dt>Artist</dt><dd>"+response.nowplaying.artist+"</dd>";
					currentsong += "</dl><p>"+response.nowplaying.notes+"</p></td>";
					currentsong += "</tr></table></div>";
				} else {
					currentsong = "<div class='address'><a>Current Topic</a></div>";
					currentsong += "<div style='margin:10px;'>"+response.nowplaying.notes+"</div>";
				}
				$('#currentsong').slideUp().html(currentsong).slideDown();
			}
		} else {
			$('#currentsong').slideUp();
		}
		if(response.evalid) {
			$('#evals .inner').append(ll.appendEvals(response.evals));
		}
		ll.evalid = response.evalid;
		ll.showid = response.showid;
		ll.nowplayingid = response.nowplaying.nowplayingid;
	}
	ll.timer = setTimeout("ll.sync()",ll.delay);
	if(ll.autoplay) { try { playstream(); ll.autoplay = false; } catch(e) {} }
};

ll.evals = [];
ll.appendEvals = function(evals) {
	var html = "";
	for(var x in evals) {
		eval = evals[x];
		html += "<div class='eval'>";
		html += "<div class='date'>"+eval.date+"</div>";
		html += "<div class='img'><img src='"+eval.img+"' /></div>";
		html += "<div class='text'>"+eval.text+"</div>";
		html += "<br style='clear:both;' /></div>";
	}
	return html;
};

ll.windows = {};
ll.open = function(stream){
	if(stream) stream = "?stream="+stream; else stream = "";
	if(typeof ll.windows['listenlive'+stream] == "undefined" || ll.windows['listenlive'+stream].closed) {
		ll.windows['listenlive'+stream] = window.open("/listenlive"+stream+"#autoplay", 'listenlive'+stream, "status=0,location=1toolbar=0,width=530,height=700,scrollbars=1")
		if(ll.windows['listenlive'+stream]) return false;
	} else {
		ll.windows['listenlive'+stream].focus();
		return false;
	}
};

function openListenLive(stream) { return ll.open(stream); }

// only called in /listenlive.php
// $(document).ready(function(){ll.sync()});
