var geocoder = new google.maps.Geocoder();

jQuery(window).ready(function(){
	
	if ($('.googlemap').length){
		$('.googlemap').each(function(){
			var address = $(this).find('address').text();
			var latlng = new google.maps.LatLng(0, 0);
			var myOptions = {
				zoom: 13,
				center: latlng,
				mapTypeId: google.maps.MapTypeId.ROADMAP
			}
			var map = new google.maps.Map(this, myOptions);
			codeAddress(map, address);
		});
	}
	
	$('.recens').click(function(){
		var l = $(this).find('a').attr('href');
		if (l){
			document.location.href = l;
		}
	})
	
	if ($('.fancybox')){
		$('.fancybox').fancybox();
	}
	
	$('.cnt table tr.sub td:first')
	.css('cursor', 'pointer')
	.click(function(){
	  document.location.href = $(this).find('a').attr('href');
	});
	
	$('.cnt table tr.parent').toggle(
		function(){
			var _id = $(this).attr('id');
			ShowHideTableRow($('.cnt table tr.child-of-' + _id), true, null);
		},
		function(){
			var _id = $(this).attr('id');
			ShowHideTableRow($('.cnt table tr.child-of-' + _id), false, null)
		}
	);
	
	function ShowHideTableRow(rowSelector, show, callback)
	{
	    var childCellsSelector = $(rowSelector).children("td");
	    var ubound = childCellsSelector.length - 1;
	    var lastCallback = null;

	    childCellsSelector.each(function(i)
	    {
	        // Only execute the callback on the last element.
	        if (ubound == i)
	            lastCallback = callback

	        if (show)
	        {
	            $(this).fadeIn("slow", lastCallback)
	        }
	        else
	        {
	            $(this).fadeOut("slow", lastCallback)
	        }
	    });
	}
	
	$(".service").tipsy({gravity: 's'});
	$("aside .author a, #team ul li a").tipsy({gravity: 'se'});
	
	$('#logos li')
		.hover(
			function(){
				$(this).find('img').stop().animate({
					width: '192px',
					height: '192px',
					marginTop: '-106px',
					marginLeft: '-96px'
				});
				$(this).find('strong').fadeIn();
			},
			function(){
				$(this).find('img').stop().animate({
					width: '148px',
					height: '148px',
					marginTop: '-90px',
					marginLeft: '-74px'
				});
				$(this).find('strong').fadeOut();
			}
		)
		.click(
			function(){
				var _id = $(this).find('a').attr('href').replace(/#/, '');
				ShowHideTableRow($('.cnt table tr.child-of-' + _id), true, null);
			}
		);
	$('#logos a.tbl').click(function(e){
		var _id = $(this).attr('href').replace(/#/, '');
		ShowHideTableRow($('.cnt table tr.child-of-' + _id), true, null);
	})
})


jQuery(window).load(function(){
	$('#slider').nivoSlider({
		effect:'sliceUpDownRight', // Specify sets like: 'fold,fade,sliceDown'
		slices:15, // For slice animations
		pauseTime: 4000,
		captionOpacity: 0.8,
		directionNav: false
	});
})

function codeAddress(map, address) {
    geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      map.setCenter(results[0].geometry.location);
      var marker = new google.maps.Marker({
          map: map, 
          position: results[0].geometry.location
      });
    } else {
       alert("Geocode was not successful for the following reason: " + status);
    }
  });
}
