/**
	Projeto - Projeto (CLIENTE) v: 0.1
	Developer(s):
		+ Fernando Basilio (fernando.basilio@hellointeractive.com.br)
*/

//Trim
function Trim(str){ return str.replace(/^\s+|\s+$/g,"")}

//tags do google analytics individuais;
function tagGA(str){
	str = Trim(str);
	str = str.replace(/ /g,"_");
	str = str.replace(/_>_/g,"/");
	pageTracker._trackPageview(str);
}

// copyHeight
function copyHeight(obj){
	hMax = $(obj).eq(0).height();
	for (i=0; i<$(obj).length; i++){
		if(hMax < $(obj).eq(i).height()){
			hMax = $(obj).eq(i).height();
		}
	}
	$(obj).height(hMax)
	//$(obj).css({ height: hMax });
}

function abrePopupDestaque(pagina, id) {
    openPopup('/admin/' + pagina + '/item.aspx?return=1&id=' + id, 'Destaque', 800, 535, 1, true);
}

function abrePopEnviePorEmail(srcImagem) {
    openPopup('/responsabilidade/popEnviePorEmail.aspx?srcImagem=' + srcImagem, 'EnviePorEmail', 800, 535, 1, true);
}

function abrePopIndiqueAmigo() {
    openPopup('/popIndiqueAmigo.aspx', 'IndiqueAmigo', 800, 370, 1, true);
}

function abrePopCameraAoVivo(urlCamera) {
    openPopup('/popCameraAoVivo.aspx?urlCamera='+urlCamera, 'cameraAoVivo', 370, 340, 1, true);
}

// Popup handler
/*
function openPopup(u,n,w,h,o,c){
    var l=t=18;
    if(c){
        l=(screen.availWidth-w)/2;
        t=(screen.availHeight-h)/2;
    }
    p=window.open(u,'pop_'+n,'left='+l+',top='+t+',width='+w+',height='+h+''+((o)?','+o:''));
}
*/
function openPopup(u, n, w, h, o, c) {
    var l = t = 18;
    if (c) {
        l = (screen.availWidth - w) / 2;
        t = (screen.availHeight - h) / 2;
    }
    p = window.open(u, 'pop_' + n, 'left=' + l + ',top=' + t + ',width=' + w + ',height=' + h + ',scrollbars=' + ((o) ? o : '1'));
}

function popPrint(u) {
	openPopup(u,'print',350,299,'scrollbars=1',true);
}


// centralizeElement
function centralizeElement(obj){
	var obj = "." + obj;
	if ($("body").height() > $(obj).height()) {
		$(obj).css({top: '50%', marginTop: -($(obj).height()/2) + 'px'});
	}else {
		$(obj).css({marginTop: '0px', top: '0px'});
	}
}


// validação
function checkEmail(obj) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(obj)) return true;
}

function checkBlank(obj){
	if ($(obj).val() != '') return true;
	else $(obj).focus();
}

function checkNaN(obj){
	if (isNaN($(obj).val()) == true) return true;
}

//checkAll
function checkAll(obj){
	if($(obj).attr("checked") == true){
		$(".setp2_newsletter input").attr("checked","checked");
	}else{
		$(".setp2_newsletter input").attr("checked","");
	}
}

//unCheck
function unCheck(){
	var tt = $(".setp2_newsletter input").length;
	for(i=1;i<tt;i++){
		if(!$(".setp2_newsletter input").eq(i).attr("checked") == true){
			$(".setp2_newsletter input").eq(0).attr("checked","");
			return false;
		}
	}
	if(i=tt){ $(".setp2_newsletter input").eq(0).attr("checked","checked");	}
}

//Breadcrumb
function initBreadcrumb(){
	$(".breadcrumb ul li:last").css({"background":"none","font-weight":"bold"});
}

//accordion
function initAccordion() {
	//console.log($(".content_accordion").length);

	jQuery.each($(".content_accordion"), function(index) {
		$(this).attr('title',$(this).height())
		$(this).height('0');
	});
	
	
	$(".header").click(function(){
		if($(this).next().css('display') == 'none' || $(this).next().css('height') == '0px'){
			$(".haccordion").find("h3").removeClass("on");
			$(".header").next().animate({
					height: "0px"
				},500, function(){
					$(this).hide();
			});
			$(this).addClass("on");
			$(this).next().show().animate({
				height: parseInt($(this).next().attr("title"))+5+"px"
			},500);
		}else{
			$(".haccordion").find("h3").removeClass("on");
			$(".header").next().animate({
					height: "0px"
				},500, function(){
					$(this).hide();
			});
		}
	});

}

//carrossel
function carrossel(obj, quant){
	var init = 0;
	var min = 1;
	var itensCarrossel = $(obj +" li");
	for(var i = 0; i < itensCarrossel.length; i++){
		if(i < quant) $(itensCarrossel).eq(i).show();
	}
	$('.btNextGaleria').click(function(){
		if(itensCarrossel.length > quant && min <= itensCarrossel.length){
			//alert(quant + " - " + itensCarrossel.length + " - " + min)
			$(obj + " li").eq(init++).hide();
			$(obj + " li").eq(quant++).show();
		}
		return false;
	});
	$('.btPrevGaleria').click(function(){
		if(init != 0){
			$(obj + " li").eq(--quant).hide();
			$(obj + " li").eq(--init).show();
		}
		return false;
	});
}


//carrosselVertical
function carrosselVertical(obj, quant){
	carrossel(obj, quant)
	$(obj +" li img").css('opacity','0.5');
	$(obj +" li a").mouseover(function(){
		$(this).parent().find("img").css('opacity','1');
	});
	$(obj +" li a").mouseout(function(){
		if($(this).attr("class") == "on" || $(this).parent().attr("class") == "on"){
			$(this).parent().find("img").css('opacity','1');
		}else{
			$(this).parent().find("img").css('opacity','0.5');
		}
		
	});
}

// galeria de fotos

//showButtons
function showButtons(){
	//$('.bt_prev, .bt_next').toggle();
	//$('.setasGaleria').toggle();
}

// loadImg 
function loadImg(obj,ind){
	showButtons();
	$(obj + " img").remove();
	$(obj).hide();
	$(".legenda").text(" ");
	var imgTemp = new Image();
	var urlImg = $(".list_slider li span").eq(ind).html().split("|")[0];
	var legend = $(".list_slider li span").eq(ind).html().split("|")[1];
	var credit = $(".list_slider li span").eq(ind).html().split("|")[2];
	$(imgTemp).load(function(){
		if($(imgTemp).load){
			$(imgTemp).attr("width","530").attr("height","260");
			$(obj).eq(ind).append(imgTemp).fadeIn("slow");
			$(".contagem").text(ind+1 + " de " + $(obj).length);
			$(".legenda").append(legend + "<br />" + credit);
			showButtons();	
		}
	}).error(function(){
		$(imgTemp).attr("src","img/nao_disponivel.jpg");
	}).attr("src", urlImg);
}


// changePhoto
function changePhoto(obj){
	var init = 0;
	var qtde = $(obj).length;
	$('a[rel="mudaFoto"]').click(function(){
		var orientation = $(this).attr("class");
		if(orientation == "bt_prev"){
			if(init <= qtde-1 && init != 0){
				init--;
			}else{
				init = qtde-1;
			}
		}else if(orientation == "bt_next"){
			if(init < qtde-1){
				init++;
			}else{
				init = 0;
			}
		}else{
			$(this).parent().parent().find("img").css("border-color","#999");
			$(this).find("img").css("border-color","#fff");
			init = $(this).find("img").attr("alt");
		}
		loadImg(obj,init);
		return false;
	});
	loadImg(obj,init);
}

// loadGalleriaFull
function loadGalleriaFull(id){
	$.get("/sobre/GaleriaFotosItem.aspx", { idGaleria: id } ,function(data){
		$(".galeriaInterna").empty().html(data);
	});	
}

//chamada que o flash
function flashExpand(obj){
	$(".box_cameraAoVivo").remove();
	var hFlash;
	if(obj == "open"){
		hFlash = "350px";
	}else if(obj == "close" ){
		hFlash = "92px";
	}	
	$(".boxFlashAcompanhe").animate({ 
			height: hFlash
		}, 1000
	);	
}


// handleEnter
function handleEnter(field, event) {
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13) {
		var strLocation = "/busca/Default.aspx?pesquisa=" + field.value;
		window.location = strLocation;
		return false;
	}
} 


// monteSeuRoteiro
function monteSeuRoteiro(){
	var viewportWidth =(self.innerWidth || (document.documentElement.clientWidth || (document.body.clientWidth || 0))); // Grava a largura do viewport
	var maxWidth = $("#container").width();
	var dif;
	if (viewportWidth > maxWidth){
		dif = viewportWidth-maxWidth;
		if(navigator.appName!='Microsoft Internet Explorer'){
			dif = dif -16;
		}
	}else{
		dif = 0;
	}
	var wPosition = maxWidth - $(".monte_seu_roteiro").width() + dif/2;
	$(".monte_seu_roteiro").css({ zIndex: "99999", top: "279px", left: wPosition+"px" });
	hBox = ($(".monte_seu_roteiro").height() == 0) ? "400" : "0";
	$(".monte_seu_roteiro").animate({ height: hBox+"px" }, 300 );
	return false;
}


// abas home
function showAbaHome(aba,item){
	$(".aba_home").hide();
	$(aba).show();
	$(".abas_home_noticias h3").css("opacity","1");
	$(".abas_home_noticias h3").eq(item).css("opacity","0.4");
}

//close bannerHome
function closeBanner(id) {
	$(id).remove();
	$(".box_cameraAoVivo").show();
}

//centralizar banner home
function centerBannerFull() {
	var centerTop = -300
	var centerLeft = Math.floor($("#bannerDhtml").parent().width() / 2 - $("#bannerDhtml").width() / 2);
	$("#bannerDhtml").css({ position: "absolute", left: centerLeft + "px", top: centerTop + "px", display: "block" });
}

//lightbox home
function showImagemHome(url){
	var url = '/modal.aspx?urlImg='+url
	openModal(url);
}

/*CLOSE MODAL*/
function closeModal() {
	/*HIDE MODAL*/
	$('.view_modal').fadeTo('fast', 0, function() {
		$(this).remove();
	});	
	/*HIDE BACKGROUND*/
	$('.bg_modal').fadeTo('fast', 0, function() {
		$(this).remove();
		/*SHOW SELECT*/
		$('select').css('visibility', 'visible');
	});
}

/*MODAL*/
function openModal(url) {
	/*CREATE ELEMENTS*/
	$('body').append($('<img />').attr({src: "../img/load.gif", width: 88, height: 78, alt: "", className: 'load'})).append($('<div></div>').addClass('bg_modal')).append($('<div></div>').addClass('view_modal'));
	$('.bg_modal').width($('html')[0].scrollWidth).height($('html')[0].scrollHeight);
	/*OPACITY*/
	$('.view_modal').css('opacity', 0);
	$('.bg_modal').css('opacity', 0);
	/*SELECTS HIDE*/
	$('select').css('visibility', 'hidden');
	/*SHOW BACKGROUND*/
	$('.bg_modal').fadeTo('fast', 0.2, function() {
		$('.view_modal').load(url + '?' + (new Date()).getTime(), function() {
			/*REMOVE LOAD*/
			$('.load').remove();
			/*CENTRALIZE MODAL*/
			$('.view_modal').css({marginTop: -$('.view_modal').height()/2 + 'px', marginLeft: -$('.view_modal').width()/2 + 'px'});
			/*MODAL HIDE*/
			$('.view_modal').fadeTo('fast', 1);
			/*CLOSE MODAL*/
			$("a[rel='modalclose']").click(function() {
				closeModal();
				return false;
			})
		});
	}).click(function(){
		closeModal();
		return false;
	});
}