function _(url) {
	if($.browser.msie && parseInt($.browser.version) < 8) {
		var date = new Date();
		
		if(url.indexOf("?") != -1) {
			url += "&";
		} else {
			url += "?";
		}
		
		url += "_cache=" + date.toGMTString().replace(/[^\d]/g, "");
	}
	
	return url;
}

function getAction(url, params) {
	$("#direita").html("Aguarde...");
	
	if(!params) {
		params = {};
	}
	
	$.ajax({
		url: __site + _(url),
		type: "post",
		data: params,
		success: function (e) {
			$("#direita").html(e);
		},
		
		error: function (xhr, e) {
			$("#direita").html("OPS! Ocorreu um erro! <pre>" + xhr.responseText + "</pre>");
		}
	});
}

function createTimer(h, m, s, t) {
	var _t = setInterval(function () {
		s--;
		
		if(s <= 0 && m <= 0 && h <= 0) {
			clearInterval(_t);
			
			location.reload();
			return;
		}
		
		if(s <= 0) {
			s = 59;
			m--;
			
			if(m <= 0 && h > 0) {
				h--;
				m = 59;
			}
		}
		
		$("#" + t).html(
			(h < 10 ? "0" + h : h) + ":" + (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s)
		);
	}, 1000);
}

function expBarValue(obj, val, mx, text, effect) {
	obj = $(obj);
	
	mx = parseInt(mx);
	
	var _nw = ($(obj).width() / mx) * val;
	var _ow = $(obj).width();
	
	if(_nw > _ow) {
		_nw = _ow;
	}
	
	if(effect) {
		if(val >= (mx / 2)) {
			color = "#0066CC";
		} else if(val >= (mx / 3)) {
			color = "#FF9900";
		} else if(val < (mx / 3)) {
			color = "#FF0000";
		}
		
		//obj.css("background-color", color);
		obj.animate({backgroundColor: color});
	}
	
	$("#b", obj).animate({width: _nw});
	$("#t", obj).html(text);
}

$(document).ready(function () {
	$("#f-login-email").focus(function () {
		if($(this).val() == "digite seu email") {
			$(this).val("");
		}
	}).blur(function () {
		if(!$(this).val()) {
			$(this).val("digite seu email");
		}
	});

	$("#f-login-password-fake").focus(function () {
		$(this).hide();
		$("#f-login-password").show().focus();
	})
	
	$("#f-login-password").blur(function () {
		if(!$(this).val()) {
			$(this).hide();
			$("#f-login-password-fake").show();
		}
	});
});

function updateTooltips() {
	$("div.tooltip").each(function () {
		var t = $(this);
		
		if(t.attr("is_tooltip")) {
			return;
		} else {
			t.attr("is_tooltip", 1);
		}
		
		$("#" + t.attr("title"))
			.mouseover(function () {
				t.show("fast");
			})
			.mouseout(function () {
				t.hide("fast");
			});
	});
}

var __isInventoryShown = false;
var __inventoryContainer = null;
var __inventoryBlockContainer = null;
var __blockInventory = false;

function doInventory() {
	if(__blockInventory) {
		jalert('Inventário não disponível no momento');
		
		return;
	}
	
	if(!__inventoryContainer) {
		__inventoryContainer = $(document.createElement("DIV"));
		__inventoryBlockContainer = $(document.createElement("DIV"));
	}
	
	if(__isInventoryShown) {
		$(__inventoryContainer).remove();
		$(__inventoryBlockContainer).remove();

		$(document.body).css("overflow", "auto");	
		
		__isInventoryShown = false;
	} else {
		$.ajax({
			url: __site + "inventory/index",
			type: "post",
			success: function (e) {
				__inventoryContainer.html(e);			
			}
		});
		
		__inventoryContainer
			.html("Aguarde...")
			.addClass("d-inventory-container");
		
		$(window).resize(function () {
			inventoryUpdatePos();
		});
		
		$(document.body).css("overflow", "hidden");
		
		$(document.body).append(__inventoryContainer);
		$(document.body).append(__inventoryBlockContainer);
		
		__isInventoryShown = true;

		__inventoryBlockContainer
			.addClass("d-inventory-block-container")
			.animate({opacity: .7});
		
		inventoryUpdatePos();
	}
}

function inventoryUpdatePos() {
	if(__isInventoryShown) {
		__inventoryContainer.css("left", $(document.body).width() / 2 - __inventoryContainer.width() / 2).css("z-index", 100);
	}
}

function inventoryBuy(i, t, f) {
	$("#h-item", $("#" + f)).val(i);
	$("#h-total", $("#" + f)).val($('#' + t).val());
	
	getAction("inventory/buy", $("#" + f).serialize());
}

function inventorySell() {
	
}

function jalert(m, t, f) {
	if(!t) {
		t = "Aviso!";
	}
	
	var d = $(document.createElement("DIV"));
	
	d.html(m);
	
	$(document.body).append(d);
	
	d.dialog({
		modal: true,
		width: 300,
		title: t,
		close: function () {
			d.remove();
			
			if(f) {
				f.apply();
			}
		},
		buttons: {
			"Fechar": function () {
				d.remove();
			
				if(f) {
					f.apply();
				}
			}
		}
	});
}

function jconfirm(m, t, k, c) {
	if(!t) {
		t = "Aviso";
	}

	var d = $(document.createElement("DIV"));
	
	d.html(m);
	
	$(document.body).append(d);
	
	d.dialog({
		modal: true,
		width: 300,
		title: t,
		close: function () {
			try {
				c.apply([]);
			} catch(ee) {}
			
			d.remove();
		},
		buttons: {
			"Cancelar": function () {
				try {
					c.apply([]);
				} catch(ee) {}
				
				d.remove();
			},
			"Ok": function () {
				try {
					k.apply([]);
				} catch(ee) {}
				
				d.remove();				
			}
		}
	});	
}

$("button.ui-button, .toolbar a").live('mousemove', function () {
	$(this).addClass("ui-state-hover");
}).live('mouseout', function () {
	$(this).removeClass("ui-state-hover");
}); 

$(window).scroll(function (event) {
						   
	if($(window).scrollTop() > 340) {
		$("#pIcones").stop().animate({top: $(window).scrollTop() - 200 }, 500);
		
		//$("#pIcones").css("top", $(window).scrollTop() - 200);
	} else {
		$("#pIcones").stop().animate({top: 120 }, 500);
	}
});

$(document).ready(function () {
	$("#pIcones .i, #pIcones .i2").css("opacity", .2);
});

$("#pIcones .i").live('mouseover', function (event) {
	$(this).css("opacity", 1);
	
	$(".t", $(this)).show().css("opacity", .8); //.bg(10);
})
			.live('mouseout', function (event) {
	$(this).css("opacity", .2);

	$(".t", $(this)).hide();
	$(".invetoryDetailPopup").remove();
});


//social

function sendOrkut( title, url ) {
	window.open('http://promote.orkut.com/preview?nt=orkut.com&tt='+ encodeURIComponent(title) +'&du='+ encodeURIComponent(url),'windowOrkut', "width=650,height=500");
}

function sendTwitter( title, url ) {
	var targetUrl = 'http://twitter.com/share?url=' + encodeURIComponent(url) +'&text='+ encodeURIComponent(title + ' em ') +'&via=narutogame';
	window.open(targetUrl, 'ptm', 'height=450,width=650').focus();
}

function sendFacebook( title, url ) {
	var targetUrl = 'http://www.facebook.com/share.php?t='+ encodeURIComponent(title) +'&u='+ encodeURIComponent(url);
	window.open(targetUrl, 'ptm', 'height=450,width=600').focus();
}
