jQuery(document).ready(function() {
	
	jQuery.noConflict();
	
	// Pop-up Login Box //
				
	jQuery('.np-link-primary').attr({href: '#', title: ''});
	
	/*var email = 'Email:';
	var password = 'Password:';
			
	function resetFields(){
			jQuery('#np-login.box .np-email').val(email);
			jQuery('#np-login.box .np-password').val(password);
	};

	function clearFields(){		
		jQuery('#np-login.box :input[type=text]').focus(function(){
			if (this.value == this.defaultValue){
				this.value = "";
			}
		});
		jQuery('#np-login.box :input[type=text]').blur(function(){
			if (this.value == ''){
				this.value = this.defaultValue;
				}
			});
		};*/

	jQuery(function () {
        jQuery('#np-login.box').each(function () {
            var distance = 0;
            var time = 200;
            var hideDelay = 200;
            var hideDelayTimer = null;
            var beingShown = false;
            var shown = false;
            var trigger = jQuery('.np-link-primary', this);
            var info = jQuery('.np-login', this).css('opacity', 0);

            jQuery([trigger.get(0), info.get(0)]).mouseover(function () {
                if (hideDelayTimer) clearTimeout(hideDelayTimer);
                if (beingShown || shown) {
                    // don't trigger the animation again
                    return;
                } else {
                    // reset position of info box
                    beingShown = true;
                    info.css({
                        top: 20,
                        left: -1,
                        display: 'block'
                    }).animate({
                        opacity: 1
                    }, time, 'swing', function() {
                        beingShown = false;
                        shown = true;
                    });
                }					
                return false;
            }).mouseout(function () {				
                if (hideDelayTimer) clearTimeout(hideDelayTimer);
                hideDelayTimer = setTimeout(function () {
                    hideDelayTimer = null;
                    info.animate({
                        opacity: 0,
						display: 'none'
                    }, time, 'swing', function () {
                        shown = false;
                        info.css('display', 'none');
						/*resetFields();*/
                    });
                }, hideDelay);				
                return false;
            });
        });
    });


	/*jQuery(function(){
		clearFields();
		resetFields();
		});*/

// End Popup Login Box //


// Registration Form //
	jQuery('.dynamic-content .additional-info').hide();
	function showDynamicContent() {
		if (jQuery('.dynamic-content input.dynamic-selector').is(':checked')) {
			jQuery('.dynamic-content .additional-info').slideDown();
			jQuery('.submit').addClass('disabled').attr('disabled','disabled');
		}
		else if (jQuery('.dynamic-content input.dynamic-selector').not(':checked')) {
			jQuery('.dynamic-content .additional-info').slideUp();
			jQuery('.submit').removeClass('disabled').removeAttr('disabled','disabled');
		}
	};
	showDynamicContent();	
	jQuery('.dynamic-content input:radio').click(function(){
		showDynamicContent();
	});
		
	function accordion(selector) {
		jQuery(selector).toggleClass('open').siblings('ul').slideToggle('fast');
		jQuery(selector).parents('li').toggleClass('selected');
	};
		
	function preferences(choice){
			jQuery(choice).toggleClass('open').next('.np-hide').slideToggle('fast');
	};
	preferences();
	
	/* This function highlights a parent h5 if a child input is checked, and removes the highlights when no checkboxes are selected */
	function setHeaderStyle(headerElement) {
		if (jQuery(headerElement).siblings().children().children('input:checked').length > 0) {
			jQuery(headerElement).addClass('selected');
		}
		else {
			jQuery(headerElement).removeClass('selected');
		}
	}
	function setLabelStyle(inputElement) {
		if (jQuery(inputElement).siblings('input:checked').length > 0) {
			jQuery(inputElement).addClass('selected');
		}
		else {
			jQuery(inputElement).removeClass('selected');
		}
	}	
	
	/* This loops through all the h5's, and actions "setHeaderStyle" */
	jQuery('h5').each(function() {
        var element = this;
       setHeaderStyle(this);
    });
	
	/* This loops through all the labels, and actions "setLabelStyle" */
	jQuery('label').each(function() {
        var element = this;
       setLabelStyle(this);
    });
	
	// Bookmarks -- for template purpose only - can remove //
	jQuery('#np-bookmarks h4').addClass('show-hide');
	jQuery('.show-hide').siblings('ul').hide();
	jQuery('.show-hide').click(function(){
			accordion(jQuery(this));
		}); // can remove this function for delivery		
	
	jQuery('.nhm-linkasset h6').addClass('show-hide');
	jQuery('.show-hide').siblings('ul').hide();
	jQuery('.show-hide').click(function(){
			accordion(jQuery(this));
		});
		
	// Package assest //	
	jQuery('.package h5').addClass('show-hide');
	jQuery('.show-hide').siblings('ul').hide();
	jQuery('.show-hide').click(function(){			
		jQuery(this).toggleClass('open').siblings('ul').slideToggle('fast');
		jQuery(this).parents('div.package').toggleClass('selected');
	});	
	// Jive doesn't like it when the accordion function is called, so had to repeat actions in click function
	
	jQuery('.profile').click(function(){
		jQuery(this).toggleClass('open').next('.jive-body-profile').slideToggle();
	});	
	
	// Preferences //	
	jQuery('.np-preferences ul ul').addClass('np-hide').prev('h5').addClass('np-accordion');
	jQuery('.np-preferences .np-accordion').click(function(){
		preferences(jQuery(this))
	});	
	
	jQuery('.np-preferences-content input:checkbox').click(function(){
		setHeaderStyle(jQuery(this).parent().parent().prev('h5'));
		setLabelStyle(jQuery(this).siblings('label'));
	});
	
	// Form Auto-advance //	
	function autoAdvance(limit, selector){
		if (selector.val().length >= limit) {
			jQuery(selector).next(selector).focus();
		}
	}
	jQuery('input.digits').keyup(function(){
		autoAdvance(4, jQuery(this));
	})
	
});
