﻿var searchBox = null;
var quoteManager = null;

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();
    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();
            }
            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() {
            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.optionNode.width(this.selectionNode.width());
    
    var that = this;
    this.selectionNode.click(
        function() {
            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() {
            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 true;
        }
    });
        
    /*
    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;
   $(main).children().each(function(i) {
           contentHeight += parseInt($(this).outerHeight());
   });
   
   var frameHeight = $('#SCIFrame').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'});
           }
       }
       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'});
           }
           contentHeight = $(document).height();
       }
   }
   else
   {
       $('#SCIFooter').removeAttr('style');
       $('.DealerBottom').removeAttr('style');
   }
   
   if (!$.browser.msie || ($.browser.msie && parseInt(jQuery.browser.version) == 8))
       main.height(contentHeight);
   if ($.browser.msie && parseInt(jQuery.browser.version) == 7)
       $('#SCIContent').height(mainHeight);
   
   $('#leftBorderCol').height(contentHeight);
   $('#rightBorderCol').height(contentHeight);
   
   if ($('#thirdLevel').children().length > 0)
   {
       $('#thirdLevel').height(contentHeight - $('#SCIHeader').outerHeight() - $('#SCIMainHeader').outerHeight() - $('.DealerBottom').outerHeight() - $('#SCIFooter').outerHeight());
       $('#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 {
       if ($.browser.msie && parseInt(jQuery.browser.version) < 7) {
           $('#thirdLevel').hide();
           $('#mainContent').css('margin-left', '0px');
       }
       /*
       $('#SCIContent').css('width', '900px');
       $('#mainContent').css({margin:'auto', width:'75%', display:'block'});
       */
   }
}


$(function() {
        resizeCol();
        $(window).resize(function() { resizeCol(); });
   
       if ($('.AD_Img').length > 0)// || $('#Pricing').length > 0)
       {
           setInterval("resizeCol();", 500);
       }
        
        searchBox = new SearchBox();
        quoteManager = new QuoteManager();
});