function tabs(id, total)
{
	for (var x = 1; x <= total; x++)
	{
		document.getElementById('tab' + x).className = '';
		document.getElementById('tabdiv' + x).style.display = 'none';
	}
	
	document.getElementById('tab' + id).className = 'current';
	document.getElementById('tabdiv' + id).style.display = 'block';
}

function cool(type, content)
{
	$.ajax({
		type: "POST",
		url: "index.php",
		data: "action_type=ajax&action=cool&type="+type+"&content="+content,
		success: function(msg)
		{
			var msg = ajax_retrieve_messages(msg);
			document.getElementById('cool-'+type+'-'+content).innerHTML = msg[0];
		}
	});
}

function vote(id, type, star)
{
	$.ajax({
		type: "POST",
		url: "index.php",
		data: "action_type=ajax&action=vote&id="+id+"&type="+type+"&star="+star,
		success: function(msg)
		{
			var msg = ajax_retrieve_messages(msg);
			
			$('#votes-total-'+id).fadeOut(100, function(){
				$('#votes-total-'+id).html(msg[1]);
				$('#votes-total-'+id).fadeIn(300);
			});
			$('#votes-'+id+'-'+type).fadeOut(100, function(){
				$('#votes-'+id+'-'+type).html(msg[0]);
				$('#votes-'+id+'-'+type).fadeIn(300);
			});
		}
	});
}

var maps = new Array();
var map_id = 0;
function load_map(search, id, txt)
{
	map_id = id;
	
	var geocoder = new GClientGeocoder();
	
	var addresses = search.split("|");
	for(var k = 0; k < addresses.length; k++)
	{
		var s = addresses[k];
		geocoder.getLatLng(addresses[k], function(point) {
			if (point) {
				maps[maps.length] = [point, txt, s];
			}
		});
	}
}

var markers;
function init_maps()
{
	var map = new GMap2(document.getElementById(map_id));
	markers = new Array();
	
	for(var i = 0; i < maps.length; i++)
	{
		map.setCenter(maps[i][0], 13);
        markers[markers.length] = new GMarker(maps[i][0], {title:maps[i][1]+", "+maps[i][2]+""});
        map.addOverlay(markers[markers.length - 1]);
	}
}

function logout()
{
	FB.logout(function(response) {
		window.location = 'http://food.com.hr/logout';
	});
}

var currentAdvanced = '';
function show_advanced()
{
	if($('#tab-advanced').hasClass('current') && currentAdvanced != '')
	{
		$('#tab-advanced').removeClass('current');
		$('#'+currentAdvanced).addClass('current');
		$('#advanced-search').slideUp(500);
	}
	else
	{
		currentAdvanced = ($('#tab-alpha').hasClass('current')) ? 'tab-alpha' : 'tab-rating';
		$('#tab-alpha').removeClass('current');	
		$('#tab-rating').removeClass('current');	
		$('#tab-advanced').addClass('current');	
		$('#advanced-search').slideDown(500);
	}
}

function calculate_bmi()
{
	
	var weight = 0;
	var height = 0;
	
	if(isNaN(parseFloat($('#bmi-weight').val())) || (parseFloat($('#bmi-weight').val()) == 0))
	{
		$("#bmi-weight-error").slideDown(500);
		return;
	}
	$("#bmi-weight-error").slideUp(500);
	
	if(isNaN(parseFloat($('#bmi-height').val())) || (parseFloat($('#bmi-height').val()) == 0))
	{
		$("#bmi-height-error").slideDown(500);
		return;
	}
	$("#bmi-height-error").slideUp(500);
	
	weight = parseFloat($('#bmi-weight').val());
	height = parseFloat($('#bmi-height').val()) / 100;
	
	var bmi = (weight / (height * height));
	
	$("#bmiform").fadeOut(500, function(){
		$("#bmi-result-r").html(bmi.toFixed(2));
		$("#bmiresult").fadeIn(500);
	});
}

function reset_bmi()
{
	$('#bmi-weight').val("");
	$('#bmi-height').val("");
	$("#bmiresult").fadeOut(500, function(){
		$("#bmi-result-r").html("");
		$("#bmiform").fadeIn(500);
	});	
}

function more_actions()
{
	var p = $("#action-page").val();
	$.ajax({
		type: "POST",
		url: "index.php",
		data: "action_type=ajax&action=more_actions&page="+p,
		success: function(msg)
		{
			var msg = ajax_retrieve_messages(msg);
			if(msg[0] == -1)
			{
				$("#more_actions").fadeOut(500);
			}
			else
			{
				p++;
				$("#actions").append(msg[0]);
				$("#action-page").val(p);
			}
		}
	});	
}

