function sleep(timeout) {
  var loop = true;
  var current = new Date();
  var now;
  var cTimestamp = current.getTime();
  
  while (loop) {
    now = new Date();
    nTimestamp = now.getTime();
  
    if(nTimestamp - cTimestamp > timeout) loop = false;
  }
}


/**
 * Concatenates the values of a variable into an easily readable string
 * by Matt Hackett [scriptnode.com]
 * @param {Object} x The variable to debug
 * @param {Number} max The maximum number of recursions allowed (keep low, around 5 for HTML elements to prevent errors) [default: 10]
 * @param {String} sep The separator to use between [default: a single space ' ']
 * @param {Number} l The current level deep (amount of recursion). Do not use this parameter: it's for the function's own use
 */
function print_r(x, max, sep, l) {
	l = l || 0;
	max = max || 10;
	sep = sep || ' ';
	if (l > max) {
		return "[WARNING: Too much recursion]\n";
	}
	var
		i,
		r = '',
		t = typeof x,
		tab = '';
	if (x === null) {
		r += "(null)\n";
	} else if (t == 'object') {
		l++;
		for (i = 0; i < l; i++) {
			tab += sep;
		}
		if (x && x.length) {
			t = 'array';
		}
		r += '(' + t + ") :\n";
		for (i in x) {
			try {
				r += tab + '[' + i + '] : ' + print_r(x[i], max, sep, (l + 1));
			} catch(e) {
				return "[ERROR: " + e + "]\n";
			}
		}
	} else {
		if (t == 'string') {
			if (x == '') {
				x = '(empty)';
			}
		}
		r += '(' + t + ') ' + x + "\n";
	}
	return r;
};

function clearCookie(cookieName) {

	var now = new Date();
	var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);

	this.setCookie(cookieName, 'cookieValue', yesterday);
};

function setCookie(cookieName, cookieValue, expires, path, domain, secure) {
	document.cookie =
		escape(cookieName) + '=' + escape(cookieValue)
		+ (expires ? '; expires=' + expires.toGMTString() : '')
		+ (path ? '; path=' + path : '')
		+ (domain ? '; domain=' + domain : '')
		+ (secure ? '; secure' : '');
}

function getCookie(cookieName) {

	var cookieValue = '';
	var posName = document.cookie.indexOf(escape(cookieName) + '=');

	if (posName != -1) {
		var posValue = posName + (escape(cookieName) + '=').length;
		var endPos = document.cookie.indexOf(';', posValue);

		if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));
		else cookieValue = unescape(document.cookie.substring(posValue));
	}

	return (cookieValue);
};

function floater(query) {
	var arrPageSizes = ___getPageSize();
	var arrPageScroll = ___getPageScroll();
	
	$('#statusinfo').fadeOut(100, function() {
		$('#mainloader').fadeIn();

		$.getJSON('ajax/ajax.php' + query, function(data) {
			$('#statusinfo').html(data.statusinfo);
			$('#mainloader').fadeOut(100, function() {
				$('#statusinfo').fadeIn();
				$('.floater').fadeOut(200, function() { $(this).remove(); });
				$('body').prepend(data.floater)
				$('.floater').find('img').batchImageLoad({
					loadingCompleteCallback: function() {
						_top = arrPageScroll[1] + (arrPageSizes[3] - $('.floater').height()) / 2;
						//_top = arrPageScroll[1] + arrPageSizes[3] / 6;
						_left = (arrPageSizes[2] - $('.floater').width()) / 2;
						$('.floater').fadeIn(200).css({top:_top, left:_left});
					}
				});
			});
		});
	});
}

function destroyFloater() {
	$('.floater').fadeOut(function() { $(this).remove(); });
}
