var lastGenre = '';
var aaa = 'aaa';
var playList = '';
var isOp = window.opera ? 1 : 0;

function startPlay()
{
    playerWindow = getRadioWindow(true, url);
    playerWindow.wimpy_loadExternalPlaylist(url + 'getradioplaylist.php?list=' + genre + '&rnd=' + Math.random());
    playerWindow.wimpy_play();
}

function onChangeGenre(url)
{
    if (isOp)
    {
        sel = document.getElementById('arRadioPlaylist');
        genre = sel.options[sel.selectedIndex].value;
        playList = url + 'getradioplaylist.php?list=' + genre + '&rnd=' + Math.random();
        playerWindow = getRadioWindow(true, url);
        alert(playerWindow.playList);
    }
    else
    {
        var opened = readCookie('ar_radio_opened');
        if (opened == null) return;
        playRadio(url);
    }
}

function waitAndPlay()
{
    playerWindow = getRadioWindow(true, '');
    playerWindow.wimpy_gotoTrack(1);
//    playerWindow.wimpy_play();
}

function playRadio(url)
{
    sel = document.getElementById('arRadioPlaylist');
    genre = sel.options[sel.selectedIndex].value;
    
    if (playList == '') playList = url + 'getradioplaylist.php?list=' + genre + '&rnd=' + Math.random();
    
    playerWindow = getRadioWindow(true, url);
    var opened = readCookie('ar_radio_opened');

    var locstr = playerWindow.location.toString();
    
    if (isOp && locstr != (url + 'radio.html') )
    {
        playerWindow.close();
//        eraseCookie('ar_radio_opened');
        playRadio(url);
        return;
    }    
    
    if (genre != playerWindow.lastGenre && playerWindow.lastGenre != '' && (opened != null || isOp ))
    {
        playList = url + 'getradioplaylist.php?list=' + genre + '&rnd=' + Math.random();
        playerWindow.lastGenre = genre;
        playerWindow.wimpy_loadExternalPlaylist(url + 'getradioplaylist.php?list=' + genre + '&rnd=' + Math.random());
        setTimeout("waitAndPlay()", 3000);
    }
    if (playerWindow.isPaused)
    {
        playerWindow.wimpy_play();
        playerWindow.isPaused = false;
    }
}

function getRadioWindow(isOpening, url)
{
    var playerWindow = null;
//    if (playerWindow != null) return playerWindow;
    var opened = readCookie('ar_radio_opened');
    if (opened == null)
    {
        // if press PLAY RADIO then open a radio otherwise just return null
        if (isOpening)
        {
            playerWindow = window.open(url + 'radio.html', 'ar_radio', 'width=420,height=48');
            if (isOp) {playerWindow.reload();}
            createCookie('ar_radio_opened', '1');
            playerWindow.blur();
            window.focus();
        }
    }
    else
    {
        playerWindow = window.open('', 'ar_radio', 'width=420,height=48');
    }
    
    /*if (!isOpening && opened == null && isOp)
    {
        playerWindow = window.open('', 'ar_radio', 'width=200,height=500');
        var locstr = playerWindow.location.toString();
        if ( locstr != (url + 'radio.html') )
        {
            playerWindow.close();
            getRadioWindow(true, url)
            return;
        }    
    }*/
    
    playerWindow.openerWindow = window;
    return playerWindow;
}

function stopRadio(url)
{
    playerWindow = getRadioWindow(false, url);
    if (playerWindow == null) return;
    playerWindow.wimpy_pause();
    playerWindow.isPaused = true;
 }

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

