
// Updates money in sidebar
function updateMoney(iNew) {
	$("span#sidebar_money").text(iNew).effect("highlight", {}, 4000);
	return true;
}

$(document).ready(function() {
	$("#popup_bg").click(function(e){
		togglePopup();
		e.preventDefault();
	});

	/* Private Messages quote functions - START */
	function toggleMessageQuote(id) {
		if($('#quote_'+id).is(":visible")) {
			$('#quote_'+id).hide();
			$('#msg_quote_expand'+id).html('+');
			if($("#msg_quote_expand"+id).hasClass("msg_quote_first")) {
				$('.msg_quote_expand_all').html('++');
			}
		} else {
			$('#quote_'+id).show();
			$('#msg_quote_expand'+id).html('-');
			$('.msg_quote_expand_all').html('--');
		}
	}
	function openMessageQuote(id) {
		$('#quote_'+id).show();
		$('#msg_quote_expand'+id).html('-');
	}
	function closeMessageQuote(id) {
		$('#quote_'+id).hide();
		$('#msg_quote_expand'+id).html('+');
	}
	
	$(".msg_quote_expand").click(function(e){ toggleMessageQuote($(this).attr('name')); });
	$(".msg_quote_expand_all").click(function(e){
		var obj = $(this);
		if($(this).is(":contains('++')")) {
			$(".msg_quote_content").each(function(i) {
				//$(this).show();
				id = $(this).attr('name');
				openMessageQuote(id);
				$(obj).html('--');
			})
		} else {
			$(".msg_quote_content").each(function(i) {
				//$(this).hide();
				id = $(this).attr('name');
				closeMessageQuote(id);
				$(obj).html('++');
			})
		}
	});
	/* Private Messages quote functions - END */
	
	/* All elements with the class "hover" will be highlighted when the mouse hovers over them */
	$(".hover").hover(function() {
		$(this).addClass('highlight');
	}, function() {
		$(this).removeClass('highlight');
	});
	
	/* All elements with the class "corner" will recieve rounded corners */
	$(".corner").corner();
});

/**
 * Shows a tooltip. Tooltip content can be loaded via AJAX if necessary
 * Based on http://www.kriesi.at/archives/create-simple-tooltips-with-css-and-jquery-part-2-smart-tooltips
 *
 * target		The element which triggers to show the tooltip
 * name			Unique name of this tooltip
 * html			Which HTML to show (set to FALSE if HTML is loaded via AJAX)
 * action		Url to run for AJAX command (final url will look like 'ajax.php?ACTION') The response will be shown in the tooltip
 * width		Width of the tooltip box in pixels. If no width is specified, the default width of 450 will be used.
 */
function goalll_tooltip(target, name, html, action, width) {
	var $TooltipName = "tooltip_"+name;
	var $Action = action;
	var $Html = html;
	var $Width = width;
	if($Width == null) { $Width = 450; }
	
	$("body").append('<div class="tooltip" id="'+$TooltipName+'" style="width: '+$Width+'px;"><img src="images/icons/ajax-loader.gif" /></div>');
	var $Tooltip = $("#"+$TooltipName);
	
	$(target).mouseover(function(){
			var $this = $Tooltip;
			
			if(!$this.hasClass('tooltip_loaded')) {
				$this.addClass('tooltip_loaded');
				if($Action != false) {
					$.get("ajax.php?"+$Action, {}, 
						function(result_html) {
							$this.html(result_html);
						}
					);
				} else {
					$this.html($Html);
				}
				$this.css({opacity: 1, display:"none"}).fadeIn(400);
			} else {
				$this.show();
			}
	}).mousemove(function(kmouse){
			var $this = $Tooltip;
			var border_top = $(window).scrollTop();
			var border_right = $(window).width();
			var left_pos;
			var top_pos;
			var offset = 15;
			if(border_right - (offset * 2) >= $this.width() + kmouse.pageX){
				left_pos = kmouse.pageX+offset;
			} else{
				left_pos = border_right-$this.width()-offset;
			}

			if(border_top + (offset *2)>= kmouse.pageY - $this.height()){
				top_pos = border_top +offset;
			} else{
				top_pos = kmouse.pageY-$this.height()-offset;
			}	

			$this.css({left:left_pos, top:top_pos});
	}).mouseout(function(){
			$Tooltip.css({left:"-9999px"});
	});
}
