var dom = "http://www.cc-livradois.fr/";
$(function(){
	initCufon()
	cycleThis()
	extLnk()
	melto()
	fontToolHandler()
	initFancy()
	FBShareHandler()
	TwitShareHandler()
	if($('#map').length>0 ) initMap()
})
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").css({fontSize:refFontSize+"px"})
		})
	$('.lower').bind('click', function(){
		refFontSize --;
		if(refFontSize<11){
			refFontSize = 11
			return;
		}
		$(".post").css({fontSize:refFontSize+"px"})
		})
	
}
function initCufon(){
	$("#comMap h3").css({top:"-38px"})
	$("#nav li:not('.last')").css({paddingRight:"29px"})
	Cufon.replace('#nav a',{fontFamily: 'Blippo',hover: {color: '#436351'},letterSpacing:"-1px"});
	Cufon.replace('#subnav a',{fontFamily: 'Blippo',hover: {color: '#7AB796'},letterSpacing:"-1px"});
	Cufon.replace('#subnav .current a',{fontFamily: 'Blippo',color: '#7AB796',letterSpacing:"-1px"});
	Cufon.replace('.block .title, #sitemap h4, #comMap h3, #comMap .contact strong, .page-title, .part-title',{fontFamily: 'Blippo'});
}

function cycleThis(){
	$('#items').cycle({
		sync:   1, 
		delay: 4000 ,
		pager:  '#links', 
		pagerAnchorBuilder: function(idx, slide) { return '#links div:eq(' + idx + ')';},
		pauseOnPagerHover:true,
		fastOnEvent: true,
		before:onBefore
	 });
	 
	bindViewer()

}
function bindViewer(){
	var el = $('#links').find('.lnk p')
	el.bind('click',function(){
		$(".selector").stop().animate({right:139},"fast")
		$(this).next().stop().animate({right:164},"fast")
		$(this).next().addClass("out")
		})
}

function onBefore(){
	nextElem = $("#links .activeSlide").next()
	if(nextElem.length == 0)  nextElem = $("#links .lnk").eq(0);
	
	if($(".out").length > 0){$(".out").removeClass('out')
	}else{
		$(".selector").stop().animate({right:139},"fast")
		nextElem.find('.selector').stop().animate({right:164},"fast")
	}
}

function initFancy(){
	$('a.fancy').fancybox({'width':500,onComplete:initSendToFriend}).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 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 initSendToFriend(){
	$('#sendtofriend').submit(function() {
		var str = $(this).serialize();
		str+="&url="+window.location;
		$.get(this.action,str,function(data){
			if(data.success){
				$('#sendtofriend').hide();
				$('#error').html('');
				$('#mess').html('<p><br/><strong>'+data.message+'</strong><br/><br/></p>');
			}else{
				var errors="";
				for(var i=0;i<data.errors.length;i++)errors+=data.errors[i]+"<br/>";
				$('#error').html('<p><br/>'+errors+'<br/></p>');
			}
		},'json');
		return false;
	});
}



