var Location = function() {}

SDNA.Location = new Location();
	
Location.prototype.setHomeLocation = function(jsonSpanID) {
	var jsonLocdata = $j('#' + jsonSpanID).text();
	$j.post(	'/ajax/User/SetHomeLocation.jsn', 
				jsonLocdata, 
				function(data,textStatus) {
					$j('#q').val('');
					$j('#searchResultsDiv').html('');
					$j('#homelocationtitle').text(data['locTitle']);
				}, 
				'json');
}

Location.prototype.setRegHomeLocation = function(jsonSpanID) {
	var jsonLocdata = $j('#' + jsonSpanID).text();	
	$j.post(	'/ajax/User/setRegHomeLocation.jsn', 
				jsonLocdata,
				function(data,textStatus) {
					$j('#q').val('');
					$j('#searchResultsDiv').hide();
					$j('#searchform').hide();
					$j('#currentloc').show();
					$j('#homelocationtitle').text(data['locTitle']);
				}, 
				'json');
}

Location.prototype.addEventLocation = function(jsonSpanID) {
	var jsonLocdata = $j('#' + jsonSpanID).text();	
	var id = $j('#eventid').val();
	jsonLocdata = jsonLocdata.substr(0, jsonLocdata.length -1) + ',"event_id":"'+id+'"}';
	
	$j.post(	'/ajax/Event/createEventLocation.jsn', 
				jsonLocdata,
				function(data,textStatus) {
					$j('#q').val('');
					$j('#searchResultsDiv').hide();
					$j('#searchform').hide();
					$j('#currentloc').show();
					$j('#eventlocationtitle').text(data['locTitle']);
					$j('#removeevent').attr('eventlocid',data['eventlocid']); 
				}, 
				'json');
}

Location.prototype.removeEventLocation = function(element) {
	
	var eventlocid = element.getAttribute('eventlocid');
	var eventid = $j('#eventid').val();
	
	
	$j.post(	'/ajax/Event/removeEventLocation.jsn', 
				{"eventid":eventid, "eventlocid":eventlocid},
				function(data) {
					$j('#q').val('');
					$j('#searchform').show();
					$j('#currentloc').hide();
				}, 
				'json');
}

Location.prototype.setCurrentLocation = function( options ){
	
	//setup the options for the request
	options.callback 	= options.callback || function(){};
	options.json 		= options.json || null;
	options.url 		= options.url || '/ajax/User/SetCurLocation.jsn';
	
	//post the request
	$j.post( 
		options.url,
		options.json,
		options.callback,
		'json'
	);
	
} 

Location.prototype.addFavorite = function( options ) {

	//setup the method to use
	options.method = 'addFavorite.jsn';	

	SDNA.Location.toggleFavorite( options );
	
}

Location.prototype.removeFavorite = function( options ){
	
	//setup the method to use
	options.method = 'removeFavorite';	

	SDNA.Location.toggleFavorite( options );
	
}

Location.prototype.toggleFavorite = function( options ){
	
	options.url 		= '/ajax/Location/' + options.method + '/id:' + options.location_id;
	
	//post the request
	$j.post( 
		options.url,
		options.vars,
		options.callback,
		'json'
	);
}




Location.prototype.setCurLocation = function(jsonSpanID) {
	var jsonLocdata = $j('#' + jsonSpanID).text();
	$j.post(	'/ajax/User/SetCurLocation.jsn', 
				jsonLocdata, 
				function(data,textStatus) {
					$j('#q').val('');
					$j('#searchResultsDiv').html('');
					if (data['success']) {
						$j('#curlocationtitle').text(data['locTitle']);
						$j('#' + jsonSpanID).parent().html("This is your current location.");
					} else {
						$j('#' + jsonSpanID).parent().children("#locationselect").html("We've experienced an error setting your location. Click here to try again.");
					}
				}, 
				'json');
}

Location.prototype.setLocationAsCurrent = function(link, id) {
	 $j(".currentloc").text('Set as current location');
	 //hmm, can't do this as we don't know the other location links id.
	 //at least setting the text back.
	 //$j(".currentloc").attr('onclick', 'SDNA.Location.setLocationAsCurrent(this, {{$locs[i].id}})');

	  $j.ajax({
        url: '/User/setLocationAsCurrent/location:'+id,
        success: function(z) {  
            $j(link).text("This is your current location.");
        }  
    });
    return false;
}

Location.prototype.setLocationAsFavorite = function(link, id) {
	  $j.ajax({
        url: '/Location/addFavorite/id:'+id,
        success: function(z) {  
            $j(link).text("Remove as Favorite");
            link.setAttribute("onclick", 'SDNA.Location.removeLocationAsFavorite(this, '+id+')');
        }  
    });
    return false;
}

Location.prototype.removeLocationAsFavorite = function(link, id) {
	  $j.ajax({
        url: '/Location/removeFavorite/id:'+id,
        success: function(z) {  
            $j(link).text("Add as Favorite");
            link.setAttribute("onclick", 'SDNA.Location.setLocationAsFavorite(this, '+id+')');
        }  
    });
    return false;
}

Location.prototype.removeLocationFromFavList = function(locId, idtoHide) {
	  $j.ajax({
        url: '/Location/removeFavorite/id:'+locId,
        success: function(z) {  
            $j('#'+idtoHide).hide("slow");
        }  
    });
    return false;
}
