function onTuningChange( value )
{
    if ($(document).data('channel') != null)
        $("#news_selector").news_selector('setValue', value);
}

function onNewsValueChange( value )
{
    $("#tuning_knob").rotate_knob('setValue', value);
}

function getNextVoice()
{
    var voiceIndex = $(document).data('voice');

    var voice = "&voice=" + $(document).data('voices')[voiceIndex];

    voiceIndex++;
    if (voiceIndex >= $(document).data('voices').length)
        voiceIndex = 0;

    $(document).data('voice', voiceIndex);

    return voice;
}

function playNewsItem( index )
{
    $("#title_player").jPlayer('clearMedia');
    $("#descr_player").jPlayer('clearMedia');

    var item = $(document).data('news')[index];

    $("#news_title").text(item.Title);
    $(document).data('curIndex', index);

    var voice = getNextVoice();

    $("#title_player").jPlayer("setMedia", { mp3: item.TitleSound + voice }).jPlayer('play');
    $("#descr_player").jPlayer("setMedia", { mp3: item.DescriptionSound + voice });
}

function onEndPlayItem()
{
    var index = $(document).data('curIndex') + 1;

    if (index < $(document).data('news').length)
        $("#news_selector").news_selector('setValue', index, true);
    else
        $("#channel_selector").channel_selector('selectNoChannel');
}

function onEndPlayTitle()
{
    $("#descr_player").jPlayer('play'); 
}

function onPlayerError()
{
    $("#channel_selector").channel_selector('selectNoChannel');
}

function finishTuning ( value )
{
    if ($(document).data('channel') == null)
        return;

    var intValue = Math.round(value);

    if (intValue == value)
        playNewsItem(intValue);
    else
    {
        $("#news_selector").news_selector('setValue', intValue, true);
    }
}

function lightOn()
{
    if ($(document).data('light') != 'on')
        return;

    $("#radio_highlight").animate({ opacity: 1 }, 300);
}

function lightOff()
{
    if ($(document).data('light') != 'off')
        return;

    $("#radio_highlight").animate({ opacity: 0 }, 300);
}

function onNewsLoaded( news )
{
    $("#news_title").text($(document).data('channel_title'));
    $("#descr_player").jPlayer('play');
    
    $(document).data('news', news);
    
    $("#news_selector").news_selector('setValue', 0);
    $("#tuning_knob").rotate_knob('setValue', 0);

    $("#news_selector").news_selector('setMaxNews', news.length);
    $("#tuning_knob").rotate_knob('setRange', 0, news.length - 1);

    $("#news_selector").news_selector('setValue', 0);
    $(document).data('curIndex', -1); // 0 will be after reading title
    
    $("#news_selector").show();
    
}

function onChannelSelect( channel )
{
    $("#title_player").jPlayer('clearMedia');
    $("#descr_player").jPlayer('clearMedia');
    $("#news_title").text("");
    
    if (channel)
    {
        $(document).data('light', 'on');
        lightOn();

        $(document).data('channel', channel);
        $(document).data('channel_title', Channels[channel].Name);
        
        $("#news_title").show();
        $("#news_title").text("Загрузка...");
        $.getJSON("./get_news.php?channel=" + channel, onNewsLoaded);
        $("#descr_player").jPlayer("setMedia", { mp3: "./get_speech.php?channel=" + channel + getNextVoice() });

    }
    else
    {
        $(document).data('light', 'off');
        lightOff();

        $(document).data('channel_title', null);
        $(document).data('channel', null);
        
        $("#news_selector").hide();
        $("#news_title").hide();
    }
}

function onVolumeChange( value )
{
    $("#title_player").jPlayer('volume', value / 100.0);
    $("#descr_player").jPlayer('volume', value / 100.0);
}

function onNewsTitleClick()
{
    var index = $(document).data('curIndex');

    if (index >= 0 && index < $(document).data('news').length)
    {
        var item = $(document).data('news')[index];
        window.open(item.Link);
    }
}


var notReadyPlayers = 2;

function onPlayerReady()
{
    notReadyPlayers--;

    if (notReadyPlayers == 0)
        $("#shadow").animate({ opacity: 0 }, 1500, undefined, function() { $("#shadow").hide(); });
}

function onConfigOpen()
{
    $("#channel_selector").unbind('mouseover');

    $("#channel_selector").channel_selector('selectNoChannel');
    $("#channel_selector").channel_selector('disableButtons');
    $("#shadow_around_radio").show();
    $("#radio_shadow").show();
    $("#shadow_around_radio").animate({ opacity: 1 }, 500);
    $("#radio_shadow").animate({ opacity: 1 }, 500);
    
    $("#channel_popup").animate({ top: 0 }, 500);

    setTimeout(function()
    {
        $("#config_button").addClass('config-btn-push2');
        setTimeout(function() { $("#config_button").addClass('config-btn-push3'); }, 50);
    }, 50);
}

function onConfigClose()
{
    $("#channel_selector").channel_selector('enableButtons');
    $("#shadow_around_radio").animate({ opacity: 0 }, 500, undefined, function() { $("#shadow_around_radio").hide(); });
    $("#radio_shadow").animate({ opacity: 0 }, 500, undefined, function() { $("#radio_shadow").hide(); });
    $("#channel_popup").animate({ top: -400 }, 500);

    setTimeout(function()
    {
        $("#config_button").removeClass('config-btn-push3');
        setTimeout(function() { $("#config_button").removeClass('config-btn-push2'); }, 50);
    }, 50);
}

function showChTip()
{
    if ($("#config_button_tip").css('display') != 'none')
        return;

    $("#config_button_tip").show().animate({ opacity: 0.8 }, 300);
}

function hideChTip()
{
    if ($("#config_button_tip").css('display') == 'none')
        return;

    $("#config_button_tip").animate({ opacity: 0 }, 300, undefined, function() { $("#config_button_tip").hide(); });
}

function showChTipOnce()
{
    showChTip();
    setTimeout(function() { $("#channel_selector").unbind('mouseover'); hideChTip(); }, 3000);
}

function init()
{
    var volume = 0.8;
    
    $("#what_is_this").hover( function( ev ) { $("#help").css({ left: ev.pageX, top: ev.pageY, display: 'block'}); }, function() { $("#help").hide(); } );

    $(".channel-drag").draggable({ zIndex: 1000, revert: true, revertDuration: 0, appendTo: 'body', helper: 'clone' });


    $("#config_button").click(onConfigOpen);
    $("#shadow_around_radio, #radio_shadow, #channel_popup_close").click(onConfigClose);

    
    $("#config_button").hover(showChTip, hideChTip);
    $("#channel_selector").mouseover(showChTipOnce);
    
    $("#channel_selector").channel_selector({ onSelect: onChannelSelect, buttonsNumber: 5, channels: SelectedChannels });
    $("#volume_knob").rotate_knob({ onValueChange: onVolumeChange, unitsPerCycle: 100, max: 100, min: 0, value: volume * 100 });

    $("#tuning_knob").rotate_knob({ onEndChange: finishTuning, onValueChange: onTuningChange, unitsPerCycle: 3 });
    $("#news_selector").news_selector({ onEndChange: finishTuning, onValueChange: onNewsValueChange });

    $("#news_title").click(onNewsTitleClick);

    $(document).bind('mousedown', function( e ) { e.preventDefault(); });

    $(document).data('voices', Voices);
    $(document).data('voice', Math.floor(Math.random() * $(document).data('voices').length));

    $("#title_player").jPlayer(
    {
        ready: onPlayerReady,
        preload: "auto",
        ended: onEndPlayTitle,
        error: onPlayerError,
        solution: "flash, html",
        supplied: "mp3",
        volume: volume,
        swfPath: "./jquery"
    });

    $("#descr_player").jPlayer(
    {
        ready: onPlayerReady,
        preload: "auto",
        ended: onEndPlayItem,
        error: onPlayerError,
        solution: "flash, html",
        supplied: "mp3",
        volume: volume,
        swfPath: "./jquery"
    });


}


