$.ajaxSetup({ scriptCharset: "utf-8" , contentType: "application/x-www-form-urlencoded; charset=iso-8859-1"});

var NULL = null;

var _iMsgCallbackI = null;
var _iMsgCallbackState;

function _iMsgCallback() {
	_iMsgCallbackI = setInterval(function () {
		if(_iMsgCallbackState) {
			$("#iMsg").animate({opacity: .4}, 400);			
		} else {
			$("#iMsg").animate({opacity: 1}, 400);
		}
		
		_iMsgCallbackState = !_iMsgCallbackState;
	}, 1000);
}

function setPValue(val, mx, desc, obj, al) {
	val = val < 0 ? 0 : val;
	
	if(al) {
		if(val <= (mx / 4)) {
			$(".progFill", $(obj)).css("background-color", "#FF3300");
		} else if (val <= (mx / 2)) {
			$(".progFill", $(obj)).css("background-color", "#FF9900");
		}
	}
	
	$(".progFill", $(obj)).animate(
		{width: (($(obj).width() / mx) * val)}, 500
	);
	
	$(".progText", $(obj)).html(desc + (desc ? ":&nbsp;" : "") + val);
}

function setPValue2(val, mx, desc, obj, al) {
	val = val < 0 ? 0 : val;
	
	if(al) {
		if(val <= (mx / 4)) {
			$("#p", $(obj)).css("background-color", "#FF3300");
		} else if (val <= (mx / 2)) {
			$("#p", $(obj)).css("background-color", "#FF9900");
		} else {
			$("#p", $(obj)).css("background-color", "#537F3D");		
		}
	}
	
	var _nw = ($(obj).width() / mx) * val;
	var _ow = $(obj).width();
	
	$("#p", $(obj)).animate(
		{width: (_nw > _ow ? _ow : _nw)}, 500
	);
	
	$("#t", $(obj)).html(desc + (desc ? ":&nbsp;" : "") + val);
}

function isBrowserClosed(e) {
	if(navigator.userAgent.indexOf("MSIE") != -1) { // IE
		var browserWindowWidth = 0;
		var browserWindowHeight = 0;
	
		// gets the width and height of the browser window
		if (parseInt(navigator.appVersion) > 3) {
			if (navigator.appName == "Netscape") {
				browserWindowWidth = window.innerWidth;
				browserWindowHeight = window.innerHeight;
			}
	
			if (navigator.appName.indexOf("Microsoft") !=- 1) {
				browserWindowWidth = top.window.document.body.offsetWidth;
				browserWindowHeight = top.window.document.body.offsetHeight;
			}
		}
	
		var r = (event.clientY < 0 && event.screenX > (browserWindowWidth - 25)) ? true : false;
		
		if(!r) {
			if(window.event.clientY < 0) {
				return true;
			} else {
				return false; // WARN: CTRL + W goes here	
			}
		} else {
			return r;
		}
	} else { // FF
		if(!window.innerWidth) {
			return true;	
		} else {
			return false;	
		}
	}
}

function logoff() {
	if(typeof(_isPVP) == "undefined") _isPVP = false;
	
	if(_isPVP) {
		$.ajax({
			url: 'index.php?acao=dojo_batalha_lutar',
			type: 'post',
			data: {action: 2},
			dataType: 'script',
			async: false,
			success: function () {
				location.href = "?acao=logoff";
			},
			error: function () {
				location.href = "?acao=logoff";
			}
		});	
	} else {
		location.href = "?acao=logoff";
	}
}

function sairVila() {
	$.ajax({
		url: '?acao=mapa_vila&e',
		type: 'get',
		dataType: 'script',
		async: false
	});
}

function mapaVila() {
	$.ajax({
		url: '?acao=mapa_vila&v',
		type: 'get',
		dataType: 'script',
		async: false
	});
}

function updateTooltips() {
	$("div.ex_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");
			});
	});
}

function ieGoto(url) {
 var a = document.createElement("a");
 if(!a.click) { //only IE has this (at the moment);
  window.location = url;
  return;
 }
 a.setAttribute("href", url);
 a.style.display = "none";
 $("body").append(a); //prototype shortcut
 a.click();
 }

$(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();
});

var ___timers = [];
function createTimer(h, m, s, t, f, identifier) {
	var _t = setInterval(function () {
		s--;
		
		if(s <= 0 && m <= 0 && h <= 0) {
			clearInterval(_t);
			
			if(!f) {			
				location.reload();
				return;
			} else {
				f.apply();
			}
		}
		
		if(s <= 0) {
			s = 59;
			m--;
			
			if(m <= 0 && h > 0) {
				h--;
				m = 59;
			}
		}
		
		if(t instanceof Array) {
			for(var ii in t) {
				$("#" + t[ii]).html(
					(h < 10 ? "0" + h : h) + ":" + (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s)
				);
			}
		} else {
			$("#" + t).html(
				(h < 10 ? "0" + h : h) + ":" + (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s)
			);
		}
	}, 1000);
	
	if(!identifier) {
		___timers.push(_t);	
	} else {
		___timers[identifier]	= _t;
	}
}

function clearTimer(id) {
	clearInterval(___timers[id]);
}

function clearTimers() {
	for(var i in ___timers) {
		clearInterval(___timers[i]);
	}
	
	___timers = [];
}

function jalert(m, t, k) {
	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();

			try {
				k.apply([]);
			} catch(ee) {}
		},
		buttons: {
			"Fechar": function () {
				d.remove();

				try {
					k.apply([]);
				} catch(ee) {}
			}
		}
	});
}

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();				
			}
		}
	});	
}

function is_flash_enabled() {
	if(!navigator.plugins.length) {
		try {
			var oFlash = eval("new ActiveXObject('ShockwaveFlash.ShockwaveFlash.9');");
			
			return oFlash ? true : false;		
		} catch(e) {
			return false;
		}
	}

	for(var i = 0; i < navigator.plugins.length; i++) {
		if(navigator.plugins.item(i).name.toLowerCase() == 'shockwave flash') {
			return true;
		}
	}
	return false;
}



function cronTimer(h, m, s, _h, _m, t) {
	var _t = setInterval(function () {
		s--;
		
		if(s <= 0 && m <= 0 && h <= 0) {
			//clearInterval(_t);
			s = 0;
			h = _h;
			m = _m;
		}
		
		if(s <= 0) {
			s = 59;
			m--;
			
			if(m <= 0 && h > 0) {
				h--;
				m = 59;
			}
		}
		
		if(t instanceof Array) {
			for(var ii in t) {
				$("#" + t[ii]).html(
					(h < 10 ? "0" + h : h) + ":" + (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s)
				);
			}
		} else {
			$("#" + t).html(
				(h < 10 ? "0" + h : h) + ":" + (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s)
			);
		}
	}, 1000);
}

function percent(p, v) {
	return Math.round(v * (p / 100));
}

function percentf(p, v) {
	return v * (p / 100);
}

function precision(v, p) {
	p = p || 2;
	v = v.toString().split('.');
	
	if(v.length > 1) {
		return v[0] + '.' + v[1].substr(0, p);
	} else {
		return v[0];
	}
}

function str_pad(s, l, c, p) {
	s = s.toString();
	c = c.toString();

	if(s.length < l) {
		for(var i = s.length; i < l; i++) {
			if(p == 0) {
				s += c;
			} else {
				s = c + s;
			}
		}
		
		return s;
	} else {
		return s;
	}
}

$(function(){
	$("#ajuda").click(function(){
		$("#msg_help").toggle("slow");
	});
});
