

// this was copied and modified from prototype_v1_5_0.js
function $(element)
{
    if (arguments.length > 1)
    {
        for (var i = 0, elements = []; i < arguments.length; i++)
        {
            elements.push($(arguments[i]));
        }
        return elements;
    }
    return document.getElementById(element);
}

var isLoadedCapTechSpec = false;
var isLoadedMap = false;
var handleInline = false;
var tabs = new Array('advertDetails', 'technicalSpecification', 'map');

function headScript(jsUrl)
{
    var scriptTag = document.createElement("script");
    scriptTag.setAttribute("type", "text/javascript");
    scriptTag.setAttribute("src", jsUrl);
    
    var headElement = document.getElementsByTagName("head").item(0);
    headElement.appendChild(scriptTag);
}

function showTechnicalSpecification(capTechSpecUrl, setHandleInLine)
{
        if (!isLoadedCapTechSpec)
        {
            headScript(capTechSpecUrl);
            isLoadedCapTechSpec = true;
        }
        else
        {
            changeDisplay('technicalSpecification');
        }

        handleInline = setHandleInLine;
}

function showMap(mapUrl, originalAdvertId, distanceFrom, dealerPostcode, userPostcode, channel, dealerId)
{
        if (!isLoadedMap)
        {
            if(navigator.userAgent.toLowerCase().indexOf('safari') < 0) {
                headScript(mapUrl + "/js/deCarta_4_2_4.js");
                headScript(mapUrl + "/js/CONFIG.js");
                headScript(mapUrl + "/js/dealerMap.js");
                headScript(mapUrl + "/js/deCartaAdOn.js");
                headScript(mapUrl + "/js/tunneller.js");
            }

            var inlineUrl = mapUrl + "/inlinejs.action?es=false&dealerId=" + dealerId + "&dealerPostcode="+ dealerPostcode.replace(" ", "+") + "&advertId=" + originalAdvertId + "&distanceFrom=" + distanceFrom + "&userPostcode=" + userPostcode.replace(" ", "+") + "&channel=" + channel;
            headScript("/www/DealerRedirectServlet?channel=" + channel + "&dealerId=" + dealerId + "&linkType=techSpecMap&source=TMG&redirectUrl=" + encodeURIComponent(inlineUrl)); 
            isLoadedMap = true;
        }
        else
        {
             changeDisplay('map');
        }
}

function showAdvertDetails()
{
    changeDisplay('advertDetails');
}

function loadTechnicalSpecification(technicalSpecification)
{
        $("technicalSpecificationContainer").innerHTML = technicalSpecification;
        changeDisplay('technicalSpecification');
        
        if (handleInline)
        {
            handleInlineLinks();
        }
}

// *********** the last one of these two functions to be called should call initmap ************
function loadMap() 
{
    $("mapIncluder").innerHTML = mapBody;
    changeDisplay('map');        
    initMapUpgraded();
}

function clearTabFlags()
{
    isLoadedCapTechSpec = false;
    isLoadedMap = false;
    handleInline = false;
}

function changeDisplay(tabToDisplay)
{       
        for (var i = 0; i < tabs.length; i++)
        {
            if (tabToDisplay == tabs[i])
            {
                setTabView(tabToDisplay, true);
            }
            else
            {
                setTabView(tabs[i], false);
            }
        }
}

function setTabView(tabName, toDisplay)
{
    var currentText = toDisplay ? 'mainTabOff' : 'mainTabOn';
    var replaceText = toDisplay ? 'mainTabOn' : 'mainTabOff';
        
    var tab = $(tabName + 'Tab');
    if (tab != null)
    {
        tab.className = tab.className.replace(currentText, replaceText);
    }

    var container = $(tabName + 'Container');
    
    //Handle UVL CSS that requires id of 'container' rather than 'advertDetailsContainer'
    if (container == null && tabName == 'advertDetails')
    {
        container = $('container');
    }
    
    if (container != null)
    {
        container.style.display = toDisplay ? 'block' : 'none';
    }
}
    
function clearTechSpecTabSelection()
{
    $("summaryTab").className = 
        $("summaryTab").className.replace("tabOn", "tabOff");
    $("performanceAndEconomyTab").className = 
        $("performanceAndEconomyTab").className.replace("tabOn", "tabOff");
    $("dimensionsTab").className = 
        $("dimensionsTab").className.replace("tabOn", "tabOff");
    $("interiorExteriorTab").className = 
        $("interiorExteriorTab").className.replace("tabOn", "tabOff");
    $("safetyTab").className = 
        $("summaryTab").className.replace("tabOn", "tabOff");
    
    $("summaryContent").className = "tabContentOff";
    $("performanceAndEconomyContent").className = "tabContentOff";
    $("dimensionsContent").className = "tabContentOff";
    $("interiorExteriorContent").className = "tabContentOff";
    $("safetyContent").className = "tabContentOff";
    
    $("disclaimer").style.display = "none";
}

function switchTechSpecTab(tabId, contentId)
{
    clearTechSpecTabSelection();
    $(tabId).className = $(tabId).className.replace("tabOff", "tabOn");
    $(contentId).className = "tabContentOn";
    if (tabId == "interiorExteriorTab" || tabId == "safetyTab")
    {
        $("disclaimer").style.display = "block";
    }
}