function getUrlQueryVars()
{
	var query = null;
	if(window.location.href.indexOf('?') > -1) {
	    query = window.location.href.slice(window.location.href.indexOf('?') + 1);
	}
	return query;
}

function loadAdditionalData() {
	$('img#loader').fadeIn('fast');
	
	var queryData = "o=" + currentDataOffset + "&cc=" + cc;
	var queryAdditional = getUrlQueryVars();
	if(queryAdditional !== null) {
		queryData += "&" + queryAdditional;
	}
	
	var baseHref = "http://" + (cc == "us" ? "" : cc + ".") + "rhombus-app.com/";
	$.get(baseHref + "inc/ajax/latest.php", queryData, function(data) {
		$('img#loader').fadeOut('fast');
		if(data != '') {
			$('ul#results li.result-listing:last').after(data);
			currentDataOffset = $('ul#results li.result-listing').length;
		}
	}, 'html');
}

$(document).ready(function() {
	if($('div#content.latest').length > 0) {
		$(window).scroll(function(){
			if($(window).scrollTop() == $(document).height() - $(window).height()){
				loadAdditionalData();
			}
		});
	}
	
	if($('div#content.latest, div#content.search').length > 0) {
		currentDataOffset = $('ul#results li.result-listing').length;
		
		$('div#region a.toggle').click(function(e) {
			e.preventDefault();
			$(this).toggleClass('active').next().toggle();
		});
		$('ul#results div.title a').live('click', function(e) {
			e.preventDefault();
		
			var rel = $(this).attr('rel').split('_');
			var id = rel[1];
			var type = rel[0];	
			
			var more_info = $(this).parent().parent();
			more_info.toggleClass('active');
			$(this).next().toggle();
			
			if(more_info.find('.more-info:empty').length > 0) {
				var baseHref = "http://" + (cc == "us" ? "" : cc + ".") + "rhombus-app.com/";
				$('img#loader').fadeIn('fast');
				more_info.find('.more-info').load(baseHref + "inc/ajax/more-info.php?cc=" + cc + "&id=" + id + "&type=" + type, function() {
					$('img#loader').fadeOut('fast');
					$(this).toggle();
				});
			} else {
				more_info.find('.more-info').toggle();
			}
		});
	}
});