$(document).ready(function() {
	$("#alpha").css("filter", "alpha(opacity=60)");
	$("#alpha").css("opacity", "0.6");

	$(".loading").css("filter", "alpha(opacity=60)");
	$(".loading").css("opacity", "0.6");

	//NAVEGAÇÃO left-and-right
	$(".next").click(function() {
		mouseover = true;
		//alert($(this).parent().find('div[class^="mask"]').html());
		var num = $(this).parent().find('div[class^="mask"]').width() / $(this).parent().find('div[class^="mask"] ul li').width();
		prox($(this).parent().find('div[class^="mask"]'), parseInt(num));

	});
	$(".prev").click(function() {
		mouseover = true;
		var num = $(this).parent().find('div[class^="mask"]').width() / $(this).parent().find('div[class^="mask"] ul li').width();
		ant($(this).parent().find('div[class^="mask"]'), parseInt(num));
	});
})
/* INICIO DROPDOWN PÁGINA PRINCIPAL */

var overScroll = false;

if(window.addEventListener) {
	// DOMMouseScroll is for mozilla.
	window.addEventListener('DOMMouseScroll', wheel, false);
}
// IE/Opera.
window.onmousewheel = document.onmousewheel = wheel;

function wheel(event) {

	var delta = 0;
	if(!event) {
		// For IE.
		event = window.event;
	}

	if(event.wheelDelta) {
		// IE/Opera.
		delta = event.wheelDelta / 120;
		/*if (window.opera)
		 {
		 delta = delta;
		 }*/

	} else if(event.detail) {
		// Mozilla case.
		delta = -event.detail / 3;
	}

	if(overScroll) {

		if(event.preventDefault) {
			event.preventDefault();
		}

		event.returnValue = false;

		moveWithWheel(delta);
	}
}

function moveWithWheel(delta) {
	if(delta > 0) {
		cima('ul_acompanhe');
	} else {
		baixo('ul_acompanhe');
	}
}

function mexer(quem) {
	var id = $(quem).attr("id");
	var tamCom = $("#" + id + " ul:first").attr("scrollHeight") - $("#" + id + " ul:first").height();
	var tamRol = $("#" + id + " div:first").height() - $("#" + id + " div:first img:first").height();
	var pos = $("#" + id + " div:first img:first").position().top - $("#" + id + " div:first").position().top;
	var val = (tamCom / tamRol) * (pos);
	$("#" + id + " ul:first").scrollTop(val);
}

function baixo(quem) {
	var id = $('#' + quem).parent().attr("id");
	var tamRol = $("#" + id + " div:first").height() - $("#" + id + " div:first img:first").height();
	var num = $("#" + id + " div:first img:first").position().top - $("#" + id + " div:first").position().top + 10;
	if(num < tamRol) {
		$("#" + id + " div:first img:first").css("top", num);
		mexer($('#' + quem).parent());
	} else {
		$("#" + id + " div:first img:first").css("top", tamRol);
		mexer($('#' + quem).parent());
	}
}

function cima(quem) {
	var id = $('#' + quem).parent().attr("id");
	var topRol = $("#" + id + " div:first").position().top;
	var num = $("#" + id + " div:first img:first").position().top - $("#" + id + " div:first").position().top - 10;
	if(num > 0) {
		$("#" + id + " div:first img:first").css("top", num);
		mexer($('#' + quem).parent());
	} else {
		$("#" + id + " div:first img:first").css("top", 0);
		mexer($('#' + quem).parent());
	}
}

/* FIM DROPDOWN PÁGINA PRINCIPAL */


/* FIM BOTOES DESLIZAM FOTOS NOS LANCAMENTOS */

//INICIO funcoes slide
var moving = false;
var mouseover = false;
var n = 0;

function ant(obj, num) {
	var mask = $(obj).width();
	var tamanho = mask * $(obj).find("li").length / num;
	var minLeft = mask - tamanho;

	$(obj).find("ul").width(parseInt(tamanho));

	var pos = (num == 1) ? $(obj).find("li").width() : mask;

	if(!moving) {
		moving = true;

		$(obj).find("ul").animate({
			"left" : "+=" + pos + "px"
		}, 1000, "", function() {
			moving = false;
			n--;
			if(parseInt($(obj).find("ul").css("left")) >= 0)
				$(obj).parent().parent().find('.prev').addClass('ocultarSetas');

			$(obj).parent().parent().find('.next').removeClass('ocultarSetas');

		});
	}
}

function prox(obj, num) {
	var mask = $(obj).width();
	var tamanho = mask * parseInt($(obj).find("li").length) / num;
	var minLeft = mask - tamanho;
	

	//alert($(obj).find("li").length);
	var pos = mask;

	$(obj).find("ul").width(parseInt(tamanho));

	if(parseInt($(obj).find("ul").css("left")) > minLeft) {
		if(!moving) {
			moving = true;

			$(obj).find("ul").animate({
				"left" : "-=" + pos + "px"
			}, 1000, "", function() {
				moving = false;
				n++;
				if(n >= ($(obj).find("li").length - 1))
					n = 0;
				if(parseInt($(obj).find("ul").css("left")) <= minLeft){
					$(obj).parent().parent().find('.next').addClass('ocultarSetas');			
				}
				$(obj).parent().parent().find('.prev').removeClass('ocultarSetas');

			});
		}
	}
}

/* INICIO DA EXECUÇÃO DOS MÉTODOS */

$(document).ready(function() {

	//botão próximo
	$(".bt_dir").click(function() {
		if(!moving) {
			//pega a largura total da div e diminui 340(a largura da imagem)
			ulw = $(this).parent().find("ul").width();
			liw = $(this).parent().find("ul li").width() * 7;
			minleft = liw - ulw;
			//se a posição for maior que o total o botão trava
			if(parseInt($(this).parent().find("ul").css("left")) > minleft) {
				moving = true;
				$(this).parent().find("ul").animate({
					left : '-=' + liw
				}, 2000, '', function() {
					moving = false;
				});
			}
		}
	});
	//botão anterior
	$(".bt_esq").click(function() {
		if(!moving) {
			//se a posição for menor que 0 o botão trava
			if(parseInt($(this).parent().find("ul").css("left")) < 0) {
				moving = true;
				$(this).parent().find("ul").animate({
					left : '+=' + liw
				}, 2000, '', function() {
					moving = false;
				});
			}
		}
	});


	
	/* INICIO DROPDOWN */
	$("#lista_acompanhe").mouseenter(function() {
		overScroll = true;
	});

	$("#lista_acompanhe").mouseleave(function() {
		overScroll = false;
	});

	$(".rolagem").click(function(event) {

		$(this).find("img").css("top", event.clientY - ($(this.parentNode).position().top + 6) + $(document).scrollTop());
		mexer($(this).parent());
	})

	$(".rolagem img:first").draggable({
		axis : 'y',
		scroll : false,
		containment : 'parent',
		drag : function() {
			mexer($(this).parent().parent());
		}
	});

	$("#lista_acompanhe").hide();

	$("a.botao_escolha").click(function() {

		$("#lista_acompanhe").slideToggle(500);
	});
	/* FIM DROPDOWN */

	/* INICIO MENU */

	if($.browser.msie && jQuery.browser.version.substr(0, 3) == "6.0") {
		$("#menu_principal ul li a span").css("display", "none");
		$("#menu_principal ul li div").css("display", "none");
	} else {
		$("#menu_principal ul li a span").hide();
		$("#menu_principal ul li div").hide();
	}

	$("#menu li").hover(function() {
		if($.browser.msie && jQuery.browser.version.substr(0, 3) == "6.0") {
			$(this).find("a span").css("display", "block");
			$(this).find("div").css("display", "block");
		} else {
			$(this).find("a span").fadeIn(500);
			$(this).find("div").fadeIn(500);
		}

	}, function() {
		if($.browser.msie && jQuery.browser.version.substr(0, 3) == "6.0") {
			$(this).find("a span").css("display", "none");
			$(this).find("div").css("display", "none");
		} else {
			$(this).find("a span").stop(true, true).fadeOut(500);
			$(this).find("div").stop(true, true).fadeOut(500);
		}
	});
	/* FIM MENU */

});
;

/* FIM DA EXECUÇÃO DOS MÉTODOS */

/* INICIO CODIGO PRETTYPHOTO */
$(document).ready(function() {
	$("a[rel^='prettyPhoto']").prettyPhoto({
		theme : 'light_square'
	});
});
//função para abrir uma foto via javascript
function myOpener(img, tit, desc) {
	var images = [];
	var titles = [];
	var descriptions = [];

	images.push(img);
	titles.push(tit);
	descriptions.push(desc);

	$.prettyPhoto.open(images, titles, descriptions);
}

/* INICIO CODIGO PRETTYPHOTO */

function mailto() {
	window.location = 'mailto:comercial@ceuengenharia.com.br;marketing@ceuengenharia.com.br';
}
