Event.observe( window, 'load', function(){
    // ***** jump to a spot on the page *****
    // if a query-string contains a parameter named 'jump' and an element (of any sort) contains an id
    // defined by the jump parameter, this function will jump to that element on the page.
    function jumpcheck(){
        var checkfor = parent.document.URL.toQueryParams();
        if (checkfor.jump){
            if (checkfor.jump == 'skinconsultation') { launchSkinConsultation(); }
            else if (checkfor.jump == 'foundationfinder') { launchFoundationFinder(); }
            else {
                onElement(checkfor.jump, function(){
                    var element = $(checkfor.jump);
                    // alert(element.cumulativeOffset());
                    // alert('found ' + checkfor.jump);
                    Effect.ScrollTo(checkfor.jump);
                }, 2, 60)
            }
        }
    };
    jumpcheck.defer();
});

document.observe('user:loaded', function () {
	
	// Not sure we need this anymore...
});
	
	
var dashboard = null;
document.observe('dom:loaded', function(){

    if (window.attachEvent && navigator.appVersion.substr(22,3)=="6.0")
        sfHover(); // For IE6

	typeaheadsearch = new TypeAheadSearch ({
		inputElementId: 'search',
		outputContainerId: 'global-typeahead-search-box',
		positionTop: 4,
		positionLeft: -126,
		activeLink: function() { $('searchbar').addClassName('searchbar-active'); },
		deactiveLink: function() { $('searchbar').removeClassName('searchbar-active'); }
	});

    dashboard = new UserDashboard();
    //user = new User();

    // loadAccordions();

    if ($('scroll-container')) {
        new Control.Scroller('scrollcontent', 'handle', 'track', {
            up: "button-up",
            down: "button-down",
            onScroll: function(value, scroller) {}
        });
    }

    //loadLiveChatButtons();

    // My Skin Consultation
    $$('.skinConsultationButton').each(function(button){
        button.observe('click', function(event){
            var element = Event.element(event);
            launchSkinConsultation();
        });
    });

    // Foundation Finder
    $$('.foundationFinderButton').each(function(button){
        button.observe('click', function(event){
            var element = Event.element(event);
            launchFoundationFinder();
        });
    });
});

launchFoundationFinder = function(){
    templatefactory.get('/templates/foundation_finder_overlay.tmpl').evaluateCallback({
        callback: function(html){
            generic.overlay.launch({
                content: html,
                cssStyle: {width: '798px', height: '464px'}
            });
            $('lighterwindow_close_link').observe('click', function(){
                generic.overlay.hide();
            });
        }
    });
};

launchSkinConsultation = function(){
    templatefactory.get('/templates/skin_consultation_overlay.tmpl').evaluateCallback({
        callback: function(html){
            generic.overlay.launch({
                content: html,
                cssStyle: {width: '796px', height: '448px'},
	            includeCloseLink: 0
            });
            $('lighterwindow_close_link').observe('click', function(){
                generic.overlay.hide();
                if ($('sppVideo')) {
                    $('sppVideo') .javascriptToFlash('resumeVideo');
                }
            });
        }
    });
};

/*
 * Create a lighterwindow popup button out of anything
 * with the class "lwPopupButton" applied
 * e.g.:
 *        <a href="/path/to/page.tmpl" class="lwPopupButton"
 *           popupHeight="500" popupWidth="300" printButton="true"
 *           >Inner HTML</a>
 */
function createLwPopupLinks(){
    $$('.lwPopupButton').each(function(el){
        if (!el.lwPopup) {
            var h = el.readAttribute('popupHeight') || 615;
            var w = el.readAttribute('popupWidth') || 800;
            var href = el.readAttribute('href');
            var prn = el.readAttribute('printButton') || false;

            el.lwPopup = new PopupButton( el, {
                url: href,
                divHeight: h,
                divWidth: w,
                printButton: prn
            });
        }
    });
}

// Watch for dom-load and window-load to create our popup links
document.observe("dom:loaded", function(){ createLwPopupLinks(); });
//Event.observe(window, 'load', function() { createLwPopupLinks(); });
//
// For IE 6
sfHover = function(){
    try {
        var sfEls = document.getElementById("navigation").getElementsByTagName("LI");
        for (var i=0; i<sfEls.length; i++) {
            sfEls[i].onmouseover = function(){
                this.className += " sfhover";
            }
            sfEls[i].onmouseout = function(){
                var self = this;
                var hideSubmenu = function(){
                    self.className = self.className.replace(new RegExp(" sfhover\\b"), "");
                }
                var t = setTimeout(hideSubmenu, 100);
                //this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
            }
        }
    }
    catch (err) {
        console.log('err on sfHover')
    }
};

