lineInterval = null;
pixels = [];
alternation = 26;
rope = 0;
app = 'app_main';
previousApp = app;
weatherLocation = 'NYC';
functions = {};

functions[0] = function() {
	$('main_app').show();
};

functions[1] = function() {
	$('weather_app').show();
};

window.onresize = function() {
	$('weather_input').textHolder('Location', '#777', '#000');
	$('weather_input').center();
	$('weather_form').addEventListener('submit', getWeather, 1);
	$('logo').ghost();
	removePixels();
	for (var i = 0; i <= $$.sizeX(); i++) {
		pixel = document.createElement('div');
		pixel.setAttribute('class', 'pixel');
		pixel.setStyle({position:'absolute',zIndex:10,left:Math.round(i)+'px',top:'49px',width:'2px',height:'2px',background:'#aaa'});
		pixels[i] = {'i':i,'node':pixel};
	};
	pixels.each(function(pixel) {
		$('bottom').appendChild(pixel['node']);
	});
	$$.selector('#apptable tr').each(function(app, index) {
		app.addEventListener('mousedown', function(event) {
			$$.selector('#apptable tr td').each(function(row) {
				row.style.background = '';
			});
			$$.selector('.app').each(function(eachApp) {
				eachApp.hide();
			});
			if (functions[index]) {
				functions[index]();
			};
			this.firstElementChild.setStyle({background:'url(\'ag.png\')'});
		}, 1);
	});
	calculateSine();
	$('weather_google').ghost();
};

window.onload = function() {
	window.onresize();
};

function changeRope() {
	if (rope) {
		$('rope').update('turn it into a rope');
		pixels.each(function(pixel) {
			pixel['node'].style.background = '#aaa';
		});
	} else {
		$('rope').update('turn it back into a sine wave');
		pixels.each(function(pixel) {
			pixel['node'].style.background = '#9f3a15';
		});
	};
	rope = !rope;
	calculateSine();
};

function removePixels() {
	pixels.each(function(pixel) {
		pixel['node'].setStyle({background:'none'});
		$('bottom').removeChild(pixel['node']);
	});
	pixels = [];
};

function getWeather(event) {
	try {
		$('weather_form').removeEventListener('submit', getWeather, 1);
		weatherLocation = $v('weather_input');
		var weather = null;
		$$.xhr({
			url:'weather.php?l='+escape(weatherLocation),
			method:'get',
			onSuccess:function(text) {
				eval('weather = '+text);
				if (weather['condition'] == '') {
					$('weather_input').update('Unknown location');
					$('weather_form').addEventListener('submit', getWeather, 1);
					return 0;
				};
				$('weather_input').fade({onFinish:function() {
					$('weather_input').style.display = 'none';
					var right = document.createElement('div');
					right.update('<span id="weather_location" style="opacity:0.0;"><span class="b">Location</span><br />&nbsp;&nbsp;&nbsp;&nbsp;'+weatherLocation+'<br /><br /></span><span id="temp" style="opacity:0.0;"><span class="b">Temperature</span><br />&nbsp;&nbsp;&nbsp;&nbsp;'+weather['temp_c']+'&deg;C<br /><br /></span><span id="humidity" style="opacity:0.0;"><span class="b">Humidity</span><br />&nbsp;&nbsp;&nbsp;&nbsp;'+weather['humidity']+'<br /><br /></span><span id="wind_direction" style="opacity:0.0;"><span class="b">Wind Direction</span><br />&nbsp;&nbsp;&nbsp;&nbsp;'+expandDirection(weather['wind'][0])+'<br /><br /></span><span id="wind_speed" style="opacity:0.0;"><span class="b">Wind Speed</span><br />&nbsp;&nbsp;&nbsp;&nbsp;'+(Math.round(100*(weather['wind'][1]*1.609344))/100)+' km/h<br /><br /></span>');
					right.setStyle({position:'absolute',left:'-1000000px',background:'rgba(0, 0, 0, 0.4)',top:'0px',bottom:'2px',paddingLeft:'5px',borderRight:'2px solid #777',paddingRight:'10px'});
					right.setStyle({left:(parseInt(right.getStyle('width'))*-1)+'px'});
					$('weather_app').appendChild(right);
					$$.st({
						duration:0.1,
						from:(parseInt(right.getStyle('width'))*-1),
						to:0,
						code:function(pos) {
							//~ alert(pos);
							right.style.left = pos+'px';
						},
						onFinish:function() {
							$('weather_location').appear({duration:0.5,onFinish:function() {
								$('temp').appear({duration:0.5,onFinish:function() {
									$('humidity').appear({duration:0.5,onFinish:function() {
										$('wind_direction').appear({duration:0.5,onFinish:function() {
											$('wind_speed').appear({duration:0.5});
										}});
									}});
								}});
							}});
						}
					});
					var img = document.createElement('img');
					img.setStyle({marginTop:'10px',opacity:0});
					img.src = 'weather/'+weather['condition']+'.png';
					$('weather_form').appendChild(img);
					img.center();
					img.ghost();
					img.appear();
				}});
			}
		});
	} catch(e) {
		alert(e);
	};
	event.preventDefault();
};

function expandDirection(dir) {
	dir = dir.toUpperCase();
	if (dir == 'N') {
		return 'North';
	} else if (dir == 'E') {
		return 'East';
	} else if (dir == 'S') {
		return 'South';;
	} else if (dir == 'W') {
		return 'West';
	} else if (dir == 'NE') {
		return 'North-East';
	} else if (dir == 'SE') {
		return 'South-East';
	} else if (dir == 'NW') {
		return 'North-West';
	} else if (dir == 'SW') {
		return 'South-West';
	} else {
		return 'FAIL!';
	};
};

function calculateSine() {
	pixels.each(function(pixel) {
		pixel['i']++;
		var i = pixel['i']%(alternation);
		i -= alternation/2;
		if (rope) {
			pixel['node'].style.top = 50+Math.round(20*Math.sin(i))+'px';
		} else {
			pixel['node'].style.top = 50+Math.round(15*Math.sin(i/4.3))+'px';
		};
	});
};

/*

AjaxSolr.Manager = AjaxSolr.AbstractManager.extend({
	executeRequest:function(servlet) {
		var self = this;
		if (this.proxyUrl) {
			$$.xhr({
				url:this.proxyUrl,
				data:'query=':escape(this.store.string()),
				onFinish:function(data) {
					self.handleResponse(JSON.Parse(data));
				}
			});
	    } else {
			$$.xhr({
				url:this.solrUrl+servlet+'?'+this.store.string()+'&wt=json&json.wrf=?',
				onFinish:function(data) {
					self.handleResponse(data);
				}
			});
		};
	}
});

*/