var NMERadio = function() {
	return {
		CURRENT_SONG_URL: '/radio/ajax/song-fragment/',
		CURRENT_SONG_CLASS: 'radio_current_song',
		PLAYER_LOADING_ID: 'radio_player_loading',
		PLAYER_FRAGMENT_ID: 'radio_player_fragment',
		PLAYER_FRAGMENT_URL: '/radio/ajax/player-fragment/',
		PLAYER_LINK_URL: '/radio/player/',
		PLAYER_LINK_CLASS: 'radio_pop_up_player',
		PLAYER_WINDOW_HEIGHT: 487,
		WIDGET_FRAGMENT_URL: '/radio/ajax/song-fragment/',
		WIDGET_FRAGMENT_CLASS: 'radio_widget_fragment',
		WE_PLAYED_FRAGMENT_URL: '/radio/ajax/we-played-fragment/',
		WE_PLAYED_FRAGMENT_CLASS: 'we_played_fragment',
		AD_IFRAME_SRC: '/radio/banner/',
		REFRESH_DELAY: 30*1000,
		
		init: function() { 
			NMERadio.setupPlayerLinks();
			NMERadio.refreshWePlayed();
			NMERadio.initCurrentSong(); 
		},
		
		initCurrentSong: function() {
			var ts = Number( NMERadio.getCookie( 'track_timestamp' ) );
			var text = NMERadio.getCookie( 'track_text' );
			if ( ts > 0 ) {
				NMERadio.timestamp = ts;
				NMERadio.refreshDivs(  NMERadio.CURRENT_SONG_CLASS, text );
			} else {
				NMERadio.timestamp = 0;
			}
			NMERadio.refreshCurrentSong();
		},

		initRightPanel: function() { 
			NMERadio.setupPlayerLinks();
			NMERadio.timestamp = 0;
			//NMERadio.refreshWidget();
		},

		initPlayer: function() { 
			YAHOO.util.Event.addListener( 'radio_player_collapse_link' , 'click', NMERadio.collapsePlayer );
			YAHOO.util.Event.addListener( 'radio_player_expand_link' , 'click', NMERadio.expandPlayer );
		},
		
		/**
		 * This is needed becuase nme doesn't return 404 or 500 on an error. 
		 * It returns 200 with error content and thus YUI doesn't detect it's an error.
		 */
		isError: function( res ) {
			//has error text or is just whitespace
			return res.responseText.indexOf( 'An error has occurred' )>=0 || res.responseText.replace(/^\s+|\s+$/, '')=='';
		},
		
		refreshCurrentSong: function() {
			var elements = YAHOO.util.Dom.getElementsByClassName( NMERadio.CURRENT_SONG_CLASS );
			if ( elements.length == 0 ) {
				return;
			}
			var callback = {
				success: function(o) {
					if ( NMERadio.isError( o ) ) {
						NMERadio.refreshDivs(  NMERadio.CURRENT_SONG_CLASS, 
						"<div class='title'><h4>&nbsp;</h4></div><div class='artist'>&nbsp;</div>" );
					} else {
						NMERadio.refreshCurrentSongOnSuccess( o.responseText );
					}
					setTimeout ( NMERadio.refreshCurrentSong, NMERadio.REFRESH_DELAY );
				},
				failure: function(o) {
					NMERadio.refreshDivs(  NMERadio.CURRENT_SONG_CLASS, 
					"<div class='title'><h4>&nbsp;</h4></div><div class='artist'>&nbsp;</div>" );
					setTimeout ( NMERadio.refreshCurrentSong, NMERadio.REFRESH_DELAY );
				}
			};
			var extra = ( String( Math.random() * 100000 ) ).substring( 0, 3 );
			var transaction = YAHOO.util.Connect.asyncRequest('GET', NMERadio.CURRENT_SONG_URL+extra, callback, null);
		},
		
		refreshCurrentSongOnSuccess: function( responseText ) {
			var ts = Number( responseText.match( />(\d+)</ )[1] );
			if ( ts > NMERadio.timestamp ) {
				NMERadio.timestamp = ts;
				NMERadio.refreshDivs(  NMERadio.CURRENT_SONG_CLASS, responseText );
				NMERadio.setCookie( 'track_timestamp', ts );
				NMERadio.setCookie( 'track_text', responseText );
			}
		},
		

		refreshWePlayed: function() {
			var elements = YAHOO.util.Dom.getElementsByClassName( NMERadio.WE_PLAYED_FRAGMENT_CLASS );
			if ( elements.length == 0 ) {
				return;
			}
			var callback = {
				success: function(o) {
					if ( NMERadio.isError( o ) ) {
					} else {
						NMERadio.refreshDivs(  NMERadio.WE_PLAYED_FRAGMENT_CLASS, o.responseText );
					}
				},
				failure: function(o) {
				}
			};
			var extra = ( String( Math.random() * 100000 ) ).substring( 0, 3 );
			var transaction = YAHOO.util.Connect.asyncRequest('GET', NMERadio.WE_PLAYED_FRAGMENT_URL+extra, callback, null);
		},
		
		refreshWidget: function() {
		
			var elements = YAHOO.util.Dom.getElementsByClassName( NMERadio.WIDGET_FRAGMENT_CLASS );
			if ( elements.length == 0 ) {
				//console.log( 'no divs to refresh!' );
				return;
			}
			var callback = {
				success: function(o) {
					//console.log("Refresh success"); //SUCCESS
					if ( NMERadio.isError( o ) ) {
						NMERadio.refreshDivs(  NMERadio.WIDGET_FRAGMENT_CLASS, 
						"<div class='title'><h4>&nbsp;</h4></div><div class='artist'>&nbsp;</div>" );
					} else {
						NMERadio.refreshDivs(  NMERadio.WIDGET_FRAGMENT_CLASS, o.responseText );
						NMERadio.setupPlayerLinks();
					}
					setTimeout ( NMERadio.refreshWidget, NMERadio.REFRESH_DELAY );
				},
				failure: function(o) {
					NMERadio.refreshDivs(  NMERadio.WIDGET_FRAGMENT_CLASS, 
					"<div class='title'><h4>&nbsp;</h4></div><div class='artist'>&nbsp;</div>" );
					setTimeout ( NMERadio.refreshWidget, NMERadio.REFRESH_DELAY );
				}
			};
			var transaction = YAHOO.util.Connect.asyncRequest('GET', NMERadio.WIDGET_FRAGMENT_URL, callback, null);
		},
		

		refreshDivs: function( divClass, content ) {
			var elements = YAHOO.util.Dom.getElementsByClassName( divClass );
			for ( var i=0; i<elements.length; i++ ) {
				elements[ i ].innerHTML =  content;
			}
		},
		
		refreshDiv: function( divId, content ) {
			var element = YAHOO.util.Dom.get( divId );
			element.innerHTML =  content;
		},
		
		setupPlayerLinks: function() {
			var elements = YAHOO.util.Dom.getElementsByClassName( NMERadio.PLAYER_LINK_CLASS );
			for ( var i=0; i<elements.length; i++ ) {
				elements[ i ].onclick =  NMERadio.popUpPlayer;
				elements[ i ].href =  '#';
			}
		},
	
		popUpPlayer: function() {
			window.open( NMERadio.PLAYER_LINK_URL, 'nmeradio_player', "menubar=0,location=0,resizable=0,scrollbars=0,toolbar=0,height="+NMERadio.PLAYER_WINDOW_HEIGHT+",width=600" );
		},
		
		collapsePlayer: function(e) {
			//console.log( 'from html: collapse player!' );
			window.resizeBy( 0, -390 );
			NMERadio.refreshDiv( 'radio_player_ad', '' );
			YAHOO.util.Dom.setStyle( ['radio_player_expanded'], 'display', 'none' ); 		
			YAHOO.util.Dom.setStyle( ['radio_player_collapse'], 'display', 'none' ); 		
			YAHOO.util.Dom.setStyle( ['radio_player_expand'], 'display', 'block' ); 		
			YAHOO.util.Event.preventDefault(e);
		},

		expandPlayer: function(e) {
			//console.log( 'from html: expand player!' );
			window.resizeBy( 0, 390 );
			YAHOO.util.Dom.setStyle( ['radio_player_expanded'], 'display', 'block' ); 		
			YAHOO.util.Dom.setStyle( ['radio_player_collapse'], 'display', 'block' ); 		
			YAHOO.util.Dom.setStyle( ['radio_player_expand'], 'display', 'none' ); 		
			NMERadio.refreshPlayerAd();
			YAHOO.util.Event.preventDefault(e);
		},
		
		refreshPlayerAd:function() {
			//console.log( 'refresh ad!' );
			var extra = ( String( Math.random() * 100000 ) ).substring( 0, 3 );
			var element = YAHOO.util.Dom.get( 'radio_ad_iframe' );
			element.src = NMERadio.AD_IFRAME_SRC+extra;
		},
		
		updateOnSongChange:function() {
			//console.log( 'from html:::: on song change!!!!' );
			var callback = {
				success: function(o) {
					NMERadio.refreshDiv(  NMERadio.PLAYER_FRAGMENT_ID, o.responseText );
					YAHOO.util.Dom.setStyle( ['radio_player_loading'], 'display', 'none' ); 		
				},
				failure: function(o) {
					YAHOO.util.Dom.setStyle( ['radio_player_loading'], 'display', 'none' ); 		
				}
			};
			var extra = ( String( Math.random() * 100000 ) ).substring( 0, 3 );
			var transaction = YAHOO.util.Connect.asyncRequest('GET', NMERadio.PLAYER_FRAGMENT_URL+extra, callback, null);
			NMERadio.refreshPlayerAd();
		},
		
		trackStream:function() {
			console.log( window.frames[ 'radio_omniture_iframe' ].s.t );
		},
		
		setCookie: function(name, value, expires, path, domain, secure) {
		  var curCookie = name + "=" + escape(value) +
		      ((expires) ? "; expires=" + expires.toGMTString() : "") +
		      ((path) ? "; path=" + path : "") +
		      ((domain) ? "; domain=" + domain : "") +
		      ((secure) ? "; secure" : "");
		  document.cookie = curCookie;
		},
		
		getCookie: function(name) {
		  var dc = document.cookie;
		  var prefix = name + "=";
		  var begin = dc.indexOf("; " + prefix);
		  if (begin == -1) {
		    begin = dc.indexOf(prefix);
		    if (begin != 0) return null;
		  } else
		    begin += 2;
		  var end = document.cookie.indexOf(";", begin);
		  if (end == -1)
		    end = dc.length;
		  return unescape(dc.substring(begin + prefix.length, end));
		}
		
		
	};
}();
