// search dropdown
$(function() {
    $('#q').focus(function() {
        if($('#q').val() == 'Search...')
            $('#q').attr('value','');
        $('#searchlist').fadeIn(600); 
    });
    $('#q').blur(function() {
        if($('#q').val() == '')
            $('#q').attr('value','Search...');
        $('#searchlist').animate({opacity:1}, 3000, function() {
            $(this).fadeOut(300);
        });
    });
});

// slideshow
$(window).load(function() {
    $('#slider').nivoSlider({
        effect:'fade', //Specify sets like: 'fold,fade,sliceDown'
        slices:15,
        animSpeed:500,
        pauseTime:5000,
        startSlide:0, //Set starting Slide (0 index)
        directionNav:true, //Next and Prev
        directionNavHide:true, //Only show on hover
        controlNav:false, //1,2,3...
        controlNavThumbs:false, //Use thumbnails for Control Nav
        controlNavThumbsFromRel:false, //Use image rel for thumbs
        controlNavThumbsSearch: '.jpg', //Replace this with...
        controlNavThumbsReplace: '_thumb.jpg', //...this in thumb Image src
        keyboardNav:true, //Use left and right arrows
        pauseOnHover:true, //Stop animation while hovering
        manualAdvance:false, //Force manual transitions
        captionOpacity:0.8, //Universal caption opacity
        beforeChange: function(){},
        afterChange: function(){},
        slideshowEnd: function(){} //Triggers after all slides have been shown
    });
});

function popup(url) {
    newwindow=window.open(url,'name','height=720,width=620,scrollbars=yes,resizable=yes');
    if (window.focus) {newwindow.focus()}
    return false;
}

// event loader
$(function() {
    var today = new Date();
    var lastDay = new Date();
    lastDay.setDate(today.getDate() +90);

    // Set the start variable
    var startYear = today.getFullYear();
    var startMonth = today.getMonth()+1;
    if(startMonth < 10) { startMonth = "0"+startMonth; }
    var startDate = today.getDate();
    if(startDate < 10) { startDate = "0"+startDate; }
    var start = startYear+'-'+startMonth+'-'+startDate +' 00:00:00';
   
    //set the end variable
    var endYear = lastDay.getFullYear();
    var endMonth = lastDay.getMonth()+1;
    if(endMonth < 10) { endMonth = "0"+endMonth; }
    var endDate = lastDay.getDate();
    if(endDate < 10) { endDate = "0"+endDate; }
    var end = endYear+'-'+endMonth+'-'+endDate +' 00:00:00';
    loadEvents(start, end);

    $("#calendar_widget").datepicker({
        dateFormat: 'yy-mm-dd',
        hideIfNoPrevNext: true,
        maxDate: '+3m',
        minDate: new Date(),
        onSelect: function(date, inst) {
            var start = date +" 00:00:00";
            
            var endDay = new Date(date);
            endDay.setDate(endDay.getDate() +6);
            var eYear = endDay.getFullYear();
            var eMonth = endDay.getMonth()+1;
            if(eMonth < 10) { eMonth = "0"+eMonth; }
            var eDate = endDay.getDate();
            if(eDate < 10) { eDate = "0"+eDate; }
            var end = eYear+'-'+eMonth+'-'+eDate +' 00:00:00';
            loadEvents(start, end);
            
            $('#inner_events').fadeOut(500).remove();
        }
    });
});

function loadEvents(start, end) {
    $.ajax({
        type: "GET",
        url: "/ssi/xmlgrabber/block-calendar.xml",
        dataType: "xml",
        success: function(xml) {
            var i = 0;
            var html = '<div id="inner_events">';
            $(xml).find('event').each(function() {
                var isoDate = $(this).find('isodate').text();
                var title = $(this).find('title').text();
                var eventDate = $(this).find('date').text();
                var ppurl = $(this).find('ppurl').text();
                var time = $(this).find('time').text();
                var location = $(this).find('location').text();
                var description = $(this).find('description_html').text().substring(0,95) + '...';
                // Check dates
                if(isoDate >= start && isoDate <= end && i < 5) {
                    html += '<h3><a href="" onclick="viewEvent('+"'"+ ppurl +"'"+'); return false;">'+ title +'</a></h3>';
                    html += '<div class="event-date">'+ eventDate +', ' + time +'</div><hr />';
                    
                    i++;
                }
            });
            if (i == 0) {
                html += '<p>No events for selected date.</p>';
            } else {
                // lop off last <hr>
                html = html.substring(0,html.length - 6);
            }

            html += '</div>';
            $('#events').append(html).hide().fadeIn('slow');
        }
    });
}

function viewEvent(url) {
    eventwindow = window.open(url, 'event', 'width=575,height=460,scrollbars=yes,resizable=yes,location=yes');
    if (window.focus) {eventwindow.focus()};
    return false;
}
