if (!window.Renault) window.Renault = new Object();
if (!window.Renault.Modules) window.Renault.Modules = new Object();
if (!window.Renault.Modules.VehicleSelector) window.Renault.Modules.VehicleSelector = new Object();

Renault.Modules.VehicleSelector.OnItemMouseOver = function(evt) {
    if (!evt) evt = window.event;
    var Source = (evt.srcElement ? evt.srcElement : evt.target);
    if (Source == undefined) return;
    while (Source.getAttribute("type") == null)
        Source = Source.parentNode;
    var MainImage = Source.getElementsByTagName("img")[0];
    var HoverImage = Source.getElementsByTagName("img")[1];
    var Title = Source.getElementsByTagName("span")[0];
    if (document.getElementById("Cloud") != null)
        document.body.removeChild(document.getElementById("Cloud"));
    var Cloud = Source.parentNode.getElementsByTagName("table")[0].cloneNode(true);
    Cloud.id = "Cloud";
    document.body.appendChild(Cloud);
    Cloud.style.display = "block";
    Cloud.style.left = Renault.Web.HttpRequest.absoluteLeft(MainImage) + "px"; //(evt.clientX)+"px";
    Cloud.style.top = (Renault.Web.HttpRequest.absoluteTop(MainImage) - Cloud.scrollHeight - 10) + "px"//(evt.clientY-80)+"px";
    if (Renault.Web.HttpRequest.Browser.IsIE == true) {
        var InitialValue = parseInt(HoverImage.style.filter.toString().replace(/\D/gi, ""));
        Renault.Animation.Animate(HoverImage.id, "style.filter", "Linear", "In", "alpha(opacity={0})", 5, InitialValue, 100, 50,
			Renault.Modules.VehicleSelector.OnFadeInCompleted, function(Value) { return (Value > 100 ? 100 : Value); });
    }
    else {
        var InitialValue = parseFloat(HoverImage.style.opacity);
        Renault.Animation.Animate(HoverImage.id, "style.opacity", "Linear", "In", "{0}", 5, InitialValue, 1, 50,
			Renault.Modules.VehicleSelector.OnFadeInCompleted, function(Value) { return (Value > 1 ? 1 : Value); });
    }
}

Renault.Modules.VehicleSelector.OnItemMouseOut = function(evt) {
    if (!evt) evt = window.event;
    var Source = (evt.srcElement ? evt.srcElement : evt.target);
    var Target = (evt.toElement ? evt.toElement : evt.relatedTarget);
    if (Target == undefined) return;
    while (Source.getAttribute("type") == null)
        Source = Source.parentNode;
    if (Source.contains(Target)) return;
    if (document.getElementById("Cloud") != null)
        document.body.removeChild(document.getElementById("Cloud"));
    var MainImage = Source.getElementsByTagName("img")[0];
    var HoverImage = Source.getElementsByTagName("img")[1];
    var Title = Source.getElementsByTagName("span")[0];
    if (Renault.Web.HttpRequest.Browser.IsIE == true) {
        var InitialValue = parseFloat(HoverImage.style.filter.substr(HoverImage.style.filter.indexOf("=") + 1, HoverImage.style.filter.lastIndexOf(")") - HoverImage.style.filter.indexOf("=") - 1));
        Renault.Animation.Animate(HoverImage.id, "style.filter", "Linear", "Out", "alpha(opacity={0})", 5, 0, InitialValue, 50,
			Renault.Modules.VehicleSelector.OnFadeOutCompleted, function(Value) { return (Value < 0 ? 0 : Value); });
    }
    else {
        var InitialValue = parseFloat(HoverImage.style.opacity);
        Renault.Animation.Animate(HoverImage.id, "style.opacity", "Linear", "Out", "{0}", 5, 0, InitialValue, 50,
			Renault.Modules.VehicleSelector.OnFadeOutCompleted, function(Value) { return (Value < 0 ? 0 : Value); });
    }
}

Renault.Modules.VehicleSelector.OnFadeInCompleted = function(evt) {
    if (!evt) evt = window.event;
    var Source = (evt.srcElement ? evt.srcElement : evt.target);
    if (Source.style.filter)
        Source.style.filter = "alpha(opacity=100)";
    else
        Source.style.opacity = "1.0";
}

Renault.Modules.VehicleSelector.OnFadeOutCompleted = function(evt) {
    if (!evt) evt = window.event;
    var Source = (evt.srcElement ? evt.srcElement : evt.target);
    if (Source.style.filter)
        Source.style.filter = "alpha(opacity=0)";
    else
        Source.style.opacity = "0.0";
}

Renault.Modules.VehicleSelector.OnScrollButtonClick = function(evt) {

    if (!evt) evt = window.event;
    var Source = (evt.srcElement ? evt.srcElement : evt.target);
    var Direction = parseInt(Source.getAttribute("direction"));
    var Selector = Source;
    while (Selector.itemWidth == undefined)
        Selector = Selector.parentNode;
    var Container = Selector.rows[0].cells[1].getElementsByTagName("div")[1];
    var InitialValue = Container.offsetLeft;


    var typeCars = Selector.parentNode;
    typeCars = typeCars.nextSibling;
    if (typeCars.nodeName == "#text") {
        typeCars = typeCars.nextSibling;
    }
    typeCars = typeCars.childNodes;


    if (typeCars[0].nodeName == "#text") {
        typsCars = typeCars[1].getAttribute("selectionMode");
    } else {
        typsCars = typeCars[0].getAttribute("selectionMode");
    }


    var ItemsCount = (Selector.getAttribute("selectionMode") == typsCars ? Selector.cars.length : Selector.vans.length);

    if ((Direction == 1) && (InitialValue >= -Selector.itemWidth * (ItemsCount - 5))) {
        Renault.Animation.Animate(Container.id, "style.left", "Cubic", "In", "{0}px", 10,
			InitialValue, -Selector.itemWidth, 50, Renault.Modules.VehicleSelector.UnBlockScrollers);
    }
    else if ((Direction == -1) && (InitialValue < -1)) {
        Renault.Animation.Animate(Container.id, "style.left", "Cubic", "Out", "{0}px", 10,
			-InitialValue - Selector.itemWidth, -Selector.itemWidth, 50, Renault.Modules.VehicleSelector.UnBlockScrollers);
    }
    return false;
}

Renault.Modules.VehicleSelector.BlockScrollers = function(evt) {
    if (!evt) evt = window.event;
    var Source = (evt.srcElement ? evt.srcElement : evt.target);
    var Selector = Source;
    while (Selector.itemWidth == undefined)
        Selector = Selector.parentNode;
    var Buttons = Selector.getElementsByTagName("input");
    for (var Index = 0; Index < Buttons.length; Index++)
        Buttons[Index].disabled = true;
    Buttons = Selector.parentNode.parentNode.cells[Selector.parentNode.cellIndex + 1].getElementsByTagName("div");
    for (var Index = 0; Index < Buttons.length; Index++)
        Buttons[Index].onclick = null;
}

Renault.Modules.VehicleSelector.UnBlockScrollers = function(evt) {
    if (!evt) evt = window.event;
    var Source = (evt.srcElement ? evt.srcElement : evt.target);
    var Selector = Source;
    while (Selector.itemWidth == undefined)
        Selector = Selector.parentNode;
    var Buttons = Selector.getElementsByTagName("input");
    for (var Index = 0; Index < Buttons.length; Index++) {
        if (Buttons[Index].disabled == true)
            Buttons[Index].disabled = false;
    }
    Buttons = Selector.parentNode.parentNode.cells[Selector.parentNode.cellIndex + 1].getElementsByTagName("div");
    for (var Index = 0; Index < Buttons.length; Index++)
        Buttons[Index].onclick = Renault.Modules.VehicleSelector.OnSelectionModeChange;
}

Renault.Modules.VehicleSelector.OnInit = function(ID, CarsSelectionMode, VansSelectionMode, SelectionMode) {


    var tmp = document.getElementById(ID);
    tmp = tmp.parentNode;
    tmp = tmp.nextSibling;
    if (tmp.nodeName == "#text") {
        tmp = tmp.nextSibling;
    }
    var typeCars = tmp.childNodes;

    if (typeCars[0].nodeName == "#text") {
        if (CarsSelectionMode == undefined) {
            CarsSelectionMode = typeCars[1].getAttribute("selectionMode") + "SelectionMode";
        }
        if (VansSelectionMode == undefined) {
            VansSelectionMode = typeCars[2].getAttribute("selectionMode") + "SelectionMode";
        }
        if (SelectionMode == undefined) {
            SelectionMode = typeCars[1].getAttribute("selectionMode");
        }
    } else {
        if (CarsSelectionMode == undefined) {
            CarsSelectionMode = typeCars[0].getAttribute("selectionMode") + "SelectionMode";
        }
        if (VansSelectionMode == undefined) {
            VansSelectionMode = typeCars[1].getAttribute("selectionMode") + "SelectionMode";
        }
        if (SelectionMode == undefined) {
            SelectionMode = typeCars[0].getAttribute("selectionMode");
        }
    }

    CarsSelectionMode = document.getElementById(CarsSelectionMode);
    VansSelectionMode = document.getElementById(VansSelectionMode);

    var Selector = document.getElementById(ID);

    Selector.setAttribute("selectionMode", SelectionMode);
    var Container = Selector.rows[0].cells[1].getElementsByTagName("div")[1];

    var IsInitialised = true;
    if (Selector.cars == undefined) {
        Selector.cars = new Array();
        Selector.vans = new Array();
        Selector.itemWidth = 139;
        IsInitialised = false;
    }


    for (var Index = 0; Index < Container.childNodes.length; Index++) {
        if (Container.childNodes[Index].nodeName != "#text") {
            if (typeCars[0].nodeName == "#text") {

                if (Container.childNodes[Index].getElementsByTagName("a")[0].getAttribute("type") == typeCars[1].getAttribute("selectionMode").toLowerCase()) {
                    if (!IsInitialised) {
                        Selector.cars.push({ index: Index, tooltip: Container.childNodes[Index].getElementsByTagName("table")[0] });

                    }

                    if (Selector.getAttribute("selectionMode") == typeCars[1].getAttribute("selectionMode")) {
                        Container.childNodes[Index].style.display = "";


                    }

                    else if (Selector.getAttribute("selectionMode") == typeCars[2].getAttribute("selectionMode")) {
                        Container.childNodes[Index].style.display = "none";

                    }
                }
                else if (Container.childNodes[Index].getElementsByTagName("a")[0].getAttribute("type") == typeCars[2].getAttribute("selectionMode").toLowerCase()) {

                    if (!IsInitialised) {
                        Selector.vans.push({ index: Index, tooltip: Container.childNodes[Index].getElementsByTagName("table")[0] });

                    }

                    if (Selector.getAttribute("selectionMode") == typeCars[2].getAttribute("selectionMode")) {
                        Container.childNodes[Index].style.display = "";

                    }

                    else if (Selector.getAttribute("selectionMode") == typeCars[1].getAttribute("selectionMode")) {
                        Container.childNodes[Index].style.display = "none";

                    }


                }
                else {

                    Container.childNodes[Index].style.display = "none";

                }
            } else {
                if (Container.childNodes[Index].getElementsByTagName("a")[0].getAttribute("type") == typeCars[0].getAttribute("selectionMode").toLowerCase()) {
                    if (!IsInitialised) {
                        Selector.cars.push({ index: Index, tooltip: Container.childNodes[Index].getElementsByTagName("table")[0] });

                    }

                    if (Selector.getAttribute("selectionMode") == typeCars[0].getAttribute("selectionMode")) {
                        Container.childNodes[Index].style.display = "";


                    }

                    else if (Selector.getAttribute("selectionMode") == typeCars[1].getAttribute("selectionMode")) {
                        Container.childNodes[Index].style.display = "none";

                    }
                }
                else if (Container.childNodes[Index].getElementsByTagName("a")[0].getAttribute("type") == typeCars[1].getAttribute("selectionMode").toLowerCase()) {

                    if (!IsInitialised) {
                        Selector.vans.push({ index: Index, tooltip: Container.childNodes[Index].getElementsByTagName("table")[0] });
                        
                    }
                    if (Selector.getAttribute("selectionMode") == typeCars[1].getAttribute("selectionMode")) {
                        Container.childNodes[Index].style.display = "";
                    }
                    else if (Selector.getAttribute("selectionMode") == typeCars[0].getAttribute("selectionMode")) {
                        Container.childNodes[Index].style.display = "none";
                    }

                }
                else {
                    Container.childNodes[Index].style.display = "none";
                }
            }
        }
        else {

            Container.removeChild(Container.childNodes[Index]);
            Index--;
        }
    }
	if (typeCars[0].nodeName == "#text") {
		if (Selector.getAttribute("selectionMode") == typeCars[1].getAttribute("selectionMode")) {
			Container.style.width = (Selector.cars.length * Selector.itemWidth + 1) + "px"
			Container.style.left = "-1px";
			CarsSelectionMode.className = "Active";
			VansSelectionMode.className = "";
		}
		else if (Selector.getAttribute("selectionMode") == typeCars[2].getAttribute("selectionMode")) {
			Container.style.width = (Selector.vans.length * Selector.itemWidth + 1) + "px"
			Container.style.left = "-1px";
			CarsSelectionMode.className = "";
			VansSelectionMode.className = "Active";
		}
	}else{
		if (Selector.getAttribute("selectionMode") == typeCars[0].getAttribute("selectionMode")) {
			Container.style.width = (Selector.cars.length * Selector.itemWidth + 1) + "px"
			Container.style.left = "-1px";
			CarsSelectionMode.className = "Active";
			VansSelectionMode.className = "";
		}
		else if (Selector.getAttribute("selectionMode") == typeCars[1].getAttribute("selectionMode")) {
			Container.style.width = (Selector.vans.length * Selector.itemWidth + 1) + "px"
			Container.style.left = "-1px";
			CarsSelectionMode.className = "";
			VansSelectionMode.className = "Active";
		}
	}
}

Renault.Modules.VehicleSelector.OnSelectionModeChange = function(evt) {
    if (!evt) evt = window.event;
    var Source = (evt.srcElement ? evt.srcElement : evt.target);
    var Selector = Source;
    while (Selector.nodeName.toLowerCase() != "table")
        Selector = Selector.parentNode;
    Selector = Selector.getElementsByTagName("table")[0];
    if (Selector.getAttribute("selectionMode") == Source.getAttribute("selectionMode")) return;
    //Renault.Modules.VehicleSelector.BlockScrollers({target:Selector});
    //var Cover=document.getElementById(Selector.id.substr(0,Selector.id.length-8)+"Cover");
    var Cover = document.getElementById("VehicleSelector_Cover");
    Cover.style.position = "absolute";

    var tempX = XarLib.getEl(document.getElementById('Selector')).getX();
    var tempY = XarLib.getEl(document.getElementById('Selector')).getY();
    XarLib.getEl('VehicleSelector_Cover').setX(tempX);
    XarLib.getEl('VehicleSelector_Cover').setY(tempY);

    Cover.style.width = Selector.scrollWidth + "px";
    Cover.style.height = Selector.scrollHeight + "px";
    Cover.style.display = "block";
    if (Renault.Web.HttpRequest.Browser.IsIE == true) {
        Renault.Animation.Animate(Cover.id, "style.filter", "Cubic", "In", "alpha(opacity={0})", 10, 0, 100, 50,
			Renault.Modules.VehicleSelector.OnHideCompleted, function(Value) { return (Value > 100 ? 100 : Value); });
    }
    else {
        Renault.Animation.Animate(Cover.id, "style.opacity", "Cubic", "In", "{0}", 10, 0, 100, 50,
			Renault.Modules.VehicleSelector.OnHideCompleted, function(Value) { return (Value > 1 ? 1 : Value); });
    }
}

Renault.Modules.VehicleSelector.OnHideCompleted = function(evt) {
    //var vehicleTypeNode = xmlDocIndex.selectNodes("root/site/vehicleTypes")[0];

    Renault.Modules.VehicleSelector.OnFadeInCompleted(evt);
    if (!evt) evt = window.event;
    var Cover = (evt.srcElement ? evt.srcElement : evt.target)
    var Selector = Cover.previousSibling;
    var CarsSelectionMode = Selector;
    while (CarsSelectionMode.nodeName.toLowerCase() != "td")
        CarsSelectionMode = CarsSelectionMode.parentNode;
    CarsSelectionMode = CarsSelectionMode.parentNode.cells[CarsSelectionMode.cellIndex + 1];
    CarsSelectionMode = CarsSelectionMode.getElementsByTagName("div");
    var VansSelectionMode = CarsSelectionMode[1];
    CarsSelectionMode = CarsSelectionMode[0];
    var selectorTemp = (Selector.getAttribute("selectionMode") == CarsSelectionMode.getAttribute("selectionMode") ? VansSelectionMode.getAttribute("selectionMode") : CarsSelectionMode.getAttribute("selectionMode"));


    Renault.Modules.VehicleSelector.OnInit(Selector.id, CarsSelectionMode.id, VansSelectionMode.id,
    //(Selector.getAttribute("selectionMode")=="Cars"?"Vans":"Cars"));

        (Selector.getAttribute("selectionMode") == CarsSelectionMode.getAttribute("selectionMode") ? VansSelectionMode.getAttribute("selectionMode") : CarsSelectionMode.getAttribute("selectionMode")));
    if (Renault.Web.HttpRequest.Browser.IsIE == true) {
        Renault.Animation.Animate(Cover.id, "style.filter", "Cubic", "Out", "alpha(opacity={0})", 10, 0, 100, 50,
			Renault.Modules.VehicleSelector.OnShowCompleted, function(Value) { return (Value < 0 ? 0 : Value); });
    }
    else {
        Renault.Animation.Animate(Cover.id, "style.opacity", "Cubic", "Out", "{0}", 10, 0, 100, 50,
			Renault.Modules.VehicleSelector.OnShowCompleted, function(Value) { return (Value < 0 ? 0 : Value); });
    }
}

Renault.Modules.VehicleSelector.OnShowCompleted = function(evt) {
    Renault.Modules.VehicleSelector.OnFadeInCompleted(evt);
    if (!evt) evt = window.event;
    var Cover = (evt.srcElement ? evt.srcElement : evt.target)
    Cover.style.display = "none";
    var Selector = Cover.previousSibling;
    Renault.Modules.VehicleSelector.UnBlockScrollers({ target: Selector });
}

