
var container = [];

function toggleButtonByCB(args) {
	
	var b;
	
	jQuery.each(args.button,function() {

		if ((b = $('#' + this)) && (typeof(container[this]) !== 'undefined')) {
			
			if (args.checked) {
				container[this]++;
			} else {
				container[this]--;
			}
			
			if (container[this] > 0) {
				b.attr('disabled','');	
			} else {
				b.attr('disabled','disabled');
			}
		}
	});	
	
}

function disableAndWait(target,clear) {

	var e;
	var wait = $('#dashboard-wait');
	
	if (e = $(target)) {
	
		if (clear) {

			var html = wait.html();
			e.html(html);

		} else {

			var p = e.offset();

			var o = $('#dashboard-disable');
				
			o.css({'z-index': 1000,'left': p.left + 'px','top': p.top + 'px','width': e.outerWidth() + 'px','height': e.outerHeight() + 'px','display': 'block'});
				
			var p = o.position();
				
			var left = Math.ceil(((o.width()-wait.width())/2)) + p.left;
				
			wait.css({'z-index': 1001,'left': left + 'px','top': (p.top + 200) + 'px','display': 'block'});
		}
	}
}

function showLogin(id,redirect) {

	var e;

	if (typeof(redirect) !== 'undefined') {
		$('#redirect').val(redirect);
	} else {
		$('#redirect').val('index');
	}
	
	if (e = $('#' + id)) {
		if (e.is(':visible')) {
			e.fadeOut('fast');
		} else {
			jQuery('#' + id).center();
			e.fadeIn('fast');
		}
	}
}

function ajaxRequest(serverid,target,args) {

	var t = $(target);

	if (t) {
		var options = {
			type: 'POST',
			url: serverid + '.php',
			data: args,
			dataType: 'html',
			success: function(html,textStatus) {
				//alert(html);
				t.html(html);
			},
			error: function(xhr, textStatus, errorThrown) {
				alert('An error occurred! ' + errorThrown);
			}
		};
		$.ajax(options);
	}

	return true;
}

function userRequest() {

}

jQuery.fn.showNextTo = function(target_object) {

	var e;
	
	if (e = $(target_object)) {

		var target_pos = e.offset();
	
		var target_w = e.width();
		var target_h = e.height();
		
		this.css('top', (target_pos.top + target_h - 6) + 'px');
		this.css('left', Math.floor((target_pos.left + (target_w/2)) - (this.width()/2)) + 'px');
	}
}

jQuery.fn.inArray = function(p_val) {

	var i;

	for (i = 0, l = this.length; i < l; i++) {
		if (this[i] == p_val) {
			return i;
		}
	}
	return false;
}

jQuery.fn.center = function (absolute) {
	return this.each(function () {
		var t = jQuery(this);

		t.css({
			position:	absolute ? 'absolute' : 'fixed', 
			left:		'50%', 
			top:		'30%', 
			zIndex:		'99'
		}).css({
			marginLeft:	'-' + (t.outerWidth() / 2) + 'px', 
			marginTop:	'-' + (t.outerHeight() / 2) + 'px'
		});

		if (absolute) {
			t.css({
				marginTop:	parseInt(t.css('marginTop'), 10) + jQuery(window).scrollTop(), 
				marginLeft:	parseInt(t.css('marginLeft'), 10) + jQuery(window).scrollLeft()
			});
		}
	});
};
