		var isLive = 1;
		var pid = '';	// current track pid
		
		// holding class for track data
		var Track = new Class({
			initialize: function() {
				this.track = "";
				this.artist = "";
				this.pid = "";
				this.image = "";
				this.amazonlink = "";
				this.ituneslink = "";
			}
		});


		// array to keep the last 5 tracks played in
		var lastTracks = new Array();


		var updateCurrentShow = function(showdetails,chainfunction) {
				updateCurrentShowb(showdetails,chainfunction);
		}
		
		var updateCurrentShowb = function(showdetails,chainfunction) {		
			var title = '';
		    var images = $$('#showimage img');	
	        if(images)
	            images = $$('#showimage a img');	
		        		
			if ($chk(showdetails.Title)) {
			    if ($chk(showdetails.Url)) {
				    title = '<strong><a href="' +showdetails.Url + '">' + showdetails.Title + '</a></strong>';
				} else {
					title = '<strong>' + showdetails.Title + '</strong>';
				}				
			} else {
			    title = '&nbsp;';
			}
			if ($chk(showdetails.Start)) {
				title = title + ' - ' + showdetails.Start;
			}
			
			$('show').setHTML(title);
            if (images) {
			    if ($chk(showdetails.PictureSmallUrl)) {
			        if ($chk(showdetails.Url)) {
			            $('showimage').setHTML('<a href="' + showdetails.Url + '"><img height="75" with="75" alt="on air now" src="' + showdetails.PictureUrl + '" id="showimage"/></a>');
    			        
			        } else {
                        images.each(function(item) {
                            item.setAttribute('src',showdetails.PictureUrl);	
			            });				    				
				    }				
			    } else {
			        if ($chk(showdetails.Url)) {
			            $('showimage').setHTML('<a href="' + showdetails.Url + '"><img height="75" with="75" alt="on air now" src="http://qradio.qthemusic.com/qradio/Content/IMG/DJPics/default_show.gif" id="showimage"/></a>');			        
			        } else {
                        images.each(function(item) {
                            item.setAttribute('src','http://qradio.qthemusic.com/qradio/Content/IMG/DJPics/default_show.gif');	
		                });		
		            }			    		
			    }
			}
				
		}
		var updateNextShow = function(showdetails) {
				
			var title = '';
			var nextShow = $('nextShow');
			var nextShowTime = $('nextShowTime');
			var nextShowDesc = $('nextShowDesc');
			var imagesNext = $$('#nextShowPic img');
			if(imagesNext)
			    imagesNext = $$('#nextShowPic a img');
			    
			if ($chk(showdetails.Title)) {
				if ($chk(showdetails.Url)) {
				    title = '<span>Up Next:</span><a href="' +showdetails.Url + '">' + showdetails.Title + '</a>';				
				} else {			
			        title = '<span>Up Next:</span>' +  showdetails.Title;			   
			    }
			    if (nextShow) {
			        if ($chk(showdetails.Url)) {
			            nextShow.setHTML('<a href="' +showdetails.Url + '">' + showdetails.Title  + '</a>');
			        } else {
			            nextShow.setHTML(showdetails.Title);
			        }
			    }
			} else {
			    title = '<span>Up Next:</span>&nbsp;';
			}
			if ($chk(showdetails.Start)) {
				title = title + ' - ' + showdetails.Start;
			    if (nextShowTime) {
			        nextShowTime.setHTML(showdetails.Start);
			    }				
			}
			if ($chk(showdetails.ShortSynopsis)) {
			    if (nextShowDesc) {
			        nextShowDesc.setHTML(showdetails.ShortSynopsis);
			    }				
			}	

            if (imagesNext) {
			    if ($chk(showdetails.PictureSmallUrl)) {
			        if ($chk(showdetails.Url)) {
			            if ($('nextShowPic')) {
			                $('nextShowPic').setHTML('<a href="' + showdetails.Url + '"><img height="80" with="80" alt="next show" src="' + showdetails.PictureUrl + '" id="showimage"/></a>');
			            }
    			        
			        } else {
                        imagesNext.each(function(item) {
                            item.setAttribute('src',showdetails.PictureUrl);	
			            });				    				
				    }				
			    } else {
			        if ($chk(showdetails.Url)) {
			            $('nextShowPic').setHTML('<a href="' + showdetails.Url + '"><img height="80" with="80" alt="next show" src="http://qradio.qthemusic.com/qradio/Content/IMG/DJPics/default_show.gif" id="showimage"/></a>');			        
			        } else {
                        imagesNext.each(function(item) {
                            item.setAttribute('src','http://qradio.qthemusic.com/qradio/Content/IMG/DJPics/default_show.gif');	
		                });		
		            }		            		    		
			    }
			}
													
			$('next').setHTML(title);	
		}

		var updateLastShow = function(showdetails) {			
			var title = '';
			var lastShow = $('lastShow');
			var lastShowTime = $('lastShowTime');
			var lastShowDesc = $('lastShowDesc');	
			var lastListenAgain = $('lastListenAgain');
			var imageslast = $$('#lastShowPic img');
			if (imageslast)
			    imageslast = $$('#lastShowPic a img');
			    
			if ($chk(showdetails.Title)) {
				if ($chk(showdetails.Url)) {
				    title = '<strong><a href="' +showdetails.Url + '">' + showdetails.Title + '</a></strong>';				
				} else {
				    title = '<strong>' + showdetails.Title + '</strong>';
				}
			    if (lastShow) {
			        if ($chk(showdetails.Url)) {
			            lastShow.setHTML('<a href="' +showdetails.Url + '">' + showdetails.Title  + '</a>');
			        } else {
			            lastShow.setHTML(showdetails.Title);
			        }			        
			    }				
			} else {
			    title = '&nbsp;';
			}
			if ($chk(showdetails.Start)) {
				title = title + ' - ' + showdetails.Start;
			    if (lastShowTime) {
			        lastShowTime.setHTML(showdetails.Start);
			    }					
			}
			if ($chk(showdetails.ShortSynopsis)) {
			    if (lastShowDesc) {
			        lastShowDesc.setHTML(showdetails.ShortSynopsis);
			    }				
			}

            if (imageslast) {
			    if ($chk(showdetails.PictureSmallUrl)) {
			        if ($chk(showdetails.Url)) {
			            if ($('lastShowPic')) {
			                $('lastShowPic').setHTML('<a href="' + showdetails.Url + '"><img height="80" with="80" alt="last show" src="' + showdetails.PictureUrl + '" id="showimage"/></a>');
			            }
    			        
			        } else {
                        imageslast.each(function(item) {
                            item.setAttribute('src',showdetails.PictureUrl);	
			            });				    				
				    }				
			    } else {
			        if ($chk(showdetails.Url)) {
			            $('lastShowPic').setHTML('<a href="' + showdetails.Url + '"><img height="80" with="80" alt="last show" src="http://qradio.qthemusic.com/qradio/Content/IMG/DJPics/default_show.gif" id="showimage"/></a>');			        
			        } else {
                        imageslast.each(function(item) {
                            item.setAttribute('src','http://qradio.qthemusic.com/qradio/Content/IMG/DJPics/default_show.gif');	
		                });		
		            }	
			    }
			}

			if (lastListenAgain) {
			    if ($chk(showdetails.MP3File)) {
			        if ($chk(showdetails.BroadcastId)) {
				            lastListenAgain.setAttribute('href', 'http://radioplayer.qthemusic.com/?id=' + showdetails.BroadcastId);			    
			        } else {
                        lastListenAgain.setAttribute('href', 'http://radioplayer.qthemusic.com/');			    
			        }
			    } else {
			        lastListenAgain.setAttribute('href', 'http://radioplayer.qthemusic.com/');	
			    }
			}
			showdetails.BroadcastId
		}

		// get a shows details and show it
		var getShowDetails = function(showid,showdetailsfunction,chainfunction) {
			var url = 'http://qradio.qthemusic.com/cgi-bin/qradio/showdetails.pl';
			var request = new Json.Remote(url, {
				onComplete: function(jsonObj) {
					showdetailsfunction(jsonObj,chainfunction);
				},
				onFailure: function() {
					showdetailsfunction();
				}
			}).send({'id': showid});
		}


		// update now next and previous show information
		var nowNextAndPrevious = function(livechainfunction) {
			var url = 'http://qradio.qthemusic.com/cgi-bin/qradio/currentshow.pl';
			var request = new Json.Remote(url, {
				onComplete: function(jsonObj) {
					setNowNextAndPrevious(jsonObj,livechainfunction);
				},
				onFailure: function() {
					nowNextAndPrevious.delay(10000);
				}
			}).send();
		}

		var setNowNextAndPrevious = function(nownextandprevious,livechainfunction) {
			var refreshIn = (nownextandprevious.refresh_in).toInt();
			nowNextAndPrevious.delay(refreshIn * 1000);
			if (isLive) {
				getShowDetails(nownextandprevious.now.BroadcastId,updateCurrentShow);
			}			
			getShowDetails(nownextandprevious.next.BroadcastId,updateNextShow);
			getShowDetails(nownextandprevious.previous.BroadcastId,updateLastShow);
		}




		var createLastTrackHTML = function(track) {
			var el = new Element('div');
			var table = new Element('table', {'width':'100%'}).injectInside(el);
			    table.setAttribute('style', 'text-align: left;, width: 100%;');			
			var tbody = new Element('tbody').injectInside(table);
			var tr = new Element('tr').injectInside(tbody);
			var td = new Element('td', {'width': '100%'}).injectInside(tr);
			    td.setHTML('<span style="text-align: left; display:block; width: 238px;"><strong>Track name</strong> - ' + track.track + ' <br/> <strong>Artist name</strong> - ' + track.artist + '</span>');
			if ($chk(track.itunesurl)) {	
			    var td2 = new Element('td').injectInside(tr);		
                var a = new Element('a', {'class': 'buyImg', 'id':'buy'+track.pid}).injectInside(td2);		
                /*
                    a.setProperty('href', track.itunesurl);			    
                    */
                var span = new Element('span').injectInside(a);	    
                    span.setHTML('buy');
            }
            
            var buy = new Element('div', {'class': 'buyDiv','id':track.pid}).injectInside(td);
            var buyTable = new Element('table', {'style':'padding-top: 8px; float: left;', 'cellspacing':'0', 'cellpadding':'0', 'border':'0'}).injectInside(buy);
                buyTable.setStyle('float','left');
            var buyTbody = new Element('tbody').injectInside(buyTable);
            var buyTr = new Element('tr').injectInside(buyTbody);
			if ($chk(track.amazonurl)) {
                var buyTdAmazon = new Element('td').injectInside(buyTr);         
                    buyTdAmazon.setHTML('<a href="' + track.amazonurl + '"><img width="82" height="24" border="0" src="/qradio/Content/IMG/amazon.gif"/></a>');
            }
			if ($chk(track.itunesurl)) {
                var buyTdITunes = new Element('td').injectInside(buyTr);         
                    buyTdITunes.setHTML('<a href="' + track.itunesurl + '"><img width="67" height="24" border="0" src="/qradio/Content/IMG/itunes.gif"/></a>');
            }  
                      
            var div = new Element('div');
                div.setHTML('<div class="pad_tb_10"><img width="280" height="2" src="/qradio/Content/IMG/hpline280.gif"/></div>').injectInside(el);                                                             
			return el;
		}
		
		// Now Playing
		var updateNowPlayingTimer;
		var updateNowPlayingRequest

		var updateNowPlaying = function() {		    
			$clear(updateNowPlayingTimer);
			var starttime = null;
			var url = 'http://qradio.qthemusic.com/cgi-bin/qradio/nowplaying.pl';
			updateNowPlayingRequest = new Json.Remote(url, {
				onComplete: function(jsonObj) {
					setNowPlaying(jsonObj);
				},
				onFailure: function() {
					$clear(updateNowPlayingTimer);
					updateNowPlayingTimer = updateNowPlaying.delay(10000);
				}
			}).send({'starttime' : starttime});
			
			
			
		}

		var setNowPlaying = function(nowplayingdata) {
						
			if ($chk(nowplayingdata.itemcode) && nowplayingdata.itemcode != pid) {
				if ($chk(nowplayingdata.itemcode)) {
					pid = nowplayingdata.itemcode;
				} else {
					pid = '';
				}					

					var thisTrack = new Track();
					if ($chk(nowplayingdata.artist) && $chk(nowplayingdata.song)) {
					    if ($chk(nowplayingdata.itunesurl)) {					    
					        $('now').setHTML('<span>Now Playing:</span> ' + '<a href="' + nowplayingdata.itunesurl + '" target="_blank">' + nowplayingdata.artist + ' - ' +  nowplayingdata.song + '</a>');
					    } else {					    
						    $('now').setHTML('<span>Now Playing:</span> ' + nowplayingdata.artist + ' - ' +  nowplayingdata.song);
						}
						thisTrack.artist = nowplayingdata.artist;
						thisTrack.track = nowplayingdata.song;
						thisTrack.itunesurl = nowplayingdata.itunesurl;
						thisTrack.amazonurl = nowplayingdata.amazonurl;						
						var nowArtist = $('nowArtist');
						var nowTrack = $('nowTrack');
						var cover = $('cover');
						var hpAmazon = $('hpAmazon');
						var hpItunes = $('hpItunes');												
						var tracks = $('tracks');
						if (nowArtist) {					    
					        nowArtist.setHTML('<strong>Artist</strong> ' + nowplayingdata.artist);
						}						
					    if (nowTrack) {					    
					        nowTrack.setHTML('<strong>Track</strong> ' + nowplayingdata.song);
						}	
						if (cover) {
                            if ($chk(nowplayingdata.image)) {
						        cover.setProperty('src', nowplayingdata.image);
						        image = nowplayingdata.image;
						    } else {
						        cover.setProperty('src', 'http://qradio.qthemusic.com/qradio/Content/IMG/player_track.gif');
						        image = nowplayingdata.image;						    
						    }
						}
						
						if (hpAmazon && hpItunes) {
                            if ($chk(nowplayingdata.amazonurl) || $chk(nowplayingdata.itunesurl)) 
                            {
						        if ($chk(nowplayingdata.amazonurl)) 
						        {
							        hpAmazon.setProperty('href', nowplayingdata.amazonurl);
							        hpAmazon.setStyle('visibility', 'visible');
						        } 
						        else 
						        {
							        hpAmazon.setStyle('visibility', 'hidden');
						        }
						        if ($chk(nowplayingdata.itunesurl)) 
						        {
							        hpItunes.setProperty('href', nowplayingdata.itunesurl);
							        hpItunes.setStyle('visibility', 'visible');
						        } 
						        else 
						        {
    							    hpItunes.setStyle('visibility', 'hidden');
	    					    }   
		        		    } 
					    }																							
					} else {
						$('now').setHTML('<span>Now Playing:</span>&nbsp;');
					}	
					
                    thisTrack.pid = nowplayingdata.itemcode; 
                                       		
					if ($chk(thisTrack.artist)) {
						if (lastTracks.length >= 1) {
							if (thisTrack.pid != lastTracks[0].pid) {
								if (lastTracks.length >= 5) {
									lastTracks.pop();	// remove the last track if more than 5
								}
								lastTracks.unshift(thisTrack);	// add the track to the array of tracks played
							}
						} else {						
							if (lastTracks.length >= 5) {
								lastTracks.pop();	// remove the last track if more than 5
							}
							lastTracks.unshift(thisTrack);	// add the track to the array of tracks played
						}				
					}   
				    if (tracks) {		        												                  		                    						
					    if ($chk(thisTrack.artist)) {
						    tracks.empty();
						    lastTracks.each(function(track,i) {
							    if (track) {
								    var el = createLastTrackHTML(track);								
								    el.injectInside(tracks);
							    }
						    });
    							
					    }		
					}
																																
																													
			}
			
			
			hideLastPlayed();
			
			var updatein = 10000;
			if ($chk(nowplayingdata.seconds_until_refresh)) {
				updatein = (nowplayingdata.seconds_until_refresh).toInt() * 1000;
			}
			$clear(updateNowPlayingTimer);
			updateNowPlayingTimer = updateNowPlaying.delay(updatein);
		}
				
		var hideLastPlayed = function() {
            lastTracks.each(function(track,i) {
                if (track) {                    
                
                    var trackBuy =  $(track.pid);
                    
                    var trackToBuy =  $('buy'+track.pid);
                    if (trackToBuy) { 
                        trackBuy.setStyle('display', 'none');
                        
                        trackToBuy.addEvent('click', function(e){                            
                            e = new Event(e);
                            if (trackBuy.getStyle('display') ==  'none') {
                                trackBuy.setStyle('display', 'block');
                            } else {
                                trackBuy.setStyle('display', 'none');
                            }
                            e.stop();
                        });                        		                
                    }
                }
            });	
		}

		// Setup mootools events and slides
		window.addEvent('domready',function() {
            updateNowPlaying();
            nowNextAndPrevious();

                       

		});
