$(document).ready(function () {
    $('.more-posts').click(show_more_posts);
    $('#twitter_orbit').orbit({
        timer: false,
        animationSpeed: 300
    });

    $("#logo").hover(function () {
        $("#background").animate({opacity: 0.85}, 500);
    }, function () {
        $("#background").animate({opacity: 0.2}, 500);
    });

    if (typeof gallery != 'undefined') {
        var slides = [];

        $(gallery).each(function () {
            //$('#homepage_slider').append($('<div class="photo-slide"><a class="rsImg" href="' + this.guid + '" /></a></div>'))
            if (this.post_excerpt !== "") {
                $('#homepage_slider').append($('<div class="photo-slide"><a href="' + this.post_excerpt + '"><img class="rsImg" src="' + this.src + '" /></a></div>'))
            } else {
                $('#homepage_slider').append($('<div class="photo-slide"><a class="rsImg" href="' + this.src + '"></a></div>'))
            }
        });
        window.onload = function () {
            $('#homepage_slider').royalSlider({
                keyboardNavEnabled: true,
                //autoHeight: true,  - This makes royalslider try to size the box height - when we have an exact size we want to keep it
                imageScaleMode: 'fill',
                autoScaleSlider: true,
                autoScaleWidth: 970,
                //autoScaleHeight: 466,
                autoHideArrows: false,
                navigateByClick: false
            });
        };
        $('#homepage_slider').find('.rsOverflow').css('-webkit-transition', 'none');
    }
    window.onload = function () {
        $('.royalSlider, .rsOverflow').height($('.royalSlider .rsSlide img').height() + 50);
    };
    // Make excerpt boxes minimum height without compromising row alignment.
    $(window).resize(function () {

        $('.royalSlider, .rsOverflow').height($('.royalSlider .rsSlide img').height() + 50);
        $('.media-box-row .excerpt-box').css('height', '');
        // Only make the adjustment if not on mobile layout.
        if ($(document).width() > 767) {
            setTimeout(function () {
                // Check the boxes in each row against each other.
                $('.media-box-row').each(function () {
                    var rowHeight = 0;
                    $(this).find('.excerpt-box').each(function () {
                        if ($(this).outerHeight() > rowHeight) {
                            rowHeight = $(this).outerHeight();
                        }
                    });
                    // Change the height of all the row's excerpt boxes to the tallest height.
                    if (rowHeight > 0) {
                        $(this).find('.excerpt-box').css('height', rowHeight);
                    }
                });
            }, 300);
        }
    }).resize();


    //------------------------------------//
    //   ACTIVE MENU ITEM MODIFICATIONS   //
    //------------------------------------//

    // Do main nav parent highlighting for single pages.
    var subpageTypes = [
        ['blog', 'news'],
        ['single-tour_dates', 'tour'],
        ['single-albums', 'albums'],
        ['single-photo_gallery', 'media'],
        ['single-video_items', 'media'],
        ['single-projects', 'projects']
    ];
    for (var i = 0; i < subpageTypes.length; i++) {
        if ($('body').hasClass(subpageTypes[i][0])) {
            $('.main-nav li a').each(function () {
                if ($(this).html().trim().toLowerCase() == subpageTypes[i][1]) {
                    $(this).parent().addClass('active');
                }
            })
        }
    }

    // Same thing for tour dates subnav.
    var months = {
        january: '01',
        february: '02',
        march: '03',
        april: '04',
        may: '05',
        june: '06',
        july: '07',
        august: '08',
        september: '09',
        october: '10',
        november: '11',
        december: '12'
    }
    function checkDate_Time(time) {
        return (time < 10) ? ("0" + time) : time;
    }
    if ($('body').hasClass('single-tour_dates')) {
        var tourDate = $('.date-container .year').html().trim() +
                eval("months." + $('.date-container .month').html().trim().toLowerCase()) +
                $('.date-container .day').html().trim();

        var today = new Date();
        today = today.getFullYear() + checkDate_Time(today.getMonth()) + checkDate_Time(today.getDate());

        if (tourDate >= today) {
            $('.subnav .upcoming-tour-link').addClass('selected');
        } else {
            $('.subnav .past-tour-link').addClass('selected');
        }
    }
});
//var returned_posts;

function show_more_posts() {
    var el = $(this);
    if (el.data('loading'))
        return false;
    el.data('loading', true);
    //console.log(el.data());
    var hadText = el.html();
    var params = $(this).data();
    //console.log(params.paged);

    el.text('Loading...')
    params['action'] = 'more_posts'
    params['total_posts_so_far'] = $(".ajax_grid_item").length;
    //console.log(params);
//    console.log(params);
//    return;
    $.post('/wp-admin/admin-ajax.php', params, function (response_data) {
        //console.log(response_data);
        var posts = response_data.posts;
//            returned_posts = posts;
        var offset = params['total_posts_so_far'] + $(posts).filter(".ajax_grid_item").length;
        /*console.log("total so far " + params['total_posts_so_far']);
         console.log("returned count " + $(posts).filter(".ajax_grid_item").length);
         console.log("offset " + offset);*/
        var num_remaining_posts = response_data.num_remaining_posts;
        var wrap = $('<div class="moreposts-wrap"></div>').html(posts);//.hide();
        $(params.contentselector).append(wrap);
        //wrap.slideDown(3000);
        //var next_page =  parseInt(params['paged']) + 1;
        //el.data('paged', next_page);
        //el.data('offset', parseInt(params['paged']) * params["offset"]);

        el.data('offset', offset);
        el.html(hadText).data('loading', false);

        if (num_remaining_posts == 0) {
            el.remove();
        }
    }, "json"
            );

}

