/*
This file contains "hello" specific code that interacts with the global 
nuskin libraries that needs to be loaded within the "head" tag.  All of 
the jQuery stuff and nuskin code that needs to interact with jQuery is 
in the global.js file, which is loaded as a script tag at the end of the 
body tag.
*/

//subscribe to events exposed by the nuskin.account library
dojo.subscribe(
        nuskin.account.INVALID_AUTHENTICATION_MESSAGE_TYPE,
        function(object) {            
            showLoginPopup();
			dojo.query('.logInFormErrors').style('display', 'block');            
        }
  );
dojo.subscribe(
        nuskin.account.LOGOUT_MESSAGE_TYPE,
        function(object) {
            dojo.query('.loggedIn').style('display', 'none');
            dojo.query('.loggedIn-10').style('display', 'none');
            dojo.query('.loggedIn-20').style('display', 'none');
            dojo.query('.loggedIn-30').style('display', 'none');
            dojo.query('.notLoggedIn').style('display', 'block');
        }
  );
dojo.subscribe(
        nuskin.account.AUTHENTICATION_MESSAGE_TYPE,
        function(object) {
            dojo.query('.notLoggedIn').style('display', 'none');
			dojo.byId('firstName').innerHTML = object.firstName;
            dojo.query('.loggedIn').style('display', 'block');
            dojo.query('.loggedIn-10').style('display', 'none');
            dojo.query('.loggedIn-20').style('display', 'none');
            dojo.query('.loggedIn-30').style('display', 'none');
			dojo.query('.loggedIn-'+object.accountType).style('display', 'block');
			//code to add hello specific classes for formatting the myoffice dropdown
			var lastIndex = -1;
			var nodeList = dojo.query('ul.myOffice li ul li').forEach(function(node, index, nodeList){
				dojo.removeClass(node, 'first');
				dojo.removeClass(node, 'last');
				if('block' == dojo.style(node, 'display')) {
					if(lastIndex == -1) {
						dojo.addClass(node, 'first');
					}
					lastIndex = index;
				}
			});
			if (lastIndex > -1) {
				nodeList.at(lastIndex).addClass('last');
			}
        }
  );
//subscribe to events exposed by the nuskin.shop library
dojo.subscribe(
		nuskin.shop.ADD_TO_CART_MESSAGE_TYPE,
        function(object) {
			//the object has the sku and quantity that were added, if we ever need it
			try {
				dojo.query('div.addedToCart').forEach(function(item) {
					dojo.fadeIn({node: item}).play();
					setTimeout(dojo.hitch(item, function() { 
						dojo.fadeOut({node: this}).play();
					}), 4000);
				});
			} catch(e) {
				if(console) {
					console.debug(e);
				}
			}
        }
  );
var _nuskinPopupLogin = false;
dojo.subscribe(nuskin.account.LOGIN_REQUESTED, function(){
	_nuskinPopupLogin = true;
});