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(){

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

    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'); }
	});


    // 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();
        });
    });
    
    //Auto drop the special offers tab- bug 60980
    Cookie.set( 'DASH_SEEN', '1' );

    //Cookie.set( 'OFFERS_SEEN', '' );

    var offer_tab      =  $$('#navigation ul#navlist li.item8 a.item8')[0];

    var offer_tab_item =  $$('#navigation ul#navlist li.item8 #submenu8')[0];

    if (offer_tab_item && Cookie.get('OFFERS_SEEN') == null){
        offer_tab.addClassName('callout_offer');
        offer_tab_item.addClassName('callout_offer');
        offer_tab_item.hide();

        new Effect.BlindDown( offer_tab_item, {
                                        scaleX: 'true',
                                        scaleY: 'true;',
                                        scaleContent: false,
                                        queue: 'end',
                                        delay: 2,
                                        afterFinish: function ( elem ) {
                                                  new Effect.BlindUp( offer_tab_item, {
                                                  scaleX: 'true',
                                                  scaleY: 'true;',
                                                  scaleContent: false,
                                                  queue: 'end',
                                                  delay: 3,
                                                  afterFinish: function ( elem ) {
                                                  offer_tab.removeClassName('callout_offer');
                      offer_tab_item.removeClassName('callout_offer');
                      offer_tab_item.show();
                                                  }.bind(this)
                                      });
                                        }.bind(this)
          });

          Cookie.set( 'OFFERS_SEEN', '1' );
    }
    
    
    
});

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: '798px', height: '448px'}
            });
            $('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')
    }
};

