﻿var onefm_StationListIntervalID = null;
var onefm_HistoryIntervalID = null;
var stationlisthash = 0;
var historyhash = 0;
var $$ = jQuery;

function onefm_load()
{
    try { onefm_localizedates(); } catch(e) { }
    try { onefm_wireratings(); } catch(e) { }
    try { onefm_stationlist(); } catch(e) { }
    
    try
    {
    jQuery('a.chat-link')
        .click(function(event)
        {                
            event.preventDefault();
            
            var wnd = window.open(this.href, 'chatwindow', 'width=800,height=500', true);
            wnd.focus();
        });
        
    jQuery('#sitenav li a:contains("Chat")')
        .click(function(event)
        {
            if($(this).text() != 'Chat')
                return;
                
            event.preventDefault();
            
            var wnd = window.open(this.href, 'chatwindow', 'width=800,height=500', true);
            wnd.focus();
        });
    
    } catch(e) { }
    
    try
    {
    var isVisible = jQuery('.station-list-wrapper').is(':visible');
    
    if(isVisible)
        jQuery('#station-list a.collapse').addClass('collapse-opened');
    
    jQuery('#station-list a.collapse').click(function(event)
    {
        var $this = jQuery(this);
        
        if($this.hasClass('collapse-opened'))
        {
            $this.removeClass('collapse-opened');
            jQuery('.station-list-wrapper')
                .stop()
                .css('height', '')
                .slideUp(250)
                .removeClass('visible-station-list-wrapper');
        }
        else
        {
            $this.addClass('collapse-opened');
            jQuery('.station-list-wrapper')
                .stop()
                .css('height', '')
                .slideDown(250);
        }
        
        event.preventDefault();
    });
    } catch(e) { }
}

function onefm_history(identifier)
{
    clearInterval(onefm_HistoryIntervalID);
    
    onefm_HistoryIntervalID = setInterval(function()
        {
            jQuery.get('/ajax/StationHistory.aspx?station=' + identifier + '&h=' + historyhash
                , null
                , function(data, status)
                    {
                        if(data == ' ')
                            return;
                        
                        try
                        {
                            data = data.split('%%%$$$');
                            
                            historyhash = data[0];
                            
                            jQuery('.station-history-wrapper').html(data[1]);
                        }
                        catch(e) { }
                        
                        onefm_localizedates();
                        onefm_wireratings();
                    }
                );
                    
        }, 1000 * 30);
    
//    setTimeout(function()
//    {
//        this.location.reload(true);
//    }, 1000 * 60 * 20);
}

function onefm_stationlist()
{
    clearInterval(onefm_StationListIntervalID);
    
    onefm_StationListIntervalID = setInterval(onefm_dostationlist, 1000 * 30);
}

function onefm_dostationlist()
{
    jQuery.get('/ajax/ListenerCount.aspx'
        , null
        , function(data, status)
            {
                if(data == ' ')
                    return;
                    
                jQuery("#listeners dd").html(data);
            }
        );
        
    jQuery.get('/ajax/StationList.aspx?h=' + stationlisthash
        , null
        , function(data, status)
            {
                 if(data == ' ')
                    return;
                
                try
                {
                    data = data.split('%%%$$$');
                    
                    stationlisthash = data[0];
                    
                    jQuery("#station-list .station-list-wrapper").html(data[1]);
                }
                catch(e) { }
                    
                
            }
        ); 
}

function onefm_localizedates()
{
    jQuery('span.localize-date')
        .each(
            function()
            {
                jQuery(this).html( localizeDate(jQuery(this).html()) );
            })
        .removeClass('localize-date');
}

function onefm_wireratings()
{
    jQuery('.rating-wrapper')
        .find('.r2')
            .hover(function() { jQuery(this).parents('.rating-wrapper').addClass('Rating_Hover_2'); }, function() { jQuery(this).parents('.rating-wrapper').removeClass('Rating_Hover_2'); })
            .end()
        .find('.r4')
            .hover(function() { jQuery(this).parents('.rating-wrapper').addClass('Rating_Hover_4'); }, function() { jQuery(this).parents('.rating-wrapper').removeClass('Rating_Hover_4'); })
            .end()
        .find('.r6')
            .hover(function() { jQuery(this).parents('.rating-wrapper').addClass('Rating_Hover_6'); }, function() { jQuery(this).parents('.rating-wrapper').removeClass('Rating_Hover_6'); })
            .end()
        .find('.r8')
            .hover(function() { jQuery(this).parents('.rating-wrapper').addClass('Rating_Hover_8'); }, function() { jQuery(this).parents('.rating-wrapper').removeClass('Rating_Hover_8'); })
            .end()
        .find('.r10')
            .hover(function() { jQuery(this).parents('.rating-wrapper').addClass('Rating_Hover_10'); }, function() { jQuery(this).parents('.rating-wrapper').removeClass('Rating_Hover_10'); })
            .end()
        .find('a')
            .attr('target', '_self')
            .click(function(event)
                {
                    if(this.onclick)
                        this.onclick();
                        
                    event.preventDefault();
                    event.stopPropagation();
                })
            .end();
}

jQuery(onefm_load);

var g_wndChat = null;

function Chat_OnClick()
{    
	if (!(!g_wndChat || g_wndChat.closed)) 
		g_wndChat.close();
			
	g_wndChat = window.open('','chatroom','toolbar=no,width=605,height=440,directories=no,status=yes,scrollbars=no,resizable=no,menubar=no');
		
	g_wndChat.focus();
	if (!g_wndChat.opener)
		g_wndChat.opener = self;
}

function Chat_OpenAndSet(strWnd)
{
    Chat_OnClick();
    g_wndChat.location.href = strWnd;
}

function MM_openBrWindow(theURL, winName, features)
{
    if(true || winName.toLowerCase() == 'chatroom')
    {
        Chat_OpenAndSet('/Chatroom.aspx');
    }
    else
    {
        window.open(theURL,winName,features); 
    }
} 


function localizeDate(gmt)
{ 
    var gmtMS = Date.parse(gmt); 
    var jsGMT = new Date(gmtMS); 
    var Hour = jsGMT.getHours(); 
    var Mins = jsGMT.getMinutes(); 
    var AmPm; 
    
    if (Hour >= 12) 
	    AmPm = 'pm'; 
    else  
	    AmPm = 'am'; 
	    
    if(Hour > 12)  
	    Hour = Hour -12;
    if(Hour == 0)  
	    Hour = 12;
    if(Hour < 10 )  
	    Hour = '0' + Hour;
    if(Mins < 10 )  
	    Mins = '0' + Mins;
	    
    return Hour + ':' + Mins + '&nbsp;' + AmPm; 
}

function RateSong(elem, station, songid, rating)
{
    var $parent = jQuery(elem).parents('.rating-wrapper');
    var $span = jQuery('<span></span>');
    $span.addClass('m');
    
    $parent.attr('class', 'rating-wrapper').addClass('Rating_' + rating);
    
    
    jQuery.get('/ajax/Vote.aspx?station=' + station + '&sid=' + songid + '&r=' + rating
        , null
        , function(data, status)
            {
                if(data == 'OK')
                    $span.html('Got It!');
                else
                    $span.html(data);
                    
                $parent.append($span);
                setTimeout(function() { $span.fadeOut(1000, function() { $span.remove(); }) }, 10000);
            }
        );
}

function RequestSong(elem, station, songid)
{
    $parent = jQuery(elem).parents('.rating-wrapper');
    var $span = jQuery('<span></span>');
    $span.addClass('m');
    
    jQuery.get('/ajax/Request.aspx?station=' + station + '&sid=' + songid
        , null
        , function(data, status)
            {
                alert(data);
            }
        );
}

function ToggleFAQ(elem)
{
    var $answer = jQuery(elem).parents("dl").find("dd");
    
    if($answer.is(':visible'))
    {
        $answer.slideUp(200);
    }
    else
    {
        $answer.slideDown(200);
    }
}

function EmbedLogin()
{
    jQuery('#EmbedE').hide();
    
    jQuery.get('/AjaxLogin.aspx?u=' + jQuery('#EmbedU').val() + '&p=' + jQuery('#EmbedP').val()
        , null
        , function(data, status)
        {
            if(data == '1')
            {
                jQuery('#EmbedE')
                    .html('Login Success!  Refreshing. Please wait.')
                    .fadeIn();
                    
                document.location.reload(true);
            }
            else
                jQuery('#EmbedE')
                    .html('Login Failed.  Try again, or <a href="http://www.1.fm/Register.aspx">Register.</a>')
                    .fadeIn();
        });
}

function EmbedLogout()
{
    jQuery('#EmbedE').hide();
    
    jQuery.get('/Logout.aspx'
        , null
        , function(data, status)
        {
            jQuery('#EmbedE')
                .html('Logged out!  Refreshing.  Please wait.')
                .fadeIn();
                
            document.location.reload(true);
        });
}