$(document).ready(function(){
	// Adiciona sempre uma classe na tag sendo no primeiro e no ultimo elemento. Isso é bom para remover espaco e background do elemento desejado.
	$('body *:first-child').addClass('first-child');
	$('body *:last-child').addClass('last-child');

	//fix void <p> from NAS 4
	$("#BoxText p:eq(0) form:eq(0)").appendTo("#BoxTex");
	if ($("#BoxText p:eq(0) form:eq(0)").length > 0) {
		if ($("#BoxText p:eq(0)").html().length == 0) {
			$("#BoxText p:eq(0)").remove();
		}
	}
	if ($(".divCol2").length > 0) {
		if ($(".divCol2:eq(0) p:eq(0)").html().length == 0) {
			$(".divCol2:eq(0) p:eq(0)").remove();
		}
	}
	
	
	$("#btImprima").click(function(){
		self.print();	
	});
	
	$.ajaxSetup({
		url: "/modules/api/proxy.asp",
		cache: true,
		type: "POST",
		error:function(x,e){
			if(x.status==0){
				msgError = "You are offline!!\n Please Check Your Network.";
			}else if(x.status==404){
				msgError = "Requested URL not found.";
			}else if(x.status==500){
				msgError = "Internel Server Error.";
			}else if(e=='parsererror'){
				msgError = "Error.\nParsing JSON Request failed.";
			}else if(e=='timeout'){
				msgError = "Request Time out.";
			}else {
				msgError = "Unknow Error.\n"+x.responseText;
			}
			console.log(msgError);
		}
	});
	

	// Input pegando o valor do title e jogando dentro do inut
	if ($(".increaseFont").length > 0) {
		$.getScript('/templates/samarco_201011/js/jquery.fontSize.js', function(){
			//TODO: selecionando pela classe, + abrangente, + processamento
			//pelo ID, um único lugar será consultado
			$.fontSize($('#BoxText'));
		});
	}
	
	
	// Input pegando o valor do title e jogando dentro do inut
	$.fn.inputTitle = function() {
		var _t = $(this);
		_t.focus(function() {
			var bValue = $(this).val();
			if (bValue == $(this).attr("title")) {
				$(this).val("");
			}
		}).blur(function(){
			var bValue = $(this).val();
			if (bValue == "") {
				$(this).val($(this).attr("title"));
				return false;
			}
		});
	}
	$('#msgInpuBusca').inputTitle();
	
	
	function mycarousel_initCallback(carousel) {
		$('.jcarousel-control a').bind('click', function() {
			carousel.scroll($.jcarousel.intval($(this).attr("nrItem")));
			$(".jcarousel-control a").css("background-position","35px");
			if ($(this).css("background-position") == 0) {
				$('.jcarousel-control a:first').css("background-position","35px");
			} else {
				$(this).css("background-position","0px");
			}			
			return false;
		});
	};
	

	// JCAROUSEL
	//consumir conteúdo
	//alert($("#destaquesHome").attr("bID"));
	$.ajax({
		data: ({
			"apiCall" : "jcarousel",
			"b" : $("#destaquesHome").attr("bID")
		}),
		success: function(r) {
			$("#destaquesHome").html(r);

			$("#mycarousel li").each(function(i){
				if (i % 3 == 0) {
					i++;
					xObj = $("<a href='#"+i+"' nrItem="+i+"></a>");
					$("#jCCtrl").append(xObj);
				}
			});
			
			$.getScript('/templates/samarco_201011/js/jquery.jcarousel.min.js', function() {
				$('#mycarousel').jcarousel({
					scroll: 1,
					itemLoadCallback: mycarousel_initCallback
				});
				$("#destaquesHome li:last").css("padding","0px");
				$('.jcarousel-control a:first').css("background-position","0px");
			});
			
			
			$.getScript('/templates/samarco_201011/js/DD_roundies_0.0.2a-min.js', function() {
				if ($(".boxCurv").length > 0) {
					DD_roundies.addRule('.boxCurv', '5px', true);
				}
				
				if ($(".boxCurv1").length > 0) {
					DD_roundies.addRule('.boxCurv1', '5px', true);
				}
				
				if ($(".boxCurv3").length > 0) {
					DD_roundies.addRule('.boxCurv3', '10px', true);
				}
			});
			
			$("#destaquesHome").show();
		},
		dataType: "html"
	});
	
	
	np = $(".newsPag");
	if (np.length > 0) {
		np.click(function(){
			a = $(this).attr("a");			
			n = parseInt($("#aN").html());
			if (a == "n") {
				n++;
			} else {
				n--;
			}
			if (n >= 1 && n <= 5) {
				$("#aN").html("0"+n);
				$.fn.getNews(n);
			}
		});
	}

	$.fn.getNews = function(n){
		$.ajax({
			data: ({
				"apiCall" : "getNews",
				a : n,
				b : $("#modNoticias").attr("b")
			}),
			success: function(r) {
				$(".divBoxNoticias").remove();
				$(".divModNoticias").append(r);
			},
			dataType: "html"
		});
	}
	
	//por último o mapa do site
	$('#btMapaSite').click(function() {
		//if ($("#wrapper #MapaDoSite").length == 1) {
			$("#MapaDoSite").slideToggle('normal',function(){
				//fazer scroll?
			});		
		//} else {
		//}
	});
	$.ajax({
		data: ({
			"apiCall" : "MapaDoSite"
		}),
		success: function(r) {
			if ($("#MapaDoSite .divBoxItens").length == 0) {
				$("#wrapper").append(r);
			}
		},
		dataType: "html"
	});
	
	
	if ($(".notes").length > 0) {
		var x = setTimeout("$('.notes').slideUp('slow',function(){$(this).remove();})",4000);
		delete x;
	}
	
	
	if ($("#btSCFSubmit").length > 0) {
		$("#btSCFSubmit").click(function(){
			$("#btSubmitNAS").click();
		});
	}
	
	if ($("#boxNasLogin").length > 0) {
		$(".actionLogin").click(function(){
			$("#caller").val($(this).attr("a"));
			$("#frmNasLogin").submit();
		});
	}
	
	if ($('#mwTimeline').length > 0) {
		$.getScript('/modules/mwTimeline/mwTimeline.js', function() {
			
		});
	}
	
});

function resizeFrame(obj,h) {
	try {
		obj.style.height = obj.contentWindow.document.body.scrollHeight + "px";
	} catch(e) {
		obj.style.height = h;
	}
}


