/*** copyright 2010 europatweets ***/

var jQT = $.jQTouch({	icon: 'images/favicon.png',
	startupScreen: 'images/splash.jpg',
	statusBar: 'black'});

var db;

var app = {
	shortName: 'Europatweets', 
	version: '1.1 (20/08/2010)',
	displayName: 'Europatweets'
};

$(document).ready(function() {
	debugStart();
	$('#feed ul').html('');
	$('#home ul#feeds li a').bind('tap', openFeed);
	$('#home ul#feeds li a').bind('click', openFeed);
	$().ajaxError(ajaxMessage);
	$('#home form').submit(function(event) {event.preventDefault();});
	$('#feed .reload').click(refreshEntries);
	$('#about #version').html(app.displayName+" version "+app.version);
	$('#home img.clear').bind('click', clearSearch);
	$('#settings form').submit(saveSettings);
	$('#settings').bind('pageAnimationStart', loadSettings);
	showInstallBox();
});
 
function updateCache() {
	window.applicationCache.swapCache();
}

function debugStart() {
	if (localStorage.debug == "on") {
			if (navigator.onLine) {
				window.applicationCache.addEventListener("updateready", updateCache, false);
			}
			localStorage.install_box = false;
	}
}

function showInstallBox() {
	if (localStorage.install_box == undefined || 
		localStorage.install_box == 'false' ||
		localStorage.install_box == false) {
			setTimeout(function() {
					$('#install_box').show().animate(
						{top: '0px'},
						500
					);
				}, 1500);
		$('#install_box').bind('click', function() { $(this).hide()});
		localStorage.install_box = true;
	}
}

function ajaxMessage() {
	alert('Could not load data. Check internet connection');
	jQT.goTo($('#home'));
}

function clearSearch() {
	$('#home #search').val('');
}

function saveSettings() { 
	localStorage.lang = $('#lang').val();
	localStorage.debug = $('#debug').val();
	jQT.goBack(); 
	return false;}

function loadSettings() { 
	$('#lang').val(localStorage.lang);
	if (localStorage.debug == "on") {
		$('#debug').attr('checked', "true");
	}}


function openFeed() {
	$('#feed').focus();
	if (this.id != sessionStorage.feed ||
		sessionStorage.search != $('#search').val() ||
		$('#feed #lang').val() != localStorage.lang
		) {
			sessionStorage.feed = this.id;
			sessionStorage.feed_name = $(this).html();
			sessionStorage.search = $('#search').val();
			refreshEntries();
	}
}

function refreshEntries() {
	var feed_name = sessionStorage.feed_name;
	var feed = sessionStorage.feed;
	var search = sessionStorage.search;
	
	$('#feed h1').text(feed_name);
	$('#feed ul').hide();
	$('#feed ul').html('');

	$.post("feed.php", 
		{tab: feed, q: search},
		function(data) {
			if (data != null) {
				for (var i=0; i < data.length; i++) { 
					var tweet = $('#statusTemplate').clone(); 
					tweet.removeAttr('id'); 
					tweet.removeAttr('style');
					tweet.appendTo('#feed ul'); 
					tweet.find('.screen_name').text('@'+data[i].screen_name); 
					tweet.find('.status').text(data[i].status);
					tweet.find('.avatar').attr('src', data[i].avatar);				}
				translate_tweets(localStorage.lang);
				$('#feed #lang').val(localStorage.lang);
				$('#feed ul').show();
			} else {
				alert('Not connected to internet');
			}
		},
		'json'
	);
	}

function translate_tweets(dstlang) {
	if (typeof(dstlang) == "undefined" || dstlang == "") return;
	$("#feed .status").each(function() {
		var c = $(this);
		var text = c.html();
		google.language.detect(text, function(dresult) {
			if (!dresult.error && dresult.language && dstlang != dresult.language) {
				google.language.translate(text, dresult.language, dstlang,
					function(tresult) {
						if (tresult.translation) {
							c.html(tresult.translation);
						}
					});
			}
		});
	});
}
