$(document).ready(function() {
    // Stock delay of 8sec between features (slides)
    var delay = 8;
    
    // xml, player, width, height are required jQuery params on #showcase
    var params = jQuery.data($("#showcase")[0], "params");
    if (params === undefined) {
        return;
    }
    
    // IE gets the flash player
    if ($.browser.msie) {
        $('<h2>Features</h2><div id="swf">Roadside Features requires the <a href="http://www.adobe.com/products/flashplayer/">Flash Player</a>.</div>').appendTo('#showcase');
        var so = new SWFObject("/lib/flash/roadside.swf", "dl", params.width, params.height, delay, "#FFFFFF", 1);
        so.addParam("menu", "false");
        so.addVariable("dataFile", "/xml/"+params.xml+".xml");
        // + "?nocache=" + new Date().getTime());
        so.write("swf");
       return;
    }
    
    // All other browsers get Awkward Showcase
    // http://www.awkwardgroup.com/sandbox/awkward-showcase-a-jquery-plugin/
    $.ajax({
        type: "GET",
        cache: false,
        url: "/xml/"+params.xml+".xml",
        dataType: "xml",
        success: function(xml) {
            // Add the player type to #showcase as a class
            $("#showcase").addClass("showcase-player-"+params.player);
            
            $(xml).find('promo').each(function(){
                // Grab bits and pieces out of the XML
                var photo = $(this).find('photo').text();
                var headline = $(this).find('headline').text();
                var tagline = $(this).find('tagline').text();
                var quadrant = $(this).find('quadrant').text().toLowerCase();
                var linktext = $(this).find('linktext').text();
                var url = $(this).find('url').text();
                var pause = $(this).find('pause').text();
                var index = $(this).index()+1;

                // "Set up us the markup" needed by Awkward Showcase
                $('<div class="showcase-slide"><div class="showcase-content"><div class="showcase-content-wrapper"><a href="'+url+'"><img src="'+photo+'"></img></a></div></div><div class="showcase-caption"><div class="showcase-caption-quadrant-'+quadrant+'"><a href="'+url+'"><span class="showcase-caption-headline">'+headline+'</span><span class="showcase-caption-tagline">'+tagline+'</span></a></div><div class="showcase-caption-quadrant-br"><a href="'+url+'"><span class="showcase-caption-linktext">'+linktext+'</span></a></div></div></div>').appendTo('#showcase');
            });

            // Awkward Showcase uses a separate nav height which we must subtract from our content height.
            var navHeight = 30; // Default
            if (params.player == "classic") {
                navHeight = 30;
            }

            // More or less the classic player params - may change later on with other designs.
            $("#showcase").awShowcase({
                content_width: params.width,
                content_height: params.height-navHeight,
                auto: true,
                interval: delay*1000,
                continuous: true,
                arrows: false,
                transition: 'fade',
                buttons: true,
                btn_numbers: true,
                stoponclick: false
            });

            // Awkward Showcase also doesn't show a pointer on the nav. This fixes that ish.
            $('.showcase-button-wrapper span').css('cursor', 'pointer');
        }
    });
});
