$(document).ready(function(){
   //$(".submenulist").children("a").bind("focus", function(){$(this).blur();});
})

function toggle_fc_menu(menu_title, notifications_viewed)
{
	if (menu_title != '#footer_menu_alerts')
		$('#footer_menu_alerts').hide();
	
	if (menu_title != '#footer_menu_profile')
		$('#footer_menu_profile').hide();
	
	if (menu_title)
	{
		$(menu_title).toggle();
	}
	
	if ($(menu_title).css('display') == 'block')
	{
		current_fc_menu = menu_title;
		
		if (menu_title == '#footer_menu_profile')
		{
			$('#profile_image_alert').hide();
		}
		
		if (menu_title == '#footer_menu_alerts')
		{
			$('#notifications_alert').hide();
			
			if (typeof notifications_viewed != "undefined")
			{
				$.ajax({
				  type: "POST",
				  url: "/ac.php",
				  data: "action=notifications_viewed",
				  success: function(msg){
				  }
				});
			}
		}
	}
	else
	{
		current_fc_menu = '';
		
		if (menu_title == '#footer_menu_profile')
		{
			$('#profile_image_alert').show();
		}
	}
	
	return current_fc_menu;
}
function show_fc_menu(menu_title)
{
	if (menu_title)
	{
		if (menu_title != '#footer_menu_alerts')
			$('#footer_menu_alerts').hide();
		
		if (menu_title != '#footer_menu_profile')
			$('#footer_menu_profile').hide();
		
		if (menu_title == '#footer_menu_alerts')
			$('#notifications_alert').hide();
		
		if (menu_title == '#footer_menu_profile')
			$('#profile_image_alert').hide();
		
		$(menu_title).show();
	}
}

function color_fc_menu(footer_menu_link)
{
	$(footer_menu_link).css('background-color', 'white');
}

function transparent_fc_menu(footer_menu_link)
{
	if (typeof current_fc_menu == "undefined")
	{
		$(footer_menu_link).css('background-color', 'transparent');
	}
	else
		if ((current_fc_menu + '_link') != footer_menu_link)
			$(footer_menu_link).css('background-color', 'transparent');
}


function savegroupmembertitle(group,member,title)
{
	$.post('/acall.php', {
		action: 'setgroupmembertitle',
		group: group,
		member: member,
		title: title
	}, function() {
		alert('Pavadinimas pakeistas.');
	});
}

function join_group(id)
{
	$.ajax({
	  type: "POST",
	  url: "/ac.php",
	  data: "action=join_group&id="+id,
	  success: function(msg){
		  if (msg != '')
		  {
		$('#join_group'+id).remove();
		alert('Prisijungei prie grupės: '+ msg);
		  }
	  }
	});
}

function gid(name) {
	return document.getElementById(name);
}
/*-------------------------------------------------------------------
Player javascript API
-------------------------------------------------------------------*/
var jsplayitem = -1;
var jsplaystate = -1;
var jslastplayed = -1;

var js2playitem = -1;
var js2playstate = -1;
var js2lastplayed = -1;


function sendEvent(swf,typ,prm) { 
	thisMovie(swf).sendEvent(typ,prm);
};
function getUpdate(typ,pr1,pr2,swf)  
{
	if(swf == "jsplay")
	{
		// pirmiausiai duoda itema, paskui state 
		if(typ == "item")
		{
			jsplayitem = pr1;
			
			if (jsplayitem != jslastplayed)
				gid(swf+jslastplayed).src = 'http://img.supermuzika.lt/css/listen.gif';
		}
		else if (typ == "state")
		{
			jsplaystate = pr1;
			
			if (jsplaystate == 1)
			{
				jslastplayed = jsplayitem;
			}
			else if (jsplaystate == 0)
			{
				gid(swf+jsplayitem).src = 'http://img.supermuzika.lt/css/listen.gif';
			}
		}
	}
	else if(swf == "js2play")
	{
		// pirmiausiai duoda itema, paskui state 
		if(typ == "item")
		{
			js2playitem = pr1;
			
			if (js2playitem != js2lastplayed)
				gid(swf+js2lastplayed).src = 'http://img.supermuzika.lt/css/listen.gif';
		}
		else if (typ == "state")
		{
			js2playstate = pr1;
			
			if (js2playstate == 1)
			{
				js2lastplayed = js2playitem;
			}
			else if (js2playstate == 0)
			{
				gid(swf+js2playitem).src = 'http://img.supermuzika.lt/css/listen.gif';
			}
		}
	}
};
function thisMovie(movieName) {
	if(navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}
function playSong(swf,item)
{
	if ((swf == "jsplay" && item == jsplayitem && jsplaystate == 2) || (swf == "js2play" && item == js2playitem && js2playstate == 2))
	{
		sendEvent(swf,'stop');
	}
	else
	{
		if (swf == "jsplay")
		{
			gid(swf+item).src = 'http://img.supermuzika.lt/css/listen_h.gif';
			if (gid('js2play'))
				sendEvent('js2play','stop');
		}
		else if (swf == "js2play")
		{
			gid(swf+item).src = 'http://img.supermuzika.lt/css/listen_h2.gif';
			if (gid('jsplay'))
				sendEvent('jsplay','stop');
		}
		
		sendEvent(swf,'playitem', item);
	}
	
	gid(swf+item).blur();
}

function aj_rotate()
{
	//$(window).bind('load', function(){cb_randomalbum();})
	setInterval('cb_random()',12000);
}

function cb_random()
{
	$.ajax({
		type: "POST",
		url: "/top_foto_rotator.php",
		data: "id="+ $("#photo_category_id").val(),
		success: function(msg){
			if (msg)
			{
				$('#top_photo_content').fadeTo("fast", 0, function() {
						$("#top_photo_content").html(msg);
						$('#top_photo_content').fadeTo("slow", 1); 
				});
				
				//$("#top_photo_content").style.removeAttribute('filter');
			}
		}
	});
}

function next_mail_check(seconds)
{
	setInterval('mail_check()',seconds*1000);
}
function mail_check()
{
	$.ajax({
		type: "POST",
		url: "/ac.php",
		data: "action=mail_check",
		success: function(msg){
			if (msg)
			{
				if (msg != 0)
					$("#newmail").html('('+msg+')');
				else
					$("#newmail").html('');
			}
		}
	});
	
}

function acsave(action)
{
	switch(action)
	{
		
		case 'mediadescription':
			$.ajax({
			  type: "POST",
			  url: "/ac.php",
			  data: "action="+action+"&title="+encodeURIComponent($('#mediadescription_title').val())+"&value="+encodeURIComponent($('#mediadescription_text').val())+"&id="+$('#mediadescription_id').val(),
			  success: function(msg){
				$('#mediadescription_dest').html(msg);
				$('#mediadescription_text').val(msg);
				$('#meddescbox').toggle();
				$('#mediadescription_dest_wrap').toggle();
			  }
			});
			
			break;
	}
}

function resetfototitle(id)
{
	$.ajax({
		  type: "POST",
		  url: "/ac.php",
		  data: "action=resetfototitle&id="+id,
		  success: function(msg){
			  if (msg)
				  alert(msg);
		  }
		});
}

function show_fotogamemembers()
{
$.ajax({
  type: "POST",
  url: "/ac.php",
  data: "action=show_fotogamemembers",
  success: function(msg){
		 $("#fotogamemembers").html(msg);
  }
});
}

function shownepatikrinta()
{
	$('#nepatikrinta').show();
	$('#nepatikrintainfo').hide(); 
}

function loadcpmenu(action)
{
	$('#cp_menu_stats').html('Kraunasi ...');
	$.ajax({
type: "POST",
url: "/ac.php",
data: "action=loadcpmenu",
success: function(msg){
 $("#cp_menu_stats").html(msg);
 if (action && action == 'shownepatikrinta')
	 shownepatikrinta();
}
});
}

function loadmaillist(label,page)
{
	$('#load').html('Kraunasi ...');
	$.ajax({
type: "POST",
url: "/ac.php",
data: "action=loadmaillist&label="+label+"&page="+page,
success: function(msg){
 $("#load").html(msg);
 document.title = 'Mano paštas';
}
});
	$(".submenulist").css("background","");
	$("#submenulist"+label).css("background","#eee");
	if (window.event) event.returnValue = false;

}


function loadstatslist(list)
{
	$('#load').html('Kraunasi ...');
	$.ajax({
type: "POST",
url: "/ac.php",
data: "action=loadstatslist&list="+list,
success: function(msg){
 $("#load").html(msg);
 document.title = 'Mano Statistika';
}
});
}


function loadbookmarklist(type)
{
	$('#load').html('Kraunasi ...');
	$.ajax({
type: "POST",
url: "/ac.php",
data: "action=selectbookmarklist&type="+type,
success: function(msg){
 $("#load").html(msg);
 document.title = 'Man Patinka';
}
});
 $(".mybookmarks").css("background","");
 $("#bookmarksmenu"+type).css("background","#eee");
}


function loadfriendlist(list,cat,online)
{
	$('#load').html('Kraunasi ...');
	$.ajax({
	  type: "POST",
	  url: "/ac.php",
	  data: "action=loadfriendlist&list="+list+"&cat="+cat+"&online="+online,
	  success: function(msg){
		$('#load').html(msg);
		 document.title = 'Mano Kontaktai';
	  }
	});
	$(".submenulist").css("background","");
	$("#submenulist"+list+cat).css("background","#eee");
}


function save_demesiocentre(id)
{
$.ajax({
type: "POST",
url: "/ac.php",
data: "action=save_demesiocentre&id="+id+"&demesiocentretext=" + encodeURIComponent($("#demesiocentretext").val()),
success: function(msg){
 $("#demesiocentre").hide();
}
});
}

function save_noriudemesiocentre()
{
$.ajax({
type: "POST",
url: "/ac.php",
data: "action=save_noriudemesiocentre&noriudemesiocentretext=" + encodeURIComponent($("#noriudemesiocentretext").val()),
success: function(msg){
 $("#noriudemesiocentre").hide();
}
});
}

function qs_select()
{
	index = $('#qs_select')[0].selectedIndex;
	value = $('#qs_select')[0].options[index].value;
	
	current = $('#qs_current').val();
	
	$('#qs_current').val(value);
	$('#qs_'+current).toggle();
	$('#qs_'+value).toggle();
}

function add_to_field()
{
	$("#tolist").append('<select style="width: 80px; font-size: 7.5pt; margin: 1px 0;" name="type[]"><option value="profile">Nario ID</option><option value="group">Grupės ID</option></select>: <input  style="font-size: 7.5pt; margin: 1px 0;" type="text" name="to[]" value="" /><br />');
}

function add_poll_answer()
{
	$("#pollanswers").append('<input style="width: 500px; margin: 3px 0 0 0" type="text" name="forum_polls[]" value="" /><br />');
}


function toggle_message(id)
{
	$("#message2table"+id).toggle();
	$("#messagetable"+id).toggle();
	$("#messagediv"+id).toggle();
	
}

function redirect(url)
{
	window.location = url;
}

function clear_to_value(sid,oid)
{
$.ajax({
	  type: "POST",
	  url: "/ac.php",
	  data: "action=clear_to_value&sid="+sid+"&oid="+oid,
	  success: function(msg){
		$("#owner"+oid).remove();
	  }
	});
}

function deletefriends(id)
{
$.ajax({
	  type: "POST",
	  url: "/ac.php",
	  data: "action=friendlist&type=4&id="+id,
	  success: function(msg){
		$("#makefriends"+id).html(msg);
	  }
	});
}

function makefriends(id)
{
$.ajax({
	  type: "POST",
	  url: "/ac.php",
	  data: "action=friendlist&type=1&id="+id,
	  success: function(msg){
		$("#makefriends"+id).html(msg);
	  }
	});
}

function resetdefaultphoto(id)
{
	$.ajax({
		type: "POST",
		url: "/ac.php",
		data: "action=resetdefaultphoto&id="+id,
		success: function(msg){
			if (msg)
			{
				//history.go(-1);
				alert('Atlikta');
				//document.location.reload();
			}
			else
			{
				alert('Nieko nedarem');
			}
		}
	});
}

function resetregistrationcomplete(id)
{
	$.ajax({
		type: "POST",
		url: "/ac.php",
		data: "action=resetregistrationcomplete&id="+id,
		success: function(msg){
			if (msg)
			{
				//history.go(-1);
				alert('Atlikta');
				//document.location.reload();
			}
			else
			{
				alert('Nieko nedarem');
			}
		}
	});
}

function resetapproved(id)
{
	$.ajax({
		type: "POST",
		url: "/ac.php",
		data: "action=resetapproved&id="+id,
		success: function(msg){
			if (msg)
			{
				//history.go(-1);
				alert('Atlikta');
				//document.location.reload();
			}
			else
			{
				alert('Nieko nedarem');
			}
		}
	});
}

function resetapprovedok(id)
{
	$.ajax({
		type: "POST",
		url: "/ac.php",
		data: "action=resetapprovedok&id="+id,
		success: function(msg){
			if (msg)
			{
				//history.go(-1);
				alert('Atlikta');
				//document.location.reload();
			}
			else
			{
				alert('Nieko nedarem');
			}
		}
	});
}

function setprofilereal(id)
{
	$.ajax({
		type: "POST",
		url: "/ac.php",
		data: "action=setprofilereal&id="+id,
		success: function(msg){
			if (msg)
			{
				//history.go(-1);
				alert('Atlikta');
				//document.location.reload();
			}
			else
			{
				alert('Nieko nedarem');
			}
		}
	});
}

function setprofileunreal(id)
{
	$.ajax({
		type: "POST",
		url: "/ac.php",
		data: "action=setprofileunreal&id="+id,
		success: function(msg){
			if (msg)
			{
				//history.go(-1);
				alert('Atlikta');
				//document.location.reload();
			}
			else
			{
				alert('Nieko nedarem');
			}
		}
	});
}

function grouptrv(id)
{
	$.ajax({
		type: "POST",
		url: "/ac.php",
		data: "action=grouptrv&id="+id,
		success: function(msg){
			if (msg)
				alert('Atlikta');
			else
				alert('Nieko nedarem');
		}
	});
}

function grouptrn(id)
{
	$.ajax({
		type: "POST",
		url: "/ac.php",
		data: "action=grouptrn&id="+id,
		success: function(msg){
			if (msg)
				alert('Atlikta');
			else
				alert('Nieko nedarem');
		}
	});
}

function friendlistaction(id, type)
{
	$.ajax({
		type: "POST",
		url: "/ac.php",
		data: "action=friendlist&type="+type+"&id="+id,
		success: function(msg){
			if (msg)
			{
				switch(type)
				{
					case 6:
						$("#matomaslink"+id).html(msg);
						break;
					
					default:
						$("#friendlist"+id).fadeTo("slow", 0, function() {
							$("#friendlist"+id).remove();
							$("#actionstatus").html(msg);
							$("#actionstatus").show();
							//document.location.reload();
							reloadcontactmeniu();
						});
						break;
				}
			}
		}
	});
}

function fotogame(id)
{
	title = $("#fotogametitle").val();
	
	$.ajax({
		type: "POST",
		url: "/ac.php",
		data: "action=fotogame&title="+encodeURIComponent(title)+"&id="+id,
		success: function(msg){
			if (msg)
			{
				$("#fotogame").html(msg);
			}
		}
	});
}

function sendvotetoggle(type,id,game)
{
	$("#vote"+type+"_"+id).removeClass();
	$("#vote"+type+"_"+id).addClass("starloading");
	
	$.ajax({
		type: "POST",
		url: "/ac.php",
		data: "action=sendvotetoggle&type="+type+"&id="+id,
		success: function(msg){
			if (msg)
			{
				if (msg == 1)
				{
					if (type == 10)
					{
						$(".fototitle").children("img").removeClass();
						$(".fototitle").children("img").addClass("star0");
						$("#vote"+type+"_"+id).removeClass();
						$("#vote"+type+"_"+id).addClass("star1");
						
						if (game == 1)
							return fotogame(0);
					}
					else
					{
						$("#vote"+type+"_"+id).removeClass();
						$("#vote"+type+"_"+id).addClass("star1");
					}
				}
				else
				{
					$("#vote"+type+"_"+id).removeClass();
					$("#vote"+type+"_"+id).addClass("star0");
				}
			}
		}
	});
}

function sendvote(id,value,n)
{
	if (n == 1)
	{
		voting_values = "&vote_from="+$('#vote_from').val()+"&vote_to=" + $('#vote_to').val()+"&vote_sex=" + $('#vote_sex').val();
	}
	else
	{
		voting_values = '';
	}
	
	$.ajax({
		type: "POST",
		url: "/ac.php",
		data: "action=sendvote&value="+value+"&id="+id+"&n="+n+voting_values,
		success: function(msg){
			if (msg == '')
			{	
				$("#yesnowrap").remove();
			}
			else
			{
				if (n == 1)
					$("#yesno_i").html(msg);
				else
					$("#yesno").html(msg);
			}
		}
	});
}

function savecontacttocategory(id)
{
	index = $('#editcontactcategory'+id)[0].selectedIndex;
	value = $('#editcontactcategory'+id)[0].options[index].value;
	text = $('#editcontactcategory'+id)[0].options[index].text;
	
	$.ajax({
	  type: "POST",
	  url: "/ac.php",
	  data: "action=savecontact&id="+id+"&cat="+encodeURIComponent(value),
	  success: function(msg){
		$('#editcontact'+id).html('Draugas priskirtas i kategoriją <b>'+text+'<b>');
	  }
	});
}

function savecontacttocat(id,type,value,text)
{
	$.ajax({
	  type: "POST",
	  url: "/ac.php",
	  data: "action=savecontact&id="+id+"&cat="+encodeURIComponent(value),
	  success: function(msg){
		
		if (type == 'profile')
		{
			$('#editcontactlink'+id).html(text + ' gavo tavo pasiūlymą draugauti!');
		}
		else
		{
			$('#editcontact'+id).hide();
			$("#patvirtinti"+id).hide();
			$('#editcontactlink'+id).html(text);
			$('#editcontactlink'+id).show();
		}
		
		if (type != 'friends' && type != 'profile')
		{
				$("#friendlist"+id).fadeTo("slow", 0, function() {
				$("#friendlist"+id).remove();
				$("#actionstatus").html('Naujas '+text+' išsaugotas tavo draugų sąraše');
				$("#actionstatus").show();
			});
		}
		
		if (type == 'profile')
		{
			
		}
		else
			reloadcontactmeniu();
	  }
	});
}

function savecontact(id,type)
{
	value = $('#editcontactcategoryid'+id).val();
	text = $('#editcontactcategoryvalue'+id).val();
	
	$.ajax({
	  type: "POST",
	  url: "/ac.php",
	  data: "action=savecontact&id="+id+"&cat="+encodeURIComponent(value),
	  success: function(msg){
		$('#editcontact'+id).hide();
		$("#patvirtinti"+id).hide();
		$('#editcontactlink'+id).html(text);
		$('#editcontactlink'+id).show();
		
		if (type != 'friends')
		{
			$("#friendlist"+id).fadeTo("slow", 0, function() {
				$("#friendlist"+id).remove();
				$("#actionstatus").html('Naujas '+text+' išsaugotas tavo draugų sąraše');
				$("#actionstatus").show();
				reloadcontactmeniu();
			});
		}
		
	  }
	});
}

function toggle_mp3_download(music_id)
{
	$.ajax({
	  type: "POST",
	  url: "/ac.php",
	  data: "action=toggle_mp3_download&music_id="+music_id,
	  success: function(msg){
		$('#toggle_mp3_download_'+music_id).html(msg);
	  }
	});
}

function toggle_mp3_top(music_id)
{
	$(".toggle_mp3_top").children("img").attr('src','http://img.supermuzika.lt/css/check_0.gif');
	$.ajax({
	  type: "POST",
	  url: "/ac.php",
	  data: "action=toggle_mp3_top&music_id="+music_id,
	  success: function(msg){
		$('#toggle_mp3_top_'+music_id).html(msg);
	  }
	});
}

function reloadcontactmeniu()
{
	$.ajax({
	  type: "POST",
	  url: "/ac.php",
	  data: "action=reloadcontactmeniu",
	  success: function(msg){
		$('#kontaktaimenu').html(msg);
	  }
	});
}

function save_foto_title(id)
{
	value = $('#foto_title_value').val();
	
	$.ajax({
	  type: "POST",
	  url: "/ac.php",
	  data: "action=save_foto_title&id="+id+"&title="+encodeURIComponent(value),
	  success: function(msg){
		  if (msg == '2')
			  $('#foto_title_canvas').html('Pavadinimas išsaugotas, tačiau jis nebus rodomomas, nes tavo anketa yra be nuotraukos!');
		  else
			  $('#foto_title_canvas').html('Pavadinimas išsaugotas!');
	  }
	});
}

function delete_mail()
{
	var selected=new Array();
	
	$("input.mailcheck:checkbox:checked").each(function()
	{
		conversation = $(this).attr("id");
		selected.push(conversation.replace(/mailcheck/, ''));
	});
	
	if (selected.length > 0)
	{
		if (!confirm('Ar tikrai nori pašalinti pažymėtus (' + selected.length + ') laiškus?' ))
		{
			return false;
		}
		
		group_id = $('#mail_group_id').val();
		label = $('#mail_label').val();
		page = $('#mail_page').val();
		
		$.ajax({
		  type: "POST",
		  url: "/ac.php",
		  data: "action=mail&mailaction=trash&group_id="+group_id+"&label="+label+"&conversations="+encodeURIComponent(selected),
		  success: function(msg){
			  if (msg && msg > 0)
			  {
				  //$('#id').html('Ištrinti '+msg+' laiškai!');
				  if (group_id && group_id > 0)
					  redirect('/mail.php?label='+label+'&groupID='+group_id);
				  else
					  loadmaillist(label,page);
			  }
			  else
			 {	
				 //alert('error');
			 }
		  }
		});
	}
	else
	{
		alert ('Nepažymėjai nei vieno laiško!');
	}
}
function mail_check_all(obj)
{
	var checked_status = obj.checked;
	$("input.mailcheck").each(function()
	{
		this.checked = checked_status;
	});
}