var base = '';
function set_base(str)
{
	base = str;
}
function menuopen(x, total)
{
	for (var i=1; i <= total; i++)
	{
		document.getElementById('submenu' + i).style.display = 'none';
		document.getElementById('tab' + i).className = '';
	}
	document.getElementById('submenu' + x).style.display = 'block';
	document.getElementById('tab0').className = '';
	document.getElementById('tab' + x).className = 'current';
	
}
function menuclose(total)
{
	for (var i=1; i <= total; i++)
	{
		document.getElementById('submenu' + i).style.display = 'none';
		document.getElementById('tab' + i).className = '';
	}
	
	document.getElementById('tab0').className = 'current';
}
var gallery_action = false;
function gallery_move(id, cnt, dir)
{
	if(gallery_action)
		return;
		
	var curr = document.getElementById("gallery-"+id+"-holder").style.left;
	curr = parseFloat(curr);
	if(isNaN(curr))
		curr = 0;
	
	if(dir > 0)
	{
		if(curr >= 0)
			return;
	}
	else
	{
		if(curr + cnt * (190 + 20) - 3 * (190 + 20) <= 0)
			return;
	}
	gallery_action = true;
	var offset = 210;
	if(dir < 0)
		dir = "-";
	else
		dir = "+";
		
	$("#gallery-"+id+"-holder").animate(
		{left : dir+"="+offset+"px"},
		{queue:true, duration:100, complete: function() {gallery_action = false;}}
	);
}

var video_action = false;
function videos_move(dir)
{
	if(video_action)
		return;
	
	var c = (dir == '+') ? 1 : -1;
	
	var top = parseFloat($("#videos").css('top')) + c * 100;
	var height = parseFloat($("#videos").height());
	if(top > 0)
		return;
	if(top + height < 100)
		return;
		
	video_action = true;
	$("#videos").animate({'top' : dir+'=100px'}, 100, function() {video_action = false;});
}

function set_video(code, title)
{
	var html = '';
	
	html = '<h2>'+title+'</h2>';
	html += '<object width="430" height="290"><param name="movie" value="http://www.youtube.com/v/'+code+'&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+code+'&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="430" height="290"></embed></object>';
	
	$("#featured-video").html(html);
}

function poll_vote(instance)
{
	var cnt = 0;
	for (var i=0; i < document.forms['answers-'+instance].answer.length; i++)
	{
		if (document.forms['answers-'+instance].answer[i].checked)
		{
			cnt++;
			var ans = document.forms['answers-'+instance].answer[i].value;
		}
	}
	
	if(cnt > 0)
	{
		var pollid = document.getElementById('pollid-'+instance).value;
		
		$("#poll-vote-"+instance).hide();
		$("#poll-results-"+instance).hide();
		$("#poll-show-"+instance).hide();
		
		$.ajax({
			url: "index.php",
			type: "POST",
			data: ({action : 'poll_vote', action_type : 'ajax', answer : ans, pollid : pollid, instance: instance}),
			dataType: "html",
			success: function(msg)
			{
				msg = ajax_retrieve_messages(msg);
				$("#poll_cont-"+instance).attr('innerHTML', msg[0]);
			},	
		});
	}
	else
	{
		alert("Morate odabrati odgovor da bi mogli glasati!");
	}
}
function poll_results(instance)
{
	$("#poll-vote-"+instance).hide();
	$("#poll-results-"+instance).hide();
	$("#poll-show-"+instance).show();
	var pollid = document.getElementById('pollid-'+instance).value;
	
	$.ajax({
		url: base+"index.php",
		type: "POST",
		data: ({action : 'poll_results', action_type : 'ajax', pollid : pollid, instance: instance}),
		dataType: "html",
		cache:false,
		timeout:10000,
		success: function(msg)
		{
			msg = ajax_retrieve_messages(msg);
			$("#poll_cont-"+instance).attr('innerHTML', msg[0]);
		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
			alert(textStatus + ", " + errorThrown);
		}
	});
}
function poll_show(instance)
{
	$("#poll-vote-"+instance).show();
	$("#poll-results-"+instance).show();
	$("#poll-show-"+instance).hide();
	var pollid = document.getElementById('pollid-'+instance).value;
	
	$.ajax({
		url: base+"index.php",
		type: "POST",
		data: ({action : 'poll_show', action_type : 'ajax', pollid : pollid, instance: instance}),
		dataType: "html",
		success: function(msg)
		{
			msg = ajax_retrieve_messages(msg);
			$("#poll_cont-"+instance).attr('innerHTML', msg[0]);
		}
	});
}
var current_poll = 1;
function poll_slider(dir)
{
	current_poll += dir;
	var total = $('.poll').size();
	if(current_poll < 1)
		current_poll = total;
	if(current_poll > total)
		current_poll = 1;
	
	$(".poll_slider").hide();
	
	$(".poll").slideUp(300);
	$("#poll-"+current_poll).slideDown(300, function(){$(".poll_slider").fadeIn(300)});
}
function ajax_retrieve_messages(str)
{
	var i = 0;
	var counter = 0;
	var msgs = [];
	
	counter = str.indexOf('[<ajax_message>', counter)
	while(counter >= 0)
	{
		var start_m = counter;
		var end_m = str.indexOf('</ajax_message>]', counter);
		var message = str.substring(start_m + 15, end_m);
		msgs[i] = message;
		i++;
		counter = str.indexOf('[<ajax_message>', end_m)
	}
	
	return msgs;
}
function latin2_convert(str)
{
	str = str.replace(/=/g, '*eq*');
	str = str.replace(/&/g, '*amp*');
	str = str.replace(/\n/g, '*br*');
	str = str.replace(/č/g, '[ch]');
	str = str.replace(/ć/g, '[cs]');
	str = str.replace(/š/g, '[s]');
	str = str.replace(/đ/g, '[d]');
	str = str.replace(/ž/g, '[z]');
	str = str.replace(/Č/g, '[CH]');
	str = str.replace(/Ć/g, '[CS]');
	str = str.replace(/Š/g, '[S]');
	str = str.replace(/Đ/g, '[D]');
	str = str.replace(/Ž/g, '[Z]');
	return str;
}
function newsletter_process()
{
	$("#poll-vote").show();
	$("#poll-results").show();
	$("#poll-show").hide();
	var email = document.getElementById('email').value;
	
	$.ajax({
		url: "index.php",
		type: "POST",
		data: ({action : 'newsletter_process', action_type : 'ajax', email : email}),
		dataType: "html",
		success: function(msg)
		{
			msg = ajax_retrieve_messages(msg);
			$("#newsletter").attr('innerHTML', msg[0]);
		}
	});
}

var gallery_action = false;
function gallery_move(id, cnt, dir, w, m)
{
	if(gallery_action)
		return;
		
	w = (w) ? w : 200;
	m = (m) ? m : 10;
	
	var curr = document.getElementById("gallery-"+id+"-holder").style.left;
	curr = parseFloat(curr);

	if(isNaN(curr))
		curr = 0;
	if(dir > 0)
	{
		if(curr >= 0)
			return;
	}
	else
	{
		if(curr + cnt * (w + m*2) - (300 / w) * (w + m*2) <= 0)
			return;
	}

	gallery_action = true;
	var offset = w+m;

	if(dir < 0)
		dir = "-";
	else
		dir = "+";
		
	$("#gallery-"+id+"-holder").animate(
		{left : dir+"="+offset+"px"},
		{queue:true, duration:100, complete: function() {gallery_action = false;}}
	);
}

var timeline_action = false;
function timeline_move(id, cnt, dir, w, m)
{
	if(timeline_action)
		return;
		
	w = (w) ? w : 192;
	m = (m) ? m : 10;
	
	var curr = document.getElementById("timeline-"+id+"-holder").style.left;
	curr = parseFloat(curr);

	if(isNaN(curr))
		curr = 0;
	if(dir > 0)
	{
		if(curr >= 0)
			return;
	}
	else
	{
		if(curr + cnt * (w + m*2) - (2.5) * (w + m*2) <= 0)
			return;
	}

	timeline_action = true;
	var offset = w+m;

	if(dir < 0)
		dir = "-";
	else
		dir = "+";
		
	$("#timeline-"+id+"-holder").animate(
		{left : dir+"="+offset+"px"},
		{queue:true, duration:100, complete: function() {timeline_action = false;}}
	);
}
