var AjaxHSD = {};

AjaxHSD.urlbase = 'http://my.hotspotdot.net/';
AjaxHSD.counter = 1000;

AjaxHSD.logout = function()
{
    Ajax.call(AjaxHSD.urlbase + 'controller/logout.php', 'POST', '', function() {
                window.location = "default.html";           
        });
}

AjaxHSD.deleteSpot = function(spotId)
{
    Ajax.call(AjaxHSD.urlbase + 'controller/delete-hotspot.php', 'POST', 'spot_id=' + spotId, function() {
                
				$('#hotspot_li_' + spotId).remove();
           
        });
}

AjaxHSD.shareSpotEmail = function(spotId, sendTo)
{
    Ajax.call(AjaxHSD.urlbase + 'controller/share-email-hotspot.php', 'POST', 'spot_id=' + spotId + '&sendto=' + sendTo, function() {
            
			alert('Hotspot sent.');    
			           
        });
}

AjaxHSD.updateSpotLocation = function(_spotId, _newLocation, _gMap)
{

	var latlonPair = _newLocation.substr(1);
	latlonPair = latlonPair.substr(0, latlonPair.length-1);
	latlonPair  = latlonPair.split(',');
	var lat = jQuery.trim(latlonPair[0]);
	var lon = jQuery.trim(latlonPair[1]);

    var data = "spot_id=" + _spotId + "&new_location=" + _newLocation;

    Ajax.call(AjaxHSD.urlbase + 'controller/update-hotspot-location.php', 'POST', data, function() {
            
			$('#hs_' + _spotId).unbind('click');
			$('#hs_' + _spotId).click(function() { 
					
					var latlong = new google.maps.LatLng(lat, lon);
					_gMap.setCenter(latlong);
					return false;
					
					});					
           
           alert('Location updated.');
           
        });
}


AjaxHSD.getUserInfo = function(_container)
{
    Ajax.call(AjaxHSD.urlbase + 'controller/getlogin.php', 'POST', '', function(xml) {
			
			var name =  $('username', xml).text();			
			_container.text(name);
        });
}

AjaxHSD.getSpots = function(_listContainer, _gMap, _marker)
{
	$(_listContainer).html('<li><p><img style="vertical-align:middle;" src="images/preloader1.gif" alt="preloader" /> Loading...</p></li>');

    Ajax.call(AjaxHSD.urlbase + 'controller/get-all-spots.php', 'GET', '', function(xml) {			
					
             $(_listContainer).html('');
            						
			 $(xml).find('hs').each(function() {
										
					var htmlout = '';
					
					var id = $('id', this).text();		
					var title = $('title', this).text();		
					var lat = $('lat', this).text();		
					var lon = $('long', this).text();		
					
					htmlout += '<li id="hotspot_li_' + id + '">';
					
					htmlout += '<p><a id="hs_' + id + '" class="title" href="#">' + title + '</a></p>';
					
					var tags = new Array();
					$(this).find('tag').each(function() {
						
							var tg = $(this).text();
							tags.push(tg);						
						});
					
					var tags_str = tags.join(", ");
					if(tags_str.length > 0)
					{						
						htmlout += '<p class="tags">' + tags_str + '</p>';
					}
					else
					{
						htmlout += '<p class="tags"></p>';
					}
					
					updateLocClick = function(spot_id)
					{
					    var newLoc = _marker.getPosition().toString();
					    AjaxHSD.updateSpotLocation(spot_id, newLoc, _gMap);
					}
					
					updateMetaClick = function(spot_id, title, tags)
					{					
						$('#update_hotspot_dialog_title').val(title);
						$('#update_hotspot_dialog_tags').val(tags);
						$('#update_hotspot_dialog_spot_id').val(spot_id);		
									
					    $('#update_hotspot_dialog').dialog( {width: 400}, { modal: true}, { resizable: false}, { draggable: false }, { buttons: { "Update": function() { 
					    			                                         		
																	Ajax.submitForm(AjaxHSD.urlbase + 'controller/update-hotspot.php', document.update_hotspot_form, 
																					function(xml) {
																					
																						$('#hs_' + spot_id).html( $('#update_hotspot_dialog_title').val() );
																						$('#hotspot_li_' + spot_id + ' p.tags').html( $('#update_hotspot_dialog_tags').val() );
																						
																						//rebind
																						$('#hotspot_li_' + spot_id + ' a.update_meta_lnk').unbind('click');
																						$('#hotspot_li_' + spot_id + ' a.update_meta_lnk').click(function() { 
																								
																									updateMetaClick(spot_id, $('#update_hotspot_dialog_title').val(), $('#update_hotspot_dialog_tags').val()); 
																									return false;
																								
																								});			
																																												
			                                         									alert('Hotspot updated.');					
			                                         								});
		                                         								
																	$('#update_hotspot_dialog').dialog('close');			                                         								
			                                         					
			                                         			}}} );
					
					}
					
					delClick = function(spot_id)
					{
					    $('#delete_confirm').dialog( {width: 400 }, { modal: true }, { resizable: false }, { draggable: false },
			                                         { buttons: { "Cancel": function() { $('#delete_confirm').dialog('close'); }, "Delete": function() { 
			                                         
			                                         		AjaxHSD.deleteSpot(spot_id); 
			                                         		$('#delete_confirm').dialog('close');
			                                         		
			                                         		} } });
					}
					
					shareClick = function(spot_id, title, tags, loclat, loclong)
					{
						$('#share_hotspot_dialog_spot_id').val(spot_id);
						
					    $('#share_hotspot_dialog').dialog( {width: 400 }, { modal: true }, { resizable: false }, { draggable: false },
			                                         { buttons: {"Send": function() { 
			                                         
			                                         			var sendto = $('#share_hotspot_dialog_sendto').val();
			                                         			AjaxHSD.shareSpotEmail(spot_id, sendto);
			                                         		
			                                         			$('#share_hotspot_dialog').dialog('close');
			                                         		
			                                         		} } });
					}
					
					
					htmlout += '<p class="submenu"><a onclick="updateLocClick(' + id + '); return false;" href="#">update location</a> | <a class="update_meta_lnk" href="#">update metadata...</a>  | <a class="share_lnk" href="#">share (email)...</a> | <a href="#" onclick="delClick(' + id + '); return false;">delete</a></p>';
					
					htmlout += '</li>';					
										
					$(_listContainer).append(htmlout);
					
					$('#hotspot_li_' + id + ' a.update_meta_lnk').click(function() {
								
								updateMetaClick(id, title, tags_str); 								
								return false;					
								
							});
							
					$('#hotspot_li_' + id + ' a.share_lnk').click(function() {
								
								shareClick(id, title, tags_str, lat, lon); 								
								return false;					
								
							});
							
					
					$('#hs_' + id).click(function() { 
							
							var latlong = new google.maps.LatLng(lat, lon);
							_gMap.setCenter(latlong);
							return false;
							
							});			
					
			 	});		
			
        });
}

