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


//Setup the follow event
PT.follow = function( e, options ){
	
	//show the user that this target is loading
	SDNA.loading(options.target);
	
	if( options.url.match(/.htm/) ){
		
		Window.pop( 
			options.url,
			{
				close: function(){
					SDNA.loaded(options.target);
				}
			} 
		);
		
	}else{
	
		$j.getJSON( options.url , 
			function(response){
				
				//attach the target to the response
				$j.extend(response,options);
				
				if( response.success == true ){
					
					PT.followed( response );
				
				//There was an error checking in	
				}else{
					
					Window.pop( response.message, {error:true} );
					
				}
			}
		);
	}
}

PT.followed = function( response ){
	
	PT.follow.syncDOM( response );
	
	//Now that the follow has occured,
	//Publish a follow event to all listeners
	SDNA.Event.publish('follow',response);	

}

PT.unfollow = function( e, options ){
	if( options.target ){
		SDNA.loading(options.target);
	}
	
	$j.getJSON( 
		options.url, 
		function(response){
			
			//attach the target to the response
			$j.extend(response,options);		
			
			if(response.success == true){
				
				PT.unfollowed( response );
				
			//There was an error checking in	
			}else{
					
				Window.pop( response.message, {error:true} );
				
			}
		}
	);
}

PT.follow.syncDOM = function( response ){
	
	//Find all follow links on the page that are the
	//right entity (location, event, user)
	//and the right id
	$j("." + response.action)
		.filter('.' + response.entity + '_' + response.id)
		.each(
			function(){
				
				SDNA.loaded( this );
				
				$this = $j(this);
				//$action = $j(response.html);
				//$this.replaceWith( $action.addClass( $this.attr('class').replace(/unfollow|follow/,'') ));
				
				$action = $j(response.menu);
				var sClass = $this.attr('class').replace(/unfollow|follow/,'');
				$action.addClass( sClass ).find('.button').addClass( sClass );
				
				if( !$this.hasClass('button') ){
					$action.removeClass('button');
				}				
				$this.replaceWith( $action );

			}
		);

}

PT.unfollow.syncDOM = function(response){

	//Find all follow spans (or spans) on the page that are the
	//right entity (location, event, user)
	//and the right id
	$j("span.follow")
		.filter('.' + response.entity + '_' + response.id)
		.each(
			function(){
				SDNA.loaded( this );
				
				$this = $j(this);
				$action = $j(response.html);				
				var sClass = $this.attr('class').replace(/unfollow|follow/,'');
				$action.addClass( sClass ).find('.button').addClass( sClass );
				$this.replaceWith( $action );

			}
		);

}

PT.unfollowed = function( response ){
	
	//PT.follow.syncDOM( response );
	PT.unfollow.syncDOM( response );
	
	//Now that the unfollow has occured,
	//Publish a unfollow event to all listeners
	SDNA.Event.publish('unfollow',response);

}

//Handles a DOM click event on links with a class of follow
//and triggers the follow action with this link as the target
PT.follow.click = function( e ){	              
	
	//keep this link from functioning
	if( e ){
		e.preventDefault();
	}
	
	//Trigger the global follow action
	SDNA.Event.trigger(
		'follow',  
		{ 
			url: $j(this).attr('href'), 
			target: this 
		}
	);
	
	return false;

};

PT.followOptions = function(e){
	
	
	
}

PT.followOptions.createMenu = function( trigger ){

	//ref the jquery trigger
	var $trigger = $j(trigger);
	
	//create the menu
	var menu = new Menu( trigger );
		
	//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
	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, 
			className:  'unfollow ' + sClassId,
			label:		'Unfollow this ' + sNiceName
		}		
	);
	
	//last, return the menu
	return menu;

}

PT.followOptions.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 mFollow = Menu.getMenuFor( this );
	
	if( !mFollow ){
		mFollow = PT.followOptions.createMenu( this );
	} 
	
	//toggle open/closes the menu
	mFollow.toggle();
	
	return false;	

};

PT.unfollow.click = function( e ){	              
	
	//keep this link from functioning
	if(e){
		e.preventDefault();
	}
	
	var menu 		=  $j(this).parents('.menu').get(0);
	var trigger 	= Menu.getTriggerFor( menu );
	var follow 	= $j(trigger).parents('span.follow').get(0);
	
	SDNA.Event.trigger(
		'unfollow', 
		{ 
			url: $j(this).attr('href'), 
			target: follow 
		}
	);
	
	return false;
};


//Binds any link with a class of "follow" to the dom handler
PT.follow.bind = function( ){

	$j('a.follow').live('click.follow', 	PT.follow.click );
	$j('a.unfollow').live('click.follow', 	PT.unfollow.click );
	$j('a.follow_options').live('click.follow_options', PT.followOptions.click);
	
};

SDNA.Event.create('follow', 	PT.follow );
SDNA.Event.create('unfollow',	PT.unfollow );