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

	var reference = $('accountbar') ? $('accountbar').down('ul').down('li.last') : null;
	if (reference) {
		// clear all siblings and rebuild top-right nav
		var sibs = reference.siblings();
		$A(sibs).each(function(sib){
			sib.remove();
		});
		
//		new ViewBagButton($('accountbar').down('li.last'));
		
		if (user.recognized_user && user.registered_user){
			var syntax = /(^|.|\r|\n)(\#\|\s*(\w+)\s*\|)/;
			var ins = ('<li><a href="/account/index.tmpl" style="padding-right: 0px">' + 
			navrb.get('Nav_Welcome_Back') + 
			'</a><a id="welcome-link" style="padding-left: 3px" href="#">' + 
			navrb.get('Nav_Not_Name') +
			'</a></li>').interpolate(user,syntax);
			reference.insert({
				before: ins
			});
			reference.insert({before: '<li><a href="/account/index.tmpl">' + navrb.get('Nav_My_Account') +
			'</a></li>'});
		} else {
			reference.insert({before: '<li><a id="signin-link" href="/account/signin.tmpl">' + navrb.get('Nav_Sign_In') +
			'</a></li>'});		
		}
        /*if (user.signed_in) {
            reference.insert({before: '<li><a id="signout-link" href="#">' + navrb.get('Nav_Sign_Out') +
			'</a></li>'});        
        }*/
		if ( !user.pc_email_optin) {
			reference.insert({before: '<li><a id="email-link" href="/account/registration.tmpl">' + navrb.get('Nav_Sign_Up') +
			'</a></li>'});
			$('email-link').applyDefinition(FloatingPanelLink);
		}

		reference.insert({before: '<li><a id="help-link" href="/cms/customer_service/">'+ navrb.get('Nav_Need_Help') +
		'</a></li>'});	
	}

	var host = location.href.parseUri().host;

	new GenericButton( 'welcome-link', {
		click: function( evt ){
			evt.stop();
			new HiddenForm({
				action: 'https://'+host+'/account/signin.tmpl',
				method: 'get'
			}).elements({
				_SUBMIT: 'signout'
			}).submit();
		}
	});
	
	new GenericButton( 'signout-link', {
		click: function( evt ){
			evt.stop();
			new HiddenForm({
				action: 'https://'+host+'/account/signin.tmpl',
				method: 'get'
			}).elements({
				_SUBMIT: 'signout'
			}).submit();
		}
	});
	
});
	
	
document.observe('txn:loaded', function () {
    transaction.updateGlobalQty();
});

document.observe('dom:loaded', function(){

    dashboard = new UserDashboard();
    user = new User();
    transaction = new Txn();

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

    // loadAccordions();

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

    // disable livechat dashboard button 4/19/2010
    loadLiveChatButtons();

    // My Skin Consultation
    $$('.skinConsultationButton').each(function(button){
        button.observe('click', function(event){
            var element = Event.element(event);
            // KFR: CoreMetrics tagging. This will send the element along with the name of the anchor
            // to checkNavLink() to manually throw a link promotions tag
 //         checkNavLink(element.parentNode, element.parentNode.name);
            launchSkinConsultation();
        });
    }); // end of My Skin Consultation

    // Foundation Finder
    $$('.foundationFinderButton').each(function(button){
        button.observe('click', function(event){
            var element = Event.element(event);
            // KFR: CoreMetrics tagging. This will send the element along with a flag to tell
            // checkNavLink() to manually throw a link promotions tag
//          checkNavLink(element.parentNode, element.parentNode.name);
            launchFoundationFinder();
        });
    }); // end of Foundation Finder
});

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

launchSkinConsultation = function(){
    templatefactory.get('/templates/skin_consultation_overlay.tmpl').evaluateCallback({
        callback: function(html){
            overlay.launch({
                content: html,
                cssStyle: {width: '798px', height: '448px'}
            });
            $('lighterwindow_close_link').observe('click', function(){
                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')
    }
};

