(function($) {
    $(document).ready(function() {
        var nav = $("#nav_main, #nav_slim");
        var navItem = $("ul.firstLevel > li", nav);
        var subNavItem = $("ul.subNav li", nav);
        if ($.browser.msie) {
            subNavItem.addClass('white');
        }

        subNavItem.filter(".bottom-border").prev().find("a").addClass("no-bottom-rule");

        navItem.hover(
		    function() {
		        var hasSubNav = $(this).children('ul').length == 1;
		        var hoverClass = hasSubNav ? 'hoverdown' : 'hoverright';
		        $(this).find(":nth-child(1)").addClass(hoverClass);
		        if ($.browser.msie) {
		            $(this).find(":nth-child(2)").show();
		        }
		        else {
		            $(this).find(":nth-child(2)").fadeIn();
		        }
		    },
		    function() {
		        var hasSubNav = $(this).children('ul').length == 1;
		        var hoverClass = hasSubNav ? 'hoverdown' : 'hoverright';
		        $(this).find(":nth-child(1)").removeClass(hoverClass);
		        $(this).find(":nth-child(2)").hide();
		    }
	    );
        subNavItem.hover(
		    function() { $(this).addClass('hover'); },
		    function() { $(this).removeClass('hover') }
	    );

        /* Add last column class to last columns */
        $("#mod_3col div.module1, #mod_4col div.module1").last().addClass("last");

        /* Add tooltips */
        $(".zip-tip-from-info").data('tip', $(".zip-tip-from-info").tooltip({ api: true, lazy: true, tip: '#zip-tip-from-info'}));
        $(".zip-tip-from-error").data('tip', $(".zip-tip-from-error").tooltip({ api: true, lazy: true, tip: '#zip-tip-from-error'}));

        $(".zip-tip-to-info").data('tip', $(".zip-tip-to-info").tooltip({ api: true, lazy: true, tip: '#zip-tip-to-info'}));
        $(".zip-tip-to-error").data('tip', $(".zip-tip-to-error").tooltip({ api: true, lazy: true, tip: '#zip-tip-to-error'}));

        $(".delivery-time-frame2").data('tip', $(".delivery-time-frame2").tooltip({ api: true, lazy: true, tip: '#delivery-time-frame2-info' }));
        $(".delivery-time-frame2").data('tip', $(".delivery-time-frame2-error").tooltip({ api: true, lazy: true, tip: '#delivery-time-frame2-error' }));

        $(".delivery-time-frame").data('tip', $(".delivery-time-frame-info").tooltip({ api: true, lazy: true, tip: '#delivery-time-frame-info' }));
        $(".delivery-time-frame-error").data('tip', $(".delivery-time-frame-error").tooltip({ api: true, lazy: true, tip: '#delivery-time-frame-error'}));

        $(".storage-location").data('tip', $(".storage-location-info").tooltip({ api: true, lazy: true, tip: '#storage-location-info'}));
        $(".storage-location-error").data('tip', $(".storage-location-error").tooltip({ api: true, lazy: true, tip: '#storage-location-error'}));

        $(".months-storage").data('tip', $(".months-storage-info").tooltip({ api: true, lazy: true, tip: '#months-storage-info'}));
        $(".months-storage-error").data('tip', $(".months-storage-error").tooltip({ api: true, lazy: true, tip: '#months-storage-error'}));

        $(".email-error-info").data('tip', $(".email-error-info").tooltip({ api: true, lazy: true, tip: '#email-info'}));
        $(".email-error").data('tip', $(".email-error").tooltip({ api: true, lazy: true, tip: '#email-error'}));

        $(".previousquoteid-error-info").data('tip', $(".previousquoteid-error-info").tooltip({ api: true, lazy: true, tip: '#previousquoteid-info' }));
        $(".previousquoteid-error").data('tip', $(".previousquoteid-error").tooltip({ api: true, lazy: true, tip: '#previousquoteid-error' }));

        $(".container-size-info").tooltip({ api: true, lazy: true, tip: '#container-size-info' });

        $(".how-hear-info").tooltip({ api: true, lazy: true, tip: '#how-hear-info'});

		$("#quote_protection_middle .protection-plan-details .question").tooltip({ tip: '#protection-plan-details-tip' });

		//JoePerez: 09/28/2010 8ft. Modal
		$("#modal_quote_protection_middle .protection-plan-details .question").tooltip({ tip: '#protection-plan-details-tip' });
		$("#reserve-form-container .direction .icn-info").tooltip({ tip: '#direction-info-tip' });
		$("#reserve-form-container .slope .icn-info").tooltip({ tip: '#slopes-info-tip' });
		$("#reserve-form-container .obstacles .icn-info").tooltip({ tip: '#obstacles-info-tip' });

		// errors
		$("#reserve-form-container .first-name .icn-error").tooltip({tip: '#first-name-error-tip' });
		$("#reserve-form-container .last-name .icn-error").tooltip({ tip: '#last-name-error-tip' });
		$("#reserve-form-container .phone .icn-error").tooltip({ tip: '#phone-error-tip' });
		$("#reserve-form-container .email .icn-error").tooltip({ tip: '#email-error-tip' });
		$("#reserve-form-container .drop-off-address1 .icn-error").tooltip({ tip: '#drop-off-address1-error-tip' });
		$("#reserve-form-container .drop-off-city .icn-error").tooltip({ tip: '#drop-off-city-error-tip' });
		$("#reserve-form-container .drop-off-state .icn-error").tooltip({ tip: '#drop-off-state-error-tip' });
		$("#reserve-form-container .drop-off-zip .icn-error").tooltip({ tip: '#drop-off-zip-error-tip' });

		$("#reserve-form-container .card-type .icn-error").tooltip({ tip: '#card-type-error-tip' });
		$("#reserve-form-container .credit-card-number .icn-error").tooltip({ tip: '#credit-card-number-error-tip' });
		$("#reserve-form-container .expiration-month .icn-error").tooltip({ tip: '#expiration-month-error-tip' });
		$("#reserve-form-container .expiration-year .icn-error").tooltip({ tip: '#expiration-year-error-tip' });
		$("#reserve-form-container .name-on-card .icn-error").tooltip({ tip: '#name-on-card-error-tip' });
		$("#reserve-form-container .billing-address-choice .icn-error").tooltip({ tip: '#billing-address-choice-error-tip' });
		$("#reserve-form-container .billing-address1 .icn-error").tooltip({ tip: '#billing-address1-error-tip' });
		$("#reserve-form-container .billing-city .icn-error").tooltip({ tip: '#billing-city-error-tip' });
		$("#reserve-form-container .billing-state .icn-error").tooltip({ tip: '#billing-state-error-tip' });
		$("#reserve-form-container .billing-zip .icn-error").tooltip({ tip: '#billing-zip-error-tip' });

		$("#dialog1 .quote-for img.icn-info").tooltip({ tip: '#my-quote-is-for-details-tip' });

		$("#dialog1 .quote-for img.icn-error").tooltip({ tip: '#quote-for-error-tip' });
		$("#dialog1 .delivery-zip-code img.icn-error").tooltip({ tip: '#delivery-zip-code-error-tip' });
		$("#dialog1 .delivery-time-frame img.icn-error").tooltip({ tip: '#delivery-time-frame-error-tip' });
		$("#dialog1 .moving-to-zip-code img.icn-error").tooltip({ tip: '#moving-to-zip-code-error-tip' });
		$("#dialog1 .storage-location img.icn-error").tooltip({ tip: '#storage-location-error-tip' });
		$("#dialog1 .storage-months img.icn-error").tooltip({ tip: '#storage-months-error-tip' });
		$("#dialog1 .email img.icn-error").tooltip({ tip: '#edit-quote-email-error-tip' });
    });
})(jQuery);

function ChangeTooltip(formElement, className) {
    if (!formElement) {
        return;
    }

    var isErrorClass = className.indexOf('-error') > -1;
    var isSelectBox = formElement.nodeName.toLowerCase() == 'select';
    var parentDiv = jQuery(formElement).parent();
    var linkElement = parentDiv.children('::last-child');
    var offSet = [0, 0];
    var styleClass = "";
    if (isSelectBox) {
        var styleClass = isErrorClass ? 'select-error' : '';
    } else {
        var styleClass = isErrorClass ? 'error' : '';
    }
    if (isSelectBox) {
        offSet = selectBoxErrorOffset;
    } else if (isErrorClass) {
        //offSet = tooltipErrorOffset;
    } else {
        //offSet = tooltipOffset;
    }
    parentDiv.attr('class', styleClass);
    var clone = linkElement.clone();
    clone.appendTo(linkElement.parent());
    clone.attr('class', 'question-support ' + className);
    clone.tooltip({ api: true, lazy: true, tip: '#' + className });
    linkElement.remove();
}
