$(function(){

//masonry
	var masonry465 = $('.masonry-465');
	masonry465.masonry({
	  columnWidth: 465,
	  itemSelector: '.item-masonry',
	  gutter : 20
	});

	var masonry225 = $('.masonry-225');
	masonry225.masonry({
	  columnWidth: 225,
	  itemSelector: '.item-masonry',
	  gutter : 16
	});

//popin
	$('.open-popin').bind('click', function(e){
		e.preventDefault();
		var target = $(this).attr('href');
		$('.popin-wrap').fadeIn(function(){
			$(target).show();
		});
	});

	$('.close-popin, .popin-wrap').bind('click', function(e){
		e.preventDefault();
		e.stopPropagation();
		$('.popin-wrap').fadeOut(function(){
			$('.popin').hide();
		});
	});

	$('.popin').bind('click', function(e){
		e.stopPropagation();
	});
//tag it
	if($('.tag-it').length){
		var keywordsTagIt = $('.tag-it').tagit({
			allowSpaces : true
		});
		$('.list-key-add a').bind('click', function(e){
			e.preventDefault();
			var tag = $(this).attr('data-tag');
			keywordsTagIt.tagit("createTag", tag);
		});
	}


	
	// add item to collection behaviour
	$('.additemtocollection').bind('click', function(e){
		// When an item meant to be added to a collection is clicked,
		// we fill the form in the add-to-collection div
		$('#add-to-collection .item-type').val($(this).attr('data-type'));
		$('#add-to-collection .item-id').val($(this).attr('data-id'));
	});
	
	
	$(".ajax-form").submit(function(e) { // On submit Ajax Form
	    var formel = $(this);
	    $.ajax({
	        url: formel.attr("action"),
	        type: formel.attr("method"),
	        data: formel.serialize(),
	        success: function(text) {
                $('.popin-wrap').fadeIn(function(){
                    $(".additem-success").show();
                });
	            //console.log("received : " + text);
                $(".additem-success .collection-url").attr("href", text);
	        },
            error: function() {
                $('.popin-wrap').fadeIn(function(){
                    $(".additem-error").show();
                });
            }
	    });
	    formel.parents(".popin").hide();
	    return false;
	});	

	if($('#map').length){
		initmap()
	}
	function initmap() {
		// set up the map
		map = new L.Map('map');

		// create the tile layer with correct attribution
		var osmUrl='http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
		var osm = new L.TileLayer(osmUrl, {minZoom: 3, maxZoom: 20});		

		// start the map in South-East England
		map.setView(new L.LatLng(48.833, 2.333),4);
		map.addLayer(osm);

		// markers
		var marker1 = L.marker([48.833, 2.333]).addTo(map);
		marker1.bindPopup("Beaux-Arts de Paris");
		var marker2 = L.marker([47.233,-1.583]).addTo(map);
		marker2.bindPopup("Beaux-Arts de Nantes");
	}

});//ready