var _basePath = "/";
var _routeTemplates = {
  "check": {
    "username": "/check/username",
    "email": "/check/email",
    "coupon": "/check/coupon"
  },
  "company": {
    "show": "/company"
  },
  "home": {
    "show": "/"
  },
  "invite": {
    "show": "/invite/{token}",
    "accept": "/invite/{token}/accept",
    "signup": "/invite/{token}/signup",
    "reject": "/invite/{token}/reject"
  },
  "pricing": {
    "show": "/pricing"
  },
  "signup": {
    "show": "/signup",
    "check": "/signup/check/{what}",
    "confirm": "/signup/confirm",
    "free": "/signup/free",
    "receive": "/signup/receive",
    "complete": "/signup/complete",
    "declined": "/signup/declined",
    "error": "/signup/error"
  },
  "tour": {
    "show": "/tour",
    "visualize": "/tour/visualize",
    "organize": "/tour/organize",
    "communicate": "/tour/communicate",
    "improve": "/tour/improve"
  },
  "account": {
    "new": "/accounts/new",
    "create": "/accounts/create",
    "createfree": "/accounts/create/new",
    "finalizecreate": "/accounts/create/finalize",
    "receive": "/accounts/create/receive",
    "edit": "/account/{accountid}/edit",
    "update": "/account/{accountid}",
    "confirmdelete": "/account/{accountid}/delete",
    "delete": "/account/{accountid}/delete",
    "invoice": "/account/{accountid}/invoice"
  },
  "attachment": {
    "new": "/project/{projectid}/story/{storyref}/attachment/new",
    "download": "/project/{projectid}/story/{storyref}/attachment/{attachmentid}/download/{saveasfilename}",
    "thumbnail": "/project/{projectid}/story/{storyref}/attachment/{attachmentid}/thumbnail",
    "edit": "/project/{projectid}/story/{storyref}/attachment/{attachmentid}/edit",
    "update": "/project/{projectid}/story/{storyref}/attachment/{attachmentid}",
    "list": "/project/{projectid}/story/{storyref}/attachments",
    "create": "/project/{projectid}/story/{storyref}/attachments",
    "confirmdelete": "/project/{projectid}/story/{storyref}/attachment/{attachmentid}/delete",
    "delete": "/project/{projectid}/story/{storyref}/attachment/{attachmentid}/delete"
  },
  "billing": {
    "show": "/account/{accountid}/billing",
    "address": "/account/{accountid}/billing/address",
    "updateaddress": "/account/{accountid}/billing/address",
    "period": "/account/{accountid}/billing/period",
    "updateperiod": "/account/{accountid}/billing/period",
    "payment": "/account/{accountid}/billing/payment",
    "receive": "/account/{accountid}/billing/receive"
  },
  "plan": {
    "show": "/account/{accountid}/plan",
    "change": "/account/{accountid}/plan/change",
    "downgrade": "/account/{accountid}/plan/downgrade",
    "upgrade": "/account/{accountid}/plan/upgrade",
    "receive": "/account/{accountid}/plan/change/receive",
    "complete": "/account/{accountid}/complete",
    "declined": "/account/{accountid}/declined",
    "error": "/account/{accountid}/error"
  },
  "board": {
    "show": "/project/{projectid}/board",
    "sidebars": "/project/{projectid}/board/sidebars"
  },
  "dashboard": {
    "show": "/dashboard"
  },
  "develop": {
    "show": "/develop"
  },
  "error": {
    "show": "/error/{code}"
  },
  "filter": {
    "get": "/project/{projectid}/filter/{filterid}",
    "list": "/project/{projectid}/filters",
    "new": "/project/{projectid}/filters/new",
    "rename": "/project/{projectid}/filter/{filterid}/rename",
    "create": "/project/{projectid}/filters",
    "update": "/project/{projectid}/filter/{filterid}",
    "delete": "/project/{projectid}/filter/{filterid}/delete"
  },
  "help": {
    "markdown": "/help/markdown"
  },
  "interstitial": {
    "dialog": "/interstitial/dialog/{which}",
    "address": "/interstitial/address",
    "updateaddress": "/interstitial/address"
  },
  "login": {
    "show": "/login",
    "login": "/login",
    "logout": "/logout",
    "multipass": "/multipass"
  },
  "password": {
    "show": "/password",
    "update": "/password",
    "forgot": "/password/forgot",
    "reset": "/password/reset",
    "confirmreset": "/password/reset",
    "showuser": "/user/{userid}/password",
    "updateuser": "/user/{userid}/password"
  },
  "people": {
    "show": "/project/{projectid}/people",
    "new": "/project/{projectid}/people/invite",
    "add": "/project/{projectid}/people/invite",
    "remove": "/project/{projectid}/people/remove",
    "revokeinvite": "/project/{projectid}/people/invite/{inviteid}/delete"
  },
  "performance": {
    "show": "/project/{projectid}/performance",
    "metrics": "/project/{projectid}/performance/metrics",
    "cumulativeflow": "/project/{projectid}/performance/cumulativeflow",
    "leadtime": "/project/{projectid}/performance/leadtime",
    "cycletime": "/project/{projectid}/performance/cycletime",
    "worktime": "/project/{projectid}/performance/worktime",
    "waittime": "/project/{projectid}/performance/waittime",
    "blockedtime": "/project/{projectid}/performance/blockedtime",
    "efficiency": "/project/{projectid}/performance/efficiency",
    "export": "/project/{projectid}/performance/export"
  },
  "phase": {
    "new": "/project/{projectid}/phase/new",
    "create": "/project/{projectid}/phases",
    "edit": "/project/{projectid}/phase/{phaseid}",
    "update": "/project/{projectid}/phase/{phaseid}",
    "confirmdelete": "/project/{projectid}/phase/{phaseid}/delete",
    "delete": "/project/{projectid}/phase/{phaseid}/delete",
    "move": "/project/{projectid}/phase/{phaseid}/move"
  },
  "process": {
    "show": "/project/{projectid}/process"
  },
  "project": {
    "new": "/projects/new",
    "create": "/projects",
    "show": "/project/{projectid}",
    "list": "/projects",
    "edit": "/project/{projectid}/edit",
    "confirmdelete": "/project/{projectid}/delete",
    "delete": "/project/{projectid}/delete",
    "confirmarchive": "/project/{projectid}/archive",
    "archive": "/project/{projectid}/archive",
    "confirmunarchive": "/project/{projectid}/unarchive",
    "unarchive": "/project/{projectid}/unarchive",
    "getproperty": "/project/{projectid}/properties/{property}",
    "setproperty": "/project/{projectid}/properties/{property}"
  },
  "role": {
    "new": "/project/{projectid}/roles/new",
    "create": "/project/{projectid}/roles",
    "delete": "/project/{projectid}/role/{roleid}/delete",
    "adduser": "/project/{projectid}/role/{roleid}/users",
    "removeuser": "/project/{projectid}/role/{roleid}/user/{userid}/delete",
    "moveuser": "/project/{projectid}/role/{roleid}/moveuser",
    "editpermissions": "/project/{projectid}/role/{roleid}/permissions",
    "updatepermissions": "/project/{projectid}/role/{roleid}/permissions",
    "setproperty": "/project/{projectid}/role/{roleid}/properties/{property}"
  },
  "settings": {
    "show": "/settings",
    "update": "/settings",
    "newchannel": "/settings/newchannel",
    "newapikey": "/settings/newapikey",
    "hints": "/settings/hints",
    "announcement": "/settings/announcement",
    "password": "/settings/password",
    "check": "/settings/check"
  },
  "story": {
    "list": "/project/{projectid}/stories",
    "create": "/project/{projectid}/stories",
    "details": "/project/{projectid}/stories/details",
    "viewdetails": "/project/{projectid}/story/{storyref}/viewdetails",
    "tasks": "/project/{projectid}/stories/tasks",
    "comments": "/project/{projectid}/stories/comments",
    "show": "/project/{projectid}/story/{storyref}",
    "card": "/project/{projectid}/story/{storyref}/card",
    "setready": "/project/{projectid}/story/{storyref}/setready",
    "setblocked": "/project/{projectid}/story/{storyref}/setblocked",
    "delete": "/project/{projectid}/story/{storyref}/delete",
    "move": "/project/{projectid}/story/{storyref}/move",
    "reassign": "/project/{projectid}/story/{storyref}/reassign",
    "getproperty": "/project/{projectid}/story/{storyref}/properties/{property}",
    "setproperty": "/project/{projectid}/story/{storyref}/properties/{property}",
    "deadline": "/project/{projectid}/story/{storyref}/deadline",
    "addtags": "/project/{projectid}/story/{storyref}/addtags",
    "removetag": "/project/{projectid}/story/{storyref}/removetag"
  },
  "task": {
    "list": "/project/{projectid}/story/{storyref}/tasks",
    "new": "/project/{projectid}/story/{storyref}/tasks/new",
    "create": "/project/{projectid}/story/{storyref}/tasks",
    "edit": "/project/{projectid}/story/{storyref}/tasks/{taskid}",
    "update": "/project/{projectid}/story/{storyref}/tasks/{taskid}",
    "toggle": "/project/{projectid}/story/{storyref}/tasks/{taskid}/toggle",
    "move": "/project/{projectid}/story/{storyref}/tasks/{taskid}/move",
    "delete": "/project/{projectid}/story/{storyref}/tasks/{taskid}/delete"
  },
  "comment": {
    "list": "/project/{projectid}/story/{storyref}/comments",
    "new": "/project/{projectid}/story/{storyref}/comments/new",
    "create": "/project/{projectid}/story/{storyref}/comments",
    "edit": "/project/{projectid}/story/{storyref}/comments/{commentid}",
    "update": "/project/{projectid}/story/{storyref}/comments/{commentid}",
    "delete": "/project/{projectid}/story/{storyref}/comments/{commentid}/delete",
    "text": "/project/{projectid}/story/{storyref}/comments/{commentid}/text"
  },
  "work": {
    "show": "/project/{projectid}/work"
  },
  "worldview": {
    "show": "/_admin"
  },
  "manageaccounts": {
    "show": "/_admin/accounts",
    "search": "/_admin/accounts/search",
    "list": "/_admin/accounts/list",
    "change": "/_admin/accounts/change",
    "impersonate": "/_admin/accounts/impersonate",
    "changeaccountowner": "/_admin/accounts/{accountid}/changeowner",
    "commitchangeaccountowner": "/_admin/accounts/{accountid}/changeowner",
    "changeprojectowner": "/_admin/accounts/{accountid}/projects/${projectid}/changeowner",
    "commitprojectchangeowner": "/_admin/accounts/{accountid}/projects/${projectid}/changeowner"
  },
  "manageannouncement": {
    "show": "/_admin/announcement",
    "post": "/_admin/announcement",
    "clear": "/_admin/announcement/clear"
  },
  "corsapi": {
    "preflight": "/api/v1/{*path}"
  },
  "projectapi": {
    "list": "/api/v1/projects",
    "get": "/api/v1/projects/{projectid}",
    "update": "/api/v1/projects/{projectid}"
  },
  "projectmembershipapi": {
    "list": "/api/v1/projects/{projectid}/members",
    "update": "/api/v1/projects/{projectid}/members",
    "add": "/api/v1/projects/{projectid}/members",
    "remove": "/api/v1/projects/{projectid}/members/{userid}"
  },
  "inviteapi": {
    "list": "/api/v1/projects/{projectid}/invites",
    "get": "/api/v1/projects/{projectid}/invites/{inviteid}",
    "create": "/api/v1/projects/{projectid}/invites",
    "delete": "/api/v1/projects/{projectid}/invites/{inviteid}"
  },
  "roleapi": {
    "list": "/api/v1/projects/{projectid}/roles",
    "create": "/api/v1/projects/{projectid}/roles",
    "get": "/api/v1/projects/{projectid}/roles/{roleid}",
    "update": "/api/v1/projects/{projectid}/roles/{roleid}",
    "delete": "/api/v1/projects/{projectid}/roles/{roleid}"
  },
  "rolemembershipapi": {
    "list": "/api/v1/projects/{projectid}/roles/{roleid}/members",
    "update": "/api/v1/projects/{projectid}/roles/{roleid}/members",
    "add": "/api/v1/projects/{projectid}/roles/{roleid}/members",
    "remove": "/api/v1/projects/{projectid}/roles/{roleid}/members/{userid}"
  },
  "rolepermissionapi": {
    "list": "/api/v1/projects/{projectid}/roles/{roleid}/permissions",
    "update": "/api/v1/projects/{projectid}/roles/{roleid}/permissions",
    "add": "/api/v1/projects/{projectid}/roles/{roleid}/permissions",
    "remove": "/api/v1/projects/{projectid}/roles/{roleid}/permissions/{permissionid}"
  },
  "storyapi": {
    "list": "/api/v1/projects/{projectid}/stories",
    "create": "/api/v1/projects/{projectid}/stories",
    "get": "/api/v1/projects/{projectid}/stories/{storynumber}",
    "delete": "/api/v1/projects/{projectid}/stories/{storynumber}",
    "update": "/api/v1/projects/{projectid}/stories/{storynumber}",
    "bulkupdate": "/api/v1/projects/{projectid}/stories",
    "bulkdelete": "/api/v1/projects/{projectid}/stories"
  },
  "storytagapi": {
    "list": "/api/v1/projects/{projectid}/stories/{storynumber}/tags",
    "add": "/api/v1/projects/{projectid}/stories/{storynumber}/tags",
    "update": "/api/v1/projects/{projectid}/stories/{storynumber}/tags",
    "remove": "/api/v1/projects/{projectid}/stories/{storynumber}/tags/{tagname}"
  },
  "phaseapi": {
    "list": "/api/v1/projects/{projectid}/phases",
    "create": "/api/v1/projects/{projectid}/phases",
    "reorderphases": "/api/v1/projects/{projectid}/phases",
    "get": "/api/v1/projects/{projectid}/phases/{phaseid}",
    "delete": "/api/v1/projects/{projectid}/phases/{phaseid}",
    "update": "/api/v1/projects/{projectid}/phases/{phaseid}"
  },
  "phasestoriesapi": {
    "list": "/api/v1/projects/{projectid}/phases/{phaseid}/stories",
    "reorder": "/api/v1/projects/{projectid}/phases/{phaseid}/stories"
  },
  "commentapi": {
    "list": "/api/v1/projects/{projectid}/stories/{storynumber}/comments",
    "create": "/api/v1/projects/{projectid}/stories/{storynumber}/comments",
    "get": "/api/v1/projects/{projectid}/stories/{storynumber}/comments/{commentid}",
    "delete": "/api/v1/projects/{projectid}/stories/{storynumber}/comments/{commentid}",
    "update": "/api/v1/projects/{projectid}/stories/{storynumber}/comments/{commentid}"
  },
  "tagapi": {
    "list": "/api/v1/projects/{projectid}/tags",
    "create": "/api/v1/projects/{projectid}/tags",
    "get": "/api/v1/projects/{projectid}/tags/{tagname}",
    "delete": "/api/v1/projects/{projectid}/tags/{tagname}",
    "update": "/api/v1/projects/{projectid}/tags/{tagname}"
  },
  "tagstoryapi": {
    "list": "/api/v1/projects/{projectid}/tags/{tagname}/stories",
    "add": "/api/v1/projects/{projectid}/tags/{tagname}/stories",
    "update": "/api/v1/projects/{projectid}/tags/{tagname}/stories",
    "remove": "/api/v1/projects/{projectid}/tags/{tagname}/stories/{storynumber}"
  },
  "taskapi": {
    "list": "/api/v1/projects/{projectid}/stories/{storynumber}/tasks",
    "create": "/api/v1/projects/{projectid}/stories/{storynumber}/tasks",
    "reorder": "/api/v1/projects/{projectid}/stories/{storynumber}/tasks",
    "get": "/api/v1/projects/{projectid}/stories/{storynumber}/tasks/{taskid}",
    "delete": "/api/v1/projects/{projectid}/stories/{storynumber}/tasks/{taskid}",
    "update": "/api/v1/projects/{projectid}/stories/{storynumber}/tasks/{taskid}"
  },
  "attachmentapi": {
    "list": "/api/v1/projects/{projectid}/stories/{storynumber}/attachments",
    "create": "/api/v1/projects/{projectid}/stories/{storynumber}/attachments",
    "get": "/api/v1/projects/{projectid}/stories/{storynumber}/attachments/{attachmentid}",
    "delete": "/api/v1/projects/{projectid}/stories/{storynumber}/attachments/{attachmentid}",
    "update": "/api/v1/projects/{projectid}/stories/{storynumber}/attachments/{attachmentid}"
  },
  "meapi": {
    "get": "/api/v1/me",
    "update": "/api/v1/me",
    "liststories": "/api/v1/me/stories"
  },
  "rpctask": {
    "recurringbilling": "/_rpc/recurring-billing",
    "generatereports": "/_rpc/generate-reports",
    "archiveattachments": "/_rpc/archive-attachments"
  }
};

var urlfor = function(action, controller, values) {
	values = values || {};

	if (typeof(_routeTemplates[controller]) == 'undefined' || typeof(_routeTemplates[controller][action]) == 'undefined') {
		return null;
	}

	var template = _routeTemplates[controller][action];
	var tokens = template.match(/{[^}]+}/g);
	
	if (!tokens) { return template; }
	
	var url = template;
	for (var idx = 0; idx < tokens.length; idx++) {
		var token = tokens[idx];
		var name = token.substring(1, token.length - 1);

		var value = null;

		if (name == 'controller') {	value = controller; }
		else if (name == 'action') { value = action; }
		else if (typeof(values[name]) != 'undefined') { value = values[name]; }

		if (value != null) {
			url = url.replace(token, value);
		}
	}
	
	return url;
};
var contenturl = function(filename) { return _basePath + 'content/' + filename; };
var imageurl = function(filename) { return _basePath + 'content/images/' + filename; };
