var timeout	= 300;
var closetimer	= 0;
var ddmenuitem	= 0;
var tab = 0;

function mopen(id)
{	
	mcancelclosetime();
	if(ddmenuitem) {
		ddmenuitem.style.visibility = 'hidden';
		tab.className = '';
	}
	ddmenuitem = document.getElementById('m' + id);
	
	//$("#m"+id).fadeIn();
	ddmenuitem.style.visibility = 'visible';
	tab = document.getElementById('tab' + id);
	tab.className = 'current';
}

function mclose()
{
	if(ddmenuitem) {
		ddmenuitem.style.visibility = 'hidden';
		tab.className = '';
	}
}

function mclosetime()
{
	closetimer = window.setTimeout(mclose, timeout);
}

function mcancelclosetime()
{
	if(closetimer)
	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

document.onclick = mclose; 

var current_top_menu = 1;
function doswitch(id, min, max)
{
	if(id != current_top_menu)
	{
		for(var i = min; i <= max; i++)
		{
			$("#image"+i).stop();
			$("#image"+i).css('opacity', 0); 
			$("#image"+i).hide();
			document.getElementById("title" + i).className = '';
		}
		
		$("#image"+id).show();
		$("#image"+id).animate({opacity: 1}, {duration: 300});
		document.getElementById('title' + id).className = 'current';
		current_top_menu = id;
	}
}

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];
		}
	});
}

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 * (160 + 33) - 5 * (160 + 33) <= 0)
			return;
	}

	gallery_action = true;
	var offset = 160 + 33;

	if(dir < 0)
		dir = "-";
	else
		dir = "+";
		
	$("#gallery-"+id+"-holder").animate(
		{left : dir+"="+offset+"px"},
		{queue:true, duration:100, complete: function() {gallery_action = false;}}
	);
}

forum_login();
function forum_login()
{
	if(!document.getElementById('forum-login'))
		return;
	var u = document.getElementById('fusername').value;
	var p = document.getElementById('fpassword').value;

	document.getElementById('fusername').value = '';
	document.getElementById('fpassword').value = '';
	
	$.ajax({
		type: "POST",
		url: "forum.php",
		data: "action=login&username="+u+"&password="+p,
		success: function(msg)
		{
		}
	});	
}

function vote(id, vote)
{
	if(vote >= 1)
		vote = 1;
	else
		vote = -1;
		
	$("#vote-actions-"+id).fadeOut(500);
		
	$.ajax({
		type: "POST",
		url: "index.php",
		data: "action=vote&action_type=ajax&id="+id+"&v="+vote,
		success: function(msg)
		{
			var msg = ajax_retrieve_messages(msg);
			$("#comment-vote-"+id).fadeOut(500, function() {
				$("#comment-vote-"+id).html(msg[0]);
				$("#comment-vote-"+id).fadeIn(500);
			});
		}
	});	
}

var testmove = false;
function test_move(dir)
{
	var current = $("#test_page").val() * 1;
	var total = $("#test_pages").val() * 1;
	current+=dir;
	
	if(current < 1)
		return;
		
	if(current > total)
		return;
		
	if(testmove)
		return;
		
	var found = false;
	var s = current-dir;
	for(var i = 1; i <= 5; i++)
	{
		if((document.getElementById("a-"+s+"-"+i)) && (document.getElementById("a-"+s+"-"+i).checked))
		{
			found = true;
			break;
		}
	}
	if((found == false) && (dir == 1))
	{
		$("#test_message").html("Moraš izabrati barem jedan odgovor!");
		return;
	}
	else
	{
		$("#test_message").html("");
	}
	
	if(current == 1)
		$("#test_move_left").hide();
	else
		$("#test_move_left").show();
	
	if(current == total)
	{
		$("#test_move_right").hide();
		$("#test_submit").show();
	}
	else
	{
		$("#test_move_right").show();
		$("#test_submit").hide();
	}
	
		
	testmove = true;
	
	$("#test_page").val(current);
	if(dir > 0)
		dir = "-";
	else
		dir = "+";
		
	var offset = 650;
		
	$("#test_holder").animate(
		{left : dir+"="+offset+"px"},
		{queue:true, duration:500, complete: function() {testmove = false;}}
	);
	
	if(dir == "-")
		dir = "+";
	else
		dir = "-";
	
	var pstep = (1 / (total-1)) * 100;
	
	$("#progress").animate(
		{width: dir+"="+pstep+"%"},
		{queue:true}
	);
}

function test_submit(id)
{
	var current = $("#test_page").val() * 1;

	var found = false;
	var s = current;
	for(var i = 1; i <= 5; i++)
	{
		if((document.getElementById("a-"+s+"-"+i)) && (document.getElementById("a-"+s+"-"+i).checked))
		{
			found = true;
			break;
		}
	}
	if((found == false))
	{
		$("#test_message").html("Moraš odabrati odgovor");
		return;
	}
	else
	{
		$("#test_message").html("");
	}

	testmove = true;
	var offset = 650;
	$("#test_holder").animate(
		{left : "-="+offset+"px"},
		{queue:true, duration:500, complete: function() {testmove = false;}}
	);
	
	var total = $("#test_pages").val() * 1;
	var answers = new Array();
	for(s = 1; s <= total; s++)
	{
		for(var i = 1; i <= 5; i++)
		{
			if((document.getElementById("a-"+s+"-"+i)) && (document.getElementById("a-"+s+"-"+i).checked))
			{
				answers[s] = i;
				break;
			}
		}		
	}
	
	$("#test_move_left").hide();
	$("#test_move_right").hide();
	$("#test_submit").hide();
	$("#progress_bar").hide();
	
	
	answers = answers.join(",");
	$.ajax({
		type: "POST",
		url: "index.php",
		data: "action=test_submit&action_type=ajax&id="+id+"&answers="+answers,
		success: function(msg)
		{
			var msg = ajax_retrieve_messages(msg);
			document.getElementById("test_holder").style.display = "none";
			document.getElementById("progress_bar").style.display = "none";
			
			$("#test_results").html(msg[0]);
			
		}
	});	
	$("#test_page").val(1);	
}

var topsmove = false;
function tops_move(dir)
{
	var current = $("#tops_page").val() * 1;
	var total = $("#tops_pages").val() * 1;
	current+=dir;
	
	if(current < 1)
		return;
		
	if(current > total)
		return;
		
	if(topsmove)
		return;
		
	if(current == 1)
		$("#tops_move_left").hide();
	else
		$("#tops_move_left").show();
	
	if(current == total)
	{
		$("#tops_move_right").hide();
	}
	else
	{
		$("#tops_move_right").show();
	}
	
		
	topsmove = true;
	
	$("#tops_page").val(current);
	if(dir > 0)
		dir = "-";
	else
		dir = "+";
		
	var offset = 650;
		
	$("#test_holder").animate(
		{left : dir+"="+offset+"px"},
		{queue:true, duration:500, complete: function() {topsmove = false;}}
	);
	
	if(dir == "-")
		dir = "+";
	else
		dir = "-";
	
	var pstep = (1 / (total-1)) * 100;
	
	$("#progress").animate(
		{width: dir+"="+pstep+"%"},
		{queue:true}
	);
}

function head_vote(id, ord)
{
	$("#head-left-vote").fadeOut(200);
	$("#head-right-vote").fadeOut(200);

	$.ajax({
		type: "POST",
		url: "index.php",
		data: "action=head_vote&action_type=ajax&id="+id+"&ord="+ord,
		success: function(msg)
		{
			var msg = ajax_retrieve_messages(msg);
			if(msg[0] > msg[1])
			{
				$("#head-left").addClass('winner');
				$("#head-right").removeClass('winner');
			}
			else if(msg[0] < msg[1])
			{
				$("#head-left").addClass('winner');
				$("#head-right").removeClass('winner');				
			}
			else
			{
				$("#head-left").removeClass('winner');
				$("#head-right").removeClass('winner');
			}
			
			$("#h2vote_voted_1").html(msg[0]).fadeIn(500);
			$("#h2vote_voted_2").html(msg[1]).fadeIn(500);
		}
	});		
}

function crewmenu(id)
{
	$("#crew-wassup").hide();
	$("#crew-online").hide();
	$("#crew-new").hide();
	$("#crew-search").hide();

	document.getElementById("tab-crew-wassup").className = "";
	document.getElementById("tab-crew-online").className = "";
	document.getElementById("tab-crew-new").className = "";
	document.getElementById("tab-crew-search").className = "";
	
	document.getElementById("tab-" + id).className = "current";
	
	$("#" + id).fadeIn(300);
}


function key_change_status(e)
{
	var KeyID = (window.event) ? event.keyCode : e.keyCode;
	
	if(KeyID == 13)
	{
		save_user_status();
		document.getElementById('status_save').focus();
	}
}

function save_user_status()
{
	var interact = (document.getElementById('is_interact')) ? 1 : 0;
	var user_status = document.getElementById('user_status').value;
	$("#current_user_status").fadeOut("fast");

	$.ajax({
		url: "index.php",
		type: "POST",
		data: ({action : 'user_status', action_type : 'ajax', user_status : user_status, interact:interact}),
		dataType: "html",
		success: function(msg)
		{
			$("#current_user_status").stop();
			msg = ajax_retrieve_messages(msg);
			document.getElementById('user_status_holder').innerHTML = msg[0];
			
			if(document.getElementById('profile_actions'))
			{
				var i = 0;
				$("#profile_actions").prepend(msg[1]);
				$("#profile_actions").children().removeClass('odd');
				$("#profile_actions").children().not(':even').addClass('odd');
			}
			$("#last-status-"+msg[2]).show(500);
		}
	});
}

var status_status = 0;
function user_swap_status_change(id)
{
	if(id == 1)
	{
		$("#current_user_status").hide();
		$("#user_status_info").show();
		$("#user_status").focus();
		$("#status_edit").hide();
		$("#status_save").show();
		
		status_status = 1;
		
		$(document).bind("click",function(e)
		{
			if(status_status == 3)
			{
				var pos = $("#status_save").position();
				//alert(pos.left + ", " + pos.top + " " + e.pageX + ", " + e.pageY + " " + status_status);
				if((pos.left - 10 >= e.pageX) && (pos.left + 18 + 1 <= e.pageX) && (pos.top - 3 >= e.pageY) && (pos.top + 18 <= e.pageY))
				{
					$(document).unbind("click");
					save_user_status();
					status_status = 0;
				}
				else
				{
					$("#status_edit").show(); 
					$("#status_save").hide();						

					$("#user_status_info").fadeOut(300, function(){
						$("#current_user_status").show(); 
					});
					$(document).unbind("click");
				}
				status_status = 0;
			}
			status_status++;
		});	
		
		status_status = 2;		
	}
	else if(id == 2)
	{
		
		$("#user_status_info").fadeOut(500, function() {$("#current_user_status").show(); $("#status_edit").show(); $("#status_save").hide();});
		$(document).unbind("click");			
	}
}

function load_actions()
{
	var size = $("#profile_actions").children().length;
	$.ajax({
		url: "index.php",
		type: "POST",
		data: ({action : 'load_actions', action_type : 'ajax', size:size}),
		dataType: "html",
		success: function(msg)
		{
			msg = ajax_retrieve_messages(msg);
			$("#profile_actions").append(msg[0]);
			$("#profile_actions").children().removeClass('odd');
			$("#profile_actions").children().not(':even').addClass('odd');
		}
	});
}