marketplace_data_url = "http://360marketplaces.columbian.com/marketplace/api/v0/random_business_json/"
test = ""

function GetRandomBusinessInfo()
{
    completed_data_url = marketplace_data_url + "?number-requested=1&amp;photo-width=130&amp;photo-height=80&amp;business-type=enhanced&amp;callback=?";
    $.getJSON(completed_data_url, function(data){
        tb = data[0]["fields"];
        name = tb["name"];
        slug = tb["slug"];
        phone = tb["phone1"];
        thumb = tb["thumbnail"];
        RenderRandomBusinessInfo(name, phone, slug, thumb);
    })
}

function RenderRandomBusinessInfo(name, phone, slug, thumb)
{ 
    eto = $("#enhanced_business_info");
    eto.html("")
    roditel = $("#marketplacesWidget");

    var newInfoNode = $("<div></div>").attr("class", "businessInfo");
    if(thumb != null)
    {
        var newThumbnailNode = $("<img>").attr("class", "businessThumbnail").attr("src", thumb).css("float", "right");
        var newThumbnailAnchor = $("<a></a>").attr("href", "http://360marketplaces.columbian.com/marketplace/businesses/" + slug + "/").html(newThumbnailNode)
        newInfoNode.append(newThumbnailAnchor)
    }
    var newP = $("<p></p>");
    var newNameNodeAnchor = $("<a></a>").attr("href", "http://360marketplaces.columbian.com/marketplace/businesses/" + slug + "/").html(name + "&nbsp;&nbsp;");
    var newNameNode = $("<p></p>").attr("class", "businessName").html(newNameNodeAnchor)
    var newPhoneNode = $("<p></p>").attr("class", "businessPhone").text(phone);
    newP.append(newNameNode);
    newP.append(newPhoneNode);
    newInfoNode.append(newP);
    eto.append(newInfoNode);

    bodyText = "<p>Find more businesses on <a href='http://360marketplaces.columbian.com/marketplace/'>360Marketplaces</a>.</p>";
    var newCTA = $("<p></p>").attr("class", "businessCTA").html(bodyText);
    eto.append(newCTA)

    var categories = new Array()
    baseCategoryUrl = "http://360marketplaces.columbian.com/marketplace/categories/";
    categories[0] = "Arts & Entertainment|arts-and-entertainment/";
    categories[1] = "Bars|arts-and-entertainment/bars-clubs/";
    categories[2] = "Theaters|arts-and-entertainment/theatre/";
    categories[3] = "<br />"
    categories[4] = "Restaurants|food-and-dining/restaurants/";
    categories[5] = "Services|services";
    categories[6] = "Real Estate|real-estate-rentals";
    potentio = $("<div><div>").attr("id", "businessCategoryList");
    n = 0;
    while(n < categories.length)
    {
        var thisCat = categories[n];
        var pipeIndex = thisCat.indexOf("|");
        if(pipeIndex != -1)
        {
            var catAnchor = $("<a></a>").attr("href", baseCategoryUrl + thisCat.substr(pipeIndex + 1)).text(thisCat.substr(0, pipeIndex));
        }
        else
        {
            var catAnchor = $(thisCat)
        }
        potentio.append(catAnchor);
        n = n + 1;
    }
    roditel.append(potentio);
}

$(document).ready(function(){
    GetRandomBusinessInfo()
}
);
