//Setup a PlanetTagger namespace
var PT = PT || {};

//Setup a user namespace
PT.user = PT.user || {};

PT.user.followClick = function( e ){	              
	
	//keep this link from functioning
	if(e){
		e.preventDefault();
	}
	
	SDNA.Event.trigger(
		'follow', 
		{ 
			url: $j(this).attr('href'), 
			target: null 
		}
	);
};

PT.user.unFollowClick = function( e ){	              
	
	//keep this link from functioning
	if(e){
		e.preventDefault();
	}
	
	SDNA.Event.trigger(
		'unfollow', 
		{ 
			url: $j(this).attr('href'), 
			target: null
		}
	);
};

PT.user.createMenu = function( trigger ){

	//ref the jquery trigger
	var $trigger = $j(trigger);
	
	//setup the menu offset
	var offset = {top:0,left:5};
	
	//create the menu
	var menu = new Menu( trigger, {offset:offset } );
		
	//get the data associated with this menu	
	var data = eval('(' + $trigger.attr('data') + ')');
	
	var sNiceName = data.entity == 'Location'? 'place' : sNiceName;
	sNiceName = data.entity == 'Event'? 'event' : sNiceName;
	sNiceName = data.entity == 'User'? 'person' : sNiceName;
	
	var sClassId = data.entity.toLowerCase() + '_' + data.id;	
	
	//now add options to the menu
	if( Number(window.CONFIG.viewer_id) == data.id ){
		
		menu.addOptions(
			{	
				action: 		'/User/View/id:' + data.id, 
				label:		'Goto your profile'
			},
			{	
				action: 		'/User/Edit/id:' + data.id, 
				label:		'Edit your settings'
			},
			{	
				action: 		'javascript:UserVoice.Popin.show();', 
				label:		'Got feedback?'
			}
		);
		
	}else{
		
		//now add options to the menu
		menu.addOptions(
			{	
				action: 		'/User/View/id:' + data.id, 
				label:		"View person's profile"
			}
		);
		
		if( Boolean(data.following) == true ){
			menu.addOptions(
				{	
					action: 		'/ajax/' + data.entity + '/Follow.htm/id:' + data.id, 
					className:  'follow ' + sClassId,
					label:		'Change follow settings'
				},
				{	
					action: 		'/ajax/' + data.entity + '/UnFollow.jsn/id:' + data.id, 
					label:		'Unfollow this ' + sNiceName,
					click: 		PT.user.unFollowClick
				}				
			);		
		}else{
			menu.addOptions(
				{	
					action: 		'/ajax/' + data.entity + '/Follow.htm/id:' + data.id, 
					label:		'Follow this ' + sNiceName,
					click:		PT.user.followClick
				}			
			);	
		}
			
	}
	
	//last, return the menu
	return menu;

};

PT.user.click = function( e ){	              
	
	//keep this link from functioning
	if(e){
		e.preventDefault();
		e.stopPropagation();
	}
	
	//get a ref to this link
	var $this = $j(this);	
	
	//get the menu
	var menu = Menu.getMenuFor( this );
	
	if( menu ){
		menu.destroy();
	} 
	menu = PT.user.createMenu( this );
	
	//toggle open/closes the menu
	menu.toggle();
	
	return false;	

};

PT.user.syncMenuData = function( e, response ){
	
	$j('.person_menu')
		.filter('.user_' + response.id)
		.each(
			function(){				
				$this = $j(this);
				var oData = eval('(' + $this.attr('data') + ')');
				oData.following = response.action=='follow'? true:false;
				$this.attr('data', SDNA.utils.json2String(oData) );
			}
		);
	
}

//Binds any user related actions
PT.user.bind = function( ){

	$j('a.person_menu').live('click.person_menu', PT.user.click);
	
};

SDNA.Event.subscribe('follow', PT.user.syncMenuData );
SDNA.Event.subscribe('unfollow', PT.user.syncMenuData );