﻿var searchBox = null;
var quoteManager = null;
// QuoteManager Config.
var QMAnimationTime = 700;
var QMClosingTime = 1000;

function DropDown(divElement)
{
    this.divElement = divElement;
    this.selectedDiv = divElement.find('div:first div:first');
    this.selectElement = divElement.find('select');
    var divClass = this.divElement.attr('id');
    this.isProvince = (/province/i).test(divClass);
    this.isModel = (/model/i).test(divClass);
    this.isTrim = (/trim/i).test(divClass);
    this.isPayment = (/payment/i).test(divClass);

    var that = this;

    /*
    this.divElement.find('div').click(function() {
        that.selectElement.click();
    });
    */
    this.selectElement.change(function() {
        that.selectedDiv.removeClass('veneerNextItem');
        that.updateVeneer();
    });
    this.resize = function() {
        var veneerWidth = that.selectElement.width();
        that.selectedDiv.width(veneerWidth);
    };    
  
    this.selectElement.fadeTo(0,0);
    var selected = this.selectElement.find('option[selected="selected"]');
    if (selected.length > 0)
        this.selectedDiv.text(selected.text());
    else
        this.selectedDiv.text(this.selectElement.find('option:first').text());
    if ((/disabled/i).test(this.divElement.attr('class')))
        this.Disable();
}
DropDown.prototype.Ready = function() {
    if (this.selectElement.find('option').length > 1)
        this.Enable();
};
DropDown.prototype.Enable = function() {
    this.selectElement.removeAttr('disabled');
    this.divElement.removeClass('pricingOptionDisabled');
};
DropDown.prototype.Disable = function() {
    this.selectElement.attr('disabled','disabled');
    this.divElement.addClass('pricingOptionDisabled');
};
DropDown.prototype.updateVeneer = function() {
    var selectedOption = this.selectElement.find('option:selected');
    this.selectedDiv.text(selectedOption.text());
}
DropDown.prototype.val = function() {
    if (this.selectElement != undefined && this.selectElement.length > 0)
        return this.selectElement.val();
};

function PagingControl(divElement, dropdownClass) {
    this.divElement = divElement;    
    this.dropdownClass = dropdownClass;
    this.dropdown = $(divElement).find('.' + dropdownClass).data('data');
    this.pageTemplate = $('#pageTemplate').text();
    this.nextAnchor = $(divElement).find('a.nextDoc');
    this.prevAnchor = $(divElement).find('a.prevDoc');
    this.selectElement = $(divElement).find('select.pages');
    this.currentPage = 1;
    this.maxPage = this.selectElement.find('option').length;
    this.otherControls = null;
    
    this.nextCallback     = null;
    this.prevCallback     = null;
    this.dropdownCallback = null;
    
    var that = this;
    this.selectElement.change(function() {
        that.LoadPage();        
    });
    this.nextAnchor.click(function() {
        that.LoadNext();
        return false;
    });
    this.prevAnchor.click(function() {
        that.LoadPrev();
        return false;
    });
};
PagingControl.prototype.UpdateOthers = function() {
    var that = this;
    if (this.otherControls == null) {
        this.otherControls = new Array();
        $('.' + this.dropdownClass + ':visible').each(function() {
            var parentClass = $(this).parent().attr('class');
            if ((/pagingControl/i).test(parentClass)) {
                if ($(this).parent().data('data') != that)
                    that.otherControls.push($(this).parent().data('data'));
            }
        });
    }
    
    $.each(this.otherControls, function() {
        var other = $(this)[0];
        other.currentPage = that.currentPage;
        other.UpdateDisplay();
    });
};
PagingControl.prototype.UpdateDisplay = function() {
    this.selectElement.find('option:selected').removeAttr('selected');
    this.selectElement.find('option[value="' + this.currentPage + '"]').attr('selected', 'selected');
    this.dropdown.updateVeneer();
    
    if (this.currentPage == 1) {
        this.prevAnchor.css('visibility', 'hidden');
        this.nextAnchor.css('visibility', 'visible');
    }
    else if (this.currentPage == this.maxPage) {
        this.prevAnchor.show();
        this.prevAnchor.css('visibility', 'visible');
        this.nextAnchor.css('visibility', 'hidden');
    }
    else {
        this.prevAnchor.show();
        this.prevAnchor.css('visibility', 'visible');
        this.nextAnchor.css('visibility', 'visible');
    }
};
PagingControl.prototype.LoadPage = function() {
    this.currentPage = parseInt(this.selectElement.val());
    
    if (this.currentPage <= 0)
        this.currentPage = 1;
    if (this.currentPage > this.maxPage)
        this.currentPage = this.maxPage;
        
    this.UpdateOthers();
    this.UpdateDisplay();
    if (this.dropdownCallback != null && this.dropdownCallback != undefined) {
        try {
            this.dropdownCallback();
        }
        catch(e) {
        }
    }
};
PagingControl.prototype.LoadNext = function() {
    if (this.currentPage + 1 > this.maxPage)
        return;
    
    this.currentPage++;

    this.UpdateOthers();
    this.UpdateDisplay();
    if (this.nextCallback != null && this.nextCallback != undefined) {
        try {
            this.nextCallback();
        }
        catch(e) {
        }
    }
};
PagingControl.prototype.LoadPrev = function() {
    if (this.currentPage -1 < 1)
        return;
    
    this.currentPage--;
    
    this.UpdateOthers();
    this.UpdateDisplay();
    if (this.nextCallback != null && this.nextCallback != undefined) {
        try {
            this.prevCallback();
        }
        catch(e) {
        }
    }
};
PagingControl.prototype.UpdateMaxPage = function(maxPage) {
    if (this.maxPage == maxPage)
        return;
    
    this.currentPage = 1;
    this.maxPage = maxPage;
    
    if (this.maxPage > 1)
    {
        this.selectElement.find('option').remove();
        
        var options = '';
        for (var i = 1; i <= this.maxPage; i++)
        {
            var text = this.pageTemplate;
            text = text.replace('[page]', i);
            text = text.replace('[maxPage]', this.maxPage);
            
            var selected = '';
            if (i == this.currentPage)
                selected = ' selected="selected"';
            
            options += '<option value="' + i + '"' + selected + '>' + text + '</option>';
        }
        this.selectElement.append($(options));
        
        if (this.dropdown != null)
            this.dropdown.updateVeneer();
        
        this.prevAnchor.css('visibility', 'hidden');
        this.nextAnchor.css('visibility', 'visible');
        
        this.divElement.show();
    }
    else
    {
        this.divElement.hide();
    }
};

function QuoteManager() {
    this.divNode = $('div.accountDiv');
    this.hoverNode = this.divNode.find('div.QMHover');
    this.titleNode = this.divNode.find('div.QMTitle');
    this.loginNode = this.divNode.find('div.QMLogin');
    this.loginCreateButton = this.divNode.find('a.QMLoginCreate');
    this.quoteCreateButton = this.divNode.find('a.QMQuoteCreate');
    this.createNode = this.divNode.find('div.QMCreate');
    this.quoteNode = this.divNode.find('div.QMQuotes');
    this.forgotButton = this.divNode.find('a.QMForgotButton');

    this.originalWidth = this.divNode.width();
    if ($('body.body2011').length <= 0)
        this.divNode.width(this.originalWidth);    
    var that = this;
    this.counter = 1;
    
    this.showingAccountCreation = false;
    
    this.CheckAccount = function() {
        
        
        return true;
    }
    
    this.divNode.mouseenter(function() {
            that.hoverNode.css({position:'absolute'});
            that.hoverNode.show();
            if (!that.showingAccountCreation) {
                that.loginNode.show();
                that.quoteNode.show();
            }
            else {
                $('input.password1').val('');
                $('input.password2').val('')
                that.createNode.show();
            }
            if ($('body.body2011').length <= 0)
            that.hoverNode.width(that.originalWidth);
    });
    this.divNode.mouseleave(function() {
//        alert("mouseleave " + that.divNode.data('hasFocus'));
        if (that.divNode.data('hasFocus') == false || that.divNode.data('hasFocus') == undefined) {
//            alert("Close");
        that.hoverNode.hide();
        }
    });
/*    
    this.divNode.hover(
        function() {            
            that.hoverNode.css({ position:'absolute'});
            that.hoverNode.show();
            that.loginNode.show();
            that.hoverNode.width(that.originalWidth);
        },
        function() {
            if (that.divNode.data('hasFocus') == false)
                that.hoverNode.hide();
        }
    );
/*
    this.divNode.find('input').focus(
        function(e) {
            that.divNode.data('hasFocus', true);
            e.stopPropagation();
            return false;
        }
    );
    */
    this.loginNode.children().hover(
        function(e) {
            e.stopPropagation();
            return false;
        },
        function(e) {
            e.stopPropagation();
            return false;
        }
    );
    this.createNode.children().hover(
        function(e) {
            e.stopPropagation();
            return false;
        },
        function(e) {
            e.stopPropagation();
            return false;
        }
    );
    this.quoteNode.children().hover(
        function(e) {
            e.stopPropagation();
            return false;
        },
        function(e) {
            e.stopPropagation();
            return false;
        }
    );
    /*
    this.createNode.mouseleave(
        function(e) {
            that.divNode.data('hasFocus', false);
        }
    );
    */
    this.createNode.mouseleave(
        function(e) {
            that.divNode.data('hasFocus', false);
            that.showingAccountCreation = false;
            that.createNode.hide();
        }
    );
    this.loginNode.mouseleave(
        function(e) {
//            alert("hover out");
            that.divNode.data('hasFocus', false);
//            that.divNode.mouseleave();
        }
    );
    this.quoteNode.mouseleave(
        function(e) {
            that.divNode.data('hasfocus', false);
        }
    );
    
    this.ShowCreateAccount = function() {
        that.showingAccountCreation = true;
        $('input.password1').val('');
        $('input.password2').val('')
        that.createNode.show();
        that.loginNode.hide();
        that.quoteNode.hide();
    }
    this.loginCreateButton.click(
        function() {
            TrackPageView('/virtualpv/account/create');
            that.ShowCreateAccount();
            return false;
        }
    );
    this.quoteCreateButton.click(
        function() {
            that.ShowCreateAccount();
            return false;
        }
    );
    this.forgotButton.click(
        function(e) {
            var loc = window.location.href;
            loc.match(/WebSiteID=(\d+)/i);
            var id =  RegExp.$1;
            $.ajax({
                type: "GET",
                url: "PricingConfig/MyQuote.aspx?Forgotten=Y&username=" + $('.QMEmailInput').val() + "&WebSiteID=" + id,
                dataType: "xml",
                success:function(xml) {
                        alert($(xml).find("message").text());
                    }
            }); 

            e.stopPropagation();
            return false;
        }
    );
}

function SearchBox() {
    this.divNode = $('div.TopSearchDiv');
    this.inputNode = $('input.TopSearchInput');
    this.searchButton = $('input.TopSearchButton');
    this.optionNode = $('div.TopSearchOptions');
    this.selectionNode = $('div.TopSelection');
    this.arrow = $('div.TopArrow');
    /*
    this.allCheckbox = $('.TSO_All').find('input');
    this.dealerCheckbox = $('.TSO_Dealer').find('input');
    this.siteCheckbox = $('.TSO_Site').find('input');
    */
    this.focused = false;
    this.numOptions = $(this.optionNode).find('span').length;
    if (this.numOptions <= 1)
    {
        $(this.selectionNode).css('cursor', 'default');
        $(this.arrow).hide();
    }
    
    this.optionNode.width(this.selectionNode.width());
    
    var that = this;
    
    this.searchButton.click(
        function(e) {
            if (that.inputNode.data('helpShown') != 'y')
            {
                return false;
            }
//            TrackPageView('/virtualpv/dealerlocate/result');
        }
    );
    
    this.selectionNode.click(
        function() {
            if (that.numOptions > 1)
                that.optionNode.show();
        }
    );
    that.divNode.hover(
        function() {
            that.arrow.addClass('TopArrowEnabled');
        },
        function() {
            that.optionNode.hide();
            that.arrow.removeClass('TopArrowEnabled');
        }
    );
    this.optionNode.hover(
        function() {
            that.optionNode.show();
        },
        function() {
            that.optionNode.hide();
        }
    );
    $('span.TSO_Option').click(
        function(e) {
            that.selectionNode.find('input').val($(this).attr('data'));
            $('div.TopTitle').text($(this).text());
            that.optionNode.hide();
            e.stopPropagation();
            return false;
        }
    );
    
    this.inputNode.focus(function() {
            if ($(this).data('helpShown') == undefined)
            {
                $(this).val('');
                $(this).data('helpShown', 'y');
            }
            that.focused = true;
    });
    this.inputNode.blur(function() {
            that.focused = false;
    });
    this.inputNode.keypress(function(e) {
            if (!that.focused)
                return false;
            
        if (e.which == 13){
            $(that.searchButton).click();
            return false;
        }
    });
        
    /*
    this.allCheckbox.click(function() {
            if ($(this).attr('checked')) {
                that.dealerCheckbox.attr('checked', true);
                that.siteCheckbox.attr('checked', true);
            }
    });
    this.dealerCheckbox.click(function() {
            if (!$(this).attr('checked')) {
                that.allCheckbox.attr('checked', false);
            }
            else {
                if (that.siteCheckbox.attr('checked'))
                    that.allCheckbox.attr('checked', true);
            }
    });
    this.siteCheckbox.click(function() {
            if (!$(this).attr('checked')) {
                that.allCheckbox.attr('checked', false);
            }
            else {
                if (that.dealerCheckbox.attr('checked'))
                    that.allCheckbox.attr('checked', true);
            }
    });
*/
}


function resizeCol() {
   var main = $('#SCIMainCol');
   var contentHeight = 0;
   if ($('#thirdLevel').children().length > 0)
       $('#thirdLevel').removeAttr('style');
   
   $(main).children().each(function(i) {
           contentHeight += parseInt($(this).outerHeight());
   });
   
   var frameHeight = Math.max($('#SCIFrame').height(), $('#SCIMainCol').height()); // Math.Max(parseInt($('#SCIFrame').height()), parseInt($('#SCIMainCol').height());
   var mainHeight = $('#mainContent').height();

   if (frameHeight > contentHeight)
       contentHeight = frameHeight;
   
   if (mainHeight < 400) {
       if ( $(window).height() >= $(document).height()) {
           if ($.browser.msie) {
               $('#SCIFooter').css({bottom:'0px'});
               /*
               if (parseInt(jQuery.browser.version) < 8)
                   $('span.DealerBottom').css({bottom:'25px'});
               else
                   $('span.DealerBottom').css({bottom:'25px', position:'absolute'});
                   */
           }
           else {
               //$('#SCIFooter').css({bottom:'0px', position:'absolute'});
               //$('span.DealerBottom').css({bottom:'25px', position:'absolute', 'padding-bottom':'10px'});
           }
       }
       else {
           if ($.browser.msie) {
                if (parseInt(jQuery.browser.version) < 8) {
                    $('#SCIFooter').css({bottom:'0px'});
                    $('span.DealerBottom').css({bottom:'auto'});
                }
                else {
                    $('#SCIFooter').css({bottom:'0px', position:'inherit'});
                    $('span.DealerBottom').css({bottom:'auto', position:'inherit'});
                }
           }
           else {
                $('#SCIFooter').css({bottom:'auto', position:'auto'});
                $('span.DealerBottom').css({bottom:'auto', position:'inherit', 'padding-bottom':'0px'});
           }
           contentHeight = $(document).height();
       }
   }
   else
   {
       $('#SCIFooter').removeAttr('style');
       $('.DealerBottom').removeAttr('style');
   }
   
   if ($('#thirdLevel').children().length > 0)
   {
       $('#thirdLevel').height(Math.max(contentHeight, $(window).height()) - $('#SCIHeader').outerHeight() - $('#SCIMainHeader').outerHeight() - $('.DealerBottom').outerHeight() - $('#SCIFooter').outerHeight());
       if ($('#thirdLevel').height() < $('#mainContent').height()) 
       {
           $('#thirdLevel').height(mainHeight);
       }
       
       $('#mainContent').width(690);
       if ($.browser.msie) {
           if (parseInt(jQuery.browser.version) < 8)
               $('#SCIContent').height($('#thirdLevel').height());
           if (parseInt(jQuery.browser.version) < 7) {
               $('#TLN_BG').width('auto');
               $('#mainContent').removeAttr('style');
           }
       }
   }
   else {
       $('#thirdLevel').hide();
       if ($.browser.msie && parseInt(jQuery.browser.version) < 7) {
           $('#mainContent').css('margin-left', '0px');
       }
       /*
       $('#SCIContent').css('width', '900px');
       $('#mainContent').css({margin:'auto', width:'75%', display:'block'});
       */
   }
   
   if ($('body.body2011').length > 0) {
       
       if ($('#SCIMainCol').height() + $('div.footer2011').height() < $(window).height() && $(window).height() > 700)
       {
           $('div.footer2011').css({position:'absolute',bottom:'0px'});
           
           var mainColumn = $('#SCIMainCol'); 
           mainColumn.removeAttr('style');
           var docHeight = $(document).height();
           var a = $('div.footer2011').outerHeight();
           var b = mainColumn.outerHeight();
           var delta = docHeight - a - b;
        
           mainColumn.height(mainColumn.height() + delta);          
       }
       else
       {
           $('div.footer2011').removeAttr('style');
           $('#SCIMainCol').removeAttr('style');
       }
   }
}

function NewQuoteManager() {
    this.divNode = $('div.accountDiv');
    this.hoverNode = this.divNode.find('div.QMHover2012');
    this.loginNode = this.divNode.find('div#loginDiv');
    this.titleNode = this.divNode.find('div.QMTitle');
    this.loginNode = this.divNode.find('div.QMLogin');
    this.loginCreateButton = this.divNode.find('.QMCreateButton2012');
    this.quoteCreateButton = this.divNode.find('a.QMQuoteCreate');
    this.createNode = this.divNode.find('div.QMCreate');
    this.quoteNode = this.divNode.find('div.QMQuotes');
    this.forgotButton = this.divNode.find('a.QMForgotButton2012');
    this.isHoveringQuoteManager = null;
    this.QuoteManagerTimerTracker = null;

    this.originalWidth = this.divNode.width();
    if ($('body.body2011').length <= 0)
        this.divNode.width(this.originalWidth);
    var that = this;
    this.counter = 1;

    this.showingAccountCreation = false;

    this.CheckAccount = function () {
        return true;
    }

    if (IE_Version >= 9) {
        this.loginNode.removeClass('QMSection2012');
        this.loginNode.addClass('QMSection2012-IE');
        this.createNode.removeClass('QMCreateSection');
        this.createNode.addClass('QMCreateSection-IE');
    }

    this.divNode.hover(
        function () {
            if (megamenu != null){
                megamenu.forcecloseMM();}
            if (buyingtool != null) {
                buyingtool.isHoveringMenu = false;
                buyingtool.closeMenu();
            }
            if (ownerresources != null) {
                ownerresources.isHoveringOwnerMenu = false;
                ownerresources.closeOwnerMenu();
            }
            if (startbottommenu != null) {
                if (startbottommenu.bottomMenuContainer.is(':visible')) {
                    startbottommenu.bottomMenuContainer.hide();
                    startbottommenu.restoreBottomMenuImage();
                }
            }
            that.isHoveringQuoteManager = true;

            if (IE_Version >= 9) {
                $(this).find('.myAccount').addClass('myAccount-hover-IE');
            }
            else{
                $(this).find('.myAccount').addClass('myAccount-hover');
            }

            //IE 7 fix.
            if (IE_Version >= 7 && IE_Version < 8) {
                that.hoverNode.css({ 'left': $(this).position().left - 402, 'top': 0 });
            }

            that.hoverNode.show();
            if (!that.showingAccountCreation) {
                that.loginNode.show();
                that.quoteNode.show();
            }
            else {
                $('input.password1').val('');
                $('input.password2').val('')
                that.createNode.show();
            }
            if ($('body.body2011').length <= 0)
                that.hoverNode.width(that.originalWidth);
        },
        function () {
            that.isHoveringQuoteManager = false;
            that.QuoteManagerTimerTracker = setTimeout('quoteManager.closeQuoteManager()', QMClosingTime);
        }
    );

    this.hoverNode.hover(
        function () {
            that.isHoveringQuoteManager = true;
        },
        function () {
            that.isHoveringQuoteManager = false;
            that.QuoteManagerTimerTracker = setTimeout('quoteManager.closeQuoteManager()', QMClosingTime);
        }
    );

    this.closeQuoteManager = function () {
        if (!this.isHoveringQuoteManager) {
            that.hoverNode.hide();
            if (IE_Version >= 9) {
                that.divNode.find('.myAccount').removeClass('myAccount-hover-IE');
            }
            else{
                that.divNode.find('.myAccount').removeClass('myAccount-hover');
            }
        }
    }

    this.loginNode.children().hover(
        function (e) {
            e.stopPropagation();
            return false;
        },
        function (e) {
            e.stopPropagation();
            return false;
        }
    );
    this.createNode.children().hover(
        function (e) {
            e.stopPropagation();
            return false;
        },
        function (e) {
            e.stopPropagation();
            return false;
        }
    );
    this.quoteNode.children().hover(
        function (e) {
            e.stopPropagation();
            return false;
        },
        function (e) {
            e.stopPropagation();
            return false;
        }
    );
    /*
    this.createNode.mouseleave(
    function(e) {
    that.divNode.data('hasFocus', false);
    }
    );
    */
    this.createNode.mouseleave(
        function (e) {
            that.divNode.data('hasFocus', false);
            that.showingAccountCreation = false;
            that.isHoveringQuoteManager = false;
            that.createNode.hide();
            that.closeQuoteManager();
        }
    );

    this.loginNode.mouseleave(
        function (e) {
            //            alert("hover out");
            that.divNode.data('hasFocus', false);
            //            that.divNode.mouseleave();
        }
    );
    this.quoteNode.mouseleave(
        function (e) {
            that.divNode.data('hasfocus', false);
        }
    );

    this.ShowCreateAccount = function () {
        that.showingAccountCreation = true;
        $('input.password1').val('');
        $('input.password2').val('')
        if (IE_Version >= 7 && IE_Version < 8)
            that.createNode.css('left', ($('.myAccount').position().left + 200));
        else
            that.createNode.css('left', ($('.myAccount').position().left - 350));
        that.createNode.show();
        that.loginNode.hide();
        that.quoteNode.hide();
    }
    this.loginCreateButton.click(
        function () {
            that.ShowCreateAccount();
            return false;
        }
    );
    this.quoteCreateButton.click(
        function () {
            that.ShowCreateAccount();
            return false;
        }
    );
    this.forgotButton.click(
        function (e) {
            var loc = window.location.href;
            loc.match(/WebSiteID=(\d+)/i);
            var id = RegExp.$1;
            $.ajax({
                type: "GET",
                url: "PricingConfig/MyQuote.aspx?Forgotten=Y&username=" + $('.QMEmailInput').val() + "&WebSiteID=" + id,
                dataType: "xml",
                success: function (xml) {
                    alert($(xml).find("message").text());
                }
            });

            e.stopPropagation();
            return false;
        }
    );
}

$(function () {
    resizeCol();
    $(window).resize(function () { resizeCol(); });
    $(window).load(function () {
        if ($.browser.msie && parseInt(jQuery.browser.version) < 7)
            return;
        resizeCol();
    });

    if ($('.AD_Img').length > 0)// || $('#Pricing').length > 0)
    {
        setInterval("resizeCol();", 500);
    }

    var hiddenLegal = $('#hiddenLegal').html()
    if (hiddenLegal != undefined && hiddenLegal != '') {
        hiddenLegal = '<div>' + hiddenLegal + '</div>';
        $('#mainContent').append($(hiddenLegal));
    }

    searchBox = new SearchBox();

    var newQM = $('.newQM');
    if (newQM.length > 0)
        quoteManager = new NewQuoteManager();
    else
        quoteManager = new QuoteManager();
});
