﻿$(document).ready(function() {
    var initialContent = $('#masonrywrapper .video');
    fixmedia(initialContent);
});

function fixmedia(elements) {
    var videos = $('#masonrywrapper').find('.video');
    var vidServed;
    var vidServedWidth;
    var vidServedHeight;
    videos.each(function() {
        var video = $(this);
        vidServed = video.find('object');
        // Found the video
        if (vidServed.length !== -1) {
            vidServedWidth = vidServed.width();
            vidServedHeight = vidServed.height();
            video.css('width', vidServedWidth + 'px');
            video.css('height', vidServedHeight + 'px');
        }
    });
    // No video
    //alert('test');
}

$(window).load(function() {
    var initialVideos = $('#masonrywrapper .video');
    fixmedia(initialVideos);
    $('#masonrywrapper').masonry({
        itemSelector: '.HomepagePosts',
        columnWidth: 330,
        saveOptions: false
    });

    $('#masonrywrapper').infinitescroll({
        navSelector: "#page_nav",    // selector for the paged navigation
        nextSelector: "#page_nav a",    // selector for the NEXT link (to page 2)
        itemSelector: ".HomepagePosts",       // selector for all items you'll retrieve
        loadingImg: '/css/images/logo_bottom.jpg',
        donetext: "No more pages to load.",
        animate: true,
        debug: true,
        errorCallback: function() {
            // fade out the error message after 2 seconds
            $('#infscr-loading').animate({ opacity: .8 }, 2000).fadeOut('normal');
        }
    },

    // call masonry as a callback
    function(newElements) {
        //fixmedia(newElements);
        var gascript = document.createElement('script');
        gascript.type = 'text/javascript'; gascript.src = '/js/ga.js';
        $('#masonrywrapper').last().after(gascript);
        setTimeout(function() {
            $('#masonrywrapper').masonry({
                appendedContent: $(newElements)
            });
        }, 1000);
    });
});



$(window).ready(function() {
    $('.footerright').click(function(e) {
        e.preventDefault();
        $('html, body').animate({ scrollTop: 0 }, 'slow');
    });
});

$(window).ready(function() {
    $('#footerSlidetop').click(function(e) {
        e.preventDefault();
        $('html, body').animate({ scrollTop: 0 }, 'slow');
    });
});

$(function() {
    $('#searchbox').keypress(function(e) {
        if (e.which == 13) {
            var searchtext = $(this).val();
            searchtext = searchtext.toLowerCase().replace(/ /g, '-').replace(/[^\w-]+/g, '');
            window.location = '/search/' + searchtext;
            return false;
        }
    });
});
