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

PT.checkin = PT.checkin || {};

//Setup the checkin event
PT.checkin.handler = function( e, options ){
	
	var $action = $j(options.target);
	
	if( $action.is('[href*=.htm]') ){
		
		Window.pop( 
			options.url,
			{
				className: 'pop_search'
			} 
		);
		
	}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);
				
				if(response.success && response.html){
					
					PT.checkedIn( response );
				
				//There was an error checking in
				}else if(response.success == false){
					
					//pop an error message
					Window.pop( response.message, {error:true} );
				
				}
			}
		);
	}
}

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

}


PT.checkin.syncDOM = function( response ){
	
	//Find all checkin links (or spans) on the page that are the
	//right entity (location, event) and the right id (ie. classname of location_1005 or event_201)
	$j("a." + response.action)
		.filter('.' + response.entity + '_' + response.id)
		.each(
			function(){				
				SDNA.loaded( this );
				
				var $this = $j(this);
				var $action = $j(response.html);
				$this.replaceWith( $action.addClass( $this.attr('class') ));
			}
		);
	
	//Now we need to find all other checkin links and checkout
	$j("span." + response.action )
		.not('.' + response.entity + '_' + response.id)
		.convertElementTo('a')
		.html('<span>Check-in here</span>');
}


PT.checkin.updateCurrentLocation = function( e, response ){
	
	if( response.success == true ){
		var $current = $j('a.current_location');
		$current.each(
			function(){
				//Point this to a new link
				if( response.entity == 'location'){
					this.href = '/Location/View/id:' + response.id;
				}else{
					this.href = '/Event/View/id:' + response.id;
				}
				
				var truncatedTitle = response.title.truncate(25);
				
				//Update the text and title
				$j(this).text( truncatedTitle ).attr('title', 'View ' + response.title);
			
			}
		)	
	}

}

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

};

PT.checkin.checkinHome = {};

PT.checkin.updateCheckinHome = function(  e, response  ){
	
	if( response.success ){
		
		if( 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>");
		}
	}
		
}

//Update the hidden input for location/event id on the share form when 
//a new checkin happens
PT.checkin.updateShareForm = function(e, response){
	
	//find the share form
	$form = $j('.homepage_index form.share');
	
	//remove both event and location ids, because this new checkin is different
	$form.find(':hidden[name=location_id],:hidden[name=event_id]' ).remove();
	
	//create a new input
	$input = $j('<input />').attr(
		{
			type: 	'hidden',
			name: 	response.entity + '_id',
			value: 	response.id
		}
	);
	
	//add this location/event id
	$form.prepend( $input );
		
}

PT.checkin.checkinHome.click = function( e ){	              
	
	//keep this link from functioning
	e.preventDefault();
	
	//Trigger the global checkin action
	SDNA.Event.trigger(
		'checkin',  
		{ 
			url: $j(this).attr('href'), 
			target: this,
			message: "You're at home" 
		}
	);

};


PT.checkin.updateGMap = function( e, response ){
	
	//reload the gmap
	try{
		var ptInfoWIndow = Google.maps.getInfoWindow( response.entity, response.id );
		if( ptInfoWIndow ){
			
			ptInfoWIndow.reload(true);
		}
	}catch( e ){
		return false;
	}

}

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

	$j('a.checkin').live('click.checkin', 	PT.checkin.click );
	$j('a.checkin_home').live('click.checkin', PT.checkin.checkinHome.click );
	
};


SDNA.Event.create('checkin', PT.checkin.handler );

//Setup event subscriber to update the current location on checkin
SDNA.Event.subscribe('checkin', PT.checkin.updateCurrentLocation );

//Subscribe to checkin to update home status
SDNA.Event.subscribe('checkin', PT.checkin.updateCheckinHome );

//Subscribe to checkin to update share form location|event id
SDNA.Event.subscribe('checkin', PT.checkin.updateShareForm );

//Subscribe to checkin to # of people checked in on location|event
SDNA.Event.subscribe('checkin', PT.checkin.updateGMap );