var schedPage = '';
$(document).ready(function(){

    // all pages need:
    initCufon();
    initSound();

    // some pages need:
    var page = $('body').attr('id');
    if (page != 'schedule') initSchedule();

});

function initSchedule() {

    // 'success' happens before 'complete', and response isnt populated until 'complete'
    // so save off data for later usage
    $.ajax({
        type: 'GET',
        url:'schedule.html',
        success: function(data,status) { schedPage = $('#dates',data).html(); },
        complete: populateScheduleWidget
    });
}

function populateScheduleWidget(resp,textStatus) {

    var events = new Array();
    var serverDate = '';
    var eventDate = '';
    var output = '';
    var d = new Date();

    // IE doesnt give you the server date stamp. yay.
    if (resp.getResponseHeader('Date').length>0) d = new Date( Date.parse(resp.getResponseHeader('Date')) );

    // make server date stamp int
    serverDate = d.getFullYear()+''; // force string
    if ( (d.getMonth()+1) < 10 ) serverDate += '0';
    serverDate += d.getMonth()+'';
    if ( d.getDate() < 10 ) serverDate += '0';
    serverDate += d.getDate()+'';
    serverDate = parseInt(serverDate);

    // find relevant events
    $('.event',schedPage).each(function() {

        eventDate = parseInt($('h3',this).attr('id').replace(/d/,''));

        if (eventDate >= serverDate) {
            events.push({
                displayDate: $.trim( $('h3',this).html() ),
                date: eventDate,
                character: $.trim( $('.author strong',this).html() ),
                event: $.trim($('.name',this).html() )
            });
        }
    });


    // make widget
    if (events.length) {
        output = '<ul>';

        $(events).each(function () {
            output += '<li><a href="schedule.html#'+this.date+'" title="Select this link to view '+this.displayDate+'">';
            output += '<strong>'+this.displayDate+'</strong><br />';
//            output += this.character+', ';
            output += this.event;
            output += '</a></li>';
        });

        output += '</ul>';
    }


    // if output, add and fade in
//    alert (output);
    if (output) $('#scheduleWidget').append(output).fadeIn();

}

function initSound() {
    if ($('#audioWidget li').length>0) {
        soundManager.url = 'audio/';

        threeSixtyPlayer.config = {
          playNext: false,
          autoPlay: false,
          loadRingColor: '#ccc',
          playRingColor: '#000',
          backgroundRingColor: '#eee',
          imageRoot: 'js/360/',
          animDuration: 500,
          animTransition: Animator.tx.bouncy
        }

        // shuffle sound widget items
        $('#audioWidget li').hide();

        var num1=0;
        var num2=0;

        while(num1 == num2) {
            num1=Math.ceil(Math.random()*$('#audioWidget li').length);
            num2=Math.ceil(Math.random()*$('#audioWidget li').length);
        }

        $('#audioWidget .item'+num1).fadeIn();
        $('#audioWidget .item'+num2).fadeIn();

    }

}

function initCufon() {
    var elementsPC = new Array( 'h2' );
    var elementsCandara = new Array( '#nav li a','h3','#press #content .subheading','#contact p','table th','#left .ui360 strong' );

    Cufon.replace(elementsPC,{ separate:'none',fontFamily: 'Plantagenet Cherokee' });
    Cufon.replace(elementsCandara,{ separate:'none',fontFamily: 'Candara' });
}
