/* Author: 

*/

function init_map()
{
	//return;
	var map = new google.maps.Map(document.getElementById("map"),{
		zoom: 5
	  	,mapTypeId: google.maps.MapTypeId.ROADMAP
		,center: new google.maps.LatLng(46.75984,1.738281)
		,noClear : 0
	});
	
	var directionsRenderer = new google.maps.DirectionsRenderer();
	directionsRenderer.setMap(map);
	directionsRenderer.setPanel(document.getElementById('map_route'));
	
	var directionsService = new google.maps.DirectionsService();
	var request = {
		origin : new google.maps.LatLng(coord.lt, coord.lg)
	  	,destination : new google.maps.LatLng(45.911649, 6.095257)
	  	,travelMode : google.maps.DirectionsTravelMode.DRIVING,
	  	//provideTripAlternatives: true
	};
	
	directionsService.route(request, function(response, status) {
		if (status == google.maps.DirectionsStatus.OK)
	  	{
	    	directionsRenderer.setDirections(response);
	  	}
	  	else
	  	{
	    	alert('Error: ' + status);
	  	}
	});

}

var coord =  {'lt' : geoip_latitude(), 'lg' : geoip_longitude() }
var START = 0, TIMER = 0;
$(document).ready(function () {

	init_map();

	// top menu
    $("#topnav").stickyPanel({
		topPadding: 0
        ,afterDetachCSSClass: "menufixed"
        ,savePanelSpace: false
	});


	// change menu during srcoll
	$(window).scroll(function(evt){
	
		if($(window).scrollTop() >= ($('a[name*="o"]:eq(3)').offset().top-75))
		{
			$(".topnav a").removeClass('on');
			$('.topnav li a[href="#contact-developpeur-web"]').addClass("on");
		}	
		else if($(window).scrollTop() >= ($('a[name*="o"]:eq(2)').offset().top-75))
		{
			$(".topnav a").removeClass('on');
			$('.topnav li a[href="#olivier-rocques"]').addClass("on");
		}	
		else if($(window).scrollTop() >= ($('a[name*="o"]:eq(1)').offset().top-75))
		{
			$(".topnav a").removeClass('on');
			$('.topnav li a[href="#portfolio-integrateur-web"]').addClass("on");
		}	

		else
		{
			$(".topnav a").removeClass('on');
			$('.topnav li a[href="#developpeur-web-independant"]').addClass("on");
		}	

	});

	// menu scrollTo
	$(".topnav a[href^='#']").click(function (event) {
		event.preventDefault();
		var $this   = $(this)
	    target  = this.hash,
	    $target = $('a[name="' + target.replace(/#/, '') + '"]');


		
		$("html,body").stop().animate({
			"scrollTop": $target.offset().top-70
		}, 600, "swing", function () {
			window.location.hash = target;
		});
	});

	// form placeHolder
	jQuery.support.placeholder = false;
	test = document.createElement('input');
	if('placeholder' in test) jQuery.support.placeholder = true;

	if(!$.support.placeholder)
	{

		var active = document.activeElement;
		$('input:[type=email]').focus(function() {
			if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
				$(this).val('').removeClass('hasPlaceholder');
			}
		}).blur(function() {
			if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
				$(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');


			}
		});
		$('input:[type=email]').blur();
		$(active).focus();
	}
	
	


	
	
	// google map
/*
	if (navigator.geolocation)
	{
  		navigator.geolocation.getCurrentPosition(function(position){
  			coord =  {'lt' : position.coords.latitude, 'lg' : position.coords.longitude }
			init_map();
  		}
  		,function(msg){  return;  });
	}
*/	


	// btn project
	$('#portfolio_more').bind('click', function(evt){
		evt.preventDefault();
		$('#portfolio_more').text("loading...");
		var $this = $(this);	
		$.ajax({
			type: "POST"
			,url: 'action.php'
			,data: {'action' : 'projets', 'start' : START}
			,success: function(data){

				var li = "";
				
				$.each(data.projet, function(k, v){
					li+= '<li style="display:none"><a href="'+v.url_site+'" target="_blank" class="details_link"><figure><div class="details"><h6>'+v.nom+'</h6>'+v.text+'</div><img src="images/target.jpg" /><img src="images/projets/'+v.url_image+'" class="real" /></figure></a></li>';
				});

				START+=3;
				setTimeout(function(){ 
					if(li)
					{
						$('#portfolio_content li:last').before(li).parent('ul').find('li:not(visible)').fadeIn('slow'); 
					}

					if(data.total > START)
					{
						$('#portfolio_more').text("plus de réalisations").css('display', 'block');
					}
					else
					{
						$this.unbind('click').bind('click', function(evt){ evt.preventDefault(); }).text("done").css({'cursor' : 'default', 'background' : '#DEDEDE'});
					}

					TIMER = 800;
			
				}, TIMER);


			}
		}, 'json');
		
		
		
	});
	
	// start portfolio
	$('#portfolio_more').trigger('click');

	// project with no link
	$('.details_link').live('click', function(evt){

		if(!$(this).attr('href'))
		{
			evt.preventDefault();
			alert("Désolé mais le site ou l'application n'est plus disponible en ligne");
		}
	});


	$('form').submit(function(evt){
		evt.preventDefault();
		
		$.post('action.php', $(this).serialize(), function(data) {

			if(data.err.length)
			{
				$('#msg_content').html(data.err);
				return;
			}
			
  			$('form input[type="text"], form textarea ').val('');
			$('#msg_content').html("Merci votre message à bien été envoyé");
		});
	
	});
	
	
	if(!Modernizr.csstransitions)
	{
		$('.details_link').live('mouseenter', function(){
			  $(this).find('.real').animate({
			    opacity: 0.05
			  	}, 600);
	
			  $(this).find('.details').animate({
			    opacity: 1
			    ,height: 226
			  	}, 300);
	
		});
		$('.details_link').live('mouseleave', function(){
			  $(this).find('.real').animate({
			    opacity: 1
			  	}, 600);
	
			  $(this).find('.details').animate({
			    opacity: 0
			    ,height: 0
			  	}, 200);
	
	
		});
	}
});




















