﻿$(document).ready(function() {
    var fl = new FeaturedListings();
    fl.getfeaturedlisting(function(response){
        var cityName = response.result.Table.rows[0][0];
        var stateCode = response.result.Table.rows[0][1];
        var price = response.result.Table.rows[0][2];
        var priceHigh = response.result.Table.rows[0][3];
        var priceLow = response.result.Table.rows[0][4];
        var vrmYN = response.result.Table.rows[0][5];
        var pictureURL = response.result.Table.rows[0][6].replace(/\/Large/g,'/Small');
        var landingPage = response.result.Table.rows[0][7];
        var customPictureURL = response.result.Table.rows[0][8];
        $(".featured-city").html(cityName + ", " + stateCode);
        var pictureContainer = document.createElement("img");
        if(customPictureURL.length > 5){        
            pictureContainer.src = customPictureURL;
        } else {
            pictureContainer.src = pictureURL;
        }
        $(".featured-image").html(pictureContainer);
        if(vrmYN == "N"){
            $(".featured-novrm").css("display","block");
            $(".novrm-price").html(formatPrice(price));
        } else {
            $(".featured-vrm").css("display","block");
            $(".vrm-low").html(formatPrice(priceLow));
            $(".vrm-high").html(formatPrice(priceHigh));
        }
        var lpContainer = document.createElement("a");
        lpContainer.href = landingPage;
        lpContainer.target = "_parent";
        lpContainer.innerHTML = "VIEW DETAILS >>";
        $(".featured-details").append(lpContainer);
        var luxLink = document.createElement("a");
        luxLink.href = "http://luxsocal.com";
        luxLink.target = "_blank";
        luxLink.innerHTML = "See more luxury homes.";
        $(".lux-link").append(luxLink);
    });
});

function formatPrice(price){
    var noDecimal = price.substring(0,price.length - 3);
    var partModulus = noDecimal.length%3;
    var firstPricePart = noDecimal.substring(0,partModulus);
    var lastPricePart = noDecimal.substring(partModulus,noDecimal.length);
    var formattedPrice = "$" + firstPricePart;
    for(var i=0;i<lastPricePart.length;i=i+3){
        formattedPrice += "," + lastPricePart.substring(i,i+3);
    }
    return formattedPrice;
}