/*
	Author: Anderson Juhasc Programador Front-End, saiba mais: www.andersonjuhasc.com
*/

(function() {

	// GALERIA
	if ($("div.gallery").length > 0) {

		$(".scrollable").scrollable();

		$(".items img").click(function() {
			// see if same thumb is being clicked
			if ($(this).hasClass("active")) { return;}

			// retira o _thumb e pega a img maior
			var url = $(this).attr("url");

			// get handle to element that wraps the image and make it semi-transparent
			var wrap = $(this).parent().parent().parent().siblings('.image_wrap').fadeTo("medium", 0.5);

			var img = new Image();
			
			// Exibe o nome da imagem no topo da pagina
			var title_img = $(this).attr("alt");
			$(".titulo_img").text(title_img);
			
			// call this function after it's loaded
			img.onload = function() {

				// make wrapper fully visible
				wrap.fadeTo("fast", 1);

				// change the image
				wrap.find("img").attr("src", url);

			};

			// begin loading the image from www.flickr.com
			img.src = url;

			// activate item
			$(".items img").removeClass("active");
			$(this).addClass("active");

		// when page loads simulate a "click" on the first image
		}).filter(":first").click();
		
		setInterval(function() {
			if($(".prev").hasClass("disabled")){ $(".prev").addClass("prevD"); }else{ $(".prev").removeClass("prevD"); }
			if($(".next").hasClass("disabled")){ $(".next").addClass("nextD"); }else{ $(".next").removeClass("nextD"); }
		}, 1000);
		
	}

	// SLIDER
	$('#runCycle')
		.cycle({
			fx:     'fade', 
			speed:  'fast', 
			timeout: 5000, // Velocidade da mudança de slide 5000 = 5s
			next:   '#nextS', 
			prev:   '#prevS',
			after:	function() {
				var title = $(this).attr('alt');

				$('div.viewTitle').html(title);
			}
		}
	);

	$('#slider')
		.cycle({
			fx:     'fade', 
			speed:  'fast', 
			timeout: 5000, // Velocidade da mudança de slide 5000 = 5s
			next:   '#prev', 
			prev:   '#next',
			pager:  '#nav', 
			pagerAnchorBuilder: function(idx, slide) { 
				// return selector string for existing anchor 
				return '#nav li:eq(' + idx + ') a'; 
			},
			after:	function(currSlideElement, nextSlideElement, options, forwardFlag) {
				var title = $(this).attr('alt');
				$('h1.titulo_img').html(title);
				//Get image size
				var imgHeight = $(this).height();
				var imgWidth = $(this).width();
				//Get container size
				var slider = $(this).parent();
				var containerHeight = slider.height();
				var containerWidth = slider.width();
				var top = Math.floor((containerHeight / 2) - (imgHeight / 2));
				var left = Math.floor((containerWidth / 2) - (imgWidth / 2));
				$(this).css({"top" : top,"left" : left});

				$('#nav').parent('.nav').scrollTo( $('#nav li.li_' + $(currSlideElement).attr('id')), 500, {axis:'x'} );
			}
		}
	);

	//Get image size
	var img = $("#slider img");
	var imgHeight = $(img).height();
	var imgWidth = $(img).width();
	//Get container size
	var slider = $(img).parent();
	var containerHeight = slider.height();
	var containerWidth = slider.width();
	var top = Math.floor((containerHeight / 2) - (imgHeight / 2));
	var left = Math.floor((containerWidth / 2) - (imgWidth / 2));
	$(img).css({"top" : top,"left" : left});

	// Altura igual OBS: a versao para o ie7 e ie8 está np footer
	if (!($.browser.msie && $.browser.version <= 8)) {
		if ($('div.prodIndustriais').length > 0) {
			var h = $('#getHeight').outerHeight();

			$('div.prodIndustriais > nav ul').height(h);
		}
	}
	
	function menuPrincipalOver(el) {
	  if (!el.is(':visible'))
	    el.slideDown();
	  else
	    el.slideUp();
	}
	
	function menuPrincipalOut(el) {
	  if (el.is(':visible'))
	    el.slideUp();
	  else
	    el.slideDown();
	}

	// Menu effect
	$('nav.menuPrincipal li.hover').hover(function(e) {
		e.preventDefault();
		var el = $(this).find('ul.content');
		menuPrincipalOver(el)
	}, function(e) {
		e.preventDefault();
		var el = $(this).find('ul.content');
		menuPrincipalOut(el);
	});

	// polyfill placeholder fix html5
	if(!Modernizr.input.placeholder){
		$('[placeholder]').focus(function() {
		  var input = $(this);
		  if (input.val() == input.attr('placeholder')) {
			input.val('');
			input.removeClass('placeholder');
		  }
		}).blur(function() {
		  var input = $(this);
		  if (input.val() == '' || input.val() == input.attr('placeholder')) {
			input.addClass('placeholder');
			input.val(input.attr('placeholder'));
		  }
		}).blur();
		$('[placeholder]').parents('form').submit(function() {
		  $(this).find('[placeholder]').each(function() {
			var input = $(this);
			if (input.val() == input.attr('placeholder')) {
			  input.val('');
			}
		  })
		});
	}
}());
