summaryrefslogtreecommitdiff
path: root/docroot/classes/application.js
blob: d8776d827dfafb97a77754dbd37bb6e805cb0400 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
 * Material Experience - Main Application Code
 * 
 * EYEMG - Interactive Media Group
 * Created by Mike Crute (mcrute@eyemg.com)
 * Updated by Mike Crute (mcrute@eyemg.com) on 9/26/07
 * 
 * Core application code that is responsible for starting up the application
 * and initializing the core objects.
 */

/*
 * Register global actions for AJAX responders.
 */
Ajax.Responders.register(
{
	/*
	 * When an AJAX connection is created show the bezel that says 
	 * "loading data".
	 */
	onCreate: function() 
	{
		if (!SME.AJAXBezel) 
		{
			SME.AJAXBezel = new Bezel({ displayTime: 0, destroy: false }).show(Strings.loadingAnim); 
		} 
		else 
		{
			SME.AJAXBezel.show(Strings.loadingAnim);
		}
	},
	
	/*
	 * Each time a requester completes we check to see if it was the last 
	 * one, if it is then we take down the loading bezel.
	 */
	onComplete: function() 
	{
		if (Ajax.activeRequestCount == 0) 
		{
			SME.AJAXBezel.hide();
		}
	},
	
	/*
	 * When something goes wrong with loading data we die. 
	 */
	onException: function(transport, exception) 
	{
		if (SME.debug) 
		{
			console.error(exception);
		}
		
		SME.AJAXBezel.hide();
		new Bezel({ displayTime: 0 }).show(Strings.ajaxError);
	}
});

/*
 * Load the data for the card tables.
 */
function loadTables() 
{
	new Ajax.Request(SME.url.tableList, 
	{
		method: "get", 
		
		onSuccess: function(transport) 
		{
			transport.responseText.evalJSON().each(function(data) 
			{
				var windowDims = window.getDimensions();
				
				var table = new CardTable(
				{
					color:    data.color, 
					name:     data.name, 
					id:       data.tid,
					decorate: data.decorate
				});

				// Push the table onto the global table cache (see the SME namespace
				// for more information about the global table cache)
				SME.tables.push(table);

				// Subtracting the max chip width and height ensures that cards don't
				// fall too far off the tables
				table.loadChipData(SME.url.cardTables, 
				{
					table: data.tid, 
					w:     windowDims.width  - (SME.sizes.chipMax.width  / 2), 
					h:     windowDims.height - (SME.sizes.chipMax.height / 2)
				}, false);
			});
			
			// Initialize the sketchbook
			SME.sketchbook = new Sketchbook();
			
			// By default show the home table. When the history manager loads for the
			// first time (after this step) it will load the right table from the URL
			// if applicable. This just ensures that a table is always displayed.
			CardTable.showTable("home");
			
			// Show the tool box in the upper right
			showToolBox();
		}
	});
	
	// Start up the history manager
	SME.history = new HistoryManager().pollEvents();
}

/*
 * Show an intro card. This function will gracefully pass if there are no 
 * intro cards to be shown.
 */
function showIntro() 
{
	new Ajax.Request(SME.url.introCards, 
	{
		method: "get", 

		onSuccess: function(transport) 
		{
			var data = transport.responseText.cleanJSON();

			// If there is no intro card then just pass
			if (data == "" || data == "\n") 
			{
				return loadTables();
			} 
			else 
			{
				data = data.evalJSON();
				
				// By default just show the first card in the
				// feed
				var myCard = data[0];
				
				// If more than one card then pick one at random
				// to display (per client requirements).
				if (data.length > 1)
				{
					myCard = data[Math.floor(1 + (data.length - 1) * Math.random())];
				}
			}

			var card = new Card(
			{
				color: SME.colors.grey,
				title: '', 
				addExtraButtons: false,
				contID: myCard,

				onFadeComplete: function() 
				{
					loadTables();
				}
			}).show();
		}
	});
}

/*
 * Show the toolbox in the upper right side of the screen.
 */
function showToolBox() 
{
	new Effect.Appear($$("div#header div.history")[0]);
	
	// On mouseover of the history link show the dropdown
	$$("div.history a.history")[0].observe("mouseover", function() 
	{
		SME.history.getDropDown();
	});
	
	// Show the login screen when the login link is clicked
	$("loginLink").observe("click", Sketchbook.showLoginScreen);
	
	// Check the login when they first hit the page, saves people logging 
	// in again
	Sketchbook.checkLogin();
}

/*
 * Main program function, this starts up the interface and does various
 * little fixups of interface elements.
 */
function main() 
{
	if (SME.debug) 
	{
		new Bezel({ displayTime: 5 }).show("Full Debug Mode is Enabled");
	}
	
	// Check the resolution at load and when the screen size changes
	window.checkResolution();
	Event.observe(window, "resize", window.checkResolution);
	
	// Show the intro card or load the tables
	if (SME.skipIntro || window.location.hash.length > 1) 
	{
		loadTables();
	} 
	else 
	{
		showIntro();
	}
	
	// Per Bryan this should be a single year if 2007 otherwise it should 
	// be a date range starting on the year that the site was released.
	if (new Date().getFullYear() > 2007) 
	{
		$("copyright").innerHTML = $("copyright").innerHTML.replace(/####/, "2007-" + 
						new Date().getFullYear());
	} 
	else 
	{
		$("copyright").innerHTML = $("copyright").innerHTML.replace(/####/, "2007");
	}
}

/*
 * Start the program up when the window loads.
 */
Event.observe(window, "load",   main);