JPGMAG.confirmDeleteContact=function(){if(!confirm('Are you sure you want to remove this person from your contacts?')){return false;}}
JPGMAG.confirmBlockMember=function(){if(!confirm('Are you sure you want to block this person from contacting you?')){return false;}}
JPGMAG.checkDescriptionLength=function(){
	var desc=$j(this).val();
	$j('#charCount').html(500-desc.length+' left');
	if(desc.length>=500)this.val(desc.substr(0,499));
}
JPGMAG.paymentAddressCountrySwap=function(){
	if($j('#addr_country').val()!='US' && $j('#addr_country').val()!='CA'){
		$j('#paymentStateAlt').show();
		$j('#paymentState').hide();
	}else{
		$j('#paymentStateAlt').hide();
		$j('#paymentState').show();
	}
}
JPGMAG.makeThatContact=function(){
	var origSpan=$j(this).parent('span'),origInner=origSpan.html(),userID=$j(this).attr('href').match(/\d+$/);
	var handleError=function(){alert('Argh! For some reason, that didn\'t work. Give it another go, and we\'ll try it again on our end.');origSpan.html(origInner);origSpan.find('.add').click(JPGMAG.makeThatContact);}
	$j(this).unbind('click');
	origSpan.html('Adding...');
	$j.ajax({type:'GET',url:'/actions/contacts.php',data:{action:'add',user_id:userID},success:function(d){if(d=='0'){handleError();}else{origSpan.addClass('contacted').html('A Contact!');}},error:handleError});
	return false;
}
JPGMAG.Profile = {
    toggle_editvotable: function(){
        $j('#people_votable_public').toggleClass('hideme');
        $j('#people_votable_private').toggleClass('hideme');
    },
    toggle_desc: function(e){
        JPGMAG.stopEvent(e);
        var el = this.id.split('_');
        var desctype = el[1];
        var tid = el[2];
        
        if(desctype == 'longdesc'){
            var old_tid = JPGMAG.Profile.current_desc;
            $j('#shortdesc_' + old_tid).toggleClass('hideme');
            $j('#longdesc_' + old_tid).toggleClass('hideme');
            
            JPGMAG.Profile.current_desc = tid;
        } else {
            JPGMAG.Profile.current_desc = null;
        }
        $j('#shortdesc_' + tid).toggleClass('hideme');
        $j('#longdesc_' + tid).toggleClass('hideme');
    }
};

$j(function(){
	$j('#deleteThisContact').click(JPGMAG.confirmDeleteContact);
	$j('#blockThisPerson').click(JPGMAG.confirmBlockMember);
	$j('#biofield').keyup(JPGMAG.checkDescriptionLength).keyup();
	$j('#addr_country').change(JPGMAG.paymentAddressCountrySwap);
	$j('#people').find('.add').click(JPGMAG.makeThatContact);
    
    $j('.trigger_people_votable_private').bind('click', JPGMAG.Profile.toggle_editvotable);
    $j('.trigger_desc').bind('click', JPGMAG.Profile.toggle_desc);
    $j('#profile_sort').bind('change', function(){ this.form.submit(); });
    $j('#profile_filter').bind('change', function(){ this.form.submit(); });
    
});