$(document).ready(function() 
{
	$('.chro').tipsy({ gravity: 'nw', fade: false, opacity: 1 });
	$('.vote').tipsy({ gravity: 'nw', fade: false, opacity: 1 });
	$('.rates').tipsy({ gravity: 'nw', fade: false, opacity: 1 });
	$('.status').tipsy({ gravity: 'nw', fade: false, opacity: 1 });
	$('.reg').tipsy({ trigger: 'focus', gravity: 'w', fade: false });
});

function adminAct(act, act_data)
{
	$(document).ready(function()
	{
		var answer = confirm("Ar tikrai norite atlikti šį veiksmą?")
		
		if (answer)
		{
			$( this ).dialog( "close" );
			$.ajax({
				type: "POST",
				url: "validation.php",
				data: "act="+act+"&id="+act_data,
				success: function()
				{	
					$('#act').load('index.php #act');
				}
			});
		}
	});
}

function getCookie(c_name)
{
	if (document.cookie.length>0)
  {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1)
    {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) 
				c_end=document.cookie.length;
				
			return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}

function ajaxLoading(p) 
{
	$(p).html("<img src='user/images/small_loader.gif' border='0'>").fadeIn('fast');
}

function voteInfo(msg) 
{
	$(document).ready(function()
	{
		$.fx.speeds._default = 500;
		$('#dialog').append("<div class='vinfo'>"+msg+"</div>");
		$('#dialog .vinfo').dialog({
			resizable: false,
			modal: true,
			width: 350,
			height: 100,
			title: "Informacija",
			show: "puff",
			hide: "puff"
		});
	});
}

function showConfirm(msg, dtitle, goUrl, btn1, btn2, id, w, h) 
{
	$(document).ready(function()
	{
		var btns = {};
		btns[btn1] = function(){ $( this ).dialog( "close" ); window.location = goUrl; };
		btns[btn2] = function(){ $( this ).dialog( "close" ); };
	
		$.fx.speeds._default = 500;
		$('#confirmation').append("<div class='"+id+"'>"+msg+"</div>");
		$('#confirmation .'+id).dialog({
			resizable: false,
			modal: true,
			buttons: btns,
			width: w,
			height: h,
			title: dtitle,
			show: "puff",
			hide: "puff"
		});
	});
}

function createCookie(name, value, days) 
{
	if (days) 
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function changeCode(value, id, page)
{
	$(document).ready(function()
	{
		$('#code').live('change', changed(value, id, page));
	});
}

function changed(value, id, page)
{
	if (value == 1)
		$('#vote_codes').text("<a href='"+page+"/index.php?do=vote&id="+id+"' target='_blank'><img src='"+page+"/system/modules/imgr.php' border='0' alt='vote image'></a>")
	else if (value == 2)
		$('#vote_codes').text("<a href='"+page+"/index.php?do=vote&id="+id+"' target='_blank'><img src='"+page+"/user/images/vote_images/vote_2.png' border='0' alt='vote'></a>");
	else if (value == 3)
		$('#vote_codes').text("<a href='"+page+"/index.php?do=vote&id="+id+"' target='_blank'><img src='"+page+"/user/images/vote_images/vote_3.png' border='0' alt='vote'></a>");
	else if (value == 4)
		$('#vote_codes').text("<a href='"+page+"/index.php?do=vote&id="+id+"' target='_blank'><img src='"+page+"/user/images/vote_images/vote_4.png' border='0' alt='vote'></a>");
	else
		value = 1;
}

function sleep(milliseconds) 
{
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) 
	{
    if ((new Date().getTime() - start) > milliseconds)
      break;
  }
}

function submitForm(form_id, response_id)
{
	$(document).ready(function()
	{	
		var form_data = $('#'+form_id).serialize();
			
		$.ajax({
			type: "POST",
			url: "validation.php",
			data: form_data,
			success: function(msg) 
			{
				$("#response_"+response_id).html(msg);
			}
		});
	});
}

function refresh()
{
	window.location.reload();
}

function reload_vote_codes()
{
	$(document).ready(function()
	{	
		$('#vote_codes_reload').load('index.php?do=user #vote_codes_reload');
	});
}

function openFile(_title, file, id, _height, _width)
{
	$.fx.speeds._default = 500;
	$(document).ready(function()
	{
		if (_height == '')
			_height = 250;
	
		if (_width == '')
			_width = 500;
		
		var dialogOpts = {
			title: _title,
			modal: true,
			height: _height,
			width: _width,
			draggable: false,
			resizable: false,
			show: "puff",
			hide: "puff"
		};
		
		$("#"+id).dialog(dialogOpts);
   
		$("#"+id).load(file, [], function(){ $("#"+id).dialog("open"); });
	});
}

function vote(id)
{
	$.fx.speeds._default = 500;
	$(document).ready(function() 
	{	 
		var btns = {};
		btns['Taip'] = function(){
			$( this ).dialog( "close" );
			
			$('#v_'+id).ajaxSend(function() { ajaxLoading(this); });
		
			$.ajax({
				type: "POST",
				url: "validation.php",
				data: "vote_submit=1&id="+id,
				success: function(msg)
				{	
					$.getScript("user/themes/default/js/jquery.js"); 
					$.getScript("user/themes/default/js/jquery.tipsy.js"); 
					$.getScript("user/themes/default/js/jquery-ui.js"); 					
					$.getScript("user/themes/default/js/custom.js"); 
					$('#r').load('index.php #r');	
					voteInfo(msg);
				}
			});
		};
		
		btns['Ne'] = function(){
			$(this).dialog("close"); 
		};
	
		
		$('#dialog').append("<div class='vconfirm'>Ar tikrai norite atiduoti balsą šiam serveriui?</div>");
		$('#dialog .vconfirm').dialog({
				resizable: false,
				draggable: false,
				modal: true,
				buttons: btns,
				width: 300,
				height: 130,
				title: "Balsavimo patvirtinimas",
				show: "puff",
				hide: "puff"
		});
	});
}

function rVote(form_id, id)
{
	$(document).ready(function()
	{	
		var form_data = $('#'+form_id).serialize();
			
		$.ajax({
			type: "POST",
			url: "validation.php",
			data: form_data,
			success: function() 
			{
				window.location.reload();
			}
		});
	});
}

function register(form_id)
{
	$(document).ready(function()
	{	
		var form_data = $('#'+form_id).serialize();
			
		$.ajax({
			type: "POST",
			url: "validation.php",
			data: form_data,
			success: function() 
			{
				window.location.reload();
			}
		});
	});
}
