		var userCookie;
		var volumeSteps = 75;

		var wmpIsPlaying = 0;
		var flashIsPlaying = 0;
		var totalSeconds = 0;
//		var playerType = 'wmp';
		var playerType = 'flash';
		var canPause = 0;
		var isPaused = 0;
		var isLive = 1;
		var currentShowStartSeconds = 0;
		var archiveShowLengthSeconds = 0;
		var volume = 100;
		var swf = null;

		var tickTimer;
		var runHeartbeat = 0;

		// Google Analytics tracker object
		var pageTracker;

	
		function wmpinit(url) {
			if ((navigator.appName.indexOf("Microsoft") != -1) && (navigator.userAgent.toLowerCase().indexOf("win") != -1)) {
				if (wmpradioplayer && wmpradioplayer.controls != null) {
					wmpradioplayer.url = url;
					play();
					setVolume(volume);
				} else {
					// windows media is to old to run
				}
			} else {
				// not on ie, better do something here
			}
		}

		function flashinit(url,file,skipto) {
			if ((navigator.appName.indexOf("Microsoft") != -1) && (navigator.userAgent.toLowerCase().indexOf("win") != -1)) {
				window.qflashplayer = document.getElementById('qflashplayer');
			}
			if (FlashDetect.installed && FlashDetect.major >= 9) {
				swf = getSWF("qflashplayer");
				if (file) {
					swf.setRadioStream(url,file);
				} else {
					swf.setRadioUrl(url);
				}
				play(skipto);
				setVolume(volume);
			} else {
				alert("You need to upgrade your version of Flash to enjoy Q Radio");
			}
		}

		function tick() {
			if (wmpIsPlaying) {
				$('statustext').setText(wmpStatusMessage(wmpradioplayer.playState));
			} else if (flashIsPlaying) {
				$('statustext').setText(swf.playState());
			}
			if ((wmpIsPlaying && wmpradioplayer.playState == 3) || flashIsPlaying) {
				if (isLive) {
					$('timetext').setText(formatTime(totalSeconds));
				} else {
					$('timetext').setHTML(formatTime(totalSeconds) + ' <span class="lightgreytxt">/ ' + formatTime(archiveShowLengthSeconds) + '</span>');
				}
				if (flashIsPlaying) {
					totalSeconds = swf.currentPosition();
				} else {
					totalSeconds = wmpradioplayer.controls.currentPosition;
				}
			}
		}
	
		function play(skipto) {
			if (playerType == "wmp" && window.wmpradioplayer) {
				if (wmpIsPlaying > 0) {
					if (canPause) {
						pause();
					} 
				} else {
					if (flashIsPlaying) {
						stop();
					}
					wmpradioplayer.controls.play();
					wmpIsPlaying = 1;
					setVolume(volume);
					totalSeconds = wmpradioplayer.controls.currentPosition;
					// totalSeconds = 0;	
					updateNowPlaying();
					tickTimer = tick.periodical(1000);
					if (!isLive) {
						canPause = 1;
						isPaused = 0;
					}
				}
			} else if (playerType == "flash") { 
				if (flashIsPlaying > 0) {
					if (canPause) {
						pause();
					}
				} else {			
					if (wmpIsPlaying) {
						stop();
					}
					if (skipto) {
						totalSeconds = (skipto).toInt();
					} else {
						totalSeconds = 0;
					}
					swf.playradio(skipto);
					setVolume(volume);
					flashIsPlaying = 1;
					// totalSeconds = swf.currentPosition();
					updateNowPlaying();
					tickTimer = tick.periodical(1000);
					if (!isLive) {
						canPause = 1;
					}
				}
			}
		}

		function pause() {
			if (playerType == "wmp" && window.wmpradioplayer && wmpIsPlaying) {
				if (flashIsPlaying) {
					stop();
				}
				wmpradioplayer.controls.pause();
				tick();
				if (wmpIsPlaying ==2) {
					wmpIsPlaying = 1;
					updateNowPlaying();
				} else {
					wmpIsPlaying = 2;
					$clear(updateNowPlayingTimer);
				}
			} else if (playerType == "flash" && flashIsPlaying) {
				if (wmpIsPlaying) {
					stop();
				}
				swf.pauseradio();
				tick();
				if (flashIsPlaying == 2) {
					flashIsPlaying = 1;
					updateNowPlaying();
				} else {
					flashIsPlaying = 2;
					$('statustext').setText("Paused");
					$clear(updateNowPlayingTimer);
				}
			}
		}
	
		function stop() {
			if (window.wmpradioplayer && wmpIsPlaying) {
				wmpradioplayer.controls.stop();
				wmpIsPlaying = 0;
				$clear(tickTimer);
				$('statustext').setText(wmpStatusMessage(wmpradioplayer.playState));
				canPause = 0;
			} else if (flashIsPlaying) {
				swf.stopradio();
				flashIsPlaying = 0;
				$clear(tickTimer);
				$('statustext').setText("Stopped");
				canPause = 0;
			}
			if (!isLive) {
				$clear(updateNowPlayingTimer);
			}
		}

		function forward5() {
			if (window.wmpradioplayer && wmpIsPlaying) {
				wmpradioplayer.controls.currentPosition = wmpradioplayer.controls.currentPosition + 300;
				totalSeconds = wmpradioplayer.controls.currentPosition;
				updateNowPlaying();
			} else if (flashIsPlaying) {
				totalSeconds = swf.currentPosition() + 300;
				updateNowPlaying();
				swf.seek(totalSeconds);
				totalSeconds = swf.currentPosition();
			}
		}



		function setVolume(percent) {
			if (flashIsPlaying) {
				swf.setVolume(percent / 100);
			} else if (wmpIsPlaying) {
				wmpradioplayer.settings.volume = percent;
			} 
			volume = percent;
		}


		function getSWF(movieName) {
			if (navigator.appName.indexOf("Microsoft") != -1) {
				return window[movieName];
			} else {
				return document[movieName];
			}
		}

		function startPlayerLive() {
			if (playerType == 'wmp') {
				isLive = 1;
				wmpinit('http://metafiles.bauerradio.com/qradio.asx');
				$('forward').setStyle('display','none');
				setImage($('playbutton'),'/images/play.png');
				$('statustext').setStyle('left','100px');
			} else if (playerType == 'flash') {
				isLive = 1;
				flashinit('http://tx-qradio.bauerdev.com/qradio',null);
				$('forward').setStyle('display','none');
				setImage($('playbutton'),'/images/play.png');
				$('statustext').setStyle('left','100px');
			}
		}

		function startPlayerListenAgain(wmpurl,flashserver,flashfile,skipto) {
			if (playerType == 'wmp') {
				isLive = 0;
				wmpinit(wmpurl);
				$('forward').setStyle('display','block');
				setImage($('playbutton'),'/images/playpause.png');
				$('statustext').setStyle('left','150px');
			} else if (playerType == 'flash') {
				isLive = 0;
				flashinit(flashserver,flashfile,skipto);
				$('forward').setStyle('display','block');
				setImage($('playbutton'),'/images/playpause.png');
				$('statustext').setStyle('left','150px');
			}
		}

		
		// format a a string to HH:MM:SS format.
		// we'll fake it using a Date object.
		function formatTime(totalSeconds) {
			var tempDate = new Date(totalSeconds * 1000);
			return (tempDate.getUTCHours() < 10 ? "0" : "") + tempDate.getUTCHours() + ":" + (tempDate.getUTCMinutes() < 10 ? "0" : "") + tempDate.getUTCMinutes() + ":" + (tempDate.getUTCSeconds() < 10 ? "0" : "") + tempDate.getUTCSeconds();
		}

		function wmpStatusMessage(playState) {
			var message = "";
			switch(playState) {
				case 1:
					message = "Stopped";
					break;
				case 2:
					message = "Paused";
					break;
				case 3:
					message = "Playing";
					break;
				case 6:
					message = "Buffering";
					break;
				case 7:
					message = "Waiting";
					break;
				case 8:
					message = "Finished";
					break;
				case 9:
					message = "Preparing";
					break;	
				case 10:
					message = "Ready";
					break;
				case 11:
					message = "Reconnecting";
					break;
				default:
					break;
			}
			return message;
		}


		var loadListenLive = function() {
			currentShowStartSeconds = 0;
			startPlayerLive();
			track_webtrends("LIVE");
			update_ads();
		}

		
		var loadListenAgainShow = function(showdetails,skipto) {
			currentShowStartSeconds = showdetails.StartSeconds;
			archiveShowLengthSeconds = showdetails.FinishMinutes - showdetails.StartMinutes;
			startPlayerListenAgain(showdetails.WMAFile,showdetails.MP3Server,showdetails.MP3File,skipto);
			track_webtrends(showdetails.BroadcastId + ' - ' + showdetails.Title);
			update_ads();
		}

		var loadShow = function(showid,skipto) {
			stop();
			if (showid) {
				isLive = 0;
				getShowDetails(showid,updateCurrentShow,loadListenAgainShow,skipto);
			} else {
				isLive = 1;
				nowNextAndPrevious(loadListenLive);
			}
		}

		


		var pid = '';	// current track pid
		var broadcastid = '';
		var showid = '';
		
		var kvItemCode = '';
		var kvPid = '';	
		var kvArtist = '';	
		var kvTrack = '';

	
		// 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();

	
		// voting
		var trackvoted = 0;
		var trackvotedslide;

	
		// pngfix puts png's into spans for ie 6 and 5.5, we need to handle the change differently for them.
		// pass in the element you need to change, and the src of the image
		// Rob Price
		var setImage = function(id,src) {
			if (id.getTag() == 'span') {
				var tempImage = new Image();
				tempImage.src = src;
				id.setStyle('width',tempImage.width + 'px');
				id.setStyle('height',tempImage.height + 'px');
				id.setStyle('filter',"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')");
			} else {
				id.setProperty('src',src);
			}
		}


		var trackVote = function(rating) {
			if (!trackvoted) {
				track_webtrends("Track voting - " + pid);			
				trackvotedslide.slideOut().chain(function() {
					trackvoted = 1;
					var url = '/cgi-bin/starrating.pl';
					var request = new Json.Remote(url, {
						onComplete: function(jsonObj) {
							setTrackVote(jsonObj);
						},
						onFailure: function() {
							setTrackVote();
						}
					}).send({'pid':  pid,'rating': rating, 'showid': showid, 'broadcastid': broadcastid});						
				});
			}
		}

		var setTrackVote = function(trackvotedata) {
			setImage($('yourstar1'),'images/whitestar.png');
			setImage($('yourstar2'),'images/whitestar.png');
			setImage($('yourstar3'),'images/whitestar.png');
			setImage($('yourstar4'),'images/whitestar.png');
			setImage($('yourstar5'),'images/whitestar.png');
			setImage($('theirstar1'),'images/whitestar.png');
			setImage($('theirstar2'),'images/whitestar.png');
			setImage($('theirstar3'),'images/whitestar.png');
			setImage($('theirstar4'),'images/whitestar.png');
			setImage($('theirstar5'),'images/whitestar.png');
			if ($chk(trackvotedata)) {
				if ($chk(trackvotedata.yousaid)) {
					for (var i=1; i <= trackvotedata.yousaid; i++) {
						var imageid = 'yourstar' + i;
						setImage($(imageid),'images/blackstar.png');
					}
				}
				if ($chk(trackvotedata.theysaid)) {
					for (var i=1; i <= trackvotedata.theysaid; i++) {
						var imageid = 'theirstar' + i;
						setImage($(imageid),'images/blackstar.png');
					}
				}
			}
			$('ratestars').setStyle('display','none');
			$('yourstars').setStyle('display','block');
			$('theirstars').setStyle('display','block');
			trackvotedslide.slideIn().chain(function() {
				update_ads();
			});
		};


		// Radio Player Slide
		var radioplayerslidehidden = 0;
                var radioplayerslide;

		var nextshowfade;
		var lastshowfade;

		var updateCurrentShow = function(showdetails,chainfunction,skipto) {
			if (radioplayerslidehidden) {
				updateCurrentShowb(showdetails,chainfunction,skipto);
			} else {
				$('liveplayerbar').setStyle('visibility','hidden');
				radioplayerslide.slideOut().chain( function() {
					radioplayerslidehidden = 1;
					updateCurrentShowb(showdetails,chainfunction,skipto);
				});
			}
		}
		
		var updateCurrentShowb = function(showdetails,chainfunction,skipto) {
			kvPid = showdetails.ShowId;
			showid = showdetails.ShowId;
			broadcastid = showdetails.BroadcastId;
			if ($chk(showdetails.Title)) {
				$('currentshowtitle').setHTML(showdetails.Title);
			} else {
				$('currentshowtitle').setHTML('Q Radio');
			} 
			if ($chk(showdetails.Start)) {
				$('currentshowtime').setHTML(' - ' + showdetails.Start);
			} else {
				$('currentshowtime').setHTML('');
			}
			if ($chk(showdetails.Dj)) {
				$('currentshowdj').setHTML(showdetails.Dj);
			} else {
				$('currentshowdj').setHTML('Q Magazine');
			}
			if ($chk(showdetails.MediumSynopsis)) {
				$('currentshowbody').setHTML(showdetails.MediumSynopsis);
			} else {
				$('currentshowbody').setHTML('');
			}
			if ($chk(showdetails.PictureUrl)) {
				$('currentshowdjimage').setAttribute('src',showdetails.PictureUrl);
			} else {
				$('currentshowdjimage').setAttribute('src','/images/djs/player_show_big.gif');
			}
			if ($chk(showdetails.Url)) {
				$('currentshowdjimagelink').setAttribute('href',showdetails.Url);
				$('currentshowdjimagelink').setAttribute('target','_blank');
				$('currentshowlink').setAttribute('href',showdetails.Url);
				$('currentshowlink').setAttribute('target','_blank');
			} else {
				$('currentshowdjimagelink').removeAttribute('href');
				$('currentshowdjimagelink').removeAttribute('target');
				$('currentshowlink').removeAttribute('href');
				$('currentshowlink').removeAttribute('target');
			}

			if (isLive) {
				$('listenliveheader').setStyle('background',"transparent url('images/playertop_listenlive.gif') no-repeat");
				$('listenlivelink').setStyle('display', 'none');
			} else {
				$('listenliveheader').setStyle('background',"transparent url('images/playertop_listenagain.gif') no-repeat");
				$('listenlivelink').setStyle('display', 'block');
			}

			$('radioplayer').setStyle('visibility','visible');
			radioplayerslide.slideIn().chain(function () {
				radioplayerslidehidden = 0;
				$('liveplayerbar').setStyle('visibility','visible');
				if ($defined(chainfunction)) {
					chainfunction(showdetails,skipto);
				}
			});
		}

		var updateNextShow = function(showdetails) {
			nextshowfade.start(1,0).chain(function() {
				var title = '';
				if ($chk(showdetails.Title)) {
					title = '<strong>Show:</strong> ' + showdetails.Title;
				} 
				if ($chk(showdetails.Start)) {
					title = title + ' - ' + showdetails.Start;
				}
				if ($chk(showdetails.Dj)) {
					title = title + ' | <strong>DJ:</strong> ' + showdetails.Dj;
				}
				$('nextshowtitle').setHTML(title);
				if ($chk(showdetails.ShortSynopsis)) {
					$('nextshowbody').setHTML(showdetails.ShortSynopsis);
					} else {
					$('nextshowbody').setHTML('');
				}
				if ($chk(showdetails.PictureUrl)) {
					$('nextshowimage').setAttribute('src',showdetails.PictureUrl);
				} else {
					$('nextshowdjimage').setAttribute('src','images/djs/player_show_65by65.gif');
				}
				nextshowfade.start(0,1)
			});
		}


		var listenagainbuttondelay;

		var updateLastShow = function(showdetails) {
			lastshowfade.start(1,0).chain(function() {
				var title = '';
				if ($chk(showdetails.Title)) {
					title = '<strong>Show:</strong> ' + showdetails.Title;
				} 
				if ($chk(showdetails.Start)) {
					title = title + ' - ' + showdetails.Start;
				}
				if ($chk(showdetails.Dj)) {
					title = title + ' | <strong>DJ:</strong> ' + showdetails.Dj;
				}
				$('lastshowtitle').setHTML(title);
				if ($chk(showdetails.ShortSynopsis)) {
					$('lastshowbody').setHTML(showdetails.ShortSynopsis);
				} else {
					$('lastshowbody').setHTML('');
				}
				if ($chk(showdetails.PictureUrl)) {
					$('lastshowimage').setAttribute('src',showdetails.PictureUrl);
				} else {
					$('lastshowdjimage').setAttribute('src','images/djs/player_show_65by65.gif');
				}
				$('listenagainlastshow').setStyle('visibility','hidden');
				$('listenagainlastshow').removeEvents();
				$clear(listenagainbuttondelay);
				if ($chk(showdetails.ListenAgain)) {
					if (showdetails.ListenAgain == true) {
						var listenagaindelay = showdetails.FinishMinutes - showdetails.NowSeconds + (20*60);
						if (listenagaindelay < 0) {
							delay = 0;
						}
    						listenagainbuttondelay = (function(){
							$('listenagainlastshow').setStyle('visibility','visible');
							$('listenagainlastshow').setStyle('cursor','pointer');
                        				$('listenagainlastshow').addEvent('click', function(e) {
                                				loadShow(showdetails.BroastcastId);
                        				});
						}).delay(listenagaindelay*1000, this);
					}
				}
				lastshowfade.start(0,1);
			});
		}

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


		// update now next and previous show information
		var nowNextAndPrevious = function(livechainfunction) {
			var url = '/perlrun/currentshow.pl';
			var request = new Json.Remote(url, {
				onComplete: function(jsonObj) {
					setNowNextAndPrevious(jsonObj,livechainfunction);
				},
				onFailure: function() {
					nowNextAndPrevious.delay(20000);
				}
			}).send();
		}

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




		var createLastTrackHTML = function(track) {
			var el = new Element('div');
			var trackel = new Element('div', {'class': 'lastplayedtrack'}).injectInside(el);

			var lastartist = new Element('div', {'class': 'lastplayedtracktrack'}).setHTML(('<strong>Artist -</strong> ' + track.artist)).injectInside(trackel);

			var lasttrack = new Element('div', {'class': 'lastplayedtrackartist'}).setHTML(('| &nbsp;&nbsp;<strong>Track -</strong> ' + track.track)).injectAfter(lastartist);

//			var buyel = new Element('div', {'class': 'buylastplayedtrack', 'id': track.pid});
//			var buytable = new Element('table').setHTML('<tr><td>Amazon</td><td>iTunes</td></tr>').injectInside(buyel);
//			buyel.injectAfter(trackel);
//			var lastbuy = new Element('div', {
//				'class': 'lastplayedtrackbuy',
//				'id' : ('buy_' + track.pid)
//			}).setHTML('Buy').injectAfter(lastartist);
			
			return el;
		}
		



		// Now Playing
		var playinginfoslidehidden = 0;
                var playinginfoslide;

		var updateNowPlayingTimer;
		var updateNowPlayingRequest

		var updateNowPlaying = function() {
/*
			$clear(updateNowPlayingTimer);
			var starttime = null;
			if (!isLive) {
				starttime = currentShowStartSeconds + totalSeconds;
			}
			var url = '/perlrun/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)) {
					if (nowplayingdata.itemcode.indexOf('QID') == 0) {
						pid = '';
					} else {
						pid = nowplayingdata.itemcode;
					}
				} else {
					pid = '';
				}
				kvItemCode = pid;
				if ($chk(nowplayingdata.artist)) {
					if (playinginfoslidehidden != 1) {
						playinginfoslide.slideOut().chain(function() {
							playinginfoslidehidden = 1;
							setNowPlaying2(nowplayingdata);
						});
					} else {
						setNowPlaying2(nowplayingdata);
					}
				} else {
					if (playinginfoslidehidden != 1) {
						playinginfoslide.slideOut().chain(function() {
							playinginfoslidehidden = 1;
						});
					}
				}
			}
			if (!($chk(nowplayingdata.itemcode)) || !($chk(nowplayingdata.artist))) {
					if (playinginfoslidehidden != 1) {
						playinginfoslide.slideOut().chain(function() {
							playinginfoslidehidden = 1;
						});
					}
			}
			var updatein = 10000;
			if ($chk(nowplayingdata.seconds_until_refresh)) {
				updatein = (nowplayingdata.seconds_until_refresh).toInt() * 1000;
			}
			$clear(updateNowPlayingTimer);
			updateNowPlayingTimer = updateNowPlaying.delay(updatein);
		}


		var setNowPlaying2 = function(nowplayingdata) {
			var thisTrack = new Track();
			if ($chk(nowplayingdata.artist)) {
				$('artist').setHTML('<strong>Artist -</strong> ' + nowplayingdata.artist + '<br />');
				thisTrack.artist = nowplayingdata.artist;
				kvArtist = nowplayingdata.artist;
			} else {
				$('artist').setHTML('');
			}			
			if ($chk(nowplayingdata.song)) {
				$('track').setHTML('<strong>Track -</strong> ' + nowplayingdata.song + '<br />');
				$('coverimage').setProperty('alt', nowplayingdata.song);
				thisTrack.track = nowplayingdata.song;
				kvTrack = nowplayingdata.song;
			} else {
				$('track').setHTML('');
				$('coverimage').setProperty('alt', '');
			}
			var image = '/images/tracks/player_track.gif';
			if ($chk(nowplayingdata.image)) {
				$('coverimage').setProperty('src', nowplayingdata.image);
				image = nowplayingdata.image;
			} else {
				$('coverimage').setProperty('src', image);
			}
			thisTrack.image = image;

			thisTrack.pid = nowplayingdata.itemcode;

			if ($chk(nowplayingdata.amazonurl) || $chk(nowplayingdata.itunesurl)) {
				if ($chk(nowplayingdata.amazonurl)) {
					$('amazonlink').setProperty('href', nowplayingdata.amazonurl);
					$('amazonbuy').setStyle('visibility', 'visible');
				} else {
					$('amazonbuy').setStyle('visibility', 'hidden');
				}
				if ($chk(nowplayingdata.itunesurl)) {
					$('ituneslink').setProperty('href', nowplayingdata.itunesurl);
					$('itunesbuy').setStyle('visibility', 'visible');
				} else {
					$('itunesbuy').setStyle('visibility', 'hidden');
				}
				$('buy').setStyle('visibility', 'visible');
			} else {
				$('buy').setStyle('visibility', 'hidden');
			}

			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
				}				
			}

			trackvoted = 0;
			$('ratestars').setStyle('display','block');
			$('yourstars').setStyle('display','none');
			$('theirstars').setStyle('display','none');
			new Asset.images([image], {onComplete: function() {
				playinginfoslide.slideIn().chain(function() {
					playinginfoslidehidden = 0;

					if ($chk(thisTrack.artist)) {
						// fade out the last played tracks, update tracks and fade back in
						var lastplayedfade = new Fx.Style('lastplayedtracks','opacity');
						lastplayedfade.set(0);
						lastplayedfade.start(1,0).chain(function() {
							$('lastplayedtracks').empty();
							lastTracks.each(function(track,i) {
								if (track) {
									var el = createLastTrackHTML(track);
									el.injectInside('lastplayedtracks');
								}
							});
							lastplayedfade.start(0,1);
						});
					}
				});
			}});
		};

		
		// ad code
		var update_adleaderboard = function(kvItemCode,kvPid,kvArtist,kvTrack) {
			var link = '/adtech/banner.html?kvItemCode=' + ($defined(kvItemCode) ? escape(kvItemCode) : '') + '&kvPid=' + ($defined(kvPid) ? escape(kvPid) : '') + '&kvArtist=' + ($defined(kvArtist) ? escape(kvArtist) : '') + '&kvTrack=' + ($defined(kvTrack) ? escape(kvTrack) : '');
			$('ad_leaderboard_iframe').setAttribute('src',link);
		}

		var update_admpu = function(kvItemCode,kvPid,kvArtist,kvTrack) {
			var link = '/adtech/mpu.html?kvItemCode=' + ($defined(kvItemCode) ? escape(kvItemCode) : '') + '&kvPid=' + ($defined(kvPid) ? escape(kvPid) : '') + '&kvArtist=' + ($defined(kvArtist) ? escape(kvArtist) : '') + '&kvTrack=' + ($defined(kvTrack) ? escape(kvTrack) : '');
			$('ad_mpu_iframe').setAttribute('src',link);
		}

		var update_adsky = function(kvItemCode,kvPid,kvArtist,kvTrack) {
			var link = '/adtech/sky.html?kvItemCode=' + ($defined(kvItemCode) ? escape(kvItemCode) : '') + '&kvPid=' + ($defined(kvPid) ? escape(kvPid) : '') + '&kvArtist=' + ($defined(kvArtist) ? escape(kvArtist) : '') + '&kvTrack=' + ($defined(kvTrack) ? escape(kvTrack) : '');
			$('ad_skyscraper_iframe').setAttribute('src',link);
		}



		var update_ads = function() {
			update_adleaderboard(kvItemCode,kvPid,kvArtist,kvTrack);
			update_admpu(kvItemCode,kvPid,kvArtist,kvTrack);
			update_adsky(kvItemCode,kvPid,kvArtist,kvTrack);
		}

		var track_webtrends = function(pagename) {
			track_google(pagename);

			WT.cg_s = pagename;
			dcsTag();
		}

		var track_google = function(pagename) {
			pageTracker._trackEvent("Radio Player", pagename, "The show played on Q Radio");
			pageTracker._trackPageview();
		}

		
		// get url parameters - http://www.netlobo.com/url_query_string_javascript.html
		function gup( name ){  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  var regexS = "[\\?&]"+name+"=([^&#]*)";  var regex = new RegExp( regexS );  var results = regex.exec( window.location.href );  if( results == null )    return "";  else    return results[1];}


		var heartbeat = function() {
			var url = '/cgi-bin/heartbeat.pl';
			var request = new Json.Remote(url, {
				onComplete: function(jsonObj) {
				},
				onFailure: function() {
				}
			}).send({'pid':  pid, 'showid': showid, 'broadcastid': broadcastid});
		}


		var whitestars = function() {
			setImage($('ratestar1'),'images/whitestar.png');
			setImage($('ratestar2'),'images/whitestar.png');
			setImage($('ratestar3'),'images/whitestar.png');
			setImage($('ratestar4'),'images/whitestar.png');
			setImage($('ratestar5'),'images/whitestar.png');
		}

		// start the player once everything has loaded
		window.addEvent('load',function() {
			if (runHeartbeat) {
				heartbeat();
				heartbeat.periodical(60*1000);	// beat once a minute
			}
			var loadshowid = gup('id');
			var skipto = gup('skipto');
			if (loadshowid) {
				if (skipto) {
					loadShow(loadshowid,skipto);
				} else {
					loadShow(loadshowid);
				}
			} else {
				startPlayerLive();
			}
		});

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

			// get the user cookie
			userCookie = new Hash.Cookie('RadioPlayer', {duration: 365});

			// get the initial volume from the user cookie
			var tvolume = userCookie.get("volume");
			if ($defined(tvolume)) {
				if (tvolume >= 0 && tvolume <= 100) {
					volume = tvolume;
				} else {
					volume = 75; 	// volume 75%
				}
			} else {
				volume = 75; // volume 75%
			}


			// listen live link
			$('listenlivelink').addEvent('click', function(e) {
				loadShow();
			});


			// radio player controls
			$('playbutton').addEvent('click', function(e) {
				play();
			});
			$('stop').addEvent('click', function(e) {
				stop();
			});
			$('forward').addEvent('click', function(e) {
				forward5();
			});
			

			// track voting
			$('ratestars').setStyle('display','block');
			$('yourstars').setStyle('display','none');
			$('theirstars').setStyle('display','none');

			$('ratestars').setStyle('cursor','pointer');

			trackvoted = 0;
			trackvotedslide = new Fx.Slide('starrating', {
				mode: 'horizontal',
				wait: false
			});
			$('ratestar1').addEvent('click', function(e) {
				trackVote('1');
			});
			$('ratestar2').addEvent('click', function(e) {
				trackVote('2');
			});
			$('ratestar3').addEvent('click', function(e) {
				trackVote('3');
			});
			$('ratestar4').addEvent('click', function(e) {
				trackVote('4');
			});
			$('ratestar5').addEvent('click', function(e) {
				trackVote('5');
			});

			$('ratestar1').addEvent('mouseover',function(e) {
				setImage($('ratestar1'),'images/blackstar.png');
				setImage($('ratestar2'),'images/whitestar.png');
				setImage($('ratestar3'),'images/whitestar.png');
				setImage($('ratestar4'),'images/whitestar.png');
				setImage($('ratestar5'),'images/whitestar.png');
			});
			$('ratestar2').addEvent('mouseover',function(e) {
				setImage($('ratestar1'),'images/blackstar.png');
				setImage($('ratestar2'),'images/blackstar.png');
				setImage($('ratestar3'),'images/whitestar.png');
				setImage($('ratestar4'),'images/whitestar.png');
				setImage($('ratestar5'),'images/whitestar.png');
			});
			$('ratestar3').addEvent('mouseover',function(e) {
				setImage($('ratestar1'),'images/blackstar.png');
				setImage($('ratestar2'),'images/blackstar.png');
				setImage($('ratestar3'),'images/blackstar.png');
				setImage($('ratestar4'),'images/whitestar.png');
				setImage($('ratestar5'),'images/whitestar.png');
			});
			$('ratestar4').addEvent('mouseover',function(e) {
				setImage($('ratestar1'),'images/blackstar.png');
				setImage($('ratestar2'),'images/blackstar.png');
				setImage($('ratestar3'),'images/blackstar.png');
				setImage($('ratestar4'),'images/blackstar.png');
				setImage($('ratestar5'),'images/whitestar.png');
			});
			$('ratestar5').addEvent('mouseover',function(e) {
				setImage($('ratestar1'),'images/blackstar.png');
				setImage($('ratestar2'),'images/blackstar.png');
				setImage($('ratestar3'),'images/blackstar.png');
				setImage($('ratestar4'),'images/blackstar.png');
				setImage($('ratestar5'),'images/blackstar.png');
			});
			$('ratestar1').addEvent('mouseout',function(e) {
				whitestars();
			});
			$('ratestar2').addEvent('mouseout',function(e) {
				whitestars();
			});
			$('ratestar3').addEvent('mouseout',function(e) {
				whitestars();
			});
			$('ratestar4').addEvent('mouseout',function(e) {
				whitestars();
			});
			$('ratestar5').addEvent('mouseout',function(e) {
				whitestars();
			});


			// hide the player control bar
			$('liveplayerbar').setStyle('visibility','hidden');

			// bounce the radio player 
			radioplayerhidden = 1;
			radioplayerslide = new Fx.Slide('radioplayer', {	
				mode: 'horizontal'
			});
			radioplayerslide.hide();

			// bounce the now playing information
			playinginfoslidehidden = 1;
			playinginfoslide = new Fx.Slide('playinginfo', {	
				mode: 'vertical',
				wait: 'true'
			});
			playinginfoslide.hide();

			lastshowfade = new Fx.Style('lastshow','opacity');
			lastshowfade.set(0);
			nextshowfade = new Fx.Style('nextshow','opacity');
			nextshowfade.set(0);

			// update show information
			nowNextAndPrevious()			


			// volume control
			var mySlide1 = new Slider($('livevolume'), $('livevolumeknob'), {
				steps: volumeSteps,
				onChange: function(step) {
					volume = Math.round((100/volumeSteps) * step);
					setVolume(volume);
					userCookie.set('volume',volume);
				}
			}).set(Math.round((volumeSteps / 100) * volume));


			// listen again links



		var show_289 = new Fx.Slide('show_289', {mode: 'vertical'});
		show_289.hide();
		$('show_289_toggle').addEvent('click', function(e) {
			e = new Event(e);
			show_289.toggle();
			e.stop();
		});


		var show_227 = new Fx.Slide('show_227', {mode: 'vertical'});
		show_227.hide();
		$('show_227_toggle').addEvent('click', function(e) {
			e = new Event(e);
			show_227.toggle();
			e.stop();
		});

		var show_79 = new Fx.Slide('show_79', {mode: 'vertical'});
		show_79.hide();
		$('show_79_toggle').addEvent('click', function(e) {
			e = new Event(e);
			show_79.toggle();
			e.stop();
		});



		var show_232 = new Fx.Slide('show_232', {mode: 'vertical'});
		show_232.hide();
		$('show_232_toggle').addEvent('click', function(e) {
			e = new Event(e);
			show_232.toggle();
			e.stop();
		});


		var show_224 = new Fx.Slide('show_224', {mode: 'vertical'});
		show_224.hide();
		$('show_224_toggle').addEvent('click', function(e) {
			e = new Event(e);
			show_224.toggle();
			e.stop();
		});





	
			$('b29394').addEvent('click', function(e) {
				loadShow('29394');
			});
	

	
			$('b29395').addEvent('click', function(e) {
				loadShow('29395');
			});
	

	
		
			$('b29378').addEvent('click', function(e) {
				loadShow('29378');
			});	
		
			$('b29384').addEvent('click', function(e) {
				loadShow('29384');
			});	
		
			$('b29403').addEvent('click', function(e) {
				loadShow('29403');
			});	
		
			$('b29409').addEvent('click', function(e) {
				loadShow('29409');
			});	
		
			$('b29415').addEvent('click', function(e) {
				loadShow('29415');
			});	
		
	

	
			$('b29388').addEvent('click', function(e) {
				loadShow('29388');
			});
	

	
		
			$('b29386').addEvent('click', function(e) {
				loadShow('29386');
			});	
		
			$('b29392').addEvent('click', function(e) {
				loadShow('29392');
			});	
		
	

	
		
			$('b29374').addEvent('click', function(e) {
				loadShow('29374');
			});	
		
			$('b29380').addEvent('click', function(e) {
				loadShow('29380');
			});	
		
			$('b29399').addEvent('click', function(e) {
				loadShow('29399');
			});	
		
			$('b29405').addEvent('click', function(e) {
				loadShow('29405');
			});	
		
			$('b29411').addEvent('click', function(e) {
				loadShow('29411');
			});	
		
	

	
			$('b29397').addEvent('click', function(e) {
				loadShow('29397');
			});
	

	
			$('b29389').addEvent('click', function(e) {
				loadShow('29389');
			});
	

	
		
			$('b29377').addEvent('click', function(e) {
				loadShow('29377');
			});	
		
			$('b29383').addEvent('click', function(e) {
				loadShow('29383');
			});	
		
			$('b29402').addEvent('click', function(e) {
				loadShow('29402');
			});	
		
			$('b29408').addEvent('click', function(e) {
				loadShow('29408');
			});	
		
			$('b29414').addEvent('click', function(e) {
				loadShow('29414');
			});	
		
	

	
			$('b29387').addEvent('click', function(e) {
				loadShow('29387');
			});
	

	
		
			$('b29376').addEvent('click', function(e) {
				loadShow('29376');
			});	
		
			$('b29382').addEvent('click', function(e) {
				loadShow('29382');
			});	
		
			$('b29401').addEvent('click', function(e) {
				loadShow('29401');
			});	
		
			$('b29407').addEvent('click', function(e) {
				loadShow('29407');
			});	
		
			$('b29413').addEvent('click', function(e) {
				loadShow('29413');
			});	
		
	

	
			$('b29396').addEvent('click', function(e) {
				loadShow('29396');
			});
	

	
			$('b29393').addEvent('click', function(e) {
				loadShow('29393');
			});
	




			// setup webtrends
			dcsVar();
			dcsMeta();
			dcsFunc("dcsAdv");
			WT.cg_n = "Q Radio Player"

			// setup google analytics
			try {
				pageTracker = _gat._getTracker("UA-11880293-13");

				pageTracker._setDomainName(".qthemusic.com");   // Sets domain used for storage of cookie. Leading '.' is required.
				pageTracker._setAllowHash(false);			// Stops tracker from using hostname to identify user.
			} catch(err) {}

			// register the page impression
			track_webtrends();

			// show ads
			update_ads();

		});
