var _osn_processing={};
function osn_processing_set( section ) {
    name= (section.split('-'))[0]
	_osn_processing[name]= true;
}
function osn_processing_unset( section ) {
    name= (section.split('-'))[0]
	_osn_processing[name]= false;
}
function osn_processing_check( section ) {
    name= (section.split('-'))[0]
	if ( _osn_processing[name] ) {
		return true;
	}
	return false;
}




function osnButtonOver( img_node , name ) {
    if ( osn_processing_check( name ) ) {
    	return;
    }
    img_node.src= "/_img/btn/" + name + "-over.png";
    if ( name == 'contact-is' ) {
	    img_node.src= "/_img/btn/contact-del-over.png";
	}
    if ( name == 'follow-is' ) {
	    img_node.src= "/_img/btn/follow-del-over.png";
	}
    if ( name == 'group_membership-is' ) {
	    img_node.src= "/_img/btn/group_membership-del-over.png";
	}
}
function osnButtonOut( img_node , name ) {
    if ( osn_processing_check( name ) ) {
    	return;
    }
	img_node.src= "/_img/btn/" + name + ".png";
    if ( name == 'contact-is' ) {
	    img_node.src= "/_img/btn/contact-is.png";
	}
    if ( name == 'follow-is' ) {
	    img_node.src= "/_img/btn/follow-is.png";
	}
    if ( name == 'group_membership-is' ) {
	    img_node.src= "/_img/btn/group_membership-is.png";
	}
}


function osnButtonClick( img_node , name , id_val  ) {
    var _authentication_token= $j('#_authentication_token').val()

    if ( osn_processing_check( name ) ) {
    	return;
    }
    osn_processing_set(name);
    img_node.src= "/_img/btn/processing.gif";
    

    
    if ( name == 'contact-add' ) {
        $j.post( '/service/osn/contact/add', { 'user.hex_id':id_val, 'format':'json' , '_authentication_token': _authentication_token } , osnContact_gotResult , "json");
        return;
    }
    else if ( name == 'contact-is' ) {
        $j.post( '/service/osn/contact/del', { 'user.hex_id':id_val, 'format':'json' , '_authentication_token': _authentication_token } , osnContact_gotResult , "json");
        return;
    }
    else if ( name == 'follow-add' ) {
        $j.post( '/service/osn/follow/add', { 'user.hex_id':id_val, 'format':'json' , '_authentication_token': _authentication_token } , osnFollow_gotResult , "json");
        return;
    }
    else if ( name == 'follow-is' ) {
        $j.post( '/service/osn/follow/del', { 'user.hex_id':id_val, 'format':'json' , '_authentication_token': _authentication_token } , osnFollow_gotResult , "json");
        return;
    }
    else if ( name == 'group_membership-add' ) {
        $j.post( '/service/osn/group/join', { 'group.hex_id':id_val, 'format':'json' , '_authentication_token': _authentication_token } , osnGroupMembership_gotResult , "json");
        return;
    }
    else if ( name == 'group_membership-is' ) {
        $j.post( '/service/osn/group/leave', { 'group.hex_id':id_val, 'format':'json' , '_authentication_token': _authentication_token } , osnGroupMembership_gotResult , "json");
        return;
    }
}


function osnContact_gotResult(responseText, statusText){
    osn_processing_unset('contact');
	$j('#btn-contact').empty();
	if ( responseText.status == 'success' ) {
	    $j('#btn-contact').empty();
	    affected= responseText['affected']
	    if ( responseText[affected].direction == 'add' ) {
	        var image= '<img \
											id="btn-contact-btn" \
											src="/_img/btn/contact-is_requested.png" \
											onMouseOver="osnButtonOver(this,\'contact-is\')" \
											onMouseOut="osnButtonOut(this,\'contact-is_requested\')" \
											onClick="osnButtonClick(this,\'contact-is\',\'###HEX###\')" \
									/>';
				image= image.replace( '###HEX###' , responseText[affected]['user.hex_id'] )
		    $j('#btn-contact').html(image);
		}
	    else if ( responseText[affected].direction == 'del' ) {
	        var image= '<img \
											id="btn-contact-btn" \
											src="/_img/btn/contact-add.png" \
											onMouseOver="osnButtonOver(this,\'contact-add\')" \
											onMouseOut="osnButtonOut(this,\'contact-add\')" \
											onClick="osnButtonClick(this,\'contact-add\',\'###HEX###\')" \
									/>';
				image= image.replace( '###HEX###' , responseText[affected]['user.hex_id'] )
		    $j('#btn-contact').html(image);
		}
	}
	else {
	    $j('#btn-contact').html('<img src="/_img/btn/error.png"/>');
	    if ( responseText['error-json'] ) {
	    	alert(responseText['error-json']);
	    }
	}	
}

function osnFollow_gotResult(responseText, statusText){
    osn_processing_unset('follow');
	$j('#btn-follow').empty();
	if ( responseText.status == 'success' ) {
	    $j('#btn-follow').empty();
	    affected= responseText['affected']
	    if ( responseText[affected].direction == 'add' ) {
	        var image= '<img \
											id="btn-follow-btn" \
											src="/_img/btn/follow-is.png" \
											onMouseOver="osnButtonOver(this,\'follow-is\')" \
											onMouseOut="osnButtonOut(this,\'follow-is\')" \
											onClick="osnButtonClick(this,\'follow-is\',\'###HEX###\')" \
									/>';
				image= image.replace( '###HEX###' , responseText[affected]['user.hex_id'] )
		    $j('#btn-follow').html(image);
		}
	    else if ( responseText[affected].direction == 'del' ) {
	        var image= '<img \
											id="btn-follow-btn" \
											src="/_img/btn/follow-add.png" \
											onMouseOver="osnButtonOver(this,\'follow-add\')" \
											onMouseOut="osnButtonOut(this,\'follow-add\')" \
											onClick="osnButtonClick(this,\'follow-add\',\'###HEX###\')" \
									/>';
				image= image.replace( '###HEX###' , responseText[affected]['user.hex_id'] )
		    $j('#btn-follow').html(image);
		}
	}
	else {
	    $j('#btn-follow').html('<img src="/_img/btn/error.png"/>');
	    if ( responseText['error-json'] ) {
	    	alert(responseText['error-json']);
	    }
	}	
}


function osnGroupMembership_gotResult(responseText, statusText){
    osn_processing_unset('group_membership');
	$j('#btn-group_membership').empty();
	if ( responseText.status == 'success' ) {
	    $j('#btn-group_membership').empty();
	    affected= responseText['affected']
	    if ( responseText[affected].direction == 'join' ) {
	        var image= '<img \
											id="btn-group_membership-btn" \
											src="/_img/btn/group_membership-is.png" \
											onMouseOver="osnButtonOver(this,\'group_membership-is\')" \
											onMouseOut="osnButtonOut(this,\'group_membership-is\')" \
											onClick="osnButtonClick(this,\'group_membership-is\',\'###HEX###\')" \
									/>';
				image= image.replace( '###HEX###' , responseText[affected]['group.hex_id'] )
		    $j('#btn-group_membership').html(image);
		}
	    else if ( responseText[affected].direction == 'leave' ) {
	        var image= '<img \
											id="btn-group_membership-btn" \
											src="/_img/btn/group_membership-add.png" \
											onMouseOver="osnButtonOver(this,\'group_membership-add\')" \
											onMouseOut="osnButtonOut(this,\'group_membership-add\')" \
											onClick="osnButtonClick(this,\'group_membership-add\',\'###HEX###\')" \
									/>';
				image= image.replace( '###HEX###' , responseText[affected]['group.hex_id'] )
		    $j('#btn-group_membership').html(image);
		}
	}
	else {
	    $j('#btn-group_membership').html('<img src="/_img/btn/error.png"/>');
	    if ( responseText['error-json'] ) {
	    	alert(responseText['error-json']);
	    }
	}	
}


function osnNotificationFriendRequest( img_node , name , notification_id , user_id_from  ) {
    var _authentication_token= $j('#_authentication_token').val()

    img_node.src= "/_img/btn/processing.gif";

    if ( name == 'ignore' ) {
        $j.post( '/service/osn/contact/request/ignore', { 'user.hex_id':user_id_from, 'notification.id': notification_id , 'format':'json' , '_authentication_token': _authentication_token } , osnNotificationFriendRequest_gotResult , "json");
        return;
    }
    if ( name == 'accept' ) {
        $j.post( '/service/osn/contact/request/accept', { 'user.hex_id':user_id_from, 'notification.id': notification_id , 'format':'json' , '_authentication_token': _authentication_token } , osnNotificationFriendRequest_gotResult , "json");
        return;
    }
}

function osnNotificationFriendRequest_gotResult(responseText, statusText){
	if ( responseText.status == 'success' ) {
	    var affected= responseText['affected']
	    var nid = responseText[affected]['notification_id']
	    $j('#notification-'+nid).remove();
	}
	else {
		alert('Error');
	}	
}


function osnNotificationGroupInvite( img_node , name , notification_id , group_id  ) {
    var _authentication_token= $j('#_authentication_token').val()

    img_node.src= "/_img/btn/processing.gif";

    if ( name == 'ignore' ) {
        $j.post( '/service/osn/group/invite/ignore', { 'group.hex_id':group_id, 'notification.id': notification_id , 'format':'json' , '_authentication_token': _authentication_token } , osnNotificationGroupInvite_gotResult , "json");
        return;
    }
    if ( name == 'accept' ) {
        $j.post( '/service/osn/group/invite/accept', { 'group.hex_id':group_id, 'notification.id': notification_id , 'format':'json' , '_authentication_token': _authentication_token } , osnNotificationGroupInvite_gotResult , "json");
        return;
    }
}

function osnNotificationGroupInvite_gotResult(responseText, statusText){
	if ( responseText.status == 'success' ) {
	    var affected= responseText['affected']
	    var nid = responseText[affected]['notification_id']
	    $j('#notification-'+nid).remove();
	}
	else {
		alert('Error');
	}	
}

