
$(document).ready(function () {
    $('#eventCalendar').fullCalendar({
        editable: false,
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'month,agendaWeek,agendaDay'
        },
        events: $.fullCalendar.calendarData('/BlogJson.pxml'),
        eventClick: function (calEvent, jsEvent, view) {
            var detailUrl = 'http://www.sunvalley.com/BlogGetPost.pxml';
            var parms = {
                'ArticleID': calEvent.articleID
            }
            var detail = $('#eventDetailTemplate').clone();
            $.fancybox.showActivity();
            $.getJSON(detailUrl, parms, function (data) {
                $.fancybox.hideActivity();
                if (data.methodResponse.params.param.value.struct) {
                    var articleHtml = data.methodResponse.params.param.value.struct.member[3].value.string;
                    articleHtml = articleHtml.replace(/\[caption[^\]]*\]/g, ''); //rip out caption
                    articleHtml = articleHtml.replace(/\[\/caption\]/g, ''); //rip out caption

                    detail.children('.eventDetail').children('.title').html(calEvent.title); //alert(calEvent.start);
                    detail.children('.eventDetail').children('.startDate').html(
								$.fullCalendar.formatDate(calEvent.start, "dddd', 'MMMM' 'dS' 'yyyy', 'h':'mmtt")
							);
                    $('.eventDetail > .footer .footerUrl', detail).attr('href', calEvent.url_alt);
                    //detail.children('.eventDetail').children('.eventBody').html(articleHtml);
                    //detail.children('.eventDetail > .eventBody').html(articleHtml);
                    $('.eventDetail > .eventBody', detail).html(articleHtml);

                    var x = 0;
                    //detail.children('.eventDetail').children('.eventBody > img').each(function() {
                    $.each($('.eventDetail > .eventBody img', detail), function (i, element) {
                        x = x + 1;
                        var maxWidth = 250; // Max width for the image
                        var maxHeight = 250;    // Max height for the image
                        var ratio = 0;  // Used for aspect ratio
                        var width = element.width;    // Current image width
                        var height = element.height;  // Current image height
                        //alert(width + ' ' + height);
                        // Check if the current width is larger than the max
                        if (width > maxWidth) {
                            ratio = maxWidth / width;   // get ratio for scaling image
                            $(this).css("width", maxWidth); // Set new width
                            $(this).css("height", height * ratio);  // Scale height based on ratio
                            height = height * ratio;    // Reset height to match scaled image
                            width = width * ratio;    // Reset width to match scaled image
                        }

                        // Check if current height is larger than max
                        if (height > maxHeight) {
                            ratio = maxHeight / height; // get ratio for scaling image
                            $(this).css("height", maxHeight);   // Set new height
                            $(this).css("width", width * ratio);    // Scale width based on ratio
                            width = width * ratio;    // Reset width to match scaled image
                        }
                    });
                    //alert(x);

                    $.fancybox({
                        'content': detail.html(),
                        'autoDimensions': true,
                        'autoScale': true,
                        'centerOnScroll': true,
                        'scrolling': 'no'/*,
								'width'		: 800,
								'height'		: 600*/
                    });
                }
            });
        }
    });

    $('#category').change(function () {
        var category = $(this).val();
        calendarCategory.updateCategoryFilter(category);
        $('#eventCalendar').fullCalendar('refetchEvents');
        //$('#eventCalendar').fullCalendar('next');
    });
}); 
