﻿function getBlog(numberOfPosts, HTMLElementToPostTo) {

    var MYBLOG_LIMIT = numberOfPosts;
    var MYWRAPPER_ID = HTMLElementToPostTo;

    var WP = { open: function (b) { var a = { posts: function () { var d = MYBLOG_LIMIT; var e = 0; var c = { all: function (g) { var f = b + "/api/get_recent_posts/"; f += "?count=" + d + "&page=" + e + "&callback=?"; jQuery.getJSON(f, function (l) { var k = l.posts; for (var j = 0; j < k.length; j++) { var h = k[j]; h.createComment = function (i, m) { i.postId = h.id; a.comments().create(i, m) } } g(k) }) }, findBySlug: function (f, h) { var g = b + "/api/get_post/"; g += "?slug=" + f + "&callback=?"; jQuery.getJSON(g, function (i) { h(i.post) }) }, limit: function (f) { d = f; return c }, page: function (f) { e = f; return c } }; return c }, pages: function () { var c = { findBySlug: function (d, f) { var e = b + "/api/get_page/"; e += "?slug=" + d + "&callback=?"; jQuery.getJSON(e, function (g) { f(g.page) }) } }; return c }, categories: function () { var c = { all: function (e) { var d = b + "/api/get_category_index/"; d += "?callback=?"; jQuery.getJSON(d, function (f) { e(f.categories) }) } }; return c }, tags: function () { var c = { all: function (e) { var d = b + "/api/get_tag_index/"; d += "?callback=?"; jQuery.getJSON(d, function (f) { e(f.tags) }) } }; return c }, comments: function () { var c = { create: function (f, e) { var d = b + "/api/submit_comment/"; d += "?post_id=" + f.postId + "&name=" + f.name + "&email=" + f.email + "&content=" + f.content + "&callback=?"; jQuery.getJSON(d, function (g) { e(g) }) } }; return c } }; return a } };

    var blog = WP.open('http://www.thesalonfocus.com/');
    blog.posts().all(function (posts) {
        for (var i = 0; i < posts.length; i++) {
            jQuery('#' + MYWRAPPER_ID).append(function () {

                //get wordpress date and remove timestamp
                $date = posts[i].date;
                $postDateandTimeArray = $date.split(" ");
                //split date by hypen to separate year, month, date
                $postDateArray = $postDateandTimeArray[0].split("-");
                var postDate = new Date($postDateArray[0], $postDateArray[1]-1, $postDateArray[2]); //year, month, date

                var strHTML = "";
                var strPostContent = posts[i].content;
                var intIndex = strPostContent.indexOf("<p>");
                strPostContent = strPostContent.substring(intIndex + 3, strPostContent.length);
                //remove all html tags and get only first 200 characters
                strPostContent = strPostContent.replace(/(<([^>]+)>)/ig, "");
                strPostContent = strPostContent.substring(0, 200);
                strHTML = "<h4 class='blogTitle'>" + posts[i].title + "</h4>";
                if (postDate != "NaN" && postDate != "Invalid Date") {
                    strHTML += "<p class='blogDate'>" + postDate.toLocaleDateString() + "</p>";
                }
                strHTML += "<div class='blogExcerpt'>" + strPostContent;
                strHTML += "... &nbsp; &nbsp; <a class='blogLink' href='" + posts[i].url + "' target='_blank'>read more</a></div>";
                return strHTML;
            });
        }
    });
};
