﻿// Comparison Functions For Acura Conquest Page
// Darong Ma April 2009

// No Fade Animation On First Load
var FirstLoad=true;
//Separator
var Sep="|;|";
//The list of features to be compared
var CPAttrs=new Array();
CPAttrs.push("Base Price MSRP");
CPAttrs.push("Horsepower @ RPM");
CPAttrs.push("Torque @ RPM");
CPAttrs.push("Transmission Speeds");
CPAttrs.push("Combined Fuel-Economy");
//CPAttrs.push("Navigation System");
CPAttrs.push("MP3 Player Jack");
CPAttrs.push("High-Intensity Discharge Headlights");
CPAttrs.push("Hands Free or Voice Activated Telephone Access");
CPAttrs.push("NHTSA Front Driver Crash Test");
CPAttrs.push("NHTSA Front Passenger Crash Test");

//This is not in the comparison but for getting the Transmission Data for both vehicles
CPAttrs.push("Transmission Auto/Manual");
//Output the feature names to the feature rows
function initAttrRows()
{
    $("#attr-data .attr-row").each
    (
        function(i)
        {
            if(CPAttrs[i]=="Combined Fuel-Economy") $(this).html(CPAttrs[i]+"<sup>6</sup>");
            else $(this).html(CPAttrs[i]);
        }
    )
}
//Set equal row heights
function equalRowHeight()
{
    var height=new Array();
    $("#attr-data .attr-row").each(function(i){height[i]=$(this).height();});
    $("#acura-data .data-row").each(function(i){if($(this).height()>height[i])height[i]=$(this).height();});
    $("#compe-data .data-row").each(function(i){if($(this).height()>height[i])height[i]=$(this).height();});
    
    $("#attr-data .attr-row").each(function(i){$(this).height(height[i]);});
    $("#acura-data .data-row").each(function(i){$(this).height(height[i]);});
    $("#compe-data .data-row").each(function(i){$(this).height(height[i]);});
}
//Output Comparison Data to Respective Rows
function initComparisonDataRows(Rows)
{
    $("#acura-data .data-row").each
    (
        function(i)
        {

            var values=Rows[i].split(Sep);

            //if ((ModelStr == 'RL') && (i==0)) {
            //    values[1] = '$46,830'
            //}
            $(this).html(values[1]);
            if(values[2].toLowerCase()=="true")$(this).addClass("advantage");
            else $(this).removeClass("advantage");
        }
    );
    $("#compe-data .data-row").each
    (
        function(i)
        {
            var value=Rows[i].split(Sep)[3];
            $(this).html(value);
        }
    )
    
    var bpurl=buildpriceurl+"?ProductLineName="+currentselectedamodel;
    var bphtml="<div class='ybprice'>"+$("#acura-data .data-row:first").html()+"</div><div class='btn-bp'><a target='_blank' href='"+bpurl+"'><img id='imgBuild' onmouseover='SwitchImg(\"imgBuild\",\"./image/btn-build-price-over.jpg\")' onmouseout='SwitchImg(\"imgBuild\",\"./image/btn-build-price.jpg\")' src='./image/btn-build-price.jpg' /></a></div>"

    $("#aprice").html(bphtml);
    $("#cprice").html($("#compe-data .data-row:first").html());
    if($("#acura-data .data-row:first").hasClass("advantage"))$("#aprice").addClass("advantage");
    if($("#compe-data .data-row:first").hasClass("advantage"))$("#cprice").addClass("advantage");
    equalRowHeight();
}
//Ajax function to retrieve comparison data xml and process the xml, then output comparison results
function getCompareXML(trim1,trim2)
{
    //For Global Tracking
    XAModel=AModels[trim1];
    XCModel=CModels[trim2];
    
    if(FirstLoad==false)$("#fade-me").fadeTo(300,0.2);
    $.post
    (
        "./compare-xml.aspx",
        {t1:trim1,t2:trim2},
        function(data)
        {
            var Rows=new Array()
            var Str="";
            var XPathStr="";
            for(var i=0;i<CPAttrs.length;i++)
            {
                Str=CPAttrs[i];
                XPathStr="\\Row[RowLabel='"+CPAttrs[i]+"']";
                var MRows=$(XPathStr,data);
                if(MRows.length<=0)MRows=$("\\SubRow[RowLabel='"+CPAttrs[i]+"']",data);
                MRows.each
                (
                    function()
                    {
                        var SRows=$(this).find("VehicleData");
                        if(SRows.length<=0)SRows=$(this).find("SubVehicleData");
                        SRows.each
                        (
                            function()
                            {
                                Str+=Sep+$(this).attr("ColData");
                                Str+=Sep+$(this).attr("AdvantageFlag");
                            }
                        );
                    }
                );
                
                var len=Str.split(Sep).length
                if(len<=2)Str+=Sep+Sep;
                if(len<=3)Str+=Sep+Sep;
                
                Rows.push(Str);
            }
            
            currentselectedamodel=getAModelNameByTrimID(trim1);
            initComparisonDataRows(Rows);
            if(FirstLoad==false)$("#fade-me").fadeTo(300,1);
            FirstLoad=false;
            
            //Show Full Comparison URL
            var model=currentselectedamodel;
            var fcurl=compareurl + "?model=" + model + "#/" + model + "_Decide_Comparisons.e.trims.c." + trim1 + "-" + trim2;
            $("a.sfc").attr("href",fcurl);
            
            //Setting Transmission Type for Tracking, The Last Row Contains The Trans Info 
            setTrans(Rows[Rows.length-1]);
            //Fire Tracking Event On Each Comparison
            trackPage("HOME","COMPARE","DEFAULT","COMPARE ACURA",XAModel,XATrans,XCModel,XCTrans);

            
            debugComparisonData(Rows,"#Table");
        }
    );

}

//Output comparison data for degugging purpose
function debugComparisonData(Rows,Ele)
{
    $(Ele).html("");
    for(var x in Rows)$(Ele).append(Rows[x]+"<br>");
}

