// JavaScript Global
/***************************************************
Este Javascript está aplicado em todas as páginas 
do site. 
Mexa apenas se souber o que esta fazendo.

Antes de alterar, compreenda a estrutura e altere 
o seletor pontualmente.

Para alteracoes pontuais em uma secao,
consulte os javascripts individuais dos modulos.
 
Modifique este arquivo apenas em caso de surgimento
de novas secoes e estruturas visuais que terao ou
ou poderão ter impacto em TODAS as paginas.

Nunca desenvolva codigos extensos na funcao de load,
para codigos maiores que 3 (tres) linhas, crie uma
funcao a parte e faca a chamada no load.

Nao utilize acentuacao neste arquivo.
****************************************************/

$(function(){

	try {
	
		//pngs com transparencia no IE 6
		$("div.fandangoEsquerda, div.fandangoDireita, div.chamada, span.play").ifixpng();
		
		estilizaSelects();
		bindOpenModal();
		
		var pageTracker = {
			_trackPageview: function(){
			}
		};
	} 
	catch (e) {
		alert('erro');
	}



}); 


function estilizaSelects(){
	$('#sHotsites, #sBusca').sSelect();	
}

function loadTooltipMercado(a){

	/* defaults
	 container: container do tooltip
	 file: include do tooltip, expressão jquery ou objeto
	 fileParameters: parametros para o include, pode ser uma query string ou um objeto
	 trigger: botão ou link que vai disparar o tooltip, expressão ou objeto
	 tooltip: maneira de identificar o tootip, expressão jquery ou objeto
	 */
	var defaults = {
		container: "#conteudo",
		file: "../global/inc/tooltipMercados.jsp",
		fileParameters: {},
		trigger: "a.compartilhar",
		tooltip: ".tooltip"
	};
	
	a = a ? a : {};
	
	a = $.extend(defaults, a);
	a = $.extend(a, {
		container: $(a.container)
	})
	
	if ($(a.tooltip).length <= 0) {
		$.ajax({
			url: a.file,
			data: a.fileParameters,
			dataType: "html",
			success: function(resp){
				a.container.append(resp);
				bindTooltip(a);
			}
		});
	}
	else {
		bindTooltip(a);
	}
	
}

function bindTooltip(a){
		a = $.extend(a, {trigger : $(a.trigger), tooltip : $(a.tooltip)})
		
		a.trigger.mouseover(function(){
			a.tooltip.fadeIn();
		});
		
		a.tooltip.mouseleave(function(){
			a.tooltip.fadeOut();
		})
		
		a.tooltip.find("a").click(function(){ a.tooltip.fadeOut();});
		

}

function criaModal(attr){

	var defs = {
		id: "",
		classe: "modal"
	};
	
	
	attr = $.extend(defs, attr);
	
	attr.idStr = attr.id == "" ? "" : "#" + attr.id;
	attr.classStr = "." + attr.classe;
	
	var str = attr.idStr + attr.classStr;
	
	
	if ($(str).length <= 0) {
		$("body").append("<div id=\"" + attr.id + "\" class=\"" + attr.classe + "\"><a class=\"btFechar\">X</a></div>")
	}
	
	$(str).children("a.btFechar").click(function(){
		$(str).modalToggle();
	});
}

function bindOpenModal(operador){

	operador = operador == null ? ".openModal" : operador;
	
	$(operador).unbind().click(function(){
		
		criaModal({
			id: "modalOpened"
		})
		
		var $this = $(this), html = "", modal = $("#modalOpened");
		
		if ($this.find("span.button.play").length > 0) {
			html = "<span class=\"xf videoPlayer\" src=\"" + $this.attr("href") + "\"></span>";
			$("#modalOpened").addClass('modalVideo');
		}
		else {
			$("#modalOpened").removeClass('modalVideo');
			html = new Image();
			html.src = $this.attr("href");
			
			$("#conteudo").append("<div id=\"preloadImages\" style=\"overflow: hidden; width: 1px; height: 1px;\"><img src=\"" + $this.attr("href") + "\" alt=\"\" /></div>");
		}
		
		modal.append(html);
		
		$("#preloadImages").remove();
		
		if ($this.find("span.button.play").length > 0) {
			XFlash.apply($("#modalGaleria"));
		}
		
		modal.css("marginLeft", ((modal.width() / 2) * (-1))).modalToggle({
			callbackOut: function(){
				modal.children(":not(:first)").fadeOut(function(){
					modal.children(":not(:first)").remove();
				});
			}
		});
		
		
		
		
		
		var intervalo = window.setInterval(exibirmodals, 100);
		function exibirmodals(){
		
			//modal.css("position","absolute");
			
			//modal.css("top",window.screen.height/2 - document.getElementById('modalOpened').offsetHeight/2);
			
			modal.css("margin-top", -document.getElementById('modalOpened').offsetHeight / 4);
			modal.css("margin-left", -document.getElementById('modalOpened').offsetWidth / 2);

			clearInterval(intervalo);
		}
		
		
		if ($.browser.msie && parseInt($.browser.version) < 7) {
			scrollTo(0, 0);
		}


		//Trecho de código adicionado para não permitir que o tamanho da modal exceda 800x600 pixels.
		double:width = $("#modalOpened").children("img:eq(0)").attr("width");
		double:height = $("#modalOpened").children("img:eq(0)").attr("height");
		
		width = getWidth(width);
		height = getHeight(height);  

		double:width = $("#modalOpened").children("img:eq(0)").attr("width",width);
		double:height = $("#modalOpened").children("img:eq(0)").attr("height",height);
		//Trecho de código adicionado para não permitir que o tamanho da modal exceda 800x600 pixels.


		return false;
	});
	
}


/**
 * Função quer retorna a largura de 800px se a largura passada
 * for maior que 800. Senão, retorna a largura passada como parâmetro.
 * @param {Object} width
 */
function getWidth(width){
	if(width > 800){
		return 800;
	}else{
		return width;
	}
}

/**
 * Função quer retorna a altura de 600px se a altura passada
 * for maior que 600. Senão, retorna a altura passada como parâmetro.
 * @param {Object} height
 */
function getHeight(height){
	
	if(height>600){
		return 600;
	}else{
		return height;
	}
}



$.fn.doDefaultString = function(){
	
	var $this = $(this),
	word = $this.val();
	
	$this.focus(function(){
		$this.val() == word ? $this.val("") : false;
	});

	$this.blur(function(){
		$this.val() == "" ? $this.val(word) : false;
	});

}
