/**
 * Validated a for thriught ajax
 * @param {Object} form form object
 * @param {String} url url to the validation service
 * @param {Object} event object object
 */
function ajaxformvalidator(form, e) {
        var status = true;
        form.find('div.textbox').removeClass('invalid');
         $.ajax({
              url: form.attr('action')+'?xhr=1&',
			  data :form.serializeArray(),
			  type : 'POST',
              dataType: 'json',
              async : false,
              success: function (data) {
                  if (data != true) {
					  status = false;
                      $.each(data['names'], function(index, value) {
                          $('#'+value).parent().addClass('invalid');
                      });
                      var message = '';
                      $.each(data['messages'], function(index, value) {
                          message += '<p>'+value+'</p>';
                      });
                      $('#errors').html(message).css('display','block');
                  }
              }
        });
        return status;
}


$(document).ready(function(){
	
/**
 * Reset default value in form
 */


$('label.inlined').addClass('inlined-active');

$('body').delegate('input, textarea', 'focus blur', function(e) {
        var $this = $(this);
        $this.siblings('label').toggleClass('focus');
		$this.parent().removeClass('invalid');
        if($this.val() == '') {
                $this.siblings('label').removeClass('has-text');
                $this.siblings('.required').show();
                $this.siblings('.optional').show();
        }

});
$('body').delegate('input', 'keydown', function(e) {
        var $this = $(this);
        $this.siblings('label').addClass('has-text');
        $this.siblings('.required').hide();
        $this.siblings('.optional').hide();
});
$.each($('input'), function(index,value){
    var el = $(value);
    if (el.val() != '') {
        el.siblings('label').addClass('has-text');
        el.siblings('.required').hide();
        el.siblings('.optional').hide();
    }
});
$("#contacts_form").submit(function (e)
{
     return ajaxformvalidator($(this),CFG_ROOT_URL+'contatti?&xhr=1&',e);
}
);


$('.menu4').click(function () {
   $('#dynSlideBox').load($(this).attr('href')+'?xhr=1').slideToggle(function(){
	   $('.dynEvents > li > a').fancybox({
		'titleShow'			: false,
		'titlePosition'		: 'inside',
		'transitionIn'		: 'none',
		'transitionOut'		: 'none'
		});
   });
   return false;
});    

$("#illocale1,#illocale2,#illocale3,#illocale4,").fancybox({
				'width'				: 800,
				'padding'			: 0,
				'autoScale'			: false,
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'type' : 'swf',
                'swf' : {
                    'flashvars':'XMLPathNoPlayList='+CFG_IMG_URL,
                    'wmode': 'transparent'
                } 
				
}); 
	
	
$('#eventi-footer').click(function () {
	$('.menu4').trigger('click');
	$.scrollTo( 0, 500);
	
	return false;
}); 

$('.detail_trigger').click(function() {
	$('#menu_detail').load(
		$(this).attr('href')+'&xhr=1'
	);
	return false;
}
);


$('.contact-form').submit(function (e)
	{
     return ajaxformvalidator($(this),e);
	}
);


});

  function initialize() {
    var myLatlng = new google.maps.LatLng(45.876204, 10.874780);
    var myOptions = {
      zoom: 16,
      center: myLatlng,
	  panControl: false,
	  zoomControl: true,
	  mapTypeControl: false,
	  scaleControl: false,
	  streetViewControl: false,
	  overviewMapControl:false,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
	if($('#map_canvas').length > 0)
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    var map2 = new google.maps.Map(document.getElementById("footerGoogleMap"), myOptions);

	var image = CFG_IMG_URL+'TL-bullet.png';
  	var myLatLng = new google.maps.LatLng(45.876192, 10.874946);
  	var tlMarker = new google.maps.Marker({
      position: myLatLng,
      map: map,
      icon: image
  	});
  	var tlMarker2 = new google.maps.Marker({
      position: myLatLng,
      map: map2,
      icon: image
  	});


  }

  function loadScript() {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=initialize";
    document.body.appendChild(script);
  }

  window.onload = loadScript;
