//*********************************************
// vars
//*********************************************
var quotes_length;
var quotes_index = 0;

//*********************************************
// document ready
//*********************************************
$(document).ready(function() {
  var el = $(".clickable");
  if(el.length) selectEntireElement(el);
  initQuotes();
  
  if ($('#gmap').length) initGMaps();
  
  if ($('#carrousel').length) carrouselHover();
  
  if($('.portfolio-overview-bp').length) initPortfolioOverviewOverlay();
  

});

$(window).load(function() {
  initNav();
});
//*********************************************
// functions
//*********************************************
function initNav() {
  // init
  var currentPageItem = $(".current_page_item");
  if(!currentPageItem.length) currentPageItem = $(".current-page-ancestor"); // if current page is a subpage
  
  $("#nav-hover").css("left", currentPageItem.position().left).css("width", currentPageItem.width());
  
  // animation
  $("#navigation li").mouseover(function() {
   var w = $(this).width();
   var x = $(this).position().left;
   $("#nav-hover").stop().animate({"width": w, "left": x}, "slow", "easeOutExpo");
  }).mouseout(function() {
   $("#nav-hover").stop().animate({"width": currentPageItem.width(), "left": currentPageItem.position().left});
  });
}
// important: only one link inside element allowed. the link attribute href is used to redirect per js inside the click function
function selectEntireElement(el) {
  el.mouseover(function() {
    $(this).find("a").addClass("hover");
  }).mouseout(function(){
    $(this).find("a").removeClass("hover");
  }).click(function() {
    window.location = $(this).find("a").attr("href");
  });
}
function initQuotes() {
  $("#statements :first-child").show();
	// quotes can be added in back-end and will correctly be handled in front-end
	quotes_length = $('#statements').children().size();
	// periodic change of quote every x ms
	timer = setInterval(changeQuote, 10000);
}
function changeQuote() {
	// vanish shown statement and display next one
	$('#st-' + quotes_index).fadeOut(180, function(){
		quotes_index == quotes_length - 1? quotes_index = 0 : quotes_index++;
		$('#st-' + quotes_index).fadeIn(220);
	});
}
function initGMaps() {
	// specify map
	var myOptions = {
		zoom: 17,
		center: new google.maps.LatLng(47.393520, 8.51524),
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	var map = new google.maps.Map(document.getElementById('gmap-canvas'), myOptions);
	
	// add marker to map
	var marker = new google.maps.Marker({
		position: new google.maps.LatLng(47.393441, 8.51424), 
		map: map, 
		title:"TaskFleet GmbH"
	});
	
	// add infowindow to map
	var infowindow = new google.maps.InfoWindow({
	    content: "<strong>TaskFleet</strong><br/>Hardturmstrasse 124a<br/>CH-8005 Z&uuml;rich"
	});
	
	// add listener to marker
	google.maps.event.addListener(marker, 'click', function() {
		infowindow.open(map,marker);
	});
}
function carrouselHover() {
  $("#carrousel").mouseover(function() {
    $("#carrousel-banner-hover").show();
  }).mouseout(function() {
    $("#carrousel-banner-hover").hide();
  });
}
// init overlay on portfolio overview site on hover
function initPortfolioOverviewOverlay() {
  $('.portfolio-overview-bp').parent().hover(
    function() {
     $(this).find('.overlay').stop(true, true).fadeIn(200);
    },
    function() {
     $(this).find('.overlay').stop(true, true).fadeOut(200);
    }
  );
  $('.portfolio-overview-sp').hover(
    function() {
     $(this).find('.overlay').stop(true, true).fadeIn(200);
    },
    function() {
     $(this).find('.overlay').stop(true, true).fadeOut(200);
    }
  );
}
