/*
    STLHService.js
    
*/

var _floorplanCache = new Object();
var _inventoryCache = new Object();
var _thumbnailSize = 380;


/*
    Floorplan Preview
*/
function showCommunityFloorplan(floorplanId,communityArea)
{
    if (_floorplanCache[floorplanId] == null)
    {
        toggleLoadingPreview(true);
        STLH.PublicSite.STLHService.GetFloorplan(floorplanId,communityArea,showFloorplanCallback);
    }
    else
    {        
        showFloorplanCallback(_floorplanCache[floorplanId]);
    }
    
}

function showFloorplanCallback(result)
{    
    _floorplanCache[result.FloorplanID] = result;
    $get("floorplanTitle").innerHTML = result.Floorplan;
    if (result.MainImage != '')
    {        
        var img = $get("floorplanImage");
        img.src = '/images/floorplans/' + result.MainImage + '.axd?size=' + _thumbnailSize;
        //img.onload = floorplanImageLoaded;
        showElement('floorplanImage');
        img.onerror = function(){hideElement("floorplanImage");};

    }   
    
    toggleLoadingPreview(false);
    hideElement("floorplanInstructions");
    hideElement("inventoryDetails");
    showElement("floorplanDetails");
}

function floorplanImageLoaded()
{    
    var img = $get("floorplanImage");
    if (img.width > 125)
    {
        img.width = "125";
    }
    showElement("floorplanImage")
}

/*
    Inventory Preview  
*/
function showInventory(inventoryId)
{
    if (_inventoryCache[inventoryId] == null)
    {
        toggleLoadingPreview(true);
        STLH.PublicSite.STLHService.GetInventory(inventoryId,showInventoryCallback);    
    }
    else
    {
        showInventoryCallback(_inventoryCache[inventoryId]);
    }
    
}

function showInventoryCallback(result)
{
    //Store in inventory cache if not already set.
    if (_inventoryCache[result.InventoryID] == null)
    {
        _inventoryCache[result.InventoryID] = result;
    }
    
    $get("inventoryTitle").innerHTML = result.FloorplanName;
    $get("inventoryDeliveryDate").innerHTML = result.DeliveryDate;
    var price = '$' + String.format("{0:c0}",result.Price).substr(1);
    
    $get("inventoryPrice").innerHTML = price;
    
    if (result.MainImage != '')
    {        
        var img = $get("inventoryImage");
        img.src = result.MainImage + '.axd?size=' + _thumbnailSize;
        //img.onload = inventoryImageLoaded;
        showElement("inventoryImage");
        img.onerror = function(){hideElement("inventoryImage");};
    }   
    
    toggleLoadingPreview(false);
    hideElement("floorplanInstructions");
    hideElement("floorplanDetails");
    showElement("inventoryDetails");
}


function inventoryImageLoaded()
{
    var img = $get("inventoryImage");
    if (img.width > 200)
    {
        img.width = "200";
    }
    showElement("inventoryImage");
}

function showElement(id)
{
    var e = $get(id);
    
    if (e != null)
    {
        e.style.visibility = "visible";
        e.style.display = "block"; 
    }
}

function hideElement(id)
{
    var e = $get(id);
    
    if (e != null)
    {
        e.style.visibility = "hidden";
        e.style.display = "none"; 
    }
}
function toggleLoadingPreview(show)
{
    var preview = $get("floorplanLoading");
    if (show)
    {
        preview.style.visibility = "visible";
        preview.style.display = "block";        
    }
    else
    {
        preview.style.visibility = "hidden";
        preview.style.display = "none";
    }
}

if(typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
