/* Author:

*/

$(document).ready(function() {
	init_slider_news(5000);
	
	// add class 'last' at the last element
	$('ul.mark-last').each(function(){
		$('li:last',this).addClass('last');
	});
	
	// add class 'first' at the first element
	$('ul.mark-first').each(function(){
		$('li:first',this).addClass('first');
	});
	
	//styling table
	//$("tr:even").css("background-color", "#F4F4F8");
  	$("tbody tr:odd").addClass("dark");
  	$("thead tr:first").addClass("header");
  	$("tr td:first-child").addClass("border-first");
  	$("tr td:last-child").addClass("border-last right");
	$("tr td:nth-child(4)").addClass("center");
	
	// tooltip sub-menus
	$(function () {
		$('.submenu').each(function () {
			
			var distance = 10;
			var time = 200;
			var hideDelay = 200;
		
			var hideDelayTimer = null;
		
			// tracker
			var beingShown = false;
			var shown = false;
			
			var trigger = $('.trigger', this);
			var popup = $('.popup', this).css('opacity', 0);
			var cTxt = $('.trigger', this).text();
		    var medio = textWidth(cTxt);
			// set the mouseover and mouseout on both element
			$([trigger.get(0), popup.get(0)]).mouseover(function () {
			  console.log(medio);
			  // stops the hide event if we move from the trigger to the popup element
			  if (hideDelayTimer) clearTimeout(hideDelayTimer);
		
			  // don't trigger the animation again if we're being shown, or already visible
			  if (beingShown || shown) {
				return;
			  } else {
				beingShown = true;
		
				// reset position of popup box
				popup.css({
				  top: 10,
				  left: medio-105,
				  display: 'block' // brings the popup back in to view
				})
		
				// (we're using chaining on the popup) now animate it's opacity and position
				.animate({
				  top: '+=' + distance + 'px',
				  opacity: 0.9
				}, time, 'swing', function() {
				  // once the animation is complete, set the tracker variables
				  beingShown = false;
				  shown = true;
				});
			  }
			}).mouseout(function () {
			  // reset the timer if we get fired again - avoids double animations
			  if (hideDelayTimer) clearTimeout(hideDelayTimer);
			  
			  // store the timer so that it can be cleared in the mouseover if required
			  hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				popup.animate({
				  top: '-=' + distance + 'px',
				  opacity: 0
				}, time, 'swing', function () {
				  // once the animate is complete, set the tracker variables
				  shown = false;
				  // hide the popup entirely after the effect (opacity alone doesn't do the job)
				  popup.css('display', 'none');
				});
			  }, hideDelay);
			});
		  });
		});

	//Fancy Box - About Offices
	
	$("#offices-gallery-01a, #offices-gallery-01b").click(function() {
		$.fancybox([
			'../img/about-gallery-01/galenicum_headquarters_01.jpg',
			'../img/about-gallery-01/galenicum_headquarters_02.jpg',
			'../img/about-gallery-01/galenicum_headquarters_03.jpg',
			'../img/about-gallery-01/galenicum_headquarters_04.jpg',
			'../img/about-gallery-01/galenicum_headquarters_05.jpg',
			'../img/about-gallery-01/galenicum_headquarters_06.jpg',
			'../img/about-gallery-01/galenicum_headquarters_07.jpg',
			'../img/about-gallery-01/galenicum_headquarters_08.jpg',
			'../img/about-gallery-01/galenicum_headquarters_09.jpg',
			'../img/about-gallery-01/galenicum_headquarters_10.jpg',
			], {
			'padding' : 0,
			'transitionIn' : 'elastic',
			'transitionOut' : 'elastic',
			'type' : 'image',
			'padding' : 10,
			'titleShow' : false
		});
	});
	
	$("#offices-gallery-02a, #offices-gallery-02b").click(function() {
		$.fancybox([
			'../img/about-gallery-02/galenicum_QcLab_01.jpg',
			'../img/about-gallery-02/galenicum_QcLab_02.jpg',
			'../img/about-gallery-02/galenicum_QcLab_03.jpg',
			'../img/about-gallery-02/galenicum_QcLab_04.jpg',
			'../img/about-gallery-02/galenicum_QcLab_05.jpg',
			'../img/about-gallery-02/galenicum_QcLab_06.jpg',
			'../img/about-gallery-02/galenicum_QcLab_07.jpg',
			'../img/about-gallery-02/galenicum_QcLab_08.jpg',
			'../img/about-gallery-02/galenicum_QcLab_09.jpg',
			], {
			'padding' : 0,
			'transitionIn' : 'elastic',
			'transitionOut' : 'elastic',
			'type' : 'image',
			'padding' : 10,
			'titleShow' : false
		});
	}); 
});

// home carousel vertical - news
function init_slider_news(time){
		$(".text .jCarouselLite").jCarouselLite({
			vertical: true,
			auto: time,
			speed: 500,
			
			btnNext: "ul#controls li a.next",
			btnPrev: "ul#controls li a.prev"
		});
}

function textWidth(text){
 var calc = '<span style="display:none">' + text + '</span>';
 $('body').append(calc);
 var width = $('body').find('span:last').width();
 $('body').find('span:last').remove();
 return width;
};










