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


//Setup the follow event
PT.changeHome = function( e, options ){
	
	//show the user that this target is loading
	SDNA.loading(options.target);
	
	var $action = $j(options.target);
	
	if( $action.is('[href*=.htm]') ){
		
		Window.pop( 
			options.url,
			{
				close: function(){
					SDNA.loaded(options.target);
					// if a user explicitly closes, then make sure they don't see the popup anymore
					$j.get('/ajax/User/HomeLocationOptOut.jsn');
					SDNA.user.home_location_set = 1;
				}
			} 
		);
		
	}else{

		//Checkin using ajax. Will get a JSON object back as response
		$j.getJSON( 
			options.url , 
			function(response){
				
				//attach the target to the response
				$j.extend(response,options);
				
				//End the loading indicator
				SDNA.loaded(response.target);
				
				if(response.success && response.html){
					
					PT.changedHome( response );
				
				//There was an error checking in
				}else if(response.success == false){
					
					//pop an error message
					Window.pop( response.message, {error:true} );
				
				}
			}
		);
	}
}

PT.changedHome = function( response ){
	
	PT.changeHome.syncDOM( response );

	//Now that the follow has occured,
	//Publish a follow event to all listeners
	SDNA.Event.publish('home_changed',response);	

	//if its the first time the user sets a home he will also checkin.
	if (response.checkin){
		response.checkin.triggered_by = 'change_home';
		PT.checkedIn(response.checkin);
	} 
	
}

PT.changeHome.syncDOM = function( response ){
	
	//Find all change home links (or spans) on the page that are the
	$j("a." + response.action)
		.filter('.' + response.entity + '_' + response.id)
		.each(
			function(){				
				SDNA.loaded( this );
				
				var $this = $j(this);
				var $action = $j(response.html);
				if( $this.hasClass('button') ){
					$this.replaceWith($action.addClass('button'));
				}else{
					$this.replaceWith($action);
				}
			}
		);
	
	//Now we need to find all other checkin links and checkout
	$j("span." + response.action )
		.not('.' + response.entity + '_' + response.id)
		.convertElementTo('a')
		.html('<span>Set as home</span>');

}

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

};

//Find any links with a class of home and update them to the new home
PT.changeHome.updateHome = function( e, response){
	
	$j('.home')
		.filter('a,span')
		.each(
			function(){
				var $this = $j(this);
				// replace the home location links with the correct
				// new home title. if there is a nested span, replace
				// the text into that, otherwise the a or span selected
				if ($this.find('span').length > 0) {
				    $this.find('span').text( response.title );
				} else {
				    $this.text( response.title);
				}
				
				if( $this.attr('href') ){
					$this.attr('href', $this.attr('href').replace(/(id:)\d+/,'$1' + response.id) );
				}
			}
		);
}

//Find the checkin home link and update the path to now checkin at the new home
PT.changeHome.updateCheckinHome = function(  e, response  ){
	
	if( response.success ){
		
		$j('.checkin_home')
			.filter('a,span')
			.each(
				function(){
					var $this = $j(this);
					if( $this.attr('href') ){
						$this.attr('href', '/ajax/User/SetCurLocation.jsn/locationid:' + response.id );
					}
				}
			).show();	
		
		if( typeof response.home !== "undefined" && response.home === true ){	
			$j('.checkin_home')
				.convertElementTo('span')
				.html("<span>You're at home</span>");				
		}else{
			$j('.checkin_home')
				.convertElementTo('a')
				.html("<span>Check in at home</span>");
		}
	}
		
}


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

	$j('a.change_home').live('click.change_home', 	PT.changeHome.click );
	
};