var _topic_bookmark = window.IPBoard;

_topic_bookmark.prototype.topic_bookmark = {
	
	init: function()
	{
		Debug.write("Initializing topic_bookmark.js");
		
		document.observe("dom:loaded", function(){   
		$('topic_bookmarks').show();
        ipb.delegate.register(".topic_bookmark", ipb.topic_bookmark.bookmarkInit);		
		});
	},

	bookmarkInit: function(e, elem)
	{   
        topicID = ipb.topic.topic_id;
        b_id    = elem.id.match('b_id_([0-9a-z]+)')[1];
        
        if( Object.isUndefined(topicID) ){ return; }		
		if( parseInt(topicID) == 0 ){ return false; }        
        if( Object.isUndefined(b_id) ){ return; }		
		if( parseInt(b_id) == 0 ){ return false; } 
		        
        Event.stop(e);
        
		var url = ipb.vars['base_url'] + 'app=socialbookmarks&module=ajax&section=topic&t=' + topicID + '&b_id=' + b_id;

		new Ajax.Request(	url,
							{
								method: 'post',
								evalJSON: 'force',
								parameters: {
									secure_key: 	ipb.vars['secure_hash']
								},
								onSuccess: function(t)
								{
    								if( t.responseJSON['error'] )
    								{
                                        alert(ipb.lang[ t.responseJSON['error'] ]);								    
    				                }
									else
                                    {
                                        if( t.responseJSON['url'] )
                                        {
                                            window.location = t.responseJSON['url'];                                            
                                        }
                                        else
                                        {
                                            alert(ipb.lang['bookmark_no_url']);
                                        }
                                    }
								}
							}
						);
	}
}
ipb.topic_bookmark.init();