/*
* Two part script. The first part writes out banner images onto the page.
* The second starts when the document is loaded and cycles those images randomly.
*/
document.write("<div class='banner'>"); // Write .banner div.

// Write out whatever images to the document.

// Advertising Banners

image(
"http://apps.facebook.com/inthemafia/status_invite.php?from=659582845&zy_track=newsfeed&recruit_feed=1&sendkey=0f43576e1e7c5a5c3ee0ccd5e2632a31%24%24hhO1VWWW2YLP0if9yNJE%21P_xfPSsGuSMG5Mo9i85%21ES5gl.eB-1iI%2CopmR.BVo&ztrack_category=recruit_mafia_needed_stream&sendtime=1259317783&friend=659582845&ref=nf",
"_blank",
"http://i198.photobucket.com/albums/aa2/djtbone001a/posters/Mafia.png",
"Join Our Mafia on Facebook",
"Join Our Mafia on Facebook",
"0",
"700", "500");

image(
null,
null,
"http://i198.photobucket.com/albums/aa2/djtbone001a/posters/Dreamscape_Bulletins.png",
"Dreamscape Radio Bulletins",
"Dreamscape Radio Bulletins",
null,
"700", "500");

//End banner List

document.write("</div>"); // Close div.

// Writes an <img> tag wrap inside a <a>. Pass null for default values.
function image(href, target, src, title, alt, border, width, height) {
    href = quote('href', href);
    target = quote('target', target);
    src = quote('src', src); // Use the quote function to format each attribute.
    title = quote('title', title);
    alt = quote('alt', alt);
    border = quote('border', border);
    width = quote('width', width);
    height = quote('height', height);

     // Set as invisble. The script will show them.
    var style = quote('style', 'display: none;');
    
    // Concat the <img> tag and write it out to the document.
    document.write('<a' + href + target + '>');
    document.write('<img' + src + title + alt + width + height + border + style + '/>');
    document.write('</a>');
}

// Takes a key value pair and formats it like (space)key='value'(space) for html attributes.
function quote(attribute, value) {
    if (value && attribute) // Only if null wasn't passed.
        return " " + attribute + "='" + value + "'" + " ";

    return ''; // Return blank if null was passed.
}

/*
* Second part of script. Should only run once.
*/

// Check pinstance.
if (!pinstance) {
    var pinstance = true;
    var minutes = 1; // How many minutes between updates.
    var currentbanner = 0; // Banner to change next.
    var banners; // jQuery selection of .banner divs
    
    // When document is loaded.
    $(document).ready(function () {
        banners = $('.banner'); // Select .banner divs.
        changeBanners(banners); // Change banners to bring up first images.
        setInterval('updateNext()', (1000 * 60) * minutes); // Now update every now and then.
    });
}

// Updates the currentbanner.
function updateNext () {
    // Select current banner and change it.
    changeBanners(banners.eq(currentbanner));
    
     // Cycle to next banner.
    currentbanner = (currentbanner + 1) % banners.length;
}

// Takes a jquery selection of banners and updates them.
function changeBanners (banners) {
    // $().each() the entire selection.
    banners.each(function (index) {
        // Find the hidden images inside this .banner.
        var hidden = $(this).find('img:hidden');
        
        // If no images there's nothing to do.
        if (hidden.length == 0) return; 
        
         // Pick a hidden image.
        var nextimage = hidden.eq(Math.ceil(Math.random() * hidden.length) - 1);
        $(this).find('img:visible').hide(); // Hide the visible one.
        nextimage.show(); // Show the next one.
    });
}