$(function () {
    $('#downloadResume a').click(function () {
        window.open('http://www.elimenop.com/EDeGruchy_resume_2011DL.pdf');
        return false;
    });

    $('#downloadPortfolio a').click(function () {
        window.open('http://www.elimenop.com/EDeGruchy_Portfolio2011.pdf');
        return false;
    });
});

function initTumblr(tumblr_api_read) {
    var tumblrPosts = [];
    $.each(tumblr_api_read['posts'], function () {
        var type = this["type"];
        var caption = '';
        var url = this['url'];
        var date = this['date'];
        var captionNeedsFormatting = true;

        switch (type) {
            case 'video':
                caption = this['video-caption'];
                break;
            case 'photo':
                caption = this['photo-caption'];
                break;
            case 'link':
                caption = this['link-text'];
                break;
            case 'regular':
                caption = this['regular-title'];
                captionNeedsFormatting = false;
                break;
        };

        tumblrPosts.push(
            {
                caption: caption,
                url: url,
                date: date,
                needsFormatting: captionNeedsFormatting
            }
        );
    });

    var formattedTumblrPosts = formatTumblrPosts(tumblrPosts);

    $('#tumblr').find('.feedInner').empty().append(formattedTumblrPosts);
}

function formatTumblrPosts(tumblrPosts) {
    var html = '';
    $.each(tumblrPosts, function () {
        var formattedCaption = this.caption;
        if (this.needsFormatting) {
            formattedCaption = formatCaption(this.caption)
        }
        else {
            if (formattedCaption.indexOf('<strong>') < 0) {
                formattedCaption = '<strong>' + formattedCaption + '</strong>';
            }
        }

        html += '<article class="feed">';
        html += '  <div class="feedText">' + formattedCaption + '</div>';
        html += '  <div><a href="' + this.url + '">' + this.url + '</a></div>';
        html += '  <div class="feedDate">' + this.date + '</div>';
        html += '</article>';
    });

    return html;
}

// Caption's coming back in format <p><strong>SOME HEADER</strong></p><p>Some descriptor</p>
function formatCaption(caption) {
    var secondParagraphStartIndex = caption.lastIndexOf('<p>');
    var title = caption.substring(0, secondParagraphStartIndex);
    var titleInnerStartIndex = '<p>'.length;
    var titleInnerEndIndex = title.indexOf('</p>');
    var innerTitle = title.substring(titleInnerStartIndex, titleInnerEndIndex);
    var description = caption.substring(secondParagraphStartIndex);
    var startIndexOfInnerDescription = '<p>'.length;
    var endIndexOfInnerDescription = description.lastIndexOf('</p>');
    var innerDescription = description.substring(startIndexOfInnerDescription, endIndexOfInnerDescription);
    if (innerDescription.length > 50) {
        innerDescription = getTruncatedHtmlString(innerDescription, 50);
    }

    var formattedDescription = '<p>' + innerDescription + '</p>';
    var foamattedInnerTitle = '<span>' + innerTitle + '</span>';
    return foamattedInnerTitle + formattedDescription;
}

function getTruncatedHtmlString(htmlText, numChars) {
    var truncatedString = '';
    var numIterations = numChars;

    if (htmlText.length < numChars) {
        // Short one
        return htmlText.length;
    }
    else {
        // Long one, need to format
        for (var i = 0; i < numIterations; i++) {
            truncatedString += htmlText[i];
        }

        var lastSpaceIndex = truncatedString.lastIndexOf(' ');
        truncatedString = truncatedString.substring(0, lastSpaceIndex);
        truncatedString += '...';
        return truncatedString;
    }
}

