﻿function populateShowroom()
{
    Carselect.Services.ShowroomService.GetNumberOfCarsInShowroom(onCompletedGetNumberOfCarsInShowroom, onFailedGetNumberOfCarsInShowroom);
    Carselect.Services.ShowroomService.GetShowroom(onCompletedGetShowroom, onFailedGetShowroom);
}

function populateRecentlyViewed()
{
    Carselect.Services.ShowroomService.GetRecentlyViewed(onCompletedGetRecentlyViewed, onFailedGetRecentlyViewed);
}

function onFailedGetRecentlyViewed(a, b, c)
{
    alert("RecentlyViewed "+a.get_message());
}

function onFailedGetShowroom(a, b, c)
{
    alert("GetShowroom "+a.get_message());
}

function onFailedGetNumberOfCarsInShowroom(a, b, c)
{
    alert("GetNumberOfCarsInShowroom "+a.get_message());
}

function onCompletedGetRecentlyViewed(results)
{
    var rec = $get('divRecentlyViewed');
    var buf = new StringBuffer();
    
    for (i = 0; i < results.length; i++)
    {
        var used = (results[i].__type == "UsedCar:#Carselect.Data.DataObjects");
        var href = String.format("{0}{1}/cars/{2}/{3}/{4}.aspx", applicationPath, (used ? "used" : "new"), results[i].Make, results[i].Model, results[i].ReferenceNo);
        
        buf.append(String.format("<a class='imgLink floatLeft imgInRecent' href='{0}' title='{1} {2} {3}'>", href, (used ? "Used" : "New"), results[i].Make, results[i].Model));
        buf.append(String.format("<img src='{0}{1}/cars/images/thumbs/{2}.aspx?{3}'></a>", applicationPath, (used ? "used" : "new"), results[i].ReferenceNo, (used && results[i].ImageVersion > 0 ? results[i].ImageVersion : "")));
    }
    rec.innerHTML = buf.toString();
}

function onCompletedGetNumberOfCarsInShowroom(results)
{
    var show = $get('litShowroomTotal');
    show.innerHTML = results;
    
}

function onCompletedGetShowroom(results)
{
    var show = $get('litShowroomShowing');
    show.innerHTML = results.length;
    
    var sh = $get('divShowroom');
    var buf = new StringBuffer();

    for (i = 0; i < results.length; i++)
    {
        var used = (results[i].__type == "UsedCar:#Carselect.Data.DataObjects");
        var href = String.format("{0}{1}/cars/{2}/{3}/{4}.aspx", applicationPath, (used ? "used" : "new"), results[i].Make, results[i].Model, results[i].ReferenceNo);
        var desc = String.format("{0} {1} {2}", results[i].Make, results[i].Model, (used ? results[i].Description : ""));
        
        buf.append(String.format("<a class=\"imgLink floatLeft\" href=\"{0}\" title='{1}'>", href, desc));
        buf.append(String.format("<img src='{0}{1}/cars/images/thumbs/{2}.aspx?{3}'></a>", applicationPath, (used ? "used" : "new"), results[i].ReferenceNo, (used && results[i].ImageVersion > 0 ? results[i].ImageVersion : "")));
        buf.append("<div class=\"basicCarDesc\">");
        buf.append(String.format("<a href=\"{0}\" title='{1}'>", href, desc)); 
        buf.append(String.format("{0} {1}", results[i].Make, results[i].Model));
        buf.append("</a>");
        buf.append(String.format("<p><strong>Year: <span>{0}</span></strong><br />", (used ? results[i].RegYear : "New")));
        buf.append(String.format("<strong>Price: <span title='{1}'>{0}</span></strong></p>", (results[i].Price == 0 ? "PoA" : String.format("£{0:n}",  results[i].Price)), (results[i].Price == 0 ? "Price on application - please call for a quote" : "")));
        buf.append("</div>");
    }
    sh.innerHTML = buf.toString();
    
   var divWait = $get('divWaitShowroom');
   hide(divWait);  
}

function onCompletedModifyShowroom(result)
{
    if (result)
        populateShowroom();
        
    var divWait = $get('divWaitShowroom');
    hide(divWait);
}

function showroomLoad()
{
    var divWait = $get('divWaitShowroom');
    showWaiting(divWait);
    
    populateShowroom();
}

function recentlyViewedLoad()
{
    populateRecentlyViewed();
}

function removeFromShowroom(refNo)
{
    Carselect.Services.ShowroomService.RemoveFromShowroom(refNo, onCompletedModifyShowroom);
}

function addToShowroom(refNo)
{
    var divWait = $get('divWaitShowroom');
    showWaiting(divWait);
    
    Carselect.Services.ShowroomService.AddToShowroom(refNo, onCompletedModifyShowroom);
}


