

/**
 * Open
 *
 * Popup window utility for creating pop up windows without intrusive using any
 * intrusive code.
 *
 * 
 */
(function ($) {

    $.open = {};

    // Default popup window parameters
    $.open.defaultParams = {
        "width":       "800",   // Window width
        "height":      "600",   // Window height
        "top":         "0",     // Y offset (in pixels) from top of screen
        "left":        "0",     // X offset (in pixels) from left side of screen
        "directories": "no",    // Show directories/Links bar?
        "location":    "no",    // Show location/address bar?
        "resizeable":  "yes",   // Make the window resizable?
        "menubar":     "no",    // Show the menu bar?
        "toolbar":     "no",    // Show the tool (Back button etc.) bar?
        "scrollbars":  "yes",   // Show scrollbars?
        "status":      "no"     // Show the status bar?
    };


    // Some configuration properties
    $.open.defaultConfig = {
        autoFocus: true
    };


    // Open popup window static function
    $.open.newWindow = function (href, params, config) {

        // Popup window defaults (don't leave it to the browser)
        var windowParams = $.extend($.open.defaultParams, params);

        // Configuration properties
        var windowConfig = $.extend($.open.defaultConfig, config);

        var windowName = params["windowName"] || "new_window";

        var i, paramString = "";

        for (i in windowParams) {
            if (windowParams.hasOwnProperty(i)) {
                paramString += (paramString === "") ? "" : ",";
                paramString += i + "=";

                // Allow true/false instead of yes/no in params
                if (windowParams[i] === true || windowParams[i] === false) {
                    paramString += (windowParams[i]) ? "yes" : "no";
                }
                else {
                    paramString += windowParams[i];
                }
            }
        }

        var popupWindow = window.open(href, windowName, paramString);

        if (windowConfig.autoFocus) {
            popupWindow.focus();
        }

        return popupWindow;
    };


    // Plugin method: $("...").popup()
    $.fn.open = function (parameters, callback) {

        var params = parameters.params || parameters;
        var config = parameters.config || {};

        // Loop over all matching elements
        this.each(function (){

            // Add an onClick behavior to this element
            $(this).click(function (event) {

                // Prevent the browser's default onClick handler
                event.preventDefault();

                // Use the target attribute as the window name
                if ($(this).attr("target")) {
                    params.windowName = $(this).attr("target");
                }

                // Determine the url to open
                //   Use param.href over element's href
                var href;
                if (params.href) {
                    href = params.href;
                }
                else if ($(this).attr("href")) {
                    href = $(this).attr("href");
                }
                else {
                    return;  // Can't openWindow anything, so stop here
                }

                
                // Pop up the window
                var windowObject = $.open.newWindow(href, params, config);

                if (callback) {
                    callback(windowObject);
                }
            });
        });

        return $;
    };

})(jQuery);

﻿/**
 * jQuery.timers - Timer abstractions for jQuery
 * Written by Blair Mitchelmore (blair DOT mitchelmore AT gmail DOT com)
 * Licensed under the WTFPL (http://sam.zoy.org/wtfpl/).
 * Date: 2009/08/13
 *
 * @author Blair Mitchelmore
 * @version 1.1.3
 *
 **/

jQuery.fn.extend({
	everyTime: function(interval, label, fn, times, belay) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, times, belay);
		});
	},
	oneTime: function(interval, label, fn) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, 1);
		});
	},
	stopTime: function(label, fn) {
		return this.each(function() {
			jQuery.timer.remove(this, label, fn);
		});
	}
});

jQuery.extend({
	timer: {
		global: [],
		guid: 1,
		dataKey: "jQuery.timer",
		regex: /^([0-9]+(?:\.[0-9]*)?)\s*(.*s)?$/,
		powers: {
			// Yeah this is major overkill...
			'ms': 1,
			'cs': 10,
			'ds': 100,
			's': 1000,
			'das': 10000,
			'hs': 100000,
			'ks': 1000000
		},
		timeParse: function(value) {
			if (value == undefined || value == null)
				return null;
			var result = this.regex.exec(jQuery.trim(value.toString()));
			if (result[2]) {
				var num = parseFloat(result[1]);
				var mult = this.powers[result[2]] || 1;
				return num * mult;
			} else {
				return value;
			}
		},
		add: function(element, interval, label, fn, times, belay) {
			var counter = 0;
			
			if (jQuery.isFunction(label)) {
				if (!times) 
					times = fn;
				fn = label;
				label = interval;
			}
			
			interval = jQuery.timer.timeParse(interval);

			if (typeof interval != 'number' || isNaN(interval) || interval <= 0)
				return;

			if (times && times.constructor != Number) {
				belay = !!times;
				times = 0;
			}
			
			times = times || 0;
			belay = belay || false;
			
			var timers = jQuery.data(element, this.dataKey) || jQuery.data(element, this.dataKey, {});
			
			if (!timers[label])
				timers[label] = {};
			
			fn.timerID = fn.timerID || this.guid++;
			
			var handler = function() {
				if (belay && this.inProgress) 
					return;
				this.inProgress = true;
				if ((++counter > times && times !== 0) || fn.call(element, counter) === false)
					jQuery.timer.remove(element, label, fn);
				this.inProgress = false;
			};
			
			handler.timerID = fn.timerID;
			
			if (!timers[label][fn.timerID])
				timers[label][fn.timerID] = window.setInterval(handler,interval);
			
			this.global.push( element );
			
		},
		remove: function(element, label, fn) {
			var timers = jQuery.data(element, this.dataKey), ret;
			
			if ( timers ) {
				
				if (!label) {
					for ( label in timers )
						this.remove(element, label, fn);
				} else if ( timers[label] ) {
					if ( fn ) {
						if ( fn.timerID ) {
							window.clearInterval(timers[label][fn.timerID]);
							delete timers[label][fn.timerID];
						}
					} else {
						for ( var fn in timers[label] ) {
							window.clearInterval(timers[label][fn]);
							delete timers[label][fn];
						}
					}
					
					for ( ret in timers[label] ) break;
					if ( !ret ) {
						ret = null;
						delete timers[label];
					}
				}
				
				for ( ret in timers ) break;
				if ( !ret ) 
					jQuery.removeData(element, this.dataKey);
			}
		}
	}
});

jQuery(window).bind("unload", function() {
	jQuery.each(jQuery.timer.global, function(index, item) {
		jQuery.timer.remove(item);
	});
});

function request_playlist(){

	$.ajax({
		url: '/escucha.xml',
		type: 'GET',
		beforeSend: function(){
			$("#reproductor").unbind("click");
			$("#reproductor").text("");
			$("#reproductor").addClass("cargando");
			$("#reproductor").removeClass("cargado");
			$("#reproductor").removeClass("sincargar");
			$("#reproductor").removeClass("errorcargando");
			$('<span>Cargando...</span>').addClass('entry_name').appendTo('#reproductor');
			$('<span>Espera unos instantes...</span>').addClass('entry_url').appendTo('#reproductor');
			$("#reproductor").attr("title","Espera unos instantes...");
		},
		error: function(){
			$("#reproductor").click(request_playlist);
			$("#reproductor").text("");
			$("#reproductor").removeClass("cargando");
			$("#reproductor").addClass("errorcargando");
			$('<span>¡Error!</span>').addClass('entry_name').appendTo('#reproductor');
			$('<span>Reinténtalo en unos minutos.</span>').addClass('entry_url').appendTo('#reproductor');
			$("#reproductor").attr("title","Pulsa aquí para cargar la radio");
		},
		success: function(xml){
			$("#reproductor").click(request_playlist);
			$("#reproductor").text("");
			$("#reproductor").attr("title","Pulsa aquí para recargar la radio");
        		$(xml).find('entry').each(function(){
				entry_name=$(this).find('name').text();
				entry_url=$(this).find('url').text();
				$('<audio></audio>').attr('type','audio/ogg; codecs=vorbis').attr('autoplay','true').attr('src',entry_url).attr('name',entry_name).appendTo("#reproductor");
				$('<span>Iniciando buffer...</span>').addClass('entry_status').appendTo('#reproductor');
				$('<span>'+entry_name+'</span>').addClass('entry_name').appendTo('#reproductor');
				$('<span>'+entry_url+'</span>').addClass('entry_url').appendTo('#reproductor');
				$(document).everyTime(800, function() {
                    audio=$('audio').get(0);
                    if(audio){
                        if(audio.networkState==1 || audio.networkState==2){
                            $("#reproductor").removeClass("cargando");
                  			$("#reproductor").addClass("cargado");
					        $(".entry_status").each(function(){
						        $(this).text("Reproduciendo...");
					        });
                        }
                        else{
                            $("#reproductor").removeClass("cargado");
                  			$("#reproductor").addClass("cargando");
					        $(".entry_status").each(function(){
						        $(this).text("Recargando buffer...");
					        });
                        }
                    }
				});
			});
		}
	});

}

$(document).ready(function(){
	$('<span>Pulsa aquí para iniciar el reproductor</span>').addClass('entry_name').appendTo('#reproductor');
	$('<span>Requiere un navegador moderno (no Internet Explorer)</span>').addClass('entry_url').appendTo('#reproductor');
	$("#reproductor").addClass("sincargar");
	$("#reproductor").click(request_playlist);
	$("#repopener").open({
	      width: 380,
	      height: 110,
	      scrollbars: false
	   });
});

