//Retorna o objeto
function get(id) { 
	try { 
		var a = document.getElementById(id); 
	} catch(e) { 
		alert("Erro encontrado: "+e); 		
	} 
	return 	a;
}

function addFlash(file, width, height, id, wmode, version,flashvars) 
{
	var params = {'wmode':wmode};
	var attributes = {'id':id};	
	swfobject.embedSWF(file, id, width, height, version,"site/js/expressInstall.swf", flashvars, params, attributes)
}

function valueField (event, obj) {
	
	e = event.type;
	
	var obj = get(obj);
	
	if(!obj.save)
	{
		obj.save = obj.value;		
	}
		
	if(e == "blur")
	{
		if(obj.value == "")
		{
			obj.value = obj.save;
		}		
		
		if($.trim(obj.id) == 'Telefone')
		{
			$('#' + obj.id).unmask();
		}
		
		if($.trim(obj.id) == 'data')
		{
			$('#' + obj.id).unmask();
		}
		
		if($.trim(obj.id) == 'CPF')
		{
			$('#' + obj.id).unmask();
		}
		
		if($.trim(obj.id) == 'cep')
		{
			$('#' + obj.id).unmask();
		}
	}
	else if(e == "focus")
	{
		if(obj.value == "")
		{
			obj.value = obj.save;
		}
		else if(obj.value == obj.save)
		{
			obj.value = "";
		}
		
		if($.trim(obj.id) == 'Telefone')
		{
			$('#' + obj.id).mask('(99) 9999-9999');
		}
		
		if($.trim(obj.id) == 'data')
		{
			$('#' + obj.id).mask('99/99/9999');
		}
		
		if($.trim(obj.id) == 'CPF')
		{
			$('#' + obj.id).mask('999.999.999-99');
		}
		
		if($.trim(obj.id) == 'cep')
		{
			$('#' + obj.id).mask('99999-999');
		}
	}
}

function in_array(needle,haystack) {
	return new RegExp('(^|\,)'+needle+'(\,|$)','gi').test(haystack);
}

function animMenu(id,num,largura)
{
	obj = "#" + id + num;
	
	$(obj).each(function(i)
	{
		
		if(!jQuery.browser.msie)
		{
			$(this).hover(
				function()
				{
					$(this).stop().animate({ width: largura + 30 }, 300);
				},		
				function()
				{
					$(this).stop().animate({ width: largura }, 400);
				}
			);
		}else{
			if(jQuery.browser.version < 7)
			{
				/*
				$(this).hover(
					function()
					{
						$(this).stop().animate({ width: largura + 30 }, 500,"easeInOutSine");
					},		
					function()
					{
						$(this).stop().animate({ width: largura }, 500,"easeInOutSine");
					}
				);
				*/
			}else{
				$(this).hover(
					function()
					{
						$(this).stop().animate({ width: largura + 30 }, 300);
					},		
					function()
					{
						$(this).stop().animate({ width: largura }, 400);
					}
				);
			}
		}		
		
	});
}

function validate(label, id, mensage)
{
	jQuery.validator.addMethod(id, function(value, element){
		
		if(value == label)
		{
			return false;
		}
		
		return true;
	}, mensage);
}

jQuery.validator.addMethod("cpf", function(value, element) {
	value = value.replace('.','');
	value = value.replace('.','');
	cpf = value.replace('-','');
	while(cpf.length < 11) cpf = "0"+ cpf;
	var expReg = /^0+$|^1+$|^2+$|^3+$|^4+$|^5+$|^6+$|^7+$|^8+$|^9+$/;
	var a = [];
	var b = new Number;
	var c = 11;
	for (i=0; i<11; i++){
		a[i] = cpf.charAt(i);
		if (i < 9) b += (a[i] * --c);
	}
	if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
	b = 0;
	c = 11;
	for (y=0; y<10; y++) b += (a[y] * c--);
	if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
	if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10]) || cpf.match(expReg)) return false;
	return true;
}, "Número de CPF inválido.<br>");

var Utils = {
	abrir: true,

	pop:function(pagina, janela, width, height) {
	   var left = (screen.width - width)/2;
	   var top = (screen.height - height)/2;;
	   var janela = window.open(pagina,janela, 'width='+width+', height='+height+', top='+top+', left='+left+', scrollbars=yes, status=no, toolbar=no, location=no, directories=no, menubar=no, resizable=no, fullscreen=no');
	   janela.focus();
	},

	getPageSize:function() {
		var xScroll, yScroll;
		if (window.innerHeight && window.scrollMaxY) {
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth;
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else {
			pageHeight = yScroll;
		}
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){
			pageWidth = xScroll;
		} else {
			pageWidth = windowWidth;
		}
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
		return arrayPageSize;
	},

	getPageScroll:function () {
		var xScroll, yScroll;
		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
			xScroll = self.pageXOffset;
		} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
			xScroll = document.documentElement.scrollLeft;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
			xScroll = document.body.scrollLeft;
		}
		arrayPageScroll = new Array(xScroll,yScroll);
		return arrayPageScroll;
	},

	resizeWindow:function(width, height, mode) {
		var arrPageSizes = Utils.getPageSize();
		var arrPageScroll = Utils.getPageScroll();

		if(!mode) { mode = 'fast'; }

		$('#utils-window-content,#utils-window-content-ajax').animate({
			width: width,
			height: height
		}, mode);
	},

	openWindow:function(pagina, params, wWindow, hWindow) {

		Utils.abrir = true;

		if(!params) { params = {}; }
		if(!wWindow) { wWindow = 500; }
		if(!hWindow) { hWindow = 450; }

		if($('#utils-window').length > 0) {
			$('#utils-window').remove();
		}
		$('body').append('<div id="utils-window"><div id="utils-window-background"></div><div id="utils-window-content"><div id="utils-window-content-bg"><div id="utils-window-close">[x] Fechar&nbsp;&nbsp;</div><div id="utils-window-loading"><img src="site/images/loading2.gif"/></div><div id="utils-window-content-ajax"></div></div></div></div>');

		var arrPageSizes = Utils.getPageSize();

		$('#utils-window-background').css({
			opacity:'0.5',
			display:'block',
			width: arrPageSizes[0],
			height: $(document).height()
		});

		$('#utils-window-content-ajax').css({
			width: wWindow,
			height: hWindow
		});

		var arrPageScroll = Utils.getPageScroll();
		var heightContentWindow = $('#utils-window-content').height()/2;
		var widthContentWindow = $('#utils-window-content').width()/2;

		$('#utils-window-content').css({
			top: (arrPageSizes[3]/2 + arrPageScroll[1]) - heightContentWindow,
			left: (arrPageSizes[2]/2 + arrPageScroll[0]) - widthContentWindow
		});

		$('#utils-window-background').fadeIn();
		setTimeout( function() { $('#utils-window-content').fadeIn(); }, 100);
		setTimeout(
			function() {
				$('#utils-window-content-ajax').load(pagina, params, function() {
					if(Utils.abrir) {
						$('#utils-window-loading').hide();
						$('#utils-window-content').animate({
							top: (arrPageSizes[3]/2 + arrPageScroll[1]) - hWindow/ 2,
							left: (arrPageSizes[2]/2 + arrPageScroll[0]) - wWindow/2,
							width: wWindow,
							height: hWindow
						}, '', 'swing', function() { $('#utils-window-content-ajax').fadeIn(); });
					}
				});
			},
		400);
		$('#utils-window-background,#utils-window-close').click(function() { Utils.closeWindow() });
	},

	closeWindow:function() {

		Utils.abrir = false;

		$('#utils-window-content, #utils-window-background, #utils-window-content-ajax').stop();

		$('#utils-window-content').fadeOut('fast');

		setTimeout(function() { $('#utils-window-background').fadeOut('fast'); }, 150);
	}
}

function abreAtendimento ()
{
	   var width = 310;
	   var height = 475;
	   var left = (screen.width - width)/2;
	   var top = (screen.height - height)/2;
	   window.open(SITE + '/site/atendente','atendimento', 'width='+width+', height='+height+', top='+top+', left='+left+', scrollbars=no, status=no, toolbar=no, location=no, directories=no, menubar=no, resizable=no,maximized=no,titlebar=no, fullscreen=no');
}

