﻿function jmsajaxurl(options) {
    var url = options.url;
    url += "/" + options.method;
    if (options.data) {
        var data = ""; for (var i in options.data) {
            if (data != "")
                data += "&"; data += i + "=" + msJSON.stringify(options.data[i]);
        }
        url += "?" + data; data = null; options.data = "{}";
    }
    return url;
};

function UpdateNowPlayingk() {

    var url = jmsajaxurl({
        url: "http://objects.klove.com/services/broadcast.asmx",
        method: "GetRecentSongs",
        data: { SiteId: 1 }
    });

    $.ajax({
        url: url + "&format=json",
        jsonpCallback: "GetRecentSongsK",
        dataType: "jsonp",
        cache: true,
        success: function(msg) {
            UpdateNowPlaying(msg, 1);
        }
    });
}

function UpdateNowPlayinga() {

    var url = jmsajaxurl({
        url: "http://objects.klove.com/services/broadcast.asmx",
        method: "GetRecentSongs",
        data: { SiteId: 2 }
    });

    $.ajax({
        url: url + "&format=json",
        jsonpCallback: "GetRecentSongsA",
        dataType: "jsonp",
        cache: true,
        success: function(msg) {
            UpdateNowPlaying(msg, 2);
        }
    });
}

function UpdateNowPlaying(data, siteIndex) {
    var divID = null;
    if (siteIndex == 1)
        divID = 'k';
    else
        divID = 'a';
        
    var songInfo = $(data.d)

    if (songInfo.length > 0) {
        var maxStringLength = 20;

        if (songInfo[0].Lyrics.length > 0)
            $('#' + divID + 'NowPlaying').html('<a href="' + songInfo[0].Lyrics + '" onclick="openLyrics(\'' + songInfo[0].Lyrics + '\');return false;">' + songInfo[0].Title + '</a>');
        else
            $('#' + divID + 'NowPlaying').text(songInfo[0].Title);

        if (songInfo[0].ArtistLink.length > 0)
            $('#' + divID + 'Artist').html('<a href="' + songInfo[0].ArtistLink + '" target="_blank">' + songInfo[0].Artist + '</a>');
        else
            $('#' + divID + 'Artist').text(songInfo[0].Artist);

        if (songInfo[0].AlbumLink.length > 0) {
            $('#' + divID + 'Album').html('<a href="' + songInfo[0].AlbumLink + '" target="_blank">' + songInfo[0].Album + '</a>');
        }
        else {
            $('#' + divID + 'Album').text(songInfo[0].Album);
        }
    }

    setTimeout("UpdateNowPlaying" + divID + "()", 30000);
}

function UpdateShowInfok() {
    var url = jmsajaxurl({
        url: "http://objects.klove.com/services/broadcast.asmx",
        method: "GetCurrentShow",
        data: { SiteId: 1 }
    });

    $.ajax({
        url: url + "&format=json",
        jsonpCallback: "GetCurrentShowK",
        dataType: "jsonp",
        cache: true,
        success: function(msg) {
            updateShowInfo(msg, 1);
        }
    });
}
function UpdateShowInfoa() {
    var url = jmsajaxurl({
        url: "http://objects.klove.com/services/broadcast.asmx",
        method: "GetCurrentShow",
        data: { SiteId: 2 }
    });

    $.ajax({
        url: url + "&format=json",
        jsonpCallback: "GetCurrentShowA",
        dataType: "jsonp",
        cache: true,
        success: function(msg) {
            updateShowInfo(msg, 2);      
        }
    });
}

function updateShowInfo(data, siteIndex) {
    var divID = null;
    if (siteIndex == 1)
        divID = 'k';
    else
        divID = 'a';
    
    var showInfo = $(data.d)

    if (showInfo.length > 0) {
        if (showInfo[0].Link != null && showInfo[0].Link.length > 0) {
            $('#' + divID + 'OnAir').html('<a href="http://www.klove.com' + showInfo[0].Link + '" target="_blank"><strong>' + showInfo[0].Title + '</strong></a>');
        }
        else {
            $('#' + divID + 'OnAir').html('<strong>' + showInfo[0].Title + '</strong>');
        }
    }

    setTimeout("UpdateShowInfo" + divID + "()", 30000);
}

$(document).ready(function() {
    //Only update Now Playing if client is not on an SSL page...
    if (window.location.protocol != "https:") {
        UpdateNowPlayingk();
        UpdateNowPlayinga();
        UpdateShowInfok();
        UpdateShowInfoa();
    }
});   
