﻿var dom = "http://www.cnajmj.fr"
//var dom = "http://www.cnajmj.dev";
$(function(){
        connexion();
	cycleThis();
	extLnk();
	melto();
	fontToolHandler();
	initFancy();
	scrollPage();
	$().UItoTop({easingType: 'easeOutQuart'});
	})

function connexion(){
    $('#frmLogin').submit(function() {

       $('#frmLogin .error').html('').hide();
       var frmData = $(this).serializeArray();
       $.getJSON($(this).attr('action')+'?mod=json',frmData,function(data){
           if (data.success == true ){
               window.location.href=data.url;
           }else{
                $('#frmLogin .error').html(data.error).show();
           }
       });

      return false;
    });
}

function scrollPage(){
	$(".post-content a[href*='#']").click(function(event){
		event.preventDefault();
		var full_url = this.href;
		var parts = full_url.split("#");
		var trgt = parts[1];
		goToByScroll(trgt);
	});
}
function goToByScroll(id){
     	$('html,body').animate({scrollTop: $("a[title="+id + " ]").offset().top},1200,'easeOutQuart');
}

	
function fontToolHandler(){
	var fontSize = $(".post-content").css('font-size')
	var refFontSize = parseInt(fontSize)
	
	$('.upper').bind('click', function(){
		refFontSize ++;
		if(refFontSize>30){
			refFontSize = 30
			return;
		}
		$(".post, .result, .desc").css({fontSize:refFontSize+"px"})

		})
	$('.lower').bind('click', function(){
		refFontSize --;
		if(refFontSize<12){
			refFontSize = 12
			return;
		}
		$(".post, .result, .desc").css({fontSize:refFontSize+"px"})
		})
	
}

function cycleThis(){
	$('#visu').cycle({
		fx: "scrollHorz",
		timeout: 4000,
		easing: "easeOutExpo",
		/*delay: -1000,*/
		sync: true
		}); 
}


function initFancy(){
	$('a.fancy').fancybox({'width':800,'titleShow':false}).click(function(){return false;})
}
function FBShareHandler(){
	$('a.fb').click(function(){
		url = window.location;
		title = document.title;
		window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(url)+'&t='+encodeURIComponent(title),'Partager sur Facebook','toolbar=0,status=0,width=626,height=436');
	});
}
function TwitShareHandler(){
	$('a.tw').click(function(){
		url = window.location;
		title = document.title;
		window.open('http://twitter.com/share?url='+encodeURIComponent(url)+'&t='+encodeURIComponent(title),'Partager sur Twitter','toolbar=0,status=0,width=626,height=436');
	});
}

	
function extLnk(){
	$('a[href^="http://"]').addClass('external').attr('target', '_blank');
	$('a[href^="'+dom+'"]').removeClass('external').removeAttr('target', '_blank');
	$('a[href$=".pdf"]').addClass('external').attr('target', '_blank');
}
function melto(){
	var lnkMail = $('a.melto')
	$(lnkMail).each(function (i){
			dom = $(this).attr("href")
			domUrl = dom.substring(11,dom.length);
			lnkMelto = "mailto:" + $(this).text() +'@'+ domUrl;				  
			$(this).attr('href',lnkMelto)
			$(this).text($(this).text() +'@'+ domUrl)
							  })
}

function sendToFriend(form){
	var str = $(form).serialize();
	$.post(form.action,str,function(data){
		if(data.status=="ok"){
			$("#sendtofriend").hide();
			$("#error").hide("");
			$("#message").html(data.message);
		}else{
			var str = "";
			for(err in data.errors){
				str += data.errors[err]+"<br/>";
			}
			$("#error").html(str);
			$("#error").show();
		}
	},'json');
	return false;
}

function initMap() {
		var myOptions = {
		  zoom: 14,
		  disableDefaultUI: true,
		  scaleControl: true,
		   navigationControl: true,
		  navigationControlOptions: {
			style: google.maps.NavigationControlStyle.SMALL
		  },
		  mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		var Gmap = new google.maps.Map(document.getElementById("map"),myOptions);
		//if(lng ==0 || lat==0){
			var  geocoder = new google.maps.Geocoder();
			geocoder.geocode( {'address': "4 rue de la mairie 63940 MARSAC EN LIVRADOIS"}, function(results, status) {
			      if (status == google.maps.GeocoderStatus.OK) {
					  var pt = results[0].geometry.location
			    	Gmap.setCenter(pt);
			    	var image = '/css/gfx/marker.png';
					var marker = new google.maps.Marker({
						position: pt, 
						map: Gmap
						});
					marker.setMap(Gmap); 
			      } else {
			        alert("Geocode was not successful for the following reason: " + status);
			      }
			    });	
		/*}else{
			var point = new google.maps.LatLng(lat,lng);
			Gmap.setCenter(point);
			var marker = new google.maps.Marker({
		        position: point, 
		        map: Gmap
		    });

		}*/
			
		 
    }
function loadDep(el){
	if(el.value!=""){
		$.get("/annuaire-profession/ajxDep/"+el.value,function(data){
			var str="";
			for(dep in data){
				str+='<option value="'+data[dep].numDep+'">'+data[dep].labDep+'</option>'
			}
			$('#dptSelect').html('<option value="">Sélectionner...</option>'+str);
			$('#dptSelect').attr("disabled",false);
		},'json')
	}else{
		$('#dptSelect').html('<option value="">Sélectionner...</option>');
		$('#dptSelect').attr("disabled",true);
	}
}
function enabledVille(el){
	if(el.value!=""){
		$('#villeInput').attr("disabled",false);
	}else{
		$('#villeInput').attr("disabled",true);
	}
}

