/**
 * Extensão das funcionalidades da jQuery para o CMS e Cliente
 * @author Jackson Caset
 */
var pathSistema = 'http://'+location.hostname+':'+location.port+'/cms';
var ico_loading = pathSistema +'/estrutura/imagens/loading/1.gif';
var pagina = 1;
var $j = jQuery.noConflict();

$j(function(){
	/* Abre links externos em nova janela */
	$j("a[rel=external], a.blank").attr('target', '_blank');
	
	/* Abre imagens em lightbox */
	if(location.href.indexOf('/cms') == -1) {
		$j('a[rel*=lightbox]').lightBox({
			overlayOpacity: 0.6, fixedNavigation:true
		});
	}
	
	/* Cria menus drop down */
	$j(".menuv li.subh, .menuh li.subv").each(function() {
		var el = $j('#'+$j(this).attr('id')+' ul:eq(0)');
		
		$j(this).hover(	function() { el.show(); }, function() { el.hide(); } );
	});	
});

(function($j) {	
	$j.bd = $j.bd || {};
	
	$j.idioma = function(sigla, idioma, cms) {
		$j.ajax({ 
			url:'/cms/cliente/utils/idiomas/index.php?alterar&lang='+sigla, cache:false,
			success:function(response) { //alert(response)
				if(cms) {
					top.frames['imodulos'].location.reload(true);
					$j.alert({
						type:'alert', html:'Apresentando informações cadastradas para o idioma: '+idioma+'!', timeout:20000, width:450
					});	
				} else {
					location.reload();
				}
			}
		});
	}
	/**
	 * Método utilizado para páginação de resultados do banco.
	 * Faz parte do objeto $j.bd do arquivo jquery.cms.db.js, porém, deve ficar neste arquivo para que se tenha acesso por parte do cliente tbm.
	 */
	$j.bd.pagReg = function(pag, acao, cont) {
		$j('#navegacao_'+cont).html('<strong>Abrindo página '+ pag +', aguarde...</strong>');
		pagina = pag;
		eval(acao);
	}
	/**
	 * Método incorporado a jQuery que substitui o padrão write do objeto document. Pode ser mais personalizado no futuro.
	 */
	$j.write = function(content) {
		document.write(content);
	}
	/**
	 * Adiciona js em documentos html
	 * @param ['arquivo1.js', 'arquivo2.js'] scripts
	 */
	$j.js = function(scripts) {		
		if(scripts.length > 0) {
			var s='';
			$j.each(scripts, function(i) {
				s += "<script type=\"text/javascript\" src=\""+scripts[i]+"\"></script>";
			});
			$j.write(s);
		}
	}
})(jQuery);

/**
 * Show an alert on the footer. This alert can be: success(green), alert(yellow), error(red), loading(blue) or a personal class
 * @param obj 	type:'success ou alert ou error ou loading', html:'Conteúdo', 
 * 				timeout:4000, hide:true ou false, out:function() {}, click:function() {}, icon:false, width:300, pulsate:true
 */
(function($j) {
	var divOverLay;
	var uniqID;
	
	$j.modal = function(act, obj) {		
		if(act == 'hide') {
			if($j(divOverLay).length > 0 && !isUndefined(divOverLay)) {
				$j(divOverLay).fadeOut('slow', function() { $j('select').show(); $j(this).remove(); });
			}
		} else {
			var bg='#000', opc=0.4;
			
			if(!isUndefined(obj)) {
				bg = isUndefined(obj.bg) ? '#000' : obj.bg;
				opc = isUndefined(obj.opacity) ? 0.4 : obj.opacity;
			}
			
			var _cssOverlay = { background:bg, opacity:opc, position:'absolute', top:0,left:0, zIndex:9, display:'none' };
			
			$window = {height: $j(window).height(),width:$j(window).width()};
			$body = {height: $j('body').height(),width:$j('body').width()};
			
			_cssOverlay.height = ($window.height > $body.height) ? $window.height : $body.height;
			_cssOverlay.width = ($window.width > $body.width) ? $window.width : $body.width;
			
			if($j.browser.msie && $j.browser.version < 7) {
				$j('select').hide(); //oculta selects no ie6 para fix bug.
			}
			
			uniqID = Math.random(1000);
			divOverLay = $j('<div id="overlay'+uniqID+'" />').css(_cssOverlay).prependTo('body');
			
			$j(divOverLay).fadeIn();	
		}
		return $j;
	}
	
	$j.fn.modal = function(act, obj) { return this.each(function() { $j.modal(act, obj); }); }
})($j);

(function($j){	
	var containers=new Array();
	
	destroy = function() { $j(containers).each(function() { $j(this).remove(); }); $j.modal('hide'); }
	
	$j.alert = function(obj) {
		if(obj.modal) {
			$j.modal('show');
		} else if(obj.modal === false) {
			$j.modal('hide');
		}
		
		var containerStyle=styleContent='', css={};
		
		containerStyle = 'position:'+($j.browser.msie && $j.browser.version < 7 ? 'absolute' : 'fixed')+';';
		containerStyle += 'z-index:10000; left:0px; width:100%; bottom:-1px; text-align: center; font-weight:bold; color:#fff; display:none';
		
		if(obj && obj != 'hide') {
			var type = !obj.type ? 'success' : obj.type;
			var html = !obj.html ? '&nbsp;' : obj.html;
			var timeout = !obj.timeout ? 4000 : obj.timeout;
			var hide = isUndefined(obj.hide) ? true : false;
			var icon = isUndefined(obj.icon) ? true : false;
			var width = isUndefined(obj.width) ? 400 : obj.width;
			var loading = !obj.loading ? $j('#alert-loading').remove() : '';
			
			var container = $j('<div id="alert-'+type+'" style="'+containerStyle+'"></div>').appendTo(document.body);
			
			styleContent = 'width:'+width+'px; padding:12px 0 12px 0; text-align:left; margin:auto; padding-left:38px; background-repeat:no-repeat; background-position:left center;';
			
			obj.title ? container.attr('title', obj.title) : null;
			
			if(type == 'success') {
				css = {borderTop:'1px solid #4E640B', background:'#74950F'};
				icon ? styleContent += 'background-image:url(/cms/estrutura/imagens/alertas/success.png);' : '';
			} else if(type == 'error') {
				css = {borderTop:'1px solid #74200E', background:'#E64826'};
				icon ? styleContent += 'background-image:url(/cms/estrutura/imagens/alertas/error.png);' : '';
			} else if(type == 'alert') {
				css = {borderTop:'1px solid #DFA200', background:'#FFCC00', color:'#333333'};
				icon ? styleContent += 'background-image:url(/cms/estrutura/imagens/alertas/alert.png);' : '';
			} else if(type == 'loading') {
				css = {borderTop:'1px solid #13439F', background:'#3366CC', color:'#ffffff'};
				icon ? styleContent += 'background-image:url(/cms/estrutura/imagens/loading/5.gif);' : '';
				html = html=='&nbsp;' ? 'Processando, aguarde...' : html;
			}
			
			if(obj.className) { container.removeClass().addClass(obj.className); }		
			if(obj.click) { container.click(obj.click).css({cursor:'pointer'}); }
			
			container.html('<div style="'+styleContent+'">'+html+'</div>').css(css);
			
			if(obj.pulsate) {
				container.fadeIn();
				for (var i=0; i < 2; i++) {
					container.animate({opacity: 0}, 800).animate({opacity: 1}, 800);
				};
			} else {
				container.slideDown();
			}
			
			hide ? setTimeout(function() { container.slideUp(); $j.modal('hide'); obj.out ? obj.out() : ''; }, timeout) : '';			
			containers.push(container);
		} else if(obj == 'hide' || obj == '0') {
			$j(containers).each(function() { $j(this).remove(); }); $j.modal('hide');
		}
		return $j;
	}
	
	$j.fn.alert = function(obj) { return this.each(function() { $j.alert(obj); }); }
})($j);

/**
 * Exibiçao de popups (Janelas do Navegador).
 * @param {url:'url-para-popup.php', width:'500px', height:'500px', scroll:1/0, resize:1/0} obj
 */
(function($j) {
	$j.popup = function(obj) {
		var url = obj.url != '' ? obj.url : 'blank';
		var width = typeof(obj.width) != 'undefined' ? obj.width : screen.width;
		var height = typeof(obj.height) != 'undefined' ? obj.height : screen.height;
		var scroll = typeof(obj.scroll) != 'undefined' ? obj.scroll : null;
		if(typeof(obj.resize) != 'undefined') {
			var resize = typeof(obj.resize) != 'undefined' ? obj.resize : null;
		} else {
			var resize = typeof(obj.resizable) != 'undefined' ? obj.resizable : null;
		}
		var left = typeof(obj.left) != 'undefined' ? obj.left : 0;
		var top = typeof(obj.top) != 'undefined' ? obj.top : 0;

		//toolbar=no, fullscreen=no, location=no, menubar=no
		return open(''+url+'', '', 'width='+width+', status=yes, height='+height+', left='+left+', top='+top+' scrollbars='+scroll+', resizable='+resize+'');
	}
	$j.fn.popup = function(obj) { return this.each(function() { $j.popup(obj); }); }
})($j);