$(function(){

    $('.multiples').each(function(){
        multiple = $(this).find('.multiple');
        // hide all multiples at first
        multiple.hide();
        // loop over them, and show if it's the first, or has the class 'show'
        multiple.each(function(i){
            if(i == 0 || $(this).hasClass('show')){
                $(this).show();
            }
        });
    });

    $('.multiple_add').click(function(){
        var parent = $(this).parent();
        parent.find('.multiple:hidden').eq(0).show();
        // if all multiples are displayed, hide the add link so it isn't clickable
        if( parent.find('.multiple:visible').length == parent.find('.multiple').length ){
            $(this).hide();
        }
		return false;
    });
    
    $('.multiple_remove').click(function(){
        var parent = $(this).parent();
        // set all fields within the multiple to blank
        parent.find('input').val('');
        // hide multiple item
        parent.hide();
        // show the add link
        parent.parent('ul').next('a').show();
		return false;
    });
    
    $('.change_password').click(function(){
        if( $(this).attr("checked") == true ){
            $('.passwords').show();
        } else {
            $('.passwords').hide();
        }
    });
    
	$('#search_form').submit(function(e){
		var keyword = $(this).find('.masthead_search_input').val();
		var url = $(this).attr('action');
		redirect = url.substr(-1) != '/' ? url +'/'+ keyword : url + keyword;
		e.preventDefault();
		window.location = redirect;
	});
	
	var search_placeholder = $('.masthead_search_input').attr('placeholder');
	$('.masthead_search_input').attr('placeholder', '').val(search_placeholder);
	$('.masthead_search_input').focus(function(){
		if($(this).val() == search_placeholder) {
			$(this).val('');
		}
	});
	$('.masthead_search_input').blur(function(){
		if($(this).val() == '') {
			$(this).val(search_placeholder);
		}
	});

	$('.home_slider').prepend('<div id="tabs"><ul></ul></div>').cycle({ 
	    fx:     'none', 
	    speed:  'fast', 
	    timeout: 8000, 
	    pager:  '#tabs ul', 
		slideExpr: '.slider_content div',
		activePagerClass: 'active',
		pause: true,

	    // callback fn that creates a thumbnail to use as pager anchor 
	    pagerAnchorBuilder: function(index, slide) {
		 	title = $(slide).attr('title');
	        return '<li><a href="#">'+ title +'</a></li>'; 
	    } 
	});
	
	show_member_type_options($('#member_type'));
	$('#member_type').change(function(){
		show_member_type_options($(this));
	});

	
	
    $("ul#news_ticker").liScroll(); 
});

var show_member_type_options = function(ele){
    var val = ele.val();
	if(val == 'health-care-professionals'){
		$('#member_types_sub_options, #suffix_options').show();
	} else {
		$('#member_types_sub_options input').attr('checked', false);
		$('#suffix').val('');
		$('#member_types_sub_options, #suffix_options').hide();
	}
}


jQuery.fn.placeholders = function(options)
{
    var settings = {
        overlap: true,
        ignore: ''             
    }

    if(options) {
        jQuery.extend(settings, options);
    };

    this.not(settings.ignore).each(function(){
        var input = $(this).next('input[type="text"], input[type="password"], textarea');
        var label = $(this);
        var placeholder = input.attr('placeholder');
        
        // first check all inputs for values, then hide it's label if it has a value
        if( settings.overlap ) {
            label.addClass('overlap');
        }
        
        if( settings.overlap && input.val() != '' ) {
            label.hide();
        }
        
        if(placeholder) {
            label.text(placeholder);
        }
        
        label.live('click', function(){
            label.hide();
            input.focus();
        }, function(){
            if( input.val() == '' ) {
                label.show();
            }
        });
        
        // then add a focus event to all inputs to hide their labels when focused on, 
        // and remain hidden on blur if a value is entered
        input.live('focus', function(){
            label.hide();
        }).live('blur', function(){
            if( $(this).val() == '' ) {
                label.show();
            }
        });
    });
}

