var lb_added = false;
var timer_id = null;
var d = '{$developer}';
var free_money_game = false;

$(document).ready(function() {
	showMessages();
	initEvents();
});

function showMessages()
{
	$('#errors').fadeIn('slow');
	$('#messages').fadeIn('slow');
	$('#infos').fadeIn('slow');
}

function initEvents()
{
	$('#l-sw').change(function(){
		$('#l-hd').val($('#l-sw').val());
		$('#l-sw-frm').submit();
	});

	$('#message').submit(function(){
		$('#message > input').attr('disabled', 'disabled');

		$.ajax({
			type: "POST",
			url: "/ajax/send_message.php",
			data: $(":input"),
			dataType: "json",
			success: function(json){
				if (json.s) {
					$('#sm-response').addClass('short-msg');
					$('#send-m').css('display', 'none');
					$('#m-sub, #m-msg').val('');
				} else {
					$('#sm-response').addClass('short-msg-e');
				}

				$('#sm-response').html(json.m);
				$('#sm-response').css('display', 'block');
				$('#message > input').removeAttr('disabled');
			}
		});

		return false;
	});
}

function loadLightbox()
{
	if (!lb_added){
		$('<link rel="stylesheet" type="text/css" href="/source/style.css" /> \
		<!--[if gte IE 6]> \
			<link rel="stylesheet" type="text/css" href="/source/ie.css" media="screen" /> \
		<![endif]--> \
		<link rel="stylesheet" type="text/css" href="/source/lightbox.css" media="screen" />').appendTo("head");

		$(function() {
			$('a[@rel*=lightbox]').lightBox();
		});

		lb_added = true;
	}
}

function viewMsgF()
{
	if ($('#send-m').css('display') == 'none')
		$('#send-m').fadeIn('slow');
	else
		$('#send-m').fadeOut('slow');
}

function timeStr(s, ud)
{
	if (ud) {
		var d = parseInt(s / 86400);
		s %= 86400;
	}

	var h = parseInt(s / 3600);
	s %= 3600;

	var m = parseInt(s / 60);
	s %= 60;

	if (ud && d < 10) d = '0' + d;
	if (h < 10) h = '0' + h;
	if (m < 10) m = '0' + m;
	if (s < 10) s = '0' + s;

	if (ud)
		s = d + ds + ' ' + h + hs + ' ' + m + ms + ' ' + s + ss;
	else
		s = h + hs + ' ' + m + ms + ' ' + s + ss;

	return s;
}

function updateAccount()
{
	if (!l_in)
		return;

	$.ajax({
		type: "GET",
		url: "/ajax/account.php",

		success: function(ret)
		{
			$("#account").html(ret);
		}
	});
}

// count chars
function comment_chrc()
{
	var l = $("#comment").val().length;
	if (l > c_max_chr) {
		$("#comment").val(oc);
	} else {
		oc = $("#comment").val();
		var t = c_max_chr - l;
		$("#comment_chars").html((t == '' || t == null) ? '0' : t);
	}
}

// comments
function showCommentForm()
{
	$("#c-on").css("display", "none");
	$("#c-off").css("display", "inline");
	$("#cf").css("display", "block");

}

function hideCommentForm()
{
	$("#c-on").css("display", "inline");
	$("#c-off").css("display", "none");
	$("#cf").css("display", "none");
}

function comments()
{
	$("#comment_form").submit(function(e){
		$.ajax({
			type: "POST",
			dataType: "json",
			data: $("#comment_form > input,textarea"),
			url: "/ajax/comment_insert.php",
			success: function(json){
				displayMessage(json.success, "#cim", json.message);
				showComments(1);

				if (json.success)
					hideCommentForm();
			}
		});

		return false;
	});

	showComments(1);
}

function showComments(page)
{
	var p = (area) ? ("&a=" + area) : ("&cp=" + cp_id);

	$.ajax({
		type: "GET",
		url: "/ajax/comments_view.php?page=" + page + p,
		success: function(ret){
			$("#cc").html(ret);
		}
	});
}

function displayMessage(success, target, message)
{
	if (success == true){
		$(target).removeClass("error");
		$(target).addClass("message");
		$("#comment_form").css("display", "none");
	} else {
		$(target).removeClass("message");
		$(target).addClass("error");
	}

	$(target).html(message);
}

function debug(str)
{
	if (!d) return;

	var html = $("#debug").html();
	$("#debug").html(str + '<br/>' + html);
}