﻿var header = null;

var EventType = {TopItemRollover:'HRollover',ItemRollout:'HRollout'};
var OverflowX = 975;

function ThirdLevelList(divNode, topListNode, secondLevel) {
    this.divNode = divNode;
    this.topListNode = topListNode;
    this.thirdLevelList = this.divNode.find('ul');
    this.anchors = this.divNode.find('a');
    this.showID = null;
    this.hideID = null;
    this.secondLevel = secondLevel;

    if (this.divNode == null || this.divNode.length <= 0 ||
        this.topListNode == null || this.topListNode.length <= 0)
        return;
    
    var that = this;
    
    this.anchors.hover(
        function() {
            $(this).addClass('HeaderMenuItemHover');
            $(this).addClass('WhiteColor');
        },
        function() {
            $(this).removeClass('HeaderMenuItemHover');
            $(this).removeClass('WhiteColor');
        }
    );
    
    this.Show = function(anchorWidth) {
        var delay = 0;
        if (that.secondLevel.showingThirdLevel)
            return;
            delay = 150;
        
        if (that.showID != null)
            return;

        that.showID = setTimeout(function() {
            if (!that.secondLevel.hovering)
            {
                that.showID = null;
                return;
            }
            
            that.showID = null;
            that.secondLevel.showingThirdLevel = true;
            var left = 0 + anchorWidth;
            var top = 0 + 'px';
            that.divNode.css({ position:'absolute', left:left, top:top, 'z-index':12});
            that.divNode.show();
            var height = that.thirdLevelList.height();
            if (height < topListNode.height())
                that.thirdLevelList.height(topListNode.height());
        }, delay);
    }
    this.Hide = function() {
        if (that.hideID != null)
            return;

        that.divNode.css('z-index', '');
        that.hideID = setTimeout(function() {  
            if (that.secondLevel.hovering)
            {
                that.hideID = null;
                return;
            }
            that.hideID = null;
            that.divNode.fadeOut(100, function() {
                    that.secondLevel.showingThirdLevel = false;
                that.divNode.hide();
                that.divNode.removeAttr('style');
            });
        }, 150);
    }
    
    this.Overflows = function(anchorWidth) {
        var left = 0 + anchorWidth;
        var top = 0 + 'px';
        that.divNode.css({ position:'absolute', left:left, top:top, 'z-index':12});
        that.divNode.show();
        
        var offset = that.divNode.offset();        
        
        that.divNode.hide();
        that.divNode.removeAttr('style');
        
        return offset.left > OverflowX;
    }
}

function SecondLevelItem(divNode, secondLevelList, bottomItem) {
    this.divNode = divNode;
    this.anchor = divNode.find('a.secondLevelLink');
    this.thirdLevelNode = this.divNode.find(".HB_ThirdLevelList");
    this.thirdLevelList = null;
    this.secondLevelList = secondLevelList;
    this.hovering = false;
    this.showingThirdLevel = false;
    this.bottomItem = bottomItem;
    
    this.hasThirdLevel = function() {
        return this.thirdLevelNode != null && this.thirdLevelNode.length > 0;
    }
    if (this.hasThirdLevel()) {
        this.thirdLevelList = new ThirdLevelList(this.thirdLevelNode, this.secondLevelList, this);
    }
    
    var that = this;    
    this.divNode.hover(
        function() {
            that.hovering = true;
            $(this).addClass('HeaderMenuItemHover');
            that.anchor.addClass('WhiteColor');
            if (that.hasThirdLevel()) {
                var left = that.anchor.outerWidth();
                if (bottomItem.overflows)
                    left = -1 * $(that.thirdLevelNode).outerWidth();
                that.thirdLevelList.Show(left);
            }
        },
        function() {
            that.hovering = false;
            $(this).removeClass('HeaderMenuItemHover');
            that.anchor.removeClass('WhiteColor');    
            if (that.hasThirdLevel())
                that.thirdLevelList.Hide();
        }
    );
    
    this.Overflows = function() {
        if (!that.hasThirdLevel())
            return false;
        
        return that.thirdLevelList.Overflows(that.anchor.outerWidth());
    };
}

function BottomItem(divNode, index, header) {
    this.div = divNode;
    this.index = index;
    this.parent = header;
    this.linkNode = this.div.find("a.HeaderBottomTitle");
    this.listNode = this.div.find("ul.SecondLevelUL");
    this.menuNodes = new Array();
    this.normalWidth = $(this.linkNode).outerWidth() + 0.3;
    this.overflows = false;
    
    var that = this;
    this.div.hover(
        function() {
            $(that.linkNode).addClass("HeaderTopTitleHover");
            $(that.linkNode).addClass('HeaderBottomTitleHover');
            $(that.listNode).addClass("displayed");
            var listWidth = $(that.listNode).width() + 0;
            $(that.listNode).css({'z-index':10, position:'absolute'});
            $(this).width(that.normalWidth);
            that.listNode.width(Math.max(that.normalWidth-2, listWidth));
            //$(that.listNode).width(Math.max($(this).width()-1, listWidth));

            if (that.overflows) {
                $(that.listNode).css({'margin-left': -1 * listWidth + that.normalWidth -2 + 'px',
                                      'text-align':'right'});
            }            
        },
        function() {
            $(that.linkNode).removeClass("HeaderTopTitleHover");
            $(that.linkNode).removeClass('HeaderBottomTitleHover');
            $(that.listNode).removeClass("displayed");
            $(this).removeAttr('style');
            $(that.listNode).removeAttr('style');
        }
    );

    var maxWidth = 0;
    this.div.find("li").each(function(i) {
            var secondLevelItem = new SecondLevelItem($(this).find('div.HB_SecondItem'), that.listNode, that);
            that.menuNodes[that.menuNodes.length] = secondLevelItem;
            if ($(this).outerWidth() > maxWidth)
                maxWidth = $(this).outerWidt();
        });

    if (this.menuNodes.length > 0) {
        var thirdLevelOverflows = false;
        
        $(that.linkNode).addClass("HeaderTopTitleHover");
        $(that.linkNode).addClass('HeaderBottomTitleHover');
        $(that.listNode).addClass("displayed");
        var listWidth = $(that.listNode).width() + 0;
        $(that.listNode).css({'z-index':10, position:'relative'});
        $(that.div).width(that.normalWidth);
        //alert(listWidth);
        that.listNode.width(Math.max(that.normalWidth-2, listWidth));
        
        thirdLevelOverflows = false;
        var i;
        for (i = 0; i < that.menuNodes.length; i++) {
            if (that.menuNodes[i].hasThirdLevel())
                thirdLevelOverflows = that.menuNodes[i].Overflows();
            
            if (thirdLevelOverflows)
                break;
        }
        
        $(that.linkNode).removeClass("HeaderTopTitleHover");
        $(that.linkNode).removeClass('HeaderBottomTitleHover');
        $(that.listNode).removeClass("displayed");
        $(that.div).removeAttr('style');
        $(that.listNode).removeAttr('style');
        
        if (thirdLevelOverflows) {
            this.overflows = true;
        }
    }    
}


function CarDetail(divNode, anchor, topItem, vehicleLink) {
    this.divNode = divNode;
    this.anchor = anchor;
    this.carPhoto = this.divNode.find('div .HeaderCarDetailPhoto');
    this.colourChips = this.divNode.find('div.HCD_ColourChip a');
    this.showID = null;
    this.hideID = null;
    this.topItem = topItem;
    this.vehicleLink = vehicleLink;
    
    var that = this;
    this.colourChips.each(function(i) {
            $(this).hover(
                function() {
                    var name = $(this).find('div.chipname').text();
                    var thumb = $(this).find('div.thumbnail').text();
                    that.carPhoto.attr('alt', name);
                    that.carPhoto.attr('title', name);
                    that.carPhoto.attr('src', thumb);
                },
                function() {
                }
            );
    });
    
    this.Reset = function() {
        this.colourChips.each(function(i) {
                if (i == 0)
                {
                    var name = $(this).find('div.chipname').text();
                    var thumb = $(this).find('div.thumbnail').text();
                    that.carPhoto.attr('alt', name);
                    that.carPhoto.attr('title', name);
                    that.carPhoto.attr('src', thumb);
                }
        });
    };
    
    this.Show = function(x, y) {
        var delay = 0;
        if (that.topItem.showingCarDetail)
            delay = 150;
        
        if (that.showID != null)
            return;
        
        that.showID = setTimeout(function() {
                if (!that.vehicleLink.hovering)
                {
                    that.showID = null;
                    return;
                }
            that.topItem.showingCarDetail = true;
            that.showID = null;
            
            that.divNode.css({ position:'absolute', left:x, top:y, 'z-index':11});
            that.Reset();
            that.divNode.fadeIn(0);
        }, delay);
    };
    this.Hide = function() {
        if (that.hideID != null)
            return;
        
        that.divNode.css('z-index', '');
        that.hideID = setTimeout(function() {
                if (that.vehicleLink.hovering)
                {
                    that.hideID = null;
                    return;
                }
                that.topItem.showingCarDetail = false;
                that.hideID = null;
                that.divNode.fadeOut(250, function() {that.showID = null; that.divNode.removeAttr('style');});
        }, 150);
    };
    
    this.Overflows = function(x) {
        that.divNode.css({ position:'absolute', left:x});
        that.divNode.show();
        var offset = that.divNode.offset();

        that.divNode.removeAttr('style');
        
        return offset.left > OverflowX;
    }
}

function VehicleLink(listItemNode, topLink, topItem) {
    this.listItemNode = listItemNode;
    this.anchor = this.listItemNode.find("div.HeaderMenuItem");
    this.carDetailNode = this.listItemNode.find("div.HeaderCarDetail");
    
    this.listNode = this.anchor.find(":parent:parent:parent");
    this.topLink = topLink;
    this.topItem = topItem;
    this.hovering = false;
    
    this.carDetail = new CarDetail(this.carDetailNode, this.anchor, this.topItem, this);
    
    var that = this;
    this.listItemNode.hover(
        function() {
            that.hovering = true;
            var left = that.anchor.outerWidth();
            if (topItem.overflows)
                left = -1 * $(that.carDetailNode).outerWidth();
            var top = that.topLink.outerHeight();
            that.carDetail.Show(left, top);
            
            that.anchor.addClass('WhiteColor');

            $(this).find('div.HT_Div').addClass('HeaderMenuItemHover');
        },
        function() {
            that.hovering = false;
            that.carDetail.Hide();
            
            that.anchor.removeClass('WhiteColor');

            $(this).find('div.HT_Div').removeClass('HeaderMenuItemHover');            
        }
    );
    
    this.Reset = function() {
        that.carDetail.Hide();
    };
    
    this.Overflows = function() {
        var left = that.anchor.outerWidth();
        return this.carDetail.Overflows(left);
    }
}

function TopItem(divNode, index, header) {
    this.div = divNode;
    this.index = index;
    this.parent = header;
    this.linkNode = this.div.find("a.HeaderTopTitle");
    this.listNode = this.div.find("ul");
    this.vehicleNodes = new Array();
    this.normalWidth = $(this.linkNode).outerWidth() + 0.5;
    this.showingCarDetail = false;
    this.overflows = false;
    
    var that = this;
    this.div.hover(
        function() {
            $.each(that.vehicleNodes, function() {$(this)[0].Reset();});
            
            $(that.linkNode).addClass("HeaderTopTitleHover");
            $(that.listNode).addClass("displayed");
            var listWidth = $(that.listNode).width() + 1;
            $(that.listNode).css({'z-index':10, position:'absolute'});
            if (that.overflows) {
                $(that.listNode).css({'margin-left': -1 * listWidth + that.normalWidth -2 + 'px',
                                      'text-align':'right'});
            }
            $(this).width(that.normalWidth);
            $(that.listNode).width(listWidth);
        },
        function() {
            $(that.linkNode).removeClass("HeaderTopTitleHover");
            $(that.listNode).removeClass("displayed");
            $(this).removeAttr('style');
            $(that.listNode).removeAttr('style');
        }
    );
    

    var maxWidth = 0;
    this.div.find("li").each(function(i) {
            var vehicleLink = new VehicleLink($(this), $(this).find("div"), that);
	        that.vehicleNodes[that.vehicleNodes.length] = vehicleLink;
            if ($(this).outerWidth() > maxWidth)
                maxWidth = $(this).outerWidt();
    });
    
    if (this.vehicleNodes.length > 0) {
        $(that.linkNode).addClass("HeaderTopTitleHover");
        $(that.listNode).addClass("displayed");
        var listWidth = $(that.listNode).width() + 1;
        $(that.listNode).css({'z-index':10, position:'relative'});
        $(that.div).width(that.normalWidth);
        $(that.listNode).width(listWidth);
        
        var detailOverflows = this.vehicleNodes[0].Overflows();
        $(that.linkNode).removeClass("HeaderTopTitleHover");
        $(that.listNode).removeClass("displayed");
        $(that.div).removeAttr('style');
        $(that.listNode).removeAttr('style');
        
        if (detailOverflows) {
            this.overflows = true;
        }
    }
}

function TopVehicleItem(divElement, isTopAligned) {
    this.div = divElement;
    this.isTopAligned = isTopAligned;
    this.linkElement = this.div.find('a');
    this.normalWidth = $(this.linkElement).outerWidth() + 0.5;
    this.detail = null;
    this.modelList = null;
    this.msrpElement = null;
    this.powerElement = null;
    this.seatingElement = null;
    this.cargoElement = null;
    this.introLink = null;
    this.specLink = null;
    this.accLink = null;
    this.pricingLink = null;
    this.carImage = null;
    this.colourName = null;
    this.colourCol = null;
    this.lastSelectedTrim = null;
    this.isDetailShowing = false;
    this.hasMouseEntered = false;
    this.hasCarData = true;
    this.isImageDimmed = false;
    this.hovering = false;
    
    this.div.data('vehicleItem', this);
    
    var that = this;
    
    this.div.hover(
        function() {
            $(this).data('hovering', true);
            that.hovering = true;
            
            if (that.isDetailShowing)
                return;
            that.isDetailShowing = true;

            that.hasMouseEntered = false;
            
            if (that.isTopAligned) {
                $(that.linkElement).addClass("HeaderTopTitleHover HeaderTopTitleHover2011");
                that.detail.css('left', -124 - that.div.position().left);
            }
            else {
                if (that.hasCarData) {
                    $('div.veh_roll_border').each(function() {
                            var item = $(this).data('vehicleItem');
                            item.DimImage();
                    });
                }
//                that.detail.css('left', -0 - that.div.position().left);
//                that.detail.css('top', -1 * (that.linkElement.height() + that.detail.height()) + 1);
                that.linkElement.css({position:'relative', 'z-index':11});
                var onImage = that.div.find('div.imageOff').text();
                onImage = onImage.replace('.jpg', '_over.jpg');
                that.linkElement.find('img').attr('src', onImage);
            }

            /*
            var delay = 0;
            if (that.div.parent().data('showing'))
                delay = 150;
            
            var showID = that.div.data('showID');
            if (showID != null && showID != undefined)
                return;
            */
//            showID = setTimeout(function() {
                that.div.data('showID', null);
                if (!that.div.data('hovering'))
                    return;
                that.div.parent().data('showing', true);
                if (!that.isTopAligned) {
                    that.detail.css('left', -0 - that.div.position().left);
                    that.detail.css('top', -1 * (that.linkElement.height() + that.detail.height()) + 1);                        
                }
                that.div.find('div.carimage_div').show();
                that.div.find('span.trim:first').mouseover();
                that.lastSelectedTrim = that.div.find('span.trim:first');
                that.lastSelectedTrim.removeClass('trimSelected');
                that.lastSelectedTrim = null;
                if (that.hasCarData)
                    that.detail.show();
//            }, delay);
//            that.div.data('showID', showID);
            $(this).width(that.normalWidth);
        },
        function() {
            $(this).data('hovering', false);
            that.hovering = false;
            /*
            var delay = 150;
            
            var hideID = that.div.data('hideID');
            if (hideID != null && hideID != undefined)
                return;
            */
            
            //hideID = setTimeout(function() {
                that.div.data('hideID', null);
                if (that.div.data('hovering'))
                    return;
                
                that.div.parent().data('showing', false);
                that.isDetailShowing = false;
                if (that.isTopAligned) {
                    $(that.linkElement).removeClass("HeaderTopTitleHover HeaderTopTitleHover2011");
                }
                else
                {
                    if (that.hasCarData) {
                        $('div.veh_roll_border').each(function() {
                                var item = $(this).data('vehicleItem');
                                if (!item.hovering)
                                    item.ActivateImage();
                        });
                    }
                    that.linkElement.find('img').attr('src', that.div.find('div.imageOff').text());
                    that.linkElement.removeAttr('style');
                }
                that.detail.hide();

            $(this).removeAttr('style');
            //}, delay);            
            //that.div.data('hideID', hideID);
        }
    );
    
    this.DimImage = function() {
        if (that.isTopAligned)
            return;
        
        if (that.isImageDimmed || !that.hasCarData)
            return;
        that.isImageDimmed = true;
        
        var onImage = that.div.find('div.imageOff').text();
        onImage = onImage.replace('.jpg', '_dimmed.jpg');
        that.linkElement.find('img').attr('src', onImage);
    };
    this.ActivateImage = function() {
        if (that.isTopAligned)
            return;
        
        if (!that.isImageDimmed)
            return
        that.isImageDimmed = false;
        
        var onImage = that.div.find('div.imageOff').text();
        that.linkElement.find('img').attr('src', onImage);
    };
    
    var AddColourChipMouseover = function(colourChip) {
        if (colourChip == null || colourChip.length <= 0)
            return;
        
        colourChip.mouseover(function() {
            that.detail.find('img.carImage').attr('src', $(this).find('div.thumbnail').text());
            that.detail.find('div.colourName').text($(this).find('div.name').text());
        });
    };
    var AddDetailMouseover = function(detail) {
        if (detail == null || detail.length <= 0)
            return;
        
        detail.mouseover(function() {
            that.div.data('hovering', true);
            that.hovering = true;
            that.hasMouseEntered = true;
        });
    };
    
    this.div.append($('<div>').addClass('vehicle_div'));
    this.detail = this.div.find('div.vehicle_div');
    this.hasCarData = this.div.find('div.data div.car').length > 0;
    if (this.hasCarData) {
        AddDetailMouseover(this.detail);
            var dataDiv = $('div.textData');
            
            var modelList = '<div class="modelList model_list_div"></div>';
            
            var priceInfoDiv = '<div class="price_info_div">';
            priceInfoDiv += '<div class="vehicle_quicklinks vehicle_quicklinks2011 modelyear"></div>';
            priceInfoDiv += '<div class="HeaderCarDetailPricing HeaderCarDetailPricing2011"></div>';
            priceInfoDiv += '<div class="HeaderMSRP2011">' + dataDiv.find('div.msrp').text() + '</div>';
            priceInfoDiv += '<div>';
            priceInfoDiv += '<div class="vehicle_quicklinks2"><span class="HCD_Black">' + dataDiv.find('div.power').text() +'</span><span class="power"></span></div>';
            priceInfoDiv += '<div class="vehicle_quicklinks2"><span class="HCD_Black">' + dataDiv.find('div.seating').text() +'</span><span class="seating"></span></div>';
            priceInfoDiv += '<div class="vehicle_quicklinks2"><span class="HCD_Black">' + dataDiv.find('div.cargo').text() +'</span><span class="cargo"></span></div>';
            priceInfoDiv += '</div></div>';
            
            var carInfoDiv = '<div class="car_info_section_div">'; 
            carInfoDiv += '<div class="vehicle_quicklinks_bold info_div"><a class="intro">' + dataDiv.find('div.intro').text() + '</a></div>';
            carInfoDiv += '<div class="divider"></div>';
            carInfoDiv += '<div class="vehicle_quicklinks_bold info_div"><a class="specs">' + dataDiv.find('div.specs').text() + '</a></div>';
            carInfoDiv += '<div class="divider"></div>';
            carInfoDiv += '<div class="vehicle_quicklinks_bold info_div"><a class="acc">' + dataDiv.find('div.acc').text() + '</a></div>';
            carInfoDiv += '<div class="divider"></div>';
            carInfoDiv += '<div class="vehicle_quicklinks_bold info_div"><a class="pricing">' + dataDiv.find('div.pricing').text() + '</a></div>';
            carInfoDiv += '<div class="carImageContainer">';
            carInfoDiv += '<div class="car_image_div"><img class="carImage"></div><div class="colourCol"></div><div class="colourName"></div>';
            carInfoDiv += '</div></div>';
            
            var carImageDiv = '<div class="carimage_div"><img src="' + this.div.find('div.image').text() + '"></div>';
            
            this.detail.append($(modelList + priceInfoDiv + carInfoDiv + carImageDiv));
            
        this.modelList = this.detail.find('div.modelList');
        if (this.div.find('div.data div.car').length > 9)
            this.modelList.addClass('modelListLong');
    
        this.msrpElement = this.detail.find('div.HeaderCarDetailPricing2011');
        this.powerElement = this.detail.find('span.power');
        this.seatingElement = this.detail.find('span.seating');
        this.cargoElement = this.detail.find('span.cargo');
        this.introLink = this.detail.find('a.intro');
        this.specLink = this.detail.find('a.specs');
        this.accLink = this.detail.find('a.acc');
        this.pricingLink = this.detail.find('a.pricing');
        this.carImage = this.detail.find('img.carImage');
        this.colourName = this.detail.find('div.colourName');
        this.colourCol = this.detail.find('div.colourCol');
        
        var trimElements = '';
        this.div.find('div.data div.car').each(function() {
                trimElements += '<div><span class="trim">' + $(this).find('div.carName').text() + '<div class="car displayHidden">' + $(this).html() + '</div></span></div>';
    
            var yearText = $('div.textData div.modelyear').text();
            yearText = yearText.replace('[year]', $(this).find('div.year').text());
            that.detail.find('div.modelyear').text(yearText);
        });
        that.modelList.append($(trimElements));
        
        this.div.find('div.data').remove();
    }
    
    
    this.div.find('span.trim').mouseover(
        function() {
            $(this).addClass('trimSelected');
            if (that.lastSelectedTrim == $(this))
                return;
            
            if (that.lastSelectedTrim != null && that.lastSelectedTrim != $(this))
            {
                that.lastSelectedTrim.removeClass('trimSelected');                
            }
            that.lastSelectedTrim = $(this);
            
            if (that.hasMouseEntered)
            {
                that.div.find('div.carimage_div').fadeOut('fast');
            }
            
            var pricingURL = $(this).find('div.car div.pricing').text();
            
            that.msrpElement.text($(this).find('div.car div.msrp').text());
            that.powerElement.text($(this).find('div.car div.powerText').text());
            that.seatingElement.text($(this).find('div.car div.seating').text());
            that.cargoElement.text($(this).find('div.car div.cargoText').text());
            that.introLink.attr('href', $(this).find('div.car div.intro').text());
            that.specLink.attr('href', $(this).find('div.car div.specs').text());
            that.accLink.attr('href', $(this).find('div.car div.acc').text());
            that.pricingLink.attr('href', pricingURL);
            
            var firstColour = $(this).find('div.car div.colours div.colour:first');
            that.carImage.attr('src', firstColour.find('div.thumbnail').text());
            that.colourName.text(firstColour.find('div.name').text());
            
            that.colourCol.empty();
            $(this).find('div.car div.colours div.colour').each(function() {
                    var name = $(this).find('div.name').text();
                    that.colourCol.append($('<div>').addClass('HCD_ColourChip2011')
                        .append($('<a>')
                            .attr('href', pricingURL + '&colourID=' + $(this).find('div.colourID').text())
                            .append($('<div>').addClass('HCD_OverlayDiv')
                                .append($('<img>')
                                        .addClass('chipOverlay')
                                        .attr('src', 'http://www.subaru.ca/Content/7907/Media/General/WebImage/2010_colorchip_overlay.png')
                                        .attr('alt', name).attr('title', name)))
                            .append($('<div>').addClass('HCD_ChipDiv')
                                .append($('<img>')
                                    .addClass('HCD_Chip')
                                    .attr('alt', name)
                                    .attr('title', name)
                                    .attr('src', $(this).find('div.chip').text())
                                    )))
                        .append($('<div>').addClass('displayHidden data')
                            .append($('<div>').addClass('thumbnail').text($(this).find('div.thumbnail').text()))
                            .append($('<div>').addClass('name').text($(this).find('div.name').text())))
                    );
            });
            that.colourCol.find('div.HCD_ColourChip2011').each(function() {
                    AddColourChipMouseover($(this));
            });
        });
}

function TopMenuItem(divElement) {
    this.div = divElement;
    this.linkNode = this.div.find("a.HeaderTopTitle");
    this.secondLevel = null;
    this.normalWidth = $(this.linkNode).outerWidth();
//    this.showID = null;
    this.hideID = null;
    this.showingThirdLevel = false;
    
    this.menuNodes = new Array();    
    this.overflows = true;   
    
    var that = this;
    this.div.hover(
        function() {
            $(that.linkNode).addClass("HeaderTopTitleHover HeaderTopTitleHover2011");
            $(this).width(that.normalWidth);
        },
        function() {
            $(that.linkNode).removeClass("HeaderTopTitleHover HeaderTopTitleHover2011");
            $(this).removeAttr('style');

            if ($.browser.msie && $.browser.version < 8)
            {
                $(this).find('div.ThirdLevelDiv').each(function() {
                        $(this).removeAttr('style');
                        $(this).find('ul').removeAttr('style');
                        $(this).hide();
                });
            }
        }
    );
    
    var hasData = this.div.find('div.secondLevel');
    if (hasData.length > 0) {
        this.div.append($('<ul>').addClass('SecondLevelUL'));
        this.secondLevel = this.div.find('ul.SecondLevelUL');
        this.div.find('div.menuLink').each(function() {
                var uri = $(this).find('div.uri:first').text();
                var title = $(this).find('div.title:first').text();
                var target = $(this).find('div.target:first').text();
                var thirdLevel = null;
                if ($(this).find('div.subMenuLink').length > 0)
                {
                    var list = null;
                    list = $('<ul>').addClass('thirdLevelList');

                    $(this).find('div.subMenuLink').each(function() {
                            var thirdURI = $(this).find('div.uri:first').text();
                            var thirdTitle = $(this).find('div.title:first').text();
                            var thirdTarget = $(this).find('div.target:first').text();
                                list.append($('<li>')
                                    .append($('<a>').addClass('HeaderMenuItem HeaderFont thirdLevelLink thirdLevelLink2011').attr('href', thirdURI).attr('target', thirdTarget).text(thirdTitle)
                                    ));
                    });
                        thirdLevel = $('<div>').addClass('ThirdLevelDiv').append(list);
                }
                    that.secondLevel.append($('<li>')
                        .append($('<div>').addClass('SecondLevelItem')
                            .append($('<a>').addClass('HeaderMenuItem HeaderFont secondLevelLink secondLevelLink2011').attr('href', uri).attr('target', target).text(title))
                            .append(thirdLevel)
                            ));
        });
        if ($.browser.msie && $.browser.version < 8)
        {
            that.div.find('div.SecondLevelItem').each(function() {
                $(this).width($(this).parent().parent().width());
            });
        }
        
        this.div.find('div.SecondLevelItem').hover(
            function() {
                $(this).data('hovering', true);

                if ($(this).data('showingThirdLevel'))
                    return;
                
                var secondLevelDiv = $(this);
                var thirdLevelDiv = $(this).find('div.ThirdLevelDiv');
                if (thirdLevelDiv.length > 0)
                {
                    if (secondLevelDiv.data('showID') != null && secondLevelDiv.data('showID') != undefined)
                        return;

                    var delay = 150;
                    
                    
                    var showID = secondLevelDiv.data('showID');
                    if (showID != null && showID != undefined)
                        return;
                    
                    showID = setTimeout(function() {
                        secondLevelDiv.data('showID', null);
                        if (!secondLevelDiv.data('hovering'))
                            return;
                        
                        secondLevelDiv.data('showingThirdLevel', true);
                        
                        thirdLevelDiv.show();
                        thirdLevelDiv.find('ul').show();
                        var delta = that.secondLevel.outerWidth() - that.div.outerWidth();
                        var top = that.linkNode.outerHeight();
                        top = thirdLevelDiv.position().top - that.linkNode.height() - 10;
                        var left = -1 * (thirdLevelDiv.outerWidth() + delta - 1) + 'px';
                        if ($.browser.msie && $.browser.version < 8)
                            left = -1 * thirdLevelDiv.outerWidth() + 'px';
                        thirdLevelDiv.css({left:left, top:top, 'z-index':12});
                        
                        if ($.browser.msie && $.browser.version < 8)
                        {
                            thirdLevelDiv.find('li').each(function() {
                                $(this).width($(this).parent().width());
                            });
                        }
                    }, delay);
                    secondLevelDiv.data('showID', showID);
                }
            },
            function() {
                $(this).data('hovering', false);
/*
                if (!that.showingThirdLevel)
                    return;
                */

                var secondLevelDiv = $(this);
                var thirdLevelDiv = $(this).find('div.ThirdLevelDiv');
                thirdLevelDiv.css('z-index', '');
                if (thirdLevelDiv.length > 0)
                {
                    if (secondLevelDiv.data('hideID') != null && secondLevelDiv.data('hideID') != undefined)
                        return;
                    
                    var hideID = secondLevelDiv.data('hideID');
                    if (hideID != null && hideID != undefined)
                        return;
                    
                    hideID = setTimeout(function() {
                        secondLevelDiv.data('hideID', null);
                        if (secondLevelDiv.data('hovering'))
                            return;
                        
                        secondLevelDiv.data('showingThirdLevel', false);
                        
                        thirdLevelDiv.fadeOut(100, function() {
                            thirdLevelDiv.removeAttr('style');
                            thirdLevelDiv.find('ul').removeAttr('style');
                            thirdLevelDiv.hide();
                        });
                    }, 150);
                    secondLevelDiv.data('hideID', hideID)
                }
            });
        
        this.div.find('div.secondLevel').remove();
    }
}

function Carousel(container)
{
    this.container = container;
    this.listElement = container.find('ul');
    this.animateID = null;
    this.visibleTime = 8000;
    this.animationDuration = 500;
    this.itemWidth = 900;
    this.currentIndex = 0;
    this.items = new Array();
    this.itemSelectors = new Array();
    this.selectedCCClass = 'selectedCC';
    this.isPlaying = true;
    this.timerElement = container.find('div.ccTimer');
    this.progressElement = container.find('div.progressBar');
    
    var that = this;
    
    var PreventClick = function(anchor) {
        if (anchor == null)
            return;
        
        anchor.click(function(e) {
            e.preventDefault();
        });
    };
    var AddControlClickHandler = function(control) {
        if (control == null)
            return;
        
        control.click(function(e) {
            var isPlaying = that.isPlaying;
            that.Pause();
            
            if (that.currentIndex >= that.itemSelectors.length)
                that.itemSelectors[0].removeClass(that.selectedCCClass);
            else
                that.itemSelectors[that.currentIndex].removeClass(that.selectedCCClass);
            var lastIndex = that.currentIndex;
            that.currentIndex = $(this).data('i');

            if (lastIndex+1 == that.itemSelectors.length && that.currentIndex == 0)
            {
                that.currentIndex = lastIndex + 1;
            }
            if (lastIndex+1 == that.items.length && that.currentIndex == 1)
            {
                lastIndex = 0;
                that.listElement.css('left', 0);
            }
            if (lastIndex == 0 && that.currentIndex+1 == that.itemSelectors.length)
            {
                that.listElement.css('left', -1 * that.itemSelectors.length * that.itemWidth);
                that.currentIndex = that.itemSelectors.length - 1;
            } 
            
            if (that.currentIndex >= that.itemSelectors.length)
                that.itemSelectors[0].addClass(that.selectedCCClass);
            else
                that.itemSelectors[that.currentIndex].addClass(that.selectedCCClass);
            
            that.progressElement.stop();
            that.progressElement.width(0);
            that.listElement.animate({left:-1 * that.currentIndex * that.itemWidth},
                that.animationDuration, 'swing');
            if (isPlaying) {
                that.isPlaying = true;
                that.progressElement.animate({width:'100%'}, that.visibleTime);
                that.animateID = setTimeout(that.AnimateNext, that.visibleTime + that.animationDuration);
            }
        });
    };
    
    this.listElement.find('li').each(function() {
        that.items[that.items.length] = $(this);
        var anchor = $(this).find('a');
        if (anchor.attr('href') == '') {
            PreventClick(anchor);
            anchor.addClass('disabled');
        }
    });
    this.container.find('div.cc').each(function(i) {
        that.itemSelectors[that.itemSelectors.length] = $(this);
        $(this).data('i', i);
        AddControlClickHandler($(this));
    });
    this.container.find('div.cc:first').addClass(this.selectedCCClass);
    
    this.container.find('div.ccPlay').click(function() {
        if (that.isPlaying)
        {
            that.container.find('div.ccPlay').text(that.container.find('#playOff').text());
            that.Pause();
        }
        else
        {
            that.container.find('div.ccPlay').text(that.container.find('#playOn').text());
            that.Play();
        }
    });
    
    
    this.AnimateNext = function() {
        clearTimeout(that.animateID);
        
        that.progressElement.stop();
        that.progressElement.width(0);
        
        that.currentIndex++;
        if (that.currentIndex + 1 > that.items.length)
        {
            that.currentIndex = 1;
            that.listElement.css('left', 0);
        }
        that.itemSelectors[that.currentIndex - 1].removeClass(that.selectedCCClass);
        
        if (that.currentIndex >= that.itemSelectors.length)
            that.itemSelectors[0].addClass(that.selectedCCClass);
        else
            that.itemSelectors[that.currentIndex].addClass(that.selectedCCClass);
        
        if (that.currentIndex + 1 >= that.items.length)
            that.listElement.animate({left:-1 * that.currentIndex * that.itemWidth},
                that.animationDuration, 'swing',
                function() {
                    that.currentIndex = 0;
                    that.listElement.css('left', 0);
                });
        else
            that.listElement.animate({left:-1 * that.currentIndex * that.itemWidth},
                that.animationDuration, 'swing');
        
        that.progressElement.animate({width:'100%'}, that.visibleTime);
        that.animateID = setTimeout(that.AnimateNext, that.visibleTime + that.animationDuration);
    };
    this.Pause = function() {
        clearTimeout(that.animateID);
        that.animateID = null;
        that.isPlaying = false;
        
        that.progressElement.stop();
    }
    this.Play = function() {
        if (that.animateID != null)
            return;
        
        if (that.itemSelectors == null || that.itemSelectors.length <= 0)
            return;
        
        var visibleTime = that.visibleTime;
        var progress = 1 - parseFloat(that.progressElement.css('width')) / 100;
        if (progress > 0)
            visibleTime = visibleTime * progress;
        
        that.progressElement.animate({width:'100%'}, visibleTime);
        that.animateID = setTimeout(that.AnimateNext, visibleTime);
        that.isPlaying = true;
    }
    
    this.Play();
}


function TopVehicleItemIE7(divElement, isTopAligned) {
    this.div = divElement;
    this.isTopAligned = isTopAligned;
    this.linkElement = this.div.find('a:first');
    this.normalWidth = $(this.linkElement).outerWidth() + 0.5;
    this.detail = null;
    this.modelList = null;
    this.msrpElement = null;
    this.powerElement = null;
    this.seatingElement = null;
    this.cargoElement = null;
    this.introLink = null;
    this.specLink = null;
    this.accLink = null;
    this.pricingLink = null;
    this.carImage = null;
    this.colourName = null;
    this.colourCol = null;
    this.lastSelectedTrim = null;
    this.isDetailShowing = false;
    this.hasMouseEntered = false;
    this.hasCarData = true;
    this.isImageDimmed = false;
    this.hovering = false;
    
    this.div.data('vehicleItem', this);
    
    var that = this;
    
    this.div.hover(
        function() {
            $(this).data('hovering', true);
            that.hovering = true;
            
            if (that.isDetailShowing)
                return;
            that.isDetailShowing = true;

            that.hasMouseEntered = false;
            
            if (that.isTopAligned) {
                $(that.linkElement).addClass("HeaderTopTitleHover HeaderTopTitleHover2011");
                that.detail.css('left', -124 - that.div.position().left);
            }
            else {
                if (that.hasCarData) {
                    $('div.veh_roll_border').each(function() {
                            var item = $(this).data('vehicleItem');
                            item.DimImage();
                    });
                }
//                that.detail.css('left', -0 - that.div.position().left);
//                that.detail.css('top', -1 * (that.linkElement.height() + that.detail.height()) + 1);
                that.linkElement.css({position:'relative', 'z-index':11});
                var onImage = that.div.find('div.imageOff').text();
                onImage = onImage.replace('.jpg', '_over.jpg');
                that.linkElement.find('img').attr('src', onImage);
            }

            /*
            var delay = 0;
            if (that.div.parent().data('showing'))
                delay = 150;
            
            var showID = that.div.data('showID');
            if (showID != null && showID != undefined)
                return;
            */
//            showID = setTimeout(function() {
                that.div.data('showID', null);
                if (!that.div.data('hovering'))
                    return;
                that.div.parent().data('showing', true);
                if (!that.isTopAligned) {
                    that.detail.css('left', -0 - that.div.position().left);
                    that.detail.css('top', -1 * (that.linkElement.height() + that.detail.height()) + 1);                        
                }
                that.div.find('div.carimage_div').show();
                that.div.find('span.trim:first').mouseover();
                that.lastSelectedTrim = that.div.find('span.trim:first');
                that.lastSelectedTrim.removeClass('trimSelected');
                that.lastSelectedTrim = null;
                if (that.hasCarData)
                    that.detail.show();
//            }, delay);
//            that.div.data('showID', showID);
            $(this).width(that.normalWidth);
        },
        function() {
            $(this).data('hovering', false);
            that.hovering = false;
            /*
            var delay = 150;
            
            var hideID = that.div.data('hideID');
            if (hideID != null && hideID != undefined)
                return;
            */
            
            //hideID = setTimeout(function() {
                that.div.data('hideID', null);
                if (that.div.data('hovering'))
                    return;
                
                that.div.parent().data('showing', false);
                that.isDetailShowing = false;
                if (that.isTopAligned) {
                    $(that.linkElement).removeClass("HeaderTopTitleHover HeaderTopTitleHover2011");
                }
                else
                {
                    if (that.hasCarData) {
                        $('div.veh_roll_border').each(function() {
                                var item = $(this).data('vehicleItem');
                                if (!item.hovering)
                                    item.ActivateImage();
                        });
                    }
                    that.linkElement.find('img').attr('src', that.div.find('div.imageOff').text());
                    that.linkElement.removeAttr('style');
                }
                that.detail.hide();

            $(this).removeAttr('style');
            //}, delay);            
            //that.div.data('hideID', hideID);
        }
    );
    
    this.DimImage = function() {
        if (that.isTopAligned)
            return;
        
        if (that.isImageDimmed || !that.hasCarData)
            return;
        that.isImageDimmed = true;
        
        var onImage = that.div.find('div.imageOff').text();
        onImage = onImage.replace('.jpg', '_dimmed.jpg');
        that.linkElement.find('img').attr('src', onImage);
    };
    this.ActivateImage = function() {
        if (that.isTopAligned)
            return;
        
        if (!that.isImageDimmed)
            return
        that.isImageDimmed = false;
        
        var onImage = that.div.find('div.imageOff').text();
        that.linkElement.find('img').attr('src', onImage);
    };
    
    var AddColourChipMouseover = function(colourChip) {
        if (colourChip == null || colourChip.length <= 0)
            return;
        
        colourChip.mouseover(function() {
            that.detail.find('img.carImage').attr('src', $(this).find('div.thumbnail').text());
            that.detail.find('div.colourName').text($(this).find('div.name').text());
        });
    };
    var AddDetailMouseover = function(detail) {
        if (detail == null || detail.length <= 0)
            return;
        
        detail.mouseover(function() {
            that.div.data('hovering', true);
            that.hovering = true;
            that.hasMouseEntered = true;
        });
    };
    
    this.detail = this.div.find('div.vehicle_div');
    AddDetailMouseover(this.detail);
    /*
    this.hasCarData = this.div.find('div.data div.car').length > 0;
    if (this.hasCarData) {
        AddDetailMouseover(this.detail);
            var dataDiv = $('div.textData');
            
            var modelList = '<div class="modelList model_list_div"></div>';
            
            var priceInfoDiv = '<div class="price_info_div">';
            priceInfoDiv += '<div class="vehicle_quicklinks vehicle_quicklinks2011 modelyear"></div>';
            priceInfoDiv += '<div class="HeaderCarDetailPricing HeaderCarDetailPricing2011"></div>';
            priceInfoDiv += '<div class="HeaderMSRP2011">' + dataDiv.find('div.msrp').text() + '</div>';
            priceInfoDiv += '<div>';
            priceInfoDiv += '<div class="vehicle_quicklinks2"><span class="HCD_Black">' + dataDiv.find('div.power').text() +'</span><span class="power"></span></div>';
            priceInfoDiv += '<div class="vehicle_quicklinks2"><span class="HCD_Black">' + dataDiv.find('div.seating').text() +'</span><span class="seating"></span></div>';
            priceInfoDiv += '<div class="vehicle_quicklinks2"><span class="HCD_Black">' + dataDiv.find('div.cargo').text() +'</span><span class="cargo"></span></div>';
            priceInfoDiv += '</div></div>';
            
            var carInfoDiv = '<div class="car_info_section_div">'; 
            carInfoDiv += '<div class="vehicle_quicklinks_bold info_div"><a class="intro">' + dataDiv.find('div.intro').text() + '</a></div>';
            carInfoDiv += '<div class="divider"></div>';
            carInfoDiv += '<div class="vehicle_quicklinks_bold info_div"><a class="specs">' + dataDiv.find('div.specs').text() + '</a></div>';
            carInfoDiv += '<div class="divider"></div>';
            carInfoDiv += '<div class="vehicle_quicklinks_bold info_div"><a class="acc">' + dataDiv.find('div.acc').text() + '</a></div>';
            carInfoDiv += '<div class="divider"></div>';
            carInfoDiv += '<div class="vehicle_quicklinks_bold info_div"><a class="pricing">' + dataDiv.find('div.pricing').text() + '</a></div>';
            carInfoDiv += '<div class="carImageContainer">';
            carInfoDiv += '<div class="car_image_div"><img class="carImage"></div><div class="colourCol"></div><div class="colourName"></div>';
            carInfoDiv += '</div></div>';
            
            var carImageDiv = '<div class="carimage_div"><img src="' + this.div.find('div.image').text() + '"></div>';
            
            this.detail.append($(modelList + priceInfoDiv + carInfoDiv + carImageDiv));
            
        this.modelList = this.detail.find('div.modelList');
        if (this.div.find('div.data div.car').length > 9)
            this.modelList.addClass('modelListLong');
    
        this.msrpElement = this.detail.find('div.HeaderCarDetailPricing2011');
        this.powerElement = this.detail.find('span.power');
        this.seatingElement = this.detail.find('span.seating');
        this.cargoElement = this.detail.find('span.cargo');
        this.introLink = this.detail.find('a.intro');
        this.specLink = this.detail.find('a.specs');
        this.accLink = this.detail.find('a.acc');
        this.pricingLink = this.detail.find('a.pricing');
        this.carImage = this.detail.find('img.carImage');
        this.colourName = this.detail.find('div.colourName');
        this.colourCol = this.detail.find('div.colourCol');
        
        var trimElements = '';
        this.div.find('div.data div.car').each(function() {
                trimElements += '<div class="trim">' + $(this).find('div.carName').text() + '<div class="car displayHidden">' + $(this).html() + '</div></div>';
    
            var yearText = $('div.textData div.modelyear').text();
            yearText = yearText.replace('[year]', $(this).find('div.year').text());
            that.detail.find('div.modelyear').text(yearText);
        });
        that.modelList.append($(trimElements));
        
        this.div.find('div.data').remove();
    }
    */
        this.modelList = this.detail.find('div.modelList');
        if (this.div.find('span.trim').length > 9)
            this.modelList.addClass('modelListLong');

        this.msrpElement = this.detail.find('div.HeaderCarDetailPricing2011');
        this.powerElement = this.detail.find('span.power');
        this.seatingElement = this.detail.find('span.seating');
        this.cargoElement = this.detail.find('span.cargo');
        this.introLink = this.detail.find('a.intro');
        this.specLink = this.detail.find('a.specs');
        this.accLink = this.detail.find('a.acc');
        this.pricingLink = this.detail.find('a.pricing');
        this.carImage = this.detail.find('img.carImage');
        this.colourName = this.detail.find('div.colourName');
        this.colourCol = this.detail.find('div.colourCol');
        
        this.div.find('div.car:first').each(function() {
            var yearText = $('div.textData div.modelyear').text();
            yearText = yearText.replace('[year]', $(this).find('div.year').text());
            that.detail.find('div.modelyear').text(yearText);
            
            that.div.find('div.carimage_div img').attr('src', that.div.find('div.image').text());
        });    
    
    
    
    this.div.find('span.trim').mouseover(
        function() {
            $(this).addClass('trimSelected');
            if (that.lastSelectedTrim == $(this))
                return;
            
            if (that.lastSelectedTrim != null && that.lastSelectedTrim != $(this))
            {
                that.lastSelectedTrim.removeClass('trimSelected');                
            }
            that.lastSelectedTrim = $(this);
            
            if (that.hasMouseEntered)
            {
                that.div.find('div.carimage_div').fadeOut('fast');
            }
            
            var pricingURL = $(this).find('div.car div.pricing').text();
            
            that.msrpElement.text($(this).find('div.car div.msrp').text());
            that.powerElement.text($(this).find('div.car div.powerText').text());
            that.seatingElement.text($(this).find('div.car div.seating').text());
            that.cargoElement.text($(this).find('div.car div.cargoText').text());
            that.introLink.attr('href', $(this).find('div.car div.intro').text());
            that.specLink.attr('href', $(this).find('div.car div.specs').text());
            that.accLink.attr('href', $(this).find('div.car div.acc').text());
            that.pricingLink.attr('href', pricingURL);
            
            var firstColour = $(this).find('div.car div.colours div.colour:first');
            that.carImage.attr('src', firstColour.find('div.thumbnail').text());
            that.colourName.text(firstColour.find('div.name').text());
            
            that.colourCol.empty();
            $(this).find('div.car div.colours div.colour').each(function() {
                    var name = $(this).find('div.name').text();
                    that.colourCol.append($('<div>').addClass('HCD_ColourChip2011')
                        .append($('<a>')
                            .attr('href', pricingURL + '&colourID=' + $(this).find('div.colourID').text())
                            .append($('<div>').addClass('HCD_OverlayDiv')
                                .append($('<img>')
                                        .addClass('chipOverlay')
                                        .attr('src', 'http://www.subaru.ca/Content/7907/Media/General/WebImage/2010_colorchip_overlay.png')
                                        .attr('alt', name).attr('title', name)))
                            .append($('<div>').addClass('HCD_ChipDiv')
                                .append($('<img>')
                                    .addClass('HCD_Chip')
                                    .attr('alt', name)
                                    .attr('title', name)
                                    .attr('src', $(this).find('div.chip').text())
                                    )))
                        .append($('<div>').addClass('displayHidden data')
                            .append($('<div>').addClass('thumbnail').text($(this).find('div.thumbnail').text()))
                            .append($('<div>').addClass('name').text($(this).find('div.name').text())))
                    );
            });
            that.colourCol.find('div.HCD_ColourChip2011').each(function() {
                    AddColourChipMouseover($(this));
            });
        });
}

function TopMenuItemIE7(divElement) {
    this.div = divElement;
    this.linkNode = this.div.find("a.HeaderTopTitle");
    this.secondLevel = null;
    this.normalWidth = $(this.linkNode).outerWidth();
//    this.showID = null;
    this.hideID = null;
    this.showingThirdLevel = false;
    
    this.menuNodes = new Array();    
    this.overflows = true;   
    
    var that = this;
    this.div.hover(
        function() {
            $(that.linkNode).addClass("HeaderTopTitleHover HeaderTopTitleHover2011");
            $(this).width(that.normalWidth);
        },
        function() {
            $(that.linkNode).removeClass("HeaderTopTitleHover HeaderTopTitleHover2011");
            $(this).removeAttr('style');

            if ($.browser.msie && $.browser.version < 8)
            {
                $(this).find('div.ThirdLevelDiv').each(function() {
                        $(this).removeAttr('style');
                        $(this).find('ul').removeAttr('style');
                        $(this).hide();
                });
            }
        }
    );
    
    this.secondLevel = this.div.find('ul.SecondLevelUL');
    /*
    var hasData = this.div.find('div.secondLevel');
    if (hasData.length > 0) {
        this.div.append($('<ul>').addClass('SecondLevelUL'));
        this.secondLevel = this.div.find('ul.SecondLevelUL');
        this.div.find('div.menuLink').each(function() {
                var uri = $(this).find('div.uri:first').text();
                var title = $(this).find('div.title:first').text();
                var target = $(this).find('div.target:first').text();
                var thirdLevel = null;
                if ($(this).find('div.subMenuLink').length > 0)
                {
                    var list = null;
                    list = $('<ul>').addClass('thirdLevelList');

                    $(this).find('div.subMenuLink').each(function() {
                            var thirdURI = $(this).find('div.uri:first').text();
                            var thirdTitle = $(this).find('div.title:first').text();
                            var thirdTarget = $(this).find('div.target:first').text();
                                list.append($('<li>')
                                    .append($('<a>').addClass('HeaderMenuItem HeaderFont thirdLevelLink thirdLevelLink2011').attr('href', thirdURI).attr('target', thirdTarget).text(thirdTitle)
                                    ));
                    });
                        thirdLevel = $('<div>').addClass('ThirdLevelDiv').append(list);
                }
                    that.secondLevel.append($('<li>')
                        .append($('<div>').addClass('SecondLevelItem')
                            .append($('<a>').addClass('HeaderMenuItem HeaderFont secondLevelLink secondLevelLink2011').attr('href', uri).attr('target', target).text(title))
                            .append(thirdLevel)
                            ));
        });
        if ($.browser.msie && $.browser.version < 8)
        {
            that.div.find('div.SecondLevelItem').each(function() {
                $(this).width($(this).parent().parent().width());
            });
        }
        
        this.div.find('div.SecondLevelItem').hover(
            function() {
                $(this).data('hovering', true);

                if ($(this).data('showingThirdLevel'))
                    return;
                
                var secondLevelDiv = $(this);
                var thirdLevelDiv = $(this).find('div.ThirdLevelDiv');
                if (thirdLevelDiv.length > 0)
                {
                    if (secondLevelDiv.data('showID') != null && secondLevelDiv.data('showID') != undefined)
                        return;

                    var delay = 150;
                    
                    
                    var showID = secondLevelDiv.data('showID');
                    if (showID != null && showID != undefined)
                        return;
                    
                    showID = setTimeout(function() {
                        secondLevelDiv.data('showID', null);
                        if (!secondLevelDiv.data('hovering'))
                            return;
                        
                        secondLevelDiv.data('showingThirdLevel', true);
                        
                        thirdLevelDiv.show();
                        thirdLevelDiv.find('ul').show();
                        var delta = that.secondLevel.outerWidth() - that.div.outerWidth();
                        var top = that.linkNode.outerHeight();
                        top = thirdLevelDiv.position().top - that.linkNode.height() - 10;
                        var left = -1 * (thirdLevelDiv.outerWidth() + delta - 1) + 'px';
                        if ($.browser.msie && $.browser.version < 8)
                            left = -1 * thirdLevelDiv.outerWidth() + 'px';
                        thirdLevelDiv.css({left:left, top:top, 'z-index':12});
                        
                        if ($.browser.msie && $.browser.version < 8)
                        {
                            thirdLevelDiv.find('li').each(function() {
                                $(this).width($(this).parent().width());
                            });
                        }
                    }, delay);
                    secondLevelDiv.data('showID', showID);
                }
            },
            function() {
                $(this).data('hovering', false);
/*
                if (!that.showingThirdLevel)
                    return;
                */
/*
                var secondLevelDiv = $(this);
                var thirdLevelDiv = $(this).find('div.ThirdLevelDiv');
                thirdLevelDiv.css('z-index', '');
                if (thirdLevelDiv.length > 0)
                {
                    if (secondLevelDiv.data('hideID') != null && secondLevelDiv.data('hideID') != undefined)
                        return;
                    
                    var hideID = secondLevelDiv.data('hideID');
                    if (hideID != null && hideID != undefined)
                        return;
                    
                    hideID = setTimeout(function() {
                        secondLevelDiv.data('hideID', null);
                        if (secondLevelDiv.data('hovering'))
                            return;
                        
                        secondLevelDiv.data('showingThirdLevel', false);
                        
                        thirdLevelDiv.fadeOut(100, function() {
                            thirdLevelDiv.removeAttr('style');
                            thirdLevelDiv.find('ul').removeAttr('style');
                            thirdLevelDiv.hide();
                        });
                    }, 150);
                    secondLevelDiv.data('hideID', hideID)
                }
            });
        
        this.div.find('div.secondLevel').remove();
    }
    */
        if ($.browser.msie && $.browser.version < 8)
        {
            that.div.find('div.SecondLevelItem').each(function() {
                $(this).width($(this).parent().parent().width());
            });
        }   
    
    this.div.find('div.SecondLevelItem').hover(
        function() {
            $(this).data('hovering', true);

            if ($(this).data('showingThirdLevel'))
                return;
            
            var secondLevelDiv = $(this);
            var thirdLevelDiv = $(this).find('div.ThirdLevelDiv');
            if (thirdLevelDiv.length > 0)
            {
                if (secondLevelDiv.data('showID') != null && secondLevelDiv.data('showID') != undefined)
                    return;

                var delay = 150;
                
                
                var showID = secondLevelDiv.data('showID');
                if (showID != null && showID != undefined)
                    return;
                
                showID = setTimeout(function() {
                    secondLevelDiv.data('showID', null);
                    if (!secondLevelDiv.data('hovering'))
                        return;
                    
                    secondLevelDiv.data('showingThirdLevel', true);
                    
                    thirdLevelDiv.show();
                    thirdLevelDiv.find('ul').show();
                    var delta = that.secondLevel.outerWidth() - that.div.outerWidth();
                    var top = that.linkNode.outerHeight();
                    top = thirdLevelDiv.position().top - that.linkNode.height() - 10;
                    var left = -1 * (thirdLevelDiv.outerWidth() + delta - 1) + 'px';
                    if ($.browser.msie && $.browser.version < 8)
                        left = -1 * thirdLevelDiv.outerWidth() + 'px';
                    thirdLevelDiv.css({left:left, top:top, 'z-index':12});
                    
                    if ($.browser.msie && $.browser.version < 8)
                    {
                        thirdLevelDiv.find('li').each(function() {
                            $(this).width($(this).parent().width());
                        });
                    }
                }, delay);
                secondLevelDiv.data('showID', showID);
            }
        },
        function() {
            $(this).data('hovering', false);
/*
            if (!that.showingThirdLevel)
                return;
            */

            var secondLevelDiv = $(this);
            var thirdLevelDiv = $(this).find('div.ThirdLevelDiv');
            thirdLevelDiv.css('z-index', '');
            if (thirdLevelDiv.length > 0)
            {
                if (secondLevelDiv.data('hideID') != null && secondLevelDiv.data('hideID') != undefined)
                    return;
                
                var hideID = secondLevelDiv.data('hideID');
                if (hideID != null && hideID != undefined)
                    return;
                
                hideID = setTimeout(function() {
                    secondLevelDiv.data('hideID', null);
                    if (secondLevelDiv.data('hovering'))
                        return;
                    
                    secondLevelDiv.data('showingThirdLevel', false);
                    
                    thirdLevelDiv.fadeOut(100, function() {
                        thirdLevelDiv.removeAttr('style');
                        thirdLevelDiv.find('ul').removeAttr('style');
                        thirdLevelDiv.hide();
                    });
                }, 150);
                secondLevelDiv.data('hideID', hideID)
            }
        });    
}

function Header() {
	this.top = $('#HeaderTop');
	this.topItems = new Array();
	this.bottomVehicles = new Array();
	this.bottom = null;
	this.bottomItems = new Array();
	this.carousel = null;

	header = this;
	var that = this;

    // Checking if it is IE 7 or not.	
	this.isIE7Header = $('#ie7Header').length > 0;
	
	if ($('body.body2011').length > 0) {
        if ($('ul.carouselList').length > 0)
            carousel = new Carousel($('#HeaderContent'));
        this.top.find('div.Vehicle').each(function(i){
                var topItem;
                if (that.isIE7Header)
                    topItem = new TopVehicleItemIE7($(this), true);
                else
                    topItem = new TopVehicleItem($(this), true);
                that.topItems[that.topItems.length] = topItem;	        
            });
        this.top.find("div.HeaderTopItemRight").each(function(i){
                var topItem;
                if (that.isIE7Header)
                    topItem = new TopMenuItemIE7($(this));
                else
                    topItem = new TopMenuItem($(this));
                that.topItems[that.topItems.length] = topItem;	        
            });
        $('div.veh_roll_border').each(function(i) {
                var bottomVehicle;
                if (that.isIE7Header)
                    bottomVehicle = new TopVehicleItemIE7($(this), false);
                else
                    bottomVehicle = new TopVehicleItem($(this), false);
                that.bottomVehicles[that.bottomVehicles.length] = bottomVehicle;
            });
    }
    else {
        this.top.find("div.HeaderTopItem").each(function(i){
                var topItem = new TopItem($(this), i, that);
                that.topItems[that.topItems.length] = topItem;	        
            });
        this.bottom = $('#HeaderBottom');
        this.bottom.find("div.HeaderBottomItem").each(function(i) {
                var bottomItem = new BottomItem($(this), i, that);
                that.bottomItems[that.bottomItems.length] = bottomItem;
            });
    }
}

$(function() {
   // do something on document ready
   header = new Header();
});

