$(document).ready(function() {
	/* Hauptnavigation *************************************************************/
	var offsetSelL	= $('#mainnav').offset().left;
	document.body.onresize = function (){
		offsetSelL	= $('#mainnav').offset().left;
	}
	
	if($("#mainnav ul > li.active > a").length > 0){
		var offsetL = $("#mainnav ul > li.active > a").offset().left;
		var leftPos = parseInt((offsetL + ($("#mainnav ul > li.active > a").width() / 2)) - offsetSelL - 13);
		$('#mainnav-selected').css({visibility: "visible", left: leftPos+ "px"});
		
		$("#mainnav ul").mouseenter(function() {
			$("#mainnav ul > li > a").mouseenter(function() {
				var offsetL = $(this).offset().left;
				var leftPos = parseInt((offsetL + ($(this).width() / 2)) - offsetSelL - 13);
				$('#mainnav-selected').stop().animate({left: leftPos+ "px"}, {duration: 600, easing: 'easeInOutBack'});
			});
		}).mouseleave(function(){
			//$('#subnav-outer').html($('#mainnav-selected').offset().left);
			$('#mainnav-selected').stop().animate({left: leftPos+ "px"}, {duration: 600, easing: 'easeInOutBack'});
		});
	}else{
		$("#mainnav ul").mouseenter(function() {
			$("#mainnav ul > li > a").mouseenter(function() {
				var offsetL = $(this).offset().left;
				var leftPos = parseInt((offsetL + ($(this).width() / 2)) - offsetSelL - 13);
				if($('#mainnav-selected').css('visibility') == 'hidden'){
					//$('#mainnav-selected').css({top: '-14px', opacity: 1, visibility: "visible", left: leftPos+ "px"});
					$('#mainnav-selected').css({top: '-14px', visibility: "visible", left: leftPos+ "px"});
					$('#mainnav-selected').stop().animate({top: '0px'}, {duration: 400, easing: 'easeInOutBack'});
				}else{
					$('#mainnav-selected').stop().animate({left: leftPos+ "px", top: '0px'}, {duration: 600, easing: 'easeInOutBack'});
				}	
			});
		}).mouseleave(function(){
			$('#mainnav-selected').animate({
				top: '-14px'
			}, {duration: 400, easing: 'easeInOutBack'}, function() {
				$('#mainnav-selected').css({opacity: 1, visibility: "hidden"});
			});
		});
	}
	/* Hauptnavigation *************************************************************/
	
	
	
	
	/* Common *************************************************************/
	// Mouseover Effect für Bilder mit Hyperlinks
	$('a img').not('header img, #home-slider a img').mouseenter(function() {
		$(this).stop(true, true).fadeTo(250, 0.8); 
	}).mouseleave(function(){
		$(this).stop(true, true).fadeTo(100, 1); 
	});
	/* Common *************************************************************/
	
	
	
	
	/* Startseite - Home-Slider *************************************************************/
	$('#home-slider').cycle({ 
		fx:     'fade', 
		speed:  'slow', 
		timeout: 5000, 
		pager:  '#home-slider-nav', 
		// callback fn that creates a thumbnail to use as pager anchor 
		pagerAnchorBuilder: function(idx, slide) { 
			return '<a href="#"><img src="_img/dot.png" height="7" width="7" /></a>'; 
		} 
	});
	/* Startseite - Home-Slider *************************************************************/
	
	
	
	
	/* Kunde *************************************************************/
	$(".customer-item").mouseenter(function() {
		$(this).find('.customer-links').stop().animate({
					marginTop: '0'
		}, {duration: 250, easing: 'easeInOutQuad'});
	}).mouseleave(function(){
		$(this).find('.customer-links').stop().animate({
					marginTop: '100px'
		}, {duration: 150, easing: 'easeInOutQuad'});
	});
	/* Kunde *************************************************************/
	
	
	
		
	/* Projekt Detail *************************************************************/
	// unhide/hide complete text
	$('.toggle-more').toggle(function(){
		$(this).parent().parent().find('.hidden').fadeIn(125);
		$(this).addClass('active');
	},function(){
		$(this).parent().parent().find('.hidden').fadeOut(125);
		$(this).removeClass('active');
	});	
	/* Projekt Detail *************************************************************/

	
	
	/* Startseite - Home Teaser Box *************************************************************/
	$('.home-teaser-box').mouseenter(function(){
		$(this).find('.home-teaser-box-img-color').stop(true, true).fadeTo(350, 1);
		
		$(this).stop().animate({
					backgroundPosition: '0 25'
		}, {duration: 600, easing: 'easeOutBack'});
		$(this).find('.home-teaser-box-img').stop().animate({
					marginTop: '25'
		}, {duration: 1200, easing: 'easeOutBack'});
		
		$(this).find('.home-teaser-box-text').stop().animate({
					marginTop: '25'
		}, {duration: 600, easing: 'easeOutBack'});
	}).mouseleave(function(){
		$(this).find('.home-teaser-box-img-color').stop(true, true).fadeTo(150, 0);
		
		$(this).stop().animate({
					backgroundPosition: '0 0'
		}, {duration: 300, easing: 'easeOutBack'});
		
		$(this).find('.home-teaser-box-img').stop().animate({
					marginTop: '0'
		}, {duration: 600, easing: 'easeOutBack'});
		
		$(this).find('.home-teaser-box-text').stop().animate({
					marginTop: '0'
		}, {duration: 300, easing: 'easeOutBack'});
	});	
	/* Startseite - Home Teaser Box *************************************************************/
	
	
	
	/* Google Map *************************************************************/
	if($('#map').length > 0){
		var directionDisplay;
		var directionsService = new google.maps.DirectionsService();
		var map;
		function initializeGoogleMaps(){
			directionsDisplay = new google.maps.DirectionsRenderer();
			var latlng = new google.maps.LatLng(47.393556,8.52955,15);
			var styles = [{
				elementType: "all",
				visibility: "simplified",
				stylers: [{ saturation: -100 }]
			}];
			
			var myOptions = {
				zoom:15,
				center: latlng,
				mapTypeControl: false,
				mapTypeControlOptions: {mapTypeIds: ['grayscale',google.maps.MapTypeId.HYBRID]},
				navigationControl: true,
				navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
				mapTypeId: google.maps.MapTypeId.ROADMAP
			}
			var map = new google.maps.Map(document.getElementById("map"), myOptions);
			var mapType = new google.maps.StyledMapType(styles, { name:"Karte" });    
			map.mapTypes.set('grayscale', mapType);
			map.setMapTypeId('grayscale');
			var image = '_img/gmap-pin.png';
			var spotMarker = new google.maps.Marker({
				position: latlng,
				map: map,
				icon: image
			});
			directionsDisplay.setMap(map);
			directionsDisplay.setPanel(document.getElementById("directionsPanel"));			
		}
		window.onload = initializeGoogleMaps;
		
			
		$('#gMapSubmit').click(function(){
			$('#directionsPanel span.error').remove();
			var start = $('#start').val();
			//var start = document.getElementById("start").value;
			var end = 'Nordbrücke 4, 8037 Zurich, Switzerland';
			var request = {
				origin:start, 
				destination:end,
				travelMode: google.maps.DirectionsTravelMode.DRIVING
			};
			directionsService.route(request, function(response, status){
				if(status == google.maps.DirectionsStatus.OK){
					directionsDisplay.setDirections(response);
					var start = $('#directionsPanel').show();
				}else{
					$('#directionsPanel').html($('#directionsPanelError').html());
				}
				//document.getElementById("directionsPanel").style.display = 'block';
				var start = $('#directionsPanel').show();
				//window.location.hash='map';
			});
			return false;
		});
		
		$('#start').bind('keypress', function(e) {
			if(e.keyCode == 13){
				$('#gMapSubmit').trigger('click');
			}
		});
	}
	/* Google Map *************************************************************/
	
	
	
	
	/* Projekt Detail Video *************************************************************/
	if($('.projekt-detail-video').length > 0){
		var videoId 	= '';
		var videoUrl 	= '';
		$('.projekt-detail-video video').each(function() {
			videoId 	= $(this).attr('tabindex');
			videoUrl 	= $(this).attr('src');
			fbVideo('a.fb-video' + videoId, 'projekt-detail-video' + videoId, 'projekt-detail-video-item' + videoId, videoUrl);
		})
	}
	/* Projekt Detail Video *************************************************************/
});



function calcRoute(){
	$('#directionsPanel span.error').remove();
	var start = document.getElementById("start").value;
	var end = 'Nordbrücke 4, 8037 Zurich, Switzerland';
	var request = {
		origin:start, 
		destination:end,
		travelMode: google.maps.DirectionsTravelMode.DRIVING
	};
	directionsService.route(request, function(response, status){
		if(status == google.maps.DirectionsStatus.OK){
			directionsDisplay.setDirections(response);
			document.getElementById("directionsPanel").style.display = 'block';
		}else{
			document.getElementById("directionsPanel").style.display = 'block';
			$('#directionsPanel').html($('#directionsPanelError').html());
		}
		//window.location.hash='map';
	});
	return false;
}


						
function fbVideo(clickElement, vElementOuter, vElement, vFile){
	playVideo(vElement, vFile);
	
	$(clickElement).fancybox({
		'titlePosition'		: 'inside',
		'overlayColor'		: '#000',
		'overlayOpacity'	: 0.6, 
		'onClosed'		: function() {
			$('#'+vElementOuter).html('<video id="'+vElement+'"></video>');
			playVideo(vElement, vFile);
		}
	});
}
function playVideo(element, file){
	if($("#"+element).length > 0){
		jwplayer(element).setup({
			'flashplayer': '_js/mediaplayer/player.swf',
			'file': file,
			'autostart': 'true',
			'width': 720,
			'height': 393
		});
	}
}
