var sighting_id_from = 0;
var sighting_per_page = 6;

function initFeeds() {
	$.ajax({
		type: 'GET',
		url: base_url + 'sighting/feeds/',
		data: '',
		dataType: "json",
		success: function(data){
			showFeeds(data.sighting_list);
		}
	});
}

function showFeeds(list) {
	var item = null;
	var html = '';
	var species_dir = ''
	var location = '';
	for(var i = 0; i < list.length; i++) {
		item = list[i];

		species_dir = getSpeciesDir(item.species_name);
		location = '';

		if (item.sighting_id > sighting_id_from) 	{
			sighting_id_from = item.sighting_id;
		}
		
		html += '<li><div class="updateDiv roundCorners">';
		html += '	<div class="update_img">';
		html += '		<a href="' + getBaseUrl(item.category_id) + 'species/view/' + species_dir + '" target="_blank"><img width="50" height="50" src="' + static_url + 'statics/images/species/' + item.folder + '/st.jpg" /></a>';
		html += '	</div>';
		html += '	<div class="update_title">';
		if (item.username != '') {
			html += '		<span><a href="' + getBaseUrl(item.category_id) + 'profile/' + item.user_id + '" target="_blank">' + item.username + '</a></span>';
		}
		else {
			html += '		<span>guest</span>';
		}
		if (item.sighting_time != "")	 {
			html += ' | ' + item.sighting_time.substring(0,10);
			html += ' | ' + item.sighting_time.substring(11,item.sighting_time.length);
		}		
		html += '	</div>';
		if (item.city != '' && item.city != "0" && item.city != 0) {
			location = item.city;
		}
		if (item.state != '' && item.state != "0" && item.state != 0) {
			if (location != "")	 {
				location += ", ";
			}
			location += item.state;
		}		
		html += '	<div class="update_species">';
		html += '		Sighted ' + item.number_sighted + ' <a href="' + getBaseUrl(item.category_id) + 'sighting/view/' + item.sighting_id + '" target="_blank">' + item.species_name + '</a>';
		html += '	</div>';
		html += '	<div class="update_location">';
		html += '		in ' + location + '.';
		html += '	</div>';		
		html += '</div></li>';
	}		
	$('#updatesList').html(html);
	
	if(list.length > sighting_per_page)
		scrollFeeds();
}

function scrollFeeds(){
	$(".updateticker-jcarousellite").jCarouselLite({
		vertical: true,
		hoverPause:true,
		visible: sighting_per_page,
		auto:3000,
		speed:2000
	});	
}

function getBaseUrl(categoryId) {
	var site = "www";
	if (categoryId == 1) {
		site = "bird";
	}
	else if (categoryId == 2) {
		site = "marine";
	}
	if (base_url.indexOf("dev") != -1) {
		return base_url.replace("dev", site + ".dev");
	}
	else if (base_url.indexOf("www") != -1) {
		return base_url.replace("www", site);
	}
	else {
		return base_url.replace("thewildlab", site + ".thewildlab");
	}
	return base_url;
}