﻿//=====================================================
// H G   R A D I O
//=====================================================

//
// create closure
//
(function($) {

 	//=====================================================
 	// plugin definition
	//
	$.fn.hgRadio = function(options) {

		//debug(this.size());

		// build main options before element iteration
		var opts = $.extend({}, $.fn.hgRadio.defaults, options);
		//debug(opts);

		//=====================================================
		// iterate and reformat each matched element
		return this.each(function(i) {
			var $hgRadio = $(this);
			$hgRadio.options = opts;
			$('.control-panel a', this).each( function(i) {
					var $this = $(this);
					if ($this.attr('href') != 'javascript:void(0);')
					{
						$this.attr('href', 'javascript:void(0);');
						$this.click( function() { dispatchCommand( $hgRadio, $this.attr('class') ); } );
					}
				});

			retrieveOnTheAirInfo( $hgRadio );
			setInterval( function() { retrieveOnTheAirInfo( $hgRadio )}, 30000 );
		});

	};

	//=====================================================
 	// plugin private methods definition
	//

	function retrieveOnTheAirInfo( $hgRadio )
	{
		$.ajax({
			   url: $hgRadio.options.htmlinfo_url,
			   type: 'get',
			   cache: false,
			   timeout: 25000,
			   global: false,
			   async: true,
			   success: function(html){
				   parseAndInjectOnTheAirInfo($hgRadio, html);
			   },
			   error: function(XMLHttpRequest, textStatus, errorThrown){
				   //debug(XMLHttpRequest, textStatus );
				   //debug(errorThrown, textStatus );
				   $('.who', $hgRadio).text('ERROR');
				   $('.what', $hgRadio).text('Could not connect to server.');
			   }
		});
	}

	function parseAndInjectOnTheAirInfo($hgRadio, sInfo)
	{
		var aInfo;
		aInfo = sInfo.split("\n");

		if (aInfo[0] == '==OK==')
		{
			// OK
			aInfo[1] = aInfo[1].split("\t");
			$('.who', $hgRadio).text(aInfo[1][0]);
			$('.what', $hgRadio).text(aInfo[1][1]);
		}
		else
		{
			// error
			$('.who', $hgRadio).text('ERROR');
			$('.what', $hgRadio).text('Could not connect to server.');
		}
		//debug(aInfo);
	}

	function dispatchCommand($hgRadio, sCmd )
	{
		var rhgWnd = $hgRadio.data('rhgWnd');
		if (typeof rhgWnd == 'undefined' || rhgWnd.closed)
		{
			var s;
			s = 'titlebar=yes,menubar=no,location=no,resizable=yes,scrollbars=yes,status=yes,width=400,height=560,';
			s += 'left=' + ((screen.availWidth-400) / 2) + ',top=' + ((screen.availHeight-600) / 2);


			rhgWnd = window.open("http://www.hermeticgarage.com/popupradio.php","rhgPopupWndName", s);
			//rhgWnd = window.open("","rhgPopupWndName", s);

			$hgRadio.data('rhgWnd', rhgWnd);
			if (false && rhgWnd.document.title == '')
			{
				var d = rhgWnd.document;
				//alert('wpisujemy rzeczy' + "\n" + s);

				d.writeln( '<html>' );
				d.writeln( '<head><title>RAD!O HERMETIC GARAGE</title></head>' );
				d.writeln( '<body style="background:#000;color:#fff;overflow:auto;">' );
				d.writeln( '<object id="mp3player" type="application/x-shockwave-flash" data="radio/player.swf" width="350" height="48" bgcolor="#000000">' );
				d.writeln( '<param name="type" value="application/x-shockwave-flash" />' );
				d.writeln( '<param name="codebase" value="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" />' );
				d.writeln( '<param name="bgcolor" value="#000000" />' );
				d.writeln( '<param name="movie" value="radio/player.swf" />' );
				d.writeln( '<param name="FlashVars" value="l_playing=RAD!O HG&amp;my_BackgroundColor=#000000&amp;my_loop=true&amp;file=' + $hgRadio.options.shoutcast_url + '" />' );
				d.writeln( '</object>' );
				d.writeln( '</body>' );
				d.writeln( '</html>' );
			}
		}

		//rhgWnd.document.writeln( debug( sCmd, 'dispatchCommand', true ) + '<br>');
		rhgWnd.document.title = 'RAD!O HERMETIC GARAGE';

		//window.focus();

		//debug( $( rhgWnd.document.body ) );
		//rhgWnd.document.body.innerHTML = debug( sCmd, 'dispatchCommand', true );
	}


 	//=====================================================
 	// private function for debugging
	//
	function debug(o, msg, ret) {
		var s = '['+(typeof o) + "]:\n", v;
		if (typeof o == 'object') {
			for( k in o ) {
				switch(typeof (o[k])) {
				case 'object':
				case 'function':
					v = '['+(typeof (o[k]))+']';
					break;
				case 'string':
					v = o[k].length > 50 ? o[k].substr(0, 46) + ' ...' : o[k];
					break;
				default:
					v = o[k];
				}
				s += k + ': ' + v + "\n";
			}
		}
		else
			s += o;

		s = (msg ? msg + "\n" : '' ) + s;
		return ret ? s : alert( s );
	};

	//
 	// plugin defaults
	//
	$.fn.hgRadio.defaults = {
		shoutcast_url: 'http://194.54.191.156:8000/;',
		htmlinfo_url: 'rhg-play-list.php'
	};

//
// end of closure
//
})(jQuery);


//
// run on document ready
//
jQuery(function($){
	   $('.hgRadio').hgRadio();
});


//=====================================================
// MP3 LIST PLAYER
//=====================================================

//
// create closure
//
(function($) {

 	//=====================================================
 	// plugin definition
	//
	$.fn.qMp3Player = function(options) {

		$('.aplayer').each( function(i) {
					var oSongInfo = new Object();
					var $PlayBtn = $('a.play', this);
					var $StopBtn = $('a.stop', this);

					oSongInfo.id = $(this).attr('id');
					oSongInfo.pathname = $PlayBtn.attr('href');

					$PlayBtn.attr('href', 'javascript:void(0);');
					$PlayBtn.click( function() { playSong( oSongInfo.id, oSongInfo.pathname ); return false; } );
					$StopBtn.click( function() { stopSong( oSongInfo.id ); return false; } );
				});
		return this;

	};

	$.fn.qMp3PlayerStop = function()
	{
		$('.aplayer').each( function(i) { stopSong( $(this).attr('id') ); } );
		return this;
	}

	//=====================================================
 	// plugin private methods definition
	//

	function BlinkTitle(o, op)
	{
		if (o.hasClass('current'))
			o.animate(
					{ opacity: op },
					500,
					'linear',
					function() {
						BlinkTitle( $(this), op < 1.0 ? 1.0 : 0.6);
					}
			);
		else
			o.animate(
					{ opacity: 1.0 },
					200,
					'linear'
			);
	}

	function playSong( sId, sPathName )
	{
		if ($('div#'+sId+' h4').hasClass('current'))
			return;

		$('.aplayer h4').removeClass('current');
		$('.aplayer').css('border-color', '' );

		BlinkTitle($('div#'+sId+' h4').addClass('current'), 0.6);
		$('div#'+sId).css('border-color', '#2559E3');

		var sSwfId = 'swf-' + sId;

		$('#mp3players').flash( {
					src: 'js/qsbStreamPlayer.swf',
					width: 0,
					height: 0,
					id: sSwfId,
					name: sSwfId,
					bgcolor: '#000000',
					flashvars: { snd: sPathName, id: sSwfId, name: sSwfId, auto:1 }
				},
				{ version: 8 },
				function(htmlOptions) { this.innerHTML = ''; $(this).prepend($.fn.flash.transform(htmlOptions)); }
			);
	}

	function stopSong( sId )
	{
		if ($('div#'+sId+' h4').hasClass('current'))
		{
			$('div#'+sId+' h4').removeClass('current');
			$('.aplayer').css('border-color', '' );
			$('#mp3players').html('');
		}
	}


 	//=====================================================
 	// private function for debugging
	//
	function debug(o, msg) {
		var s = '['+(typeof o) + "]:\n", v;
		if (typeof o == 'object') {
			for( k in o ) {
				switch(typeof (o[k])) {
				case 'object':
				case 'function':
					v = '['+(typeof (o[k]))+']';
					break;
				case 'string':
					v = o[k].length > 50 ? o[k].substr(0, 46) + ' ...' : o[k];
					break;
				default:
					v = o[k];
				}
				s += k + ': ' + v + "\n";
			}
		}
		else
			s += o;

		alert( (msg ? msg + "\n" : '' ) + s );
	};


//
// end of closure
//
})(jQuery);


function qsbStreamPlayer( sPlayerId, sCmd )
{
	switch(sCmd)
	{
	case 'soundComplete':
		$().qMp3PlayerStop();
		break;
	case 'soundFailed':
		$().qMp3PlayerStop();
		break;
	case 'soundLoaded':
		break;
	}
	return false;
}

//
// run on document ready
//
jQuery(function($){
	   $().qMp3Player();
});


