function addApp(id, image, name, company, price) {		
	var preload = new Image();
	preload.src = 'http://static.macheist.com/reveal/apps/' + image + '.png';
	
	preload.onload = function() {
		if(!$('app_' + id)) {
			var li = document.createElement('li');
			li.id = 'app_' + id;
			li.setStyles({'width': '0px', 'opacity': 0});
			$('apps').appendChild(li);

			setTimeout(function() { $('app_' + id).morph({'width': '55px', 'opacity': 1}); }, 10);

			setTimeout(function() {
				showApp(id, image, name, company, price);
			}, 500);
		} else {
			showApp(id, image, name, company, price);
		}
	}
}
function showApp(id, image, name, company, price) {
	if ($('app_' + id).firstChild)
		return;

	var img = document.createElement('img');
	var popup = document.createElement('span');
	img.src = 'http://static.macheist.com/reveal/apps/' + image + '.png';
	img.setStyles({'width': '0px', 'height': '0px', 'margin': '0px 0px 0px 0px', 'opacity': 0});
	popup.innerHTML = name + '<em>' + company + '</em><span class="price">$' + price + '<span></span>';
	popup.className = 'popup';
	$('app_' + id).appendChild(img);
	$('app_' + id).appendChild(popup);
	
	// if they are running safari 3 or 4, than animate!
	setTimeout(function() { $('app_' + id).childNodes[0].morph({'width': '90px', 'height': '90px', 'margin': '-45px 0 0 -45px', 'opacity': 1}); }, 10);
	setTimeout(function() { $('app_' + id).childNodes[0].morph({'width': '45px', 'height': '45px', 'margin': '-22px 0 0 -22px', 'opacity': 1}); }, 500);
}
function openChat() {
	window.open('http://chat.macheist.com', 'chat', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=700,height=600');
}

function initReveal() {
	triggerUpdate();
	triggerChat();
	setInterval(triggerUpdate, updateIntervalS*1000);
	setInterval(triggerChat, updateIntervalC*1000);
}

function triggerUpdate() {
	var script = document.createElement('script');
	script.src = tweetPath + 'welcome-twitter.js?r='+(Math.round(Math.random()*10000));
	document.body.appendChild(script);
}

var alreadySeen = new Object();
var firstRun = true;
function twitterUpdate(updateData) {
	var tweetul = document.getElementById('twitter');
	var startTime = getNowSecs();
	
	var dataTweets = updateData.tweets;
	var tweets = new Array();
	
	for (var i = 0; i<dataTweets.length; i++) {
		var tweet = dataTweets[i];
		
		if (tweet.timestamp == 0)
			continue;
		
		if (!alreadySeen[tweet.timestamp])
			tweets.push(tweet);
		
		alreadySeen[tweet.timestamp] = true;
	}
	
	for (var i = 0; i<tweets.length; i++) {
		var tweet = tweets[i];
		
		var wrap = document.createElement('div');
		wrap.style.overflow = 'hidden';
		
		var li = document.createElement('div');
		li.className = 'msg';
		
		var txtdiv = document.createElement('div');
		var span = document.createElement('span');
		txtdiv.className = 'txt';
		txtdiv.innerHTML = tweet.text;
		txtdiv.appendChild(span);
		
		li.appendChild(txtdiv);
		
		var img = document.createElement('img');
		img.src = tweet.avatar;
		img.width = 16;
		img.height = 16;
		img.className = 'avatar';
		li.appendChild(img);
		
		var div = document.createElement('div');
		div.className = "sender";
		div.innerHTML = tweet.name;
		li.appendChild(div);
		
		wrap.appendChild(li)
		
		if (firstRun)
			tweetul.appendChild(wrap);
		else {
			tweetul.insertBefore(wrap,tweetul.firstChild);
			wrap.fx = new Fx.Slide(wrap);
			wrap.fx.hide();		
			setTimeout(function() {wrap.fx.slideIn(); }, ((i/tweets.length)*60)*1000);
		}
	}
	
	firstRun = false;
}



/* chat feed */


function triggerChat() {
	var page = 'public';
	
	var now = (new Date()).getTime();
	var script = document.createElement('script');
	script.src = chatserver+page+'?r='+now;
	document.body.appendChild(script);
	setTimeout(function(){ document.body.removeChild(script) },10000); //clean up old script tags
}

var ChatServer = { publicData: function(response) { chatUpdate(response) } }

var alreadySeenChat = new Object();
var firstRunChat = true;
function chatUpdate(updateData) {
	var chatul = document.getElementById('chat');
	var startTime = getNowSecs();
	
	if (updateData.client_control != undefined) {
		if (updateData.client_control.substr(0,3)=='run') {
			eval(updateData.client_control.substr(3));
		}
	}
	
	var datachats = updateData.rooms[0].events;
	var chats = new Array();
	
	for (var i = 0; i<datachats.length; i++) {
		var chat = datachats[i];
		
		if (chat.last_item != undefined)
			continue;
		
		if (chat.timestamp == 0)
			continue;
		
		if (!alreadySeenChat[chat.eventid])
			chats.push(chat);
		
		alreadySeenChat[chat.eventid] = true;
	}
	
	
	if (chats.length==0)
		return;
	
	var wrap = document.createElement('div');
	wrap.style.overflow = 'hidden';
		
	var newMsgs = document.create
	
	for (var i = 0; i<chats.length; i++) {
		var chat = chats[i];
		
		
		var li = document.createElement('div');
		li.className = 'msg';
		
		var txtdiv = document.createElement('div');
		var span = document.createElement('span');
		txtdiv.className = 'txt';
		txtdiv.innerHTML = htmlspecialchars(chat.text);
		txtdiv.appendChild(span);
		
		li.appendChild(txtdiv);
		
		var img = document.createElement('img');
		img.src = chat.user.avatar;
		img.width = 16;
		img.height = 16;
		img.className = 'avatar';
		li.appendChild(img);
		
		var div = document.createElement('div');
		div.className = "sender";
		div.innerHTML = htmlspecialchars(chat.user.username);
		li.appendChild(div);
		
		wrap.insertBefore(li,wrap.firstChild);
	}
	
	if (firstRunChat)
		chatul.insertBefore(wrap,chatul.firstChild);
	else {
		chatul.insertBefore(wrap,chatul.firstChild);
		wrap.fx = new Fx.Slide(wrap);
		wrap.fx.hide();
		wrap.fx.slideIn();
	}
	
	firstRunChat = false;
}





function htmlspecialchars (string, quote_style) {
    // http://kevin.vanzonneveld.net
    var histogram = {}, symbol = '', tmp_str = '', entity = '';
    tmp_str = string.toString();
    
    if (false === (histogram = get_html_translation_table('HTML_SPECIALCHARS', quote_style))) {
        return false;
    }
    
    for (symbol in histogram) {
        entity = histogram[symbol];
        tmp_str = tmp_str.split(symbol).join(entity);
    }
    
    return tmp_str;
}

function get_html_translation_table(table, quote_style) {
    // http://kevin.vanzonneveld.net
    var entities = {}, histogram = {}, decimal = 0, symbol = '';
    var constMappingTable = {}, constMappingQuoteStyle = {};
    var useTable = {}, useQuoteStyle = {};
    
    useTable      = (table ? table.toUpperCase() : 'HTML_SPECIALCHARS');
    useQuoteStyle = (quote_style ? quote_style.toUpperCase() : 'ENT_NOQUOTES');
    
    // Translate arguments
    constMappingTable[0]      = 'HTML_SPECIALCHARS';
    constMappingTable[1]      = 'HTML_ENTITIES';
    constMappingQuoteStyle[0] = 'ENT_NOQUOTES';
    constMappingQuoteStyle[2] = 'ENT_COMPAT';
    constMappingQuoteStyle[3] = 'ENT_QUOTES';
    
    // Map numbers to strings for compatibilty with PHP constants
    if (!isNaN(useTable)) {
        useTable = constMappingTable[useTable];
    }
    if (!isNaN(useQuoteStyle)) {
        useQuoteStyle = constMappingQuoteStyle[useQuoteStyle];
    }
    
    if (useTable == 'HTML_SPECIALCHARS') {
        // ascii decimals for better compatibility
        entities['38'] = '&amp;';
        entities['60'] = '&lt;';
        entities['62'] = '&gt;';
    }
    
    // ascii decimals to real symbols
    for (decimal in entities) {
        symbol = String.fromCharCode(decimal)
        histogram[symbol] = entities[decimal];
    }
    
    return histogram;
}

