(function($){
$('document').ready(function() 
{
	$.meta = {}
	$.meta.apiUrl = 'http://eric/base/search/t3n/';
	$.meta.apiAction = '/entries.json';
	initListNavigation('marketplace');
	initListNavigation('jobs');
	initListNavigation('startups');
	initListNavigation('opensource');
});

function initListNavigation(listType){
	$.meta.listType = listType;
	$.meta.className = '.' + listType.charAt(0).toUpperCase() + listType.substring(1);
	$('#entries_' + listType + '_linkTop a').click(function (event){ event.preventDefault(); buildListBoxTop(listType); });
	$('#entries_' + listType + '_linkBottom a').click(function (event){ event.preventDefault(); buildListBoxBottom(listType); });
}

function buildListBoxTop(listType){
	toggleLink(listType,'linkTop','hide');
	toggleLink(listType,'linkBottom','hide');
	
	var p = parseInt($('#entries_' + listType).attr('rel'));
	var apiUrl = $('#entries_' + listType + '_url').attr('rel');
	var pNew = p - 1;
	if (pNew < 1){ pNew = 1; }
	$('#entries_' + listType).attr('rel',pNew);
	$('#entries_' + listType + ' > li.entry').hide();
	$('#entries_' + listType + '_loader').show();
	$.getJSON(apiUrl, {'p' : pNew }, function(data){
		if(!data) {
			$('#entries_' + listType + '_loader').hide();
			$('#entries_' + listType + ' > li.entry').show();
			
			return;
		}
		var meta = data.meta;
		var data = data.elements;
		var html = '';
		for (var i = 0; i< data.length;i++){
            html += '<li class="entry clearfix"><a href="'+data[i].url+'">';
            if (data[i].imageUrl != '') {
                html += '<img src="'+data[i].imageUrl+'" width="50" height="50" alt="'+data[i].title+'" />';
            }
            html += '<strong>'+data[i].titleShort+'</strong> '+data[i].descriptionShort;

            if (data[i].location != '') {
                html += ' <span class="small">'+data[i].location+'</span>';
            }
            html += '</a></li>';
        }
		
		$('#entries_' + listType + ' > li.entry').remove();
		$('#entries_' + listType + '_loader').hide();
        $('#entries_' + listType + '_linkTop').after(html);
		//$('#entries_' + listType).fadeIn();
		if (parseInt(meta.currentPage) < parseInt(meta.lastPage)){
			toggleLink(listType,'linkBottom','show');
		}
		if (parseInt(meta.currentPage) > 1){
			toggleLink(listType,'linkTop','show');
		}
	});
}

function buildListBoxBottom(listType, apiUrl, apiAction, className){
	toggleLink(listType,'linkTop','hide');
	toggleLink(listType,'linkBottom','hide');
	
	var p = parseInt($('#entries_' + listType).attr('rel'));
	var apiUrl = $('#entries_' + listType + '_url').attr('rel');
	var pNew = p + 1;
	$('#entries_' + listType).attr('rel',pNew);
	$('#entries_' + listType + ' > li.entry').hide();
	$('#entries_' + listType + '_loader').show();
	$.getJSON(apiUrl, {'p' : pNew}, function(data){
		if(!data) {
			$('#entries_' + listType + '_loader').hide();
			$('#entries_' + listType + ' > li.entry').show();
			
			return;
		}
		
		var meta = data.meta;
		var data = data.elements;
		var html = '';
        for (var i = 0; i< data.length;i++){
            html += '<li class="entry clearfix"><a href="'+data[i].url+'">';
            if (data[i].imageUrl != '') {
                html += '<img src="'+data[i].imageUrl+'" width="50" height="50" alt="'+data[i].title+'" />';
            }
            html += '<strong>'+data[i].titleShort+'</strong> '+data[i].descriptionShort;

            if (data[i].location != '') {
                html += ' <span class="small">'+data[i].location+'</span>';
            }
            html += '</a></li>';
        }		
		$('#entries_' + listType + ' > li.entry').remove();
		$('#entries_' + listType + '_loader').hide();
        $('#entries_' + listType + '_linkTop').after(html);
		//$('#entries_' + listType).fadeIn();
		if (parseInt(meta.currentPage) >= parseInt(meta.lastPage)){
			toggleLink(listType,'linkBottom','hide');
		}
		else {
			toggleLink(listType,'linkBottom','show');
		}
		if (parseInt(meta.currentPage) > 1){
			toggleLink(listType,'linkTop','show');
		}
	});
}

function toggleLink(listType, action, mode){
	if (mode == 'show'){
		$('#entries_' + listType + '_' + action).removeClass(action + 'None');
		$('#entries_' + listType + '_' + action).addClass(action);
	}
	if(mode == 'hide') {
		$('#entries_' + listType + '_' + action).removeClass(action);
		$('#entries_' + listType + '_' + action).addClass(action + 'None');
	}
}
})(jQuery);
