// note: various urls in here are reliant on the url hierarchy

hwy9_directoryView = {
	moved: false,
	personID: 0,
	directoryID: 0,
	
	init: function(directoryID, personID){
		this.directoryID = directoryID;
		this.personID = personID;
		
		hwy9_directoryView.bindHandlers();
	},

	bindHandlers: function() {
		$('[name=directoryRating]').rating('disable');
		
		$('#expandMap').click(function(){
			hwy9_directoryView.expandMap();
			return false;
		});
		
		$('#shrinkMap').click(function(){
			hwy9_directoryView.shrinkMap();
			return false;
		});
		
		$('#lnkDelete').click(function(){
			hwy9_directoryView.deleteDirectory();
			return false;
		});

		$('#lnkEdit').click(function(){
			hwy9_directoryView.openEdit();
			return false;
		});
	},
	
	openEdit: function(){
		var url = '../../EditDirectory.aspx?i=' + hwy9_directoryView.directoryID + '&' + hwy9_directoryView.now();
		$('<iframe id="editDirectoryFrame" src="' + url + '" />').dialog({
			title:"Edit Directory Listing",
			autoOpen:true,
			height:500,
			width:600,
			modal:true,
			resizable:false,
			overlay: { 
				opacity: 0.5, 
				background: "black" 
			} 
		});
	},
	
	expandMap: function(){
		$('#listingCol, #mapCol').hide();
		if (!hwy9_directoryView.moved){
			$('#largeMapHidden').appendTo("#largeMap");
			$('#largeMapHidden').css({'top':'0','left':'0','position':'relative'});
			hwy9_directoryView.moved = true;
		}
		$('#largeMap, #shrinkMap').show();
	},

	shrinkMap: function(){
		$('#largeMap, #shrinkMap').hide();
		$('#listingCol, #mapCol').show();
	},
	
	deleteDirectory: function(){
		if (hwy9_directoryView.directoryID <= 0 || hwy9_directoryView.personID <= 0) return; // return silently if directoryID or personID have been tampered with
		if (!confirm("Delete this listing?\n\nNote: your email address and IP address will be recorded when the delete is performed")) return;
		
		$.post('../../../Services/Directory.ashx?DeleteDirectory&v=' + hwy9_directoryView.now(), {directoryID:hwy9_directoryView.directoryID, personID:hwy9_directoryView.personID}, function(data){
			if (data.success){
				alert('Delete is complete.');

				var hrefSplit = self.location.href.split('/');
				self.location.href = self.location.href.replace( ('/' + hrefSplit[hrefSplit.length-1]), '.aspx');
			} else {
				alert('Unable to perform the delete. Please try again. If the issue persists, please contact Customer Support.');
			}
		}, 'json');
	},

	now: function(){
		return (new Date().valueOf().toString());
	}	
}
