RSAB_PLAYER_WINDOW_NAME = 'rsablisten'

function rsab_launch_player(options_or_show) {
    var options = {}
    if(typeof options_or_show == 'string') {
        options.show = options_or_show
        var autoplay = true
        if(arguments.length > 1) {
            BOOL_TRUE = ['t', 'true', 'y', 'yes', 'on', '1', 1, true]
            autoplay = arguments[1]
            if(typeof autoplay == 'string') {
                autoplay = autoplay.toLowerCase()
            }
            autoplay = (BOOL_TRUE.indexOf(autoplay) != -1)
        }
        options.autoplay = autoplay
    }
    else if(typeof options_or_show == 'object') {
        options = options_or_show
    }

    var base_href = 'http://listen.rsab.org/index.html'
    var parameters = []
    for(var key in options) {
        parameters.push( encodeURIComponent(key) + '=' + encodeURIComponent(options[key]) )
    }
    var url = base_href
    if(parameters.length) {
        url += '?' + parameters.join('&')
    }

    return window.open(url, RSAB_PLAYER_WINDOW_NAME, 'width=820,height=680,resizable=yes')
}

if(typeof jQuery != 'undefined') {
    $(function () {
        $('.rsab-launch').each(function () {
            var node = $(this)
            var options = {}
            var classes = node.attr('class').split(/\s+/)
            $.each(classes, function(index, item) {
                if(!item.slice(0, 12) == 'rsab-launch-') {
                    return
                }
                item = item.slice(12)
                if(item.slice(0, 4) == 'day-') {
                    options.day = item.slice(4)
                }
                else if(item.slice(0, 5) == 'show-') {
                    options.show = item.slice(5)
                }
                else if(item.slice(0, 5) == 'find-') {
                    options.find = item.slice(5)
                }
                else if(item == 'autoplay') {
                    options.autoplay = true
                }
            })
            node.css('cursor', 'pointer').click(function () {
                rsab_launch_player(options)
            })
        })
    })
}

