// joshua (jquery operating system)
// http://binaerpilot.no/alexander/
// astoever@binaerpilot.no
var title = '<strong>Joshua</strong> <span id="version"/> <span class=\"dark\">Tron Edition</span>',
hist = [], // history (arrow up/down)
position = '', // position in history
expires = 1095, // cookie dies in 3 years
fade = 500, // ui fade delay
windows = ['config', 'music', 'alexander'],
custom = ['gallery', 'superplastic', 'desktop'],
allWindows = $.merge(windows, custom);

// booting up joshua
function joshuaInit(){
	$('#joshua').html('<h1><div id="tip" class="dark"/>'+title+'</h1><div id="output"/>').append('<div id="input"></div>');
	$('#input').html('<input type="text" id="cmd" autocomplete="off"/>');
	var motd = $('<div class="output"/>').load('joshua.php', {command: "motd"}, function(){
		motd.appendTo('#output');
		stealFocus(); scrollCheck();
	});
	$('#tip').load('joshua.php', {command: "tip", option: "clean"});
	// upgrading users to latest version
	$('#version').load('joshua.php', {command: "version", option: "clean"}, function(){
		var version = $('#version').html(),
		versionCheck = readCookie('release');
		if(versionCheck < 5.7) {
			eraseCookie('background');
			createCookie('theme', 'tron', expires);
			createCookie('desktop', 'true', expires);
			createCookie('fx', 'sparks', expires);
			createCookie('alexander', 'true', expires);
			createCookie('release', version, expires);
			$.each(allWindows,function(){eraseCookie('window.'+this);});
			location.reload();
		}
	});
	// settings
	var theme = readCookie('theme'),
	background = readCookie('background'),
	fx = readCookie('fx'),
	opacity = readCookie('opacity');
	if(theme) loadTheme(theme, true);
	if(background) $('#joshua').addClass(background);
	if(fx) fxInit(fx, true);	
	if(opacity) $('#joshua, .window').css({opacity:opacity});
	// window positions
	$.each(allWindows,function(){
		var cookie = readCookie('window.'+this);
		if(cookie) {
			var pos = cookie.split(',');
			$('#'+this).css({
				position: 'absolute',
				left: pos[0],
				right: pos[1],
				top: pos[2]
			});
		}
	});
	chromeInit();
	customMagic();
}

// let's go
$(document).ready(function(){
	joshuaInit();
	$('#cmd').keypress(function(e){
		// command issued with enter
		if(e.which == 13) {
			var dump = $(this).val(); // grab the input
			var input = dump.split(' '); // split the input
			var command = input[0];
			var option = input[1];
			// store history
			if(command) {
				hist.push(command);
				hist = hist.unique();
				position = hist.length;
			}
			// js commands
			if(command == "clear") clearScreen();
			// externals
			else if(command == "exit" || input == "quit" || input == "logout") window.location = "http://binaerpilot.no";
			else if(command == "wtfig") window.location = "http://binaerpilot.no/alexander/wtfig/";
			else if(command == "reviews") window.location = "http://binaerpilot.no/alexander/reviews/";
			else if(command == "code") window.location = "http://binaerpilot.no/alexander/code/";
			else if(command == "jge") window.location = "http://jgemainframe.com/";
			// rachael
			else if(command == "rachael"){
				var birthday = new Date(2010, 6-1, 29);
				var married = new Date(2009, 10-1, 7);
				$('#output').append('<div class="output"><div class="prompt">rachael</div><p>Rachael is the most beautiful girl in the world. It\'s a scientific fact. Yes, I am a scientist. We\'ve been happily married for <span class="countdown married pink"/>, her birthday is in <span class="countdown birthday pink"/> and I am still madly in love. You can <a href="http://rachaelivy.com">visit her homepage</a> if you\'d like to know more. (Potential stalkers be warned: I carry an axe.)</p></div>');
				$('.birthday').countdown({until: birthday, compact: true, format: 'OWDHMS'});
				$('.married').countdown({since: married, compact: true, format: 'OWDHMS'});
				scrollCheck();
			}
			// quit smoking
			else if(command == "smoking"){
				var quit = new Date(2010, 1-1, 7);
				$('#output').append('<div class="output"><div class="prompt">smoking</div><p>After having this nasty habit for 12 years, I\'ve been smoke free for <span class="countdown smoking light"/>. Huzzah!</p></div>');
				$('.smoking').countdown({since: quit, compact: true, format: 'OWDHMS'});
				scrollCheck();
			}
			// twitter
			else if(command == "twitter"){
				if (!$('#twitter').length) $('#output').append('<div class="output"><div class="prompt">twitter</div><div id="twitter"><p class="joshua"><strong>Joshua:</strong> Establishing connection...</p></div></div>');
				$.getJSON("http://twitter.com/statuses/user_timeline/binaerpilot.json?count=1&callback=?", function(data){
					$.each(data, function(i, item){
						$('#twitter').html('<p>'+item.text.linkify()+' <span class="dark">'+relTime(item.created_at)+'</span><br/><a class="external" href="http://twitter.com/binaerpilot">Follow me @binaerpilot.</a></p>');
					});
					scrollCheck();
				});
			}
			// windows
			else if(command == "music" || command == "config" || command == "alexander"|| command == "gallery"){
				createCookie(command,'true',expires);
				$('#'+command+':hidden').fadeIn(fade);
				if(command == "gallery") loadGallery();
			}
			else if(command == "conf" || command == "theme"){
				createCookie('config','true',expires);
				$('#config:hidden').fadeIn(fade);
			}
			else if(command == "bleep"){
				createCookie('music','true',expires);
				$('#music:hidden').fadeIn(fade);
			}
			// superplastic
			else if(command == "superplastic") loadSuperplastic();
			// desktop
			else if(command == "desktop") {
				var cookie = readCookie('desktop');
				if(cookie) {
					$('#desktop').fadeOut(fade);
					eraseCookie('desktop');
				}
				else {
					$('#desktop:hidden').fadeIn(fade);
					createCookie('desktop', 'true', expires);
				}
			}
			// mute
			else if(command == "mute") mute();
			// reset
			else if(command == "reset") loadPreset('reset');
			// engine
			else {
				if(command != "") {
					var content = $('<div class="output"/>').load('joshua.php', {command: command, option: option, dump: dump}, function(){
						$('#output').append(content);
						reBoot();
					});
				}
			}
			// clear input data
			$("#cmd").val('');
		}
	});
	// access history
	$('#cmd').keydown(function(e){
		if(e.which == 38) {
			if(position > 0) position = position-1;
			$('#cmd').val(hist[position]);
		}
		else if (e.which == 40) {
			if(position < hist.length) position = position+1;
			$('#cmd').val(hist[position]);
			if(position == hist.length) {
				$("#cmd").val('');
			}
		}
	});
});
