globals.servertime = new Date(globals.servertime*1000);
window.setInterval(function () {globals.servertime.setTime(globals.servertime.getTime() + 1000);}, 1000);

var popUpInstance;
function openSizedPopUp($url, $width, $height) {
	if (popUpInstance) popUpInstance.close();
	var width = (screen.width-$width)/2;
	var height = (screen.height-$height)/2-50;
	var properties = "toolbar=no,menubar=no,scrollbars=yes,statusbar=no,height="+$height+",width="+$width+",left="+width+",top="+height+"";
	popUpInstance = window.open($url,'popup',properties);
}

$(document).ready(function(){
	// search box
	$('#searchbox').bind({
		focus: function () {
			if ($(this).val() == 'search (username or word)') $(this).val('');
		},
		blur: function () {
			if ($(this).val() == '') $(this).val('search (username or word)');
		},
		keydown: function (e) {
			if(e.keyCode == 13) {
				$(this).parent().submit();
			}
		}
	});
	
	// bind create buttons
	$('.create-button').bind('click', function () {
		_gaq.push(['_trackPageview', 'cloud/create']);
		window.location.href = globals.basedomain;
		return false;
	});

	// bind login buttons
	$('.twitter-login-button').bind('click', function () {
		openSizedPopUp(globals.basedomain + 'auth/login.php', 900, 450);
	});

	// enable autoplace
	autoPlaceFooter();
	$(window).bind('resize', autoPlaceFooter);
	window.setInterval(autoPlaceFooter, 1000);
	
	// enable smartload
	smartload();
	$(window).scroll(smartload);
	
	// datelines
	update_datelines();
	window.setInterval(update_datelines, 1000);
	
	// analytics
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	
	// reparse
	$('.reparse').each(function(){
		if (fbml = $(this).html().match(/^<!--(.+)-->$/im)) {
			$(this).html(fbml[1]);
		}
	});
});

var update_datelines = function () {
	$('span.dateline').each(function () {
		if (typeof this.date == 'undefined') {
			this.date = parseInt($(this).html().match(/<!--(.+)-->/)[1]);
			$(this).css('visibility', 'visible');
		}
		var now = Math.round(globals.servertime.getTime() / 1000);
		
		var difference = (now - this.date);
		
		var newtime = nicetime(this.date, now);
		if ($(this).text() != newtime) $(this).text(newtime + ' ago');
	});
};

var nicetime = function (unix_timestamp, now) {
	var periods = ['second', 'minute', 'hour', 'day', 'week', 'month', 'year', 'decade'];
	var lengths = ['60','60','24','7','4.35','12','10'];

	if (now > unix_timestamp) {   
		var difference = now - unix_timestamp;
	} else {
		var difference = unix_timestamp - now;
	}

	for(j = 0; difference >= lengths[j] && j < lengths.length-1; j++) {
		difference /= lengths[j];
	}

	difference = Math.round(difference);

	if(difference != 1) {
		periods[j]+= 's';
	}

	return difference + ' ' + periods[j];
};

// smartload magic
var smartload = function () {	
	$(".smartload").each(function () {
		var pageBottom = $(window).scrollTop() + $(window).height();
		var elementTop = $(this).position().top;
		var bottomBuffer = 200;
		if (elementTop <= (pageBottom + bottomBuffer) ) {
			$(this).removeClass('smartload');
			
			var futureimage = $(this).attr('longdesc');
			$(this).removeAttr('longdesc');
			
			$(this).attr('src', futureimage);
		}
	});
	$(".smartload-background").each(function () {
		var pageBottom = $(window).scrollTop() + $(window).height();
		var elementTop = $(this).position().top;
		var bottomBuffer = 200;
		if (elementTop <= (pageBottom + bottomBuffer) ) {
			$(this).removeClass('smartload-background');
			
			var futureimage = $(this).attr('longdesc');
			$(this).removeAttr('longdesc');
			
			$(this).css('background', "transparent url('" + futureimage + "') no-repeat 0 0");
		}
	});
};

// autoplace the footer at the bottom if the content is too short
var autoPlaceFooter = function () {
	if (!$.browser.msie) {
		$('#footer').css('margin-top', 0);
		
		var marginHeight = $(window).height() - $('body').height();
		if (marginHeight > 0) $('#footer').css('margin-top', marginHeight+30);
	}
};

var processUserLoggedIn = function () {
	window.location.href = window.location.href.replace(/\#.*$/ig,'');
};

/*
* analytics
*/
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-356699-15']);
_gaq.push(['_trackPageview', globals.pagetype]);
